簡體   English   中英

Tensorflow神經網絡中的InvalidArgumentError

[英]InvalidArgumentError in Tensorflow neural network

我在神經網絡中使用張量流的簡單示例:

# define the numbers of nodes in hidden layers
nodesLayer1 = 500
nodesLayer2 = 500
nodesLayer3 = 500

# define our goal class
classes = 2
batchSize = 500

# x for input, y for output
sizeOfRow = len(data[0])
x = tensorFlow.placeholder(dtype= "float", shape=[None, sizeOfRow])
y = tensorFlow.placeholder(dtype= "float")


def neuralNetworkModel(data):
  # first step: (input * weights) + bias, linear operation like y = ax + b
  # each layer connection to other layer will represent by nodes(i) * nodes(i+1)
  hiddenLayer1 = {"weights" : tensorFlow.Variable(tensorFlow.random_normal([sizeOfRow, nodesLayer1])),
                  "biases" : tensorFlow.Variable(tensorFlow.random_normal([nodesLayer1]))}

  hiddenLayer2 = {"weights" : tensorFlow.Variable(tensorFlow.random_normal([nodesLayer1, nodesLayer2])),
                  "biases" : tensorFlow.Variable(tensorFlow.random_normal([nodesLayer2]))}

  hiddenLayer3 = {"weights": tensorFlow.Variable(tensorFlow.random_normal([nodesLayer2, nodesLayer3])),
                  "biases": tensorFlow.Variable(tensorFlow.random_normal([nodesLayer3]))}

  outputLayer = {"weights": tensorFlow.Variable(tensorFlow.random_normal([nodesLayer3, classes])),
                  "biases": tensorFlow.Variable(tensorFlow.random_normal([classes]))}

  # create the layers
  layer1 = tensorFlow.add(tensorFlow.matmul(data, hiddenLayer1["weights"]), hiddenLayer1["biases"])
  layer1 = tensorFlow.nn.relu(layer1)  # pass values to activation function (i.e sigmoid, softmax) and add it to the layer

  layer2 = tensorFlow.add(tensorFlow.matmul(layer1, hiddenLayer2["weights"]), hiddenLayer2["biases"])
  layer2 = tensorFlow.nn.relu(layer2)

  layer3 = tensorFlow.add(tensorFlow.matmul(layer2, hiddenLayer3["weights"]), hiddenLayer3["biases"])
  layer3 = tensorFlow.nn.relu(layer3)

  output = tensorFlow.matmul(layer3, outputLayer["weights"]) + outputLayer["biases"]

  return output


def neuralNetworkTrain(x):
  prediction = neuralNetworkModel(x)
  # using softmax function, normalize values to range(0,1)
  cost = tensorFlow.reduce_mean(tensorFlow.nn.softmax_cross_entropy_with_logits(prediction, y))

  # minimize the cost function
  # at the same way we can use Gradiant Decent
  # default learning rate is 0.001 in AdamOptimizer
  optimizer = tensorFlow.train.AdamOptimizer(0.0001).minimize(cost)
  epochs = 15

  # build sessions and train the model
  with tensorFlow.Session() as sess:
    sess.run(tensorFlow.initialize_all_variables())

    for epoch in range(epochs):
      epochLoss = 0
      i = 0
      for temp in range(int(len(data) / batchSize)):
        ex, ey = nextBatch(batchSize, i) # takes 500 examples
        i += 1
        # TO-DO : fix bug here
        temp, cos = sess.run((optimizer, cost)) # start session to optimize the cost function
        epochLoss += cos
      print("Epoch", epoch, "completed out of", epochs, "loss:", epochLoss)

    correct = tensorFlow.equal(tensorFlow.argmax(prediction,1), tensorFlow.argmax(y, 1))
    accuracy = tensorFlow.reduce_mean(tensorFlow.cast(correct, "float"))
    print("Accuracy:", accuracy.eval())

而且我收到此錯誤消息:

Caused by op u'Placeholder_1', defined at:
  File "/home/or/PycharmProjects/untitled/NeuralNetwork.py", line 39, in <module>
    y = tensorFlow.placeholder(dtype= "float")
  File "/usr/local/lib/python2.7/dist-packages/tensorflow/python/ops/array_ops.py", line 1332, in placeholder
    name=name)
  File "/usr/local/lib/python2.7/dist-packages/tensorflow/python/ops/gen_array_ops.py", line 1748, in _placeholder
    name=name)
  File "/usr/local/lib/python2.7/dist-packages/tensorflow/python/framework/op_def_library.py", line 749, in apply_op
    op_def=op_def)
  File "/usr/local/lib/python2.7/dist-packages/tensorflow/python/framework/ops.py", line 2380, in create_op
    original_op=self._default_original_op, op_def=op_def)
  File "/usr/local/lib/python2.7/dist-packages/tensorflow/python/framework/ops.py", line 1298, in __init__
    self._traceback = _extract_stack()

InvalidArgumentError (see above for traceback): You must feed a value for placeholder tensor 'Placeholder_1' with dtype float
     [[Node: Placeholder_1 = Placeholder[dtype=DT_FLOAT, shape=[], _device="/job:localhost/replica:0/task:0/cpu:0"]()]]

有人以前看到過這個,可以解釋一下如何解決嗎? 我嘗試了很多事情並在tensorflow網站上讀了很多東西,但找不到確切答案..

在指定占位符數據類型時,您需要清楚位信息...試試x = tf.placeholder(dtype=tf.float32) (通常ppl為了提高速度和內存而使用float32 )。 這是占位符文檔的鏈接

運行會話時,您需要為所有占位符提供具有匹配數據類型的實際數據。 neuralNetworkTrain函數更改為以下內容:

def neuralNetworkTrain(x):
   prediction = neuralNetworkModel(x)
   # ... omitted code...  
   # build sessions and train the model
   with tensorFlow.Session() as sess:
       sess.run(tensorFlow.initialize_all_variables())
       for epoch in range(epochs): 
           epochLoss = 0
           i = 0
           for temp in range(int(len(data) / batchSize)):
               ex, ey = nextBatch(batchSize, i) # takes 500 examples
               i += 1
               # TO-DO : fix bug here
               # **Add feed_dict for placeholder x**
               feed_dict = {x: ex}
               temp, cos = sess.run((optimizer, cost), feed_dict=feed_dict) # start session to optimize the cost function
               epochLoss += cos
          print("Epoch", epoch, "completed out of", epochs, "loss:", epochLoss)

          correct = tensorFlow.equal(tensorFlow.argmax(prediction,1), tensorFlow.argmax(y, 1))
          accuracy = tensorFlow.reduce_mean(tensorFlow.cast(correct, "float"))
          # **Add feed_dict for placeholder y**
          print("Accuracy:", accuracy.eval(feed_dict={y: ey}))

暫無
暫無

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

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