【问题标题】:Read values of tensor in tensorflow在张量流中读取张量的值
【发布时间】:2019-06-07 15:58:19
【问题描述】:

我正在 PyCharm 中使用 Python 3.6。

在文件site-packages/tensorflow/python/ops/nn_ops.py中,

我在第 838 行之后找到

    with ops.name_scope(name, "convolution", [input, filter]) as name:
        input = ops.convert_to_tensor(input, name="input")  
        input_shape = input.get_shape()
        filter = ops.convert_to_tensor(filter, name="filter")  
        filter_shape = filter.get_shape()
        op = Convolution(
            input_shape,
            filter_shape,
            padding,
            strides=strides,
            dilation_rate=dilation_rate,
            name=name,
            data_format=data_format)
        return op(input,filter)

我想知道输入、过滤器和返回张量的值。

我试过了,按照https://www.tensorflow.org/api_docs/python/tf/InteractiveSession做的

    with ops.name_scope(name, "convolution", [input, filter]) as name:
        input = ops.convert_to_tensor(input, name="input") 
        input_shape = input.get_shape()
        filter = ops.convert_to_tensor(filter, name="filter")  
        filter_shape = filter.get_shape()
        op = Convolution(
            input_shape,
            filter_shape,
            padding,
            strides=strides,
            dilation_rate=dilation_rate,
            name=name,
            data_format=data_format)
        temp = op(input,filter)
        import tensorflow as tf
        sess = tf.Session()
        with sess.as_default():
            assert tf.get_default_session() is sess
            test = filter.eval()
        return temp

然后,我得到了错误:

    tensorflow.python.framework.errors_impl.FailedPreconditionError: Attempting to use uninitialized value conv2d_1/kernel
     [[{{node conv2d_1/kernel/read}}]]

我做错了什么?

【问题讨论】:

    标签: python tensorflow


    【解决方案1】:

    当您打开 TensorFlow 会话时,您必须在初始化变量后通过使用 feed_dict 的占位符提供数据来运行整个模型。

    with tf.Session() as sess:
    
        # initialize your trainable variables
        sess.run(tf.global_variables_initializer())
    
        # Execute a training op [OP] by feeding [TENSOR]s through [PLACEHOLDER]s
        sess.run( [OP] , feed_dict = { [Placeholder]: [TENSOR] })
    
        # At the right point, evaluate the value of your filter object
        test = filter.eval( feed_dict = { [...] } )
    

    此时filter 的值将存储到一个numpy 对象test 中。 然后就可以退货了。

    我知道我的回答含糊不清,但我没有足够的信息来说明您拥有的数据。您可以找到一种方法来运行 TensorFlow 的会话 here。在tf.Session() 的末尾检查.eval 的使用。

    希望这会有所帮助,否则请告诉我。

    【讨论】:

    • 感谢您的反应。我应该更好地提到我的问题是在评估期间而不是培训期间提出的。如果我使用 global_variables_initializer,那么我训练的权重就消失了。
    • 我明白了,谢谢你的信息。在这种情况下,您可以使用feed_dict 删除global_variables_initializer.eval 对象吗?
    • 当我尝试 import tensorflow as tf with tf.Session() as sess: test = filter.eval(feed_dict),当我尝试改为 feed_dict={ 时,我得到了 feed_dict 未定义的错误[Placeholder]:[TENSOR]},他说 Placeholder 没有定义。我应该导入一些东西吗?
    • 您是否在开始时创建了一个占位符来将数据输入您的模型?
    • 不,我应该在我的主文件中还是在官方的 tensorflow 文件中这样做?占位符的声明是不是就像这里一样:databricks.com/tensorflow/placeholders
    猜你喜欢
    • 2017-12-12
    • 2017-10-05
    • 2018-06-25
    • 2018-04-18
    • 1970-01-01
    • 1970-01-01
    • 2017-12-01
    • 2017-11-05
    • 2021-07-03
    相关资源
    最近更新 更多