2017-05-30 154 views
0

我一直在嘗試使用Tensorflow,但我不斷收到關於我的數據形狀的錯誤。我是從這個YouTube教程讓我的代碼:https://www.youtube.com/watch?v=PwAGxqrXSCs&list=PLQVvvaa0QuDfKTOs3Keq_kaG2P55YRn5v&index=47Tensorflow形狀不正確

我的訓練數據是這樣的:

enc0 = np.array([[[1,2,3,4],[0,1,0,1],[-33,0,0,0],[1,1,1,1]],[[2,3,3,2],[0,0,0,0],[9,0,0,0],[0,0,0,1]]]) # shape (2,4,4) ms0 = np.array([[1,6],[2,7]]) # shape (2,2)

我的錯誤是這樣的:

ValueError: Dimension size must be evenly divisible by 10 but is 4 for 'gradients/Reshape_grad/Reshape' (op: 'Reshape') with input shapes: [1,4], [2].

我相信我的錯誤發生原因這些線路:

x = tf.placeholder('float',[None,16]) 
y = tf.placeholder('float',[4]) 

enc = enc0.reshape([-1,16]) 

我的整個代碼是這樣的:

enc0 = np.array([[[1,2,3,4],[0,1,0,1],[-33,0,0,0],[1,1,1,1]],[[2,3,3,2],[0,0,0,0],[9,0,0,0],[0,0,0,1]]]) 
ms0 = np.array([[1,6],[2,7]]) 

n_nodes_hl1 = 500 # hidden layer 1 
n_nodes_hl2 = 500 
n_nodes_hl3 = 500 

n_classes = 10 
batch_size = 100 # load 100 features at a time 


x = tf.placeholder('float',[None,16]) 
y = tf.placeholder('float',[4]) 

enc = enc0.reshape([-1,16]) 
ms = ms0 


def neuralNet(data): 
    hl_1 = {'weights':tf.Variable(tf.random_normal([16, n_nodes_hl1])), 
      'biases':tf.Variable(tf.random_normal([n_nodes_hl1]))} 

    hl_2 = {'weights':tf.Variable(tf.random_normal([n_nodes_hl1, n_nodes_hl2])), 
      'biases':tf.Variable(tf.random_normal([n_nodes_hl2]))} 

    hl_3 = {'weights':tf.Variable(tf.random_normal([n_nodes_hl2, n_nodes_hl3])), 
      'biases':tf.Variable(tf.random_normal([n_nodes_hl3]))} 

    output_layer = {'weights':tf.Variable(tf.random_normal([n_nodes_hl3, n_classes])), 
      'biases':tf.Variable(tf.random_normal([n_classes]))} 

    l1 = tf.add(tf.matmul(data, hl_1['weights']), hl_1['biases']) 
    l1 = tf.nn.relu(l1) 

    l2 = tf.add(tf.matmul(l1, hl_2['weights']), hl_2['biases']) 
    l2 = tf.nn.relu(l2) 

    l3 = tf.add(tf.matmul(l2, hl_3['weights']), hl_3['biases']) 
    l3 = tf.nn.relu(l3) 

    ol = tf.matmul(l3, output_layer['weights']) + output_layer['biases'] 

    return ol 


def train(x): 
    prediction = neuralNet(x) 
    print prediction 
    cost = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(logits=prediction,labels=y)) 
    optimizer = tf.train.AdamOptimizer().minimize(cost) # learning rate = 0.001 

    # cycles of feed forward and backprop 
    num_epochs = 15 

    with tf.Session() as sess: 
     sess.run(tf.global_variables_initializer()) 

     for epoch in range(num_epochs): 
      epoch_loss = 0 
      for _ in range(int(enc.shape[0])): 
       epoch_x,epoch_y = enc,ms 
       _,c = sess.run([optimizer,cost],feed_dict={x:epoch_x,y:epoch_y}) 
       epoch_loss += c 
      print 'Epoch', epoch + 1, 'completed out of', num_epochs, '\nLoss:',epoch_loss,'\n' 

     correct = tf.equal(tf.argmax(prediction,1),tf.argmax(y,1)) 
     accuracy = tf.reduce_mean(tf.cast(correct,'float')) 

     print 'Accuracy', accuracy.eval({x:enc, y:ms}) 


train(x) 

任何幫助錯誤將不勝感激。

回答

0

的原因是您正在生成從網絡n_classes預測(n_classes爲10),而在你的y佔位符4個值進行比較吧。它應該足以使用

y = tf.placeholder('float', [10]) 

然後實際向佔位符提供10個值。

+0

謝謝。我的數據現在包含10個實例,而不是2個(因此形狀是(10,2))。我的'y'佔位符現在是'y = tf.placeholder('float',[10,2])''。但是,我收到一個新錯誤:logits和標籤必須大小相同:logits_size = [10,10] labels_size = [10,2]。任何想法爲什麼發生這種情況? – HS1300

+0

你必須有10個值,因爲你有10個班級。你的佔位符的第二個維度必須是num-classes –