2016-11-24 150 views
2

我想建立一个隐藏层(1024个节点)神经网络模型不能解释feed_dict关键。隐藏层只不过是一个relu单位。我还在128Tensorflow:如张量

的输入批次处理所述输入数据的大小为28的图像* 28在下面的代码我得到线 _, c = sess.run([optimizer, loss], feed_dict={x: batch_x, y: batch_y}) Error: TypeError: Cannot interpret feed_dict key as Tensor: Tensor Tensor("Placeholder_64:0", shape=(128, 784), dtype=float32) is not an element of this graph. 这里误差被我已经写

#Initialize 

batch_size = 128 

layer1_input = 28 * 28 
hidden_layer1 = 1024 
num_labels = 10 
num_steps = 3001 

#Create neural network model 
def create_model(inp, w, b): 
    layer1 = tf.add(tf.matmul(inp, w['w1']), b['b1']) 
    layer1 = tf.nn.relu(layer1) 
    layer2 = tf.matmul(layer1, w['w2']) + b['b2'] 
    return layer2 

#Initialize variables 
x = tf.placeholder(tf.float32, shape=(batch_size, layer1_input)) 
y = tf.placeholder(tf.float32, shape=(batch_size, num_labels)) 

w = { 
'w1': tf.Variable(tf.random_normal([layer1_input, hidden_layer1])), 
'w2': tf.Variable(tf.random_normal([hidden_layer1, num_labels])) 
} 
b = { 
'b1': tf.Variable(tf.zeros([hidden_layer1])), 
'b2': tf.Variable(tf.zeros([num_labels])) 
} 

init = tf.initialize_all_variables() 
train_prediction = tf.nn.softmax(model) 

tf_valid_dataset = tf.constant(valid_dataset) 
tf_test_dataset = tf.constant(test_dataset) 

model = create_model(x, w, b) 

loss = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(model, y))  
optimizer = tf.train.GradientDescentOptimizer(0.5).minimize(loss) 

#Process 
with tf.Session(graph=graph1) as sess: 
    tf.initialize_all_variables().run() 
    total_batch = int(train_dataset.shape[0]/batch_size) 

    for epoch in range(num_steps):  
     loss = 0 
     for i in range(total_batch): 
      batch_x, batch_y = train_dataset[epoch * batch_size:(epoch+1) * batch_size, :], train_labels[epoch * batch_size:(epoch+1) * batch_size,:] 

      _, c = sess.run([optimizer, loss], feed_dict={x: batch_x, y: batch_y}) 
      loss = loss + c 
     loss = loss/total_batch 
     if epoch % 500 == 0: 
      print ("Epoch :", epoch, ". cost = {:.9f}".format(avg_cost)) 
      print("Minibatch accuracy: %.1f%%" % accuracy(predictions, batch_labels)) 
      valid_prediction = tf.run(tf_valid_dataset, {x: tf_valid_dataset}) 
      print("Validation accuracy: %.1f%%" % accuracy(valid_prediction.eval(), valid_labels)) 
    test_prediction = tf.run(tf_test_dataset, {x: tf_test_dataset}) 
    print("TEST accuracy: %.1f%%" % accuracy(test_prediction.eval(), test_labels)) 
代码

```

回答

3

可变X不是在同一图表中为模型,尝试定义在同一图表中范围内的所有这些。例如,

# define a graph 
graph1 = tf.Graph() 
with graph1.as_default(): 
    # placeholder 
    x = tf.placeholder(...) 
    y = tf.placeholder(...) 
    # create model 
    model = create(x, w, b) 

with tf.Session(graph=graph1) as sess: 
# initialize all the variables 
sess.run(init) 
# then feed_dict 
# ...... 
+0

非常感谢。我提出了你所提到的改变。然而,我没有得到以下错误'取参数0具有无效类型,必须是字符串或张量。 (不能将int转换为Tensor或Operation。)'在同一行'sess.run(..,feed_dict = ...)'中。这里是代码http://pastebin.com/raw/iGZjgEi9 – Pratyush

+0

我刚刚检查了损失是一个int,而应该是一个张量。将尝试解决这个问题。 :) – Pratyush