2016-09-20 49 views
1

所以我对张量流是新的,我的错误是,我在喂养 对train_neural_network(x)x无效的参数。Tensorflow session.run饲料字典机制

我想要做的是4999次迭代,输入一个[1,400]数组是 图片的位值。所以输入4999张照片。我用 scipy.io作为矩阵而不是张量读取图像。

我对如何使用占位符以及我的代码通常有什么问题感到困惑。因为我提供x和y的占位符,不应该输入x到train_neural_network(x)是占位符值吗?

X = tf.placeholder( '浮动',[1400]) Y = tf.placeholder( '浮动',[1,10])

DEF neural_network_model(数据):

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

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

    hidden_layer3 = {'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]))} 

    #(input * weights) + biases 

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

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

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

    output = tf.add(tf.matmul(l3, output_layer['weights']),output_layer['biases']) 

    return output 

高清train_neural_network(X):

prediction = neural_network_model(x) 
    cost = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(prediction,y)) 
    optimizer = tf.train.AdamOptimizer().minimize(cost) 

    hm_epochs = 4999 

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

     for epoch in range(hm_epochs): 

      sess.run([optimizer,cost], feed_dict = {x: input_X[epoch], y: encoded_y[epoch]}) 
      print('Epoch',epoch,'completed out of', hm_epochs) 

实际的错误是这样的:

%运行“/ US ERS/JaeWoo /桌面/研究/ tensorpractice/DeepNeural.py”

train_neural_network(X)

W¯¯tensorflow /核心/框架/ op_kernel.cc:940]参数无效:形状必须{INT32的矢量,int64},got shape []

W tensorflow/core/framework/op_kernel.cc:940]无效参数:shape必须是{int32,int64}的向量,得到shape [] ...重复for几次

InvalidArgumentError回溯(最近的最后一次呼叫)

在()

----> 1个train_neural_network(x)的

/Users/JaeWoo/Desktop/research/tensorpractice/DeepNeural.py在

train_neural_network(X)

67 
68   with tf.Session() as sess: 

---> 69 sess.run(tf.initialize_all_variables()) 71在范围历元(hm_epochs):

+0

到底是什么你得到的错误?你可以将它添加到你的问题或作为评论? – Steven

+0

你能改变这个“feed_dict = {”为“feed_dict = {”。我不记得间距是否重要。 – Steven

+0

我添加了错误。谢谢! – Djae

回答

0

我认为错误是你如何定义tf.placeholder。试试这个

x = tf.placeholder(tf.float32,shape=[1,400]) 

,如果你正在处理批次您可能还需要定义它像这样

x = tf.placeholder(tf.float32,shape=[None,400]) 
+0

其他一切看起来好吗?因为我在更改后得到相同的错误... – Djae

+0

没有使用批次。 – Djae

+0

居然有,因为你在IPython的笔记本电脑工作,你需要调用之间运行这个到小区一两件事: tf.reset_default_graph() 变量可能会被保留在内存中,你可能会得到同样的再次出错。所以只需在另一个单元中运行该函数 – Steven