【问题标题】:Error on executing sess.run() ValueError: setting an array element with a sequence执行 sess.run() ValueError 时出错:使用序列设置数组元素
【发布时间】:2019-04-04 10:45:47
【问题描述】:

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

输入数据:[33600, 784] 改造成[784, 33600]

神经网络架构:

  第 1 层具有 W1 1000 x 784 relu
第 2 层具有 W2 1000 x 1000 relu
第 3 层具有 W3 500 x 1000 relu
第 4 层具有 W4 200 x 500 relu
第 5 层具有 W5 10 x 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】:

    我得到了解决方案。正如许多其他类似问题的答案中所提到的,问题通常与提供给 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 的数组的形状、类型和内容。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-06-08
      • 2018-08-04
      • 2019-08-04
      相关资源
      最近更新 更多