【问题标题】:ValueError: cannot reshape array of size 2251 into shape (48,48)ValueError:无法将大小为 2251 的数组重塑为形状 (48,48)
【发布时间】:2021-07-18 09:10:24
【问题描述】:

当我尝试在 Google colab 中运行这部分代码来训练 cnn 时,我遇到了这个错误:

ValueError                                Traceback (most recent call last) <ipython-input-12-0b1325cd4065> in <module>()
      3 for xseq in datapoints:
      4     xx = [int(xp) for xp in xseq.split(' ')]
----> 5     xx = np.asarray(xx).reshape(width, height)
      6     X.append(xx.astype('float32'))
      7  ValueError: cannot reshape array of size 2251 into shape (48,48)

我的代码

# getting features for training
X = []
for xseq in datapoints:
    xx = [int(xp) for xp in xseq.split(' ')]
    xx = np.asarray(xx).reshape(width, height)
    X.append(xx.astype('float32'))

【问题讨论】:

  • 请注意,错误之后出现的任何代码都与问题无关(从未执行),不应在此处发布,因为它只会造成不必要的混乱(已编辑出)。
  • @desertnaut 我已经删除了不必要的代码
  • 您删除了 all 代码,这不是的想法;正如评论中所说,我已经自己编辑了多余的代码,所以不需要自己做任何其他事情,评论是为了将来(回滚)。

标签: python-3.x numpy deep-learning conv-neural-network


【解决方案1】:

一旦您拆分 xseq 数据,您可能会丢失一些数据。看到 2251 = (47.44 x 47.44) 的形状,所以它不适合并且不是所需形状 (48 x 48) 的倍数。

检查 xseq 对象的原始大小是多少,并确认您需要拆分。然后确认形状与预期的 (48,48) 匹配。

另一种可能性是调整对象的大小而不是重塑形状,但您也可能会丢失一些重要信息。

【讨论】:

    猜你喜欢
    • 2021-08-02
    • 2021-03-20
    • 2021-03-21
    • 2018-10-22
    • 2019-11-29
    • 2020-01-29
    • 1970-01-01
    相关资源
    最近更新 更多