读取张量流中的张量值

时间:2019-06-07 07:41:25

标签: python tensorflow

我正在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}}]]

我在做什么错了?

1 个答案:

答案 0 :(得分:0)

打开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。在.eval末尾检查tf.Session()的使用情况。

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