使用占位符值重塑张量
Reshape tensor using placeholder value
我想使用 [int, -1] 表示法重塑张量(例如,压平图像)。但是我不知道提前知道第一个维度。一个用例是在大批量上训练,然后在小批量上进行评估。
为什么会出现以下错误:got list containing Tensors of type '_Message'
?
import tensorflow as tf
import numpy as np
x = tf.placeholder(tf.float32, shape=[None, 28, 28])
batch_size = tf.placeholder(tf.int32)
def reshape(_batch_size):
return tf.reshape(x, [_batch_size, -1])
reshaped = reshape(batch_size)
with tf.Session() as sess:
sess.run([reshaped], feed_dict={x: np.random.rand(100, 28, 28), batch_size: 100})
# Evaluate
sess.run([reshaped], feed_dict={x: np.random.rand(8, 28, 28), batch_size: 8})
注意:当我在函数外部进行整形时,它似乎可以工作,但我有多次使用的非常大的模型,因此我需要将它们保存在一个函数中并使用参数传递 dim。
要实现此功能,请替换函数:
def reshape(_batch_size):
return tf.reshape(x, [_batch_size, -1])
…函数为:
def reshape(_batch_size):
return tf.reshape(x, tf.pack([_batch_size, -1]))
错误的原因是 tf.reshape()
expects a value that is convertible to a tf.Tensor
as its second argument. TensorFlow will automatically convert a list of Python numbers to a tf.Tensor
but will not automatically convert a mixed list of numbers and tensors (such as a tf.placeholder()
)——而不是引发您看到的有点不直观的错误消息。
tf.pack()
运算符采用 list 可转换为张量的对象,并单独转换每个元素,因此它可以处理占位符和整数的组合.
您好,所有问题都是由于 Keras 版本引起的。我首先尝试但没有成功。卸载 Keras 并通过 pip 安装。它对我有用。
我在使用 Keras 1.0.2 时遇到了这个错误并在 Keras 1.2.0 中解决了
希望这会有所帮助。谢谢
我想使用 [int, -1] 表示法重塑张量(例如,压平图像)。但是我不知道提前知道第一个维度。一个用例是在大批量上训练,然后在小批量上进行评估。
为什么会出现以下错误:got list containing Tensors of type '_Message'
?
import tensorflow as tf
import numpy as np
x = tf.placeholder(tf.float32, shape=[None, 28, 28])
batch_size = tf.placeholder(tf.int32)
def reshape(_batch_size):
return tf.reshape(x, [_batch_size, -1])
reshaped = reshape(batch_size)
with tf.Session() as sess:
sess.run([reshaped], feed_dict={x: np.random.rand(100, 28, 28), batch_size: 100})
# Evaluate
sess.run([reshaped], feed_dict={x: np.random.rand(8, 28, 28), batch_size: 8})
注意:当我在函数外部进行整形时,它似乎可以工作,但我有多次使用的非常大的模型,因此我需要将它们保存在一个函数中并使用参数传递 dim。
要实现此功能,请替换函数:
def reshape(_batch_size):
return tf.reshape(x, [_batch_size, -1])
…函数为:
def reshape(_batch_size):
return tf.reshape(x, tf.pack([_batch_size, -1]))
错误的原因是 tf.reshape()
expects a value that is convertible to a tf.Tensor
as its second argument. TensorFlow will automatically convert a list of Python numbers to a tf.Tensor
but will not automatically convert a mixed list of numbers and tensors (such as a tf.placeholder()
)——而不是引发您看到的有点不直观的错误消息。
tf.pack()
运算符采用 list 可转换为张量的对象,并单独转换每个元素,因此它可以处理占位符和整数的组合.
您好,所有问题都是由于 Keras 版本引起的。我首先尝试但没有成功。卸载 Keras 并通过 pip 安装。它对我有用。
我在使用 Keras 1.0.2 时遇到了这个错误并在 Keras 1.2.0 中解决了
希望这会有所帮助。谢谢