Tensorflow:如何用张量提供占位符变量? [英] Tensorflow: How to feed a placeholder variable with a tensor?

查看:20
本文介绍了Tensorflow:如何用张量提供占位符变量?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个占位符变量,它需要一批输入图像:

I have a placeholder variable that expects a batch of input images:

input_placeholder = tf.placeholder(tf.float32, [None] + image_shape, name='input_images')

现在我有两个输入数据来源:
1)张量和
2)一些numpy数据.

Now I have 2 sources for the input data:
1) a tensor and
2) some numpy data.

对于 numpy 输入数据,我知道如何将数据提供给占位符变量:

For the numpy input data, I know how to feed data to the placeholder variable:

sess = tf.Session()
mLoss, = sess.run([loss], feed_dict = {input_placeholder: myNumpyData})

如何向该占位符变量提供张量?

How can I feed a tensor to that placeholder variable?

mLoss, = sess.run([loss], feed_dict = {input_placeholder: myInputTensor})

给我一​​个错误:

TypeError: The value of a feed cannot be a tf.Tensor object. Acceptable feed values include Python scalars, strings, lists, or numpy ndarrays.

我不想使用 .eval() 将张量转换为 numpy 数组,因为这会减慢我的程序速度,还有其他方法吗?

I don't want to convert the tensor into a numpy array using .eval(), since that would slow my program down, is there any other way?

推荐答案

这个已经在 2016 年在 GitHub 上讨论过了,请查看 此处.这是 concretevitamin 的要点:

This has been discussed on GitHub in 2016, and please check here. Here is the key point by concretevitamin:

需要注意的一个关键点是,Tensor 只是一个符号对象.您的 feed_dict 的值是实际值,例如一个 Numpy ndarry.

One key thing to note is that Tensor is simply a symbolic object. The values of your feed_dict are the actual values, e.g. a Numpy ndarry.

张量作为符号对象在图中流动,而实际值在图之外,那么我们只能将实际值传入图中,而符号对象不能存在于图外.

The tensor as a symbolic object is flowing in the graph while the actual values are outside of it, then we can only pass the actual values into the graph and the symbolic object can not exist outside the graph.

这篇关于Tensorflow:如何用张量提供占位符变量?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆