【问题标题】:Print the value of a tensor in Tensorflow在 Tensorflow 中打印张量的值
【发布时间】:2017-11-10 02:07:19
【问题描述】:

以下是我完整代码的链接:

https://github.com/roshanDD/YAD2K/blob/master/yad2k/models/keras_yolo.py#L253

我只想打印出张量的值,称为true_areas。我运行了我的代码:

sess = tf.InteractiveSession()
true_areas = true_wh[..., 0] * true_wh[..., 1]
tf.Print(true_areas, [true_areas], message = "This is True Areas: ")
b = tf.add(true_areas, true_areas).eval()

但它给了我错误:

Caused by op 'input_2', defined at:
  File "learning.py", line 358, in <module>
    _main(args)
  File "learning.py", line 66, in _main
    model_body, model = create_model(anchors, class_names)
  File "learning.py", line 185, in create_model
    boxes_input = Input(shape=(None, 5))
  File "/home/ubuntu/anaconda2/envs/py35/lib/python3.5/site-packages/keras/engine/topology.py", line 1388, in Input
    input_tensor=tensor)
  File "/home/ubuntu/anaconda2/envs/py35/lib/python3.5/site-packages/keras/engine/topology.py", line 1299, in __init__
    name=self.name)
  File "/home/ubuntu/anaconda2/envs/py35/lib/python3.5/site-packages/keras/backend/tensorflow_backend.py", line 349, in placeholder
    x = tf.placeholder(dtype, shape=shape, name=name)
  File "/home/ubuntu/anaconda2/envs/py35/lib/python3.5/site-packages/tensorflow/python/ops/array_ops.py", line 1507, in placeholder
    name=name)
  File "/home/ubuntu/anaconda2/envs/py35/lib/python3.5/site-packages/tensorflow/python/ops/gen_array_ops.py", line 1997, in _placeholder
    name=name)
  File "/home/ubuntu/anaconda2/envs/py35/lib/python3.5/site-packages/tensorflow/python/framework/op_def_library.py", line 768, in apply_op
    op_def=op_def)
  File "/home/ubuntu/anaconda2/envs/py35/lib/python3.5/site-packages/tensorflow/python/framework/ops.py", line 2336, in create_op
    original_op=self._default_original_op, op_def=op_def)
  File "/home/ubuntu/anaconda2/envs/py35/lib/python3.5/site-packages/tensorflow/python/framework/ops.py", line 1228, in __init__
    self._traceback = _extract_stack()

InvalidArgumentError (see above for traceback): You must feed a value for placeholder tensor 'input_2' with dtype float
     [[Node: input_2 = Placeholder[dtype=DT_FLOAT, shape=[], _device="/job:localhost/replica:0/task:0/cpu:0"]()]]

它所说的占位符是什么?

【问题讨论】:

标签: python machine-learning tensorflow artificial-intelligence keras


【解决方案1】:

错误消息告诉您,您在代码中的某处定义了一个名为 input_2tf.placeholder()

当您想要评估依赖于该占位符的图表时,您需要为该占位符设置feed 值。它应该看起来像这样:

b = tf.add(true_areas, true_areas).eval({input_2: someInputValues})

【讨论】:

  • 是否可以不将其馈送到input_2 并打印出张量的值?
  • 我的意思是,我只是想看看张量的值。我不想更改任何其他变量的值。我可以定义一个新的占位符之类的吗?
  • 这个张量的值不取决于这个输入张量中的值吗?如果是,则需要提供输入张量。否则 tensorflow 无法计算这些值。如果您可以提供minimal, complete and verifiable example,这将非常有帮助。
  • true_wh 有一些值。这些价值观从何而来?
猜你喜欢
  • 2016-09-01
  • 2016-02-11
  • 1970-01-01
  • 2023-04-02
  • 2017-01-14
  • 2019-08-13
  • 2020-01-17
  • 2017-09-12
相关资源
最近更新 更多