gpt4 book ai didi

python - Tensorflow session.run feed dict机制

转载 作者:行者123 更新时间:2023-12-04 17:57:02 28 4
gpt4 key购买 nike

所以我是 tensorflow 的新手,我的错误是我在输入train_neural_network(x) 的 x 参数无效。

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

我对占位符的使用方式以及我的代码通常存在的错误感到困惑。因为我将 x 和 y 提供给占位符,所以 train_neural_network(x) 的输入 x 不应该是占位符值吗?

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


def neural_network_model(data):

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

def 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)

实际的错误是这样的:

%run "/Users/JaeWoo/Desktop/research/tensorpractice/DeepNeural.py"

train_neural_network(x)

W tensorflow/core/framework/op_kernel.cc:940] Invalid argument: shape must be a vector of {int32,int64}, got shape []

W tensorflow/core/framework/op_kernel.cc:940] Invalid argument: shape must be a vector of {int32,int64}, got shape []
... repeated for several times


InvalidArgumentError Traceback (most recent call last)

<ipython-input-86-7c7cbdae9b34> in <module>()

----> 1 train_neural_network(x)

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

train_neural_network(x)

67
68 with tf.Session() as sess:
---> 69 sess.run(tf.initialize_all_variables())
70
71 for epoch in range(hm_epochs):

最佳答案

我认为错误在于您定义 tf.placeholder 的方式。试试这个吧

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

如果您正在处理批处理,您可能还想这样定义它

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

关于python - Tensorflow session.run feed dict机制,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39604877/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com