我想使用[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})
注:當我擁有它似乎工作功能的重塑之外,但我有多次使用非常大的模型,所以我需要讓他們在功能和使用過朦朧論據。
我遇到了一些使用tf.pack()稍後的問題。 'x = reshape(100)'不再允許我執行'x.get_shape()。as_list()[ - 1]'。它表示Nonetype不可迭代,因此它正在丟失形狀信息。對此有何評論? – jstaker7
當你定義你的'batch_size'佔位符時,試着指定'shape =()'。如果您以後依賴它,也可以使用'Tensor.set_shape()'來提供其他形狀信息,這些信息不是推斷出來的。 – mrry
非常好,謝謝! – jstaker7