簡體   English   中英

TensorFlow:InvalidArgumentError:In[0] 不是矩陣

[英]TensorFlow: InvalidArgumentError: In[0] is not a matrix

我是 TensorFlow 的新手,需要為回歸任務實現一個深度神經網絡。 我假設互聯網上沒有使用深度神經網絡執行回歸的此類示例代碼(至少我找不到。請發布任何有用的鏈接,如果可用)。 因此,為了我的目的,我嘗試將有關用於分類和回歸的深度神經網絡的教程合並在一起。 正如預期的那樣,我被錯誤轟炸。 錯誤信息如下:

InvalidArgumentError: In[0] is not a matrix
 [[Node: MatMul_35 = MatMul[T=DT_FLOAT, transpose_a=false, transpose_b=false, _device="/job:localhost/replica:0/task:0/cpu:0"](_recv_Placeholder_36_0, Variable_72/read)]]  

代碼:

import tensorflow as tf
import numpy
import matplotlib.pyplot as plt

n_nodes_hl1 = 100
n_nodes_hl2 = 100

batch_size = 100

n_input = 1;
n_output = 1;
learning_rate = 0.01

train_X = numpy.asarray([3.3,4.4,5.5,6.71,6.93,4.168,9.779,6.182,7.59,2.167,
                 7.042,10.791,5.313,7.997,5.654,9.27,3.1])
train_Y = numpy.asarray([1.7,2.76,2.09,3.19,1.694,1.573,3.366,2.596,2.53,1.221,
                 2.827,3.465,1.65,2.904,2.42,2.94,1.3])

x = tf.placeholder('float')
y = tf.placeholder('float')

def neural_network_model(data):
   hidden_1_layer = {'weights':tf.Variable(tf.random_normal([n_input, n_nodes_hl1])),
                  'biases':tf.Variable(tf.random_normal([n_nodes_hl1]))}

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

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

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

   output = tf.reduce_sum(l2)

   return output

def train_neural_network(x):
   prediction = neural_network_model(x)
   cost = tf.square(y - prediction)

   optimizer = tf.train.GradientDescentOptimizer(learning_rate).minimize(cost)

   hm_epochs = 5
   with tf.Session() as sess:

      sess.run(tf.global_variables_initializer())

      for epoch in range(hm_epochs):
         epoch_loss = 0
         for (X, Y) in zip(train_X, train_Y):
             _, c = sess.run([optimizer, cost], feed_dict={x: X, y: Y})
             epoch_loss += c

         print('Epoch', epoch, 'completed out of',hm_epochs,'loss:',epoch_loss)

      plt.plot(train_X, train_Y, 'ro', label='Original data')
      plt.plot(train_X, prediction, label='Fitted line')
      plt.legend()
      plt.show()

      test_X = numpy.asarray([6.83, 4.668, 8.9, 7.91, 5.7, 8.7, 3.1, 2.1])
      test_Y = numpy.asarray([1.84, 2.273, 3.2, 2.831, 2.92, 3.24, 1.35, 1.03])
      print("Testing Data")

      correct = tf.equal(tf.argmax(prediction, 1), tf.argmax(y, 1))
      accuracy = tf.reduce_mean(tf.cast(correct, 'float'))
      print('Accuracy:',accuracy.eval({x:test_X, y:test_Y}))

train_neural_network(x)

就我而言,我猜隱藏層權重和/或偏差的維度存在問題(我可能錯了)。

旁注:這里我只是嘗試為我的項目制作一個簡單的模型,其中訓練和測試數據點取自互聯網示例。 我的實際數據將是幾個圖像的像素值。

更改此行(為我工作):

  1. matmul() 函數的輸入應該是一個矩陣 - 您正在輸入一個值。

     _, c = sess.run([optimizer, cost], feed_dict={x: [[X]], y: [[Y]]})

輸出:

('Epoch', 0, 'completed out of', 5, 'loss:', array([[  1.20472407e+14]], dtype=float32))
('Epoch', 1, 'completed out of', 5, 'loss:', array([[ 6.82631159]], dtype=float32))
('Epoch', 2, 'completed out of', 5, 'loss:', array([[ 8.83840561]], dtype=float32))
('Epoch', 3, 'completed out of', 5, 'loss:', array([[ 8.00222397]], dtype=float32))
('Epoch', 4, 'completed out of', 5, 'loss:', array([[ 7.6564579]], dtype=float32))

希望這有幫助!

評論:如果您要處理圖像,這不是一個很好的探索示例。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM