2016-02-13 77 views
7

我想使用[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}) 

注:當我擁有它似乎工作功能的重塑之外,但我有多次使用非常大的模型,所以我需要讓他們在功能和使用過朦朧論據。

回答

9

爲了使這項工作,更換功能:

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()期望的值可以轉換爲tf.Tensor作爲其第二個論點。 TensorFlow會自動將Python數字列表轉換爲tf.Tensor,但不會自動轉換數字和張量的混合列表(例如tf.placeholder()),而是增加您看到的某些不直觀的錯誤消息。

tf.pack() op需要列表可轉換爲張量的對象,並單獨轉換每個元素,因此它可以處理佔位符和整數的組合。

+0

我遇到了一些使用tf.pack()稍後的問題。 'x = reshape(100)'不再允許我執行'x.get_shape()。as_list()[ - 1]'。它表示Nonetype不可迭代,因此它正在丟失形狀信息。對此有何評論? – jstaker7

+1

當你定義你的'batch_size'佔位符時,試着指定'shape =()'。如果您以後依賴它,也可以使用'Tensor.set_shape()'來提供其他形狀信息,這些信息不是推斷出來的。 – mrry

+0

非常好,謝謝! – jstaker7

0

嗨,所有的問題是由於凱拉斯版本。我首先嚐試沒有任何成功。卸載Keras並通過pip安裝。它爲我工作。

我面臨這個錯誤與Keras 1.0.2 &與Keras解決1.2.0

希望這會有所幫助。謝謝

相關問題