2017-03-14 48 views
1

我是tensorflow的初學者,我使用tf.expand_dims我得到錯誤,我無法理解原因,所以我錯過了什麼?tensorfow tf.expand_dims錯誤

這是代碼

ML_OUTPUT = None 
input_for_classification = None 
def ConstructML(input_tensor, layers_count, node_for_each_layer): 
    global ML_OUTPUT 
    global input_for_classification 
    FeatureVector = np.array(input_tensor) 
    FeatureVector = FeatureVector.flatten() 
    print(FeatureVector.shape)       
    ML_ModelINT(FeatureVector, layers_count, node_for_each_layer) 

def ML_ModelINT(FeatureVector, layers_count, node_for_each_layer): 
     hidden_layer = [] 
     Alloutputs = [] 
     hidden_layer.append({'weights': tf.Variable(tf.random_normal([FeatureVector.shape[0], node_for_each_layer[0]])),'biases': tf.Variable(tf.random_normal([node_for_each_layer[0]]))}) 
     for i in range(1, layers_count): 
      hidden_layer.append({'weights': tf.Variable(tf.random_normal([node_for_each_layer[i - 1], node_for_each_layer[i]])),'biases': tf.Variable(tf.random_normal([node_for_each_layer[i]]))}) 
     FeatureVector = tf.expand_dims(FeatureVector,0) 
     layers_output = tf.add(tf.matmul(FeatureVector, hidden_layer[0]['weights']), hidden_layer[0]['biases']) 
     layers_output = tf.nn.relu(layers_output) 
     Alloutputs.append(layers_output) 
     for j in range(1, layers_count): 
      layers_output = tf.add(tf.matmul(layers_output, hidden_layer[j]['weights']), hidden_layer[j]['biases']) 
      layers_output = tf.nn.relu(layers_output) 
      Alloutputs.append(layers_output) 
     ML_OUTPUT = layers_output 
     input_for_classification = Alloutputs[1]    
     return ML_OUTPUT 

ML_Net = ConstructML(input,3,[1024,512,256]) 

它給我的錯誤在這行

FeatureVector = tf.expand_dims(FeatureVector,0) 

的誤差預計二進制或Unicode字符串,得到了tf.Tensor 'Relu_11:0' 形=(?,7,7,512)dtype = float32

注意輸入是另一個網絡的輸出張量和d是效果很好

+1

你的類型有些奇怪,我認爲...在函數ConstructML中,FeatureVector是一個numpy數組。然後將它傳遞給ModelINT,並對其進行tf張量運算而不必將其轉換爲... – cleros

回答

0

歐凱時,numpy的部分是錯誤的,因爲當predection功能首先把它稱爲不含提要卻爲input_imgs和numpy的代碼將無法正確excuted,和我一起tensorflow取而代之操作和它現在工作。