执行sess.run()时出错ValueError:使用序列设置数组元素

问题描述 投票:0回答:1

当我尝试运行训练步骤时会生成此错误。数据集是来自Kaggle的MNIST数据集。我正在使用神经网络来预测手写数字:

输入数据:[33600, 784]重塑为[784, 33600]

神经网络架构:

第1层具有W1 1000乘784 relu 第2层有W2 1000乘1000 relu 第3层有W3 500乘1000 relu 第4层有W4 200乘500 relu 第5层具有W5 10乘200,具有softmax 没有使用偏见

码:

print(X_train[:, 0].reshape(-1, 1).shape,"   ",y_train[:,0].reshape(-1,1).shape)`

输出:(784, 1) (10, 1)

码:

X, Y = tf.placeholder(tf.float32,[784, None]), tf.placeholder(tf.float32,[10, None])

logits = forward_propagation(X, parameters)

cost = compute_cost(logits, Y)
optimizer = tf.train.AdamOptimizer(learning_rate=1e-3).minimize(cost)

with tf.Session() as sess:
    sess.run(tf.global_variables_initializer())
    _,c = sess.run([optimizer,cost], feed_dict= {X:X_train[:,0].reshape(-1,1),
                                                 Y:y_train[:, 0].reshape(-1,1)})
print(c)

输出:

ValueError         Traceback (most recent call 
last)
<ipython-input-41-f78f499b0606> in <module>()
8 with tf.Session() as sess:
9     sess.run(tf.global_variables_initializer())
---> 10     _,c = sess.run([optimizer,cost], feed_dict= 
{X:np.asarray(X_train), Y:np.asarray(y_train)})
11 print(c)

.......
.......

ValueError: setting an array element with a sequence.

如果可以,请更正代码。

python-3.x tensorflow neural-network numpy-ndarray
1个回答
0
投票

我得到了解决方案。正如许多其他类似问题的答案中所提到的,问题通常在于提供给feed_dict的数组的形状和类型。我主要关注的只是X:X_train [:,0] .reshape(-1,1),但它的形状和类型都是正确的。错误发生在Y:y_train [:,0] .reshape(-1,1)中。我无法检测到此错误,因为我在y_train上应用了one_hot_encoding,但在转换后忘了使用.toarray()方法。所以y_train的形状似乎是正确的,但实际上它是错误的。

作为在经过许多类似问题后的一般建议,我会说彻底检查被馈送到feed_dict的数组的形状,类型和内容。

© www.soinside.com 2019 - 2024. All rights reserved.