【问题标题】:Python Deep Learning Keras: Wrong number of dimensions: expected 3, got 2 with shapePython 深度学习 Keras:错误的维数:预期为 3,形状为 2
【发布时间】:2016-05-30 03:35:09
【问题描述】:

我是深度学习库 Keras 的新手,需要您的帮助。模型构建无误,但调用model.fit(X, y)时出现如下问题:

TypeError: ('Bad input argument to theano function with name "~/machine_learning2/lib/python2.7/site-packages/keras/backend/theano_backend.py:380"  
at index 0(0-based)', 'Wrong number of dimensions: expected 3, got 2 with shape (16, 40).')

这与https://github.com/fchollet/keras/issues/815类似 我的 y 训练矩阵是一个多行一列的矩阵。

提到的一个解决方案是使用二进制 one-hot 编码将 y 转换为 3d 张量。有这样的例子吗?

【问题讨论】:

  • 能否提供您的网络架构?

标签: python keras


【解决方案1】:

你在寻找这样的东西吗?

>b=np.arange(640)
>b.reshape(16,40).shape
 (16, 40)
>c=b.reshape(1,16,40)
>c.shape
 (1, 16, 40)

【讨论】:

    【解决方案2】:

    你可以使用:

    from keras.utils import np_utils
    np_utils.to_categorical(y_train, n_classes)
    

    对于一个热编码,其中 y_train 是一个训练类向量,n_classes - 一个类的总数,

    但是,基于错误描述提到 (16, 40) 而不是 (Nx1),我怀疑您的 X 也可能有问题。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2017-01-10
      • 1970-01-01
      • 2022-12-09
      • 2019-01-10
      • 1970-01-01
      • 2019-04-27
      • 2020-06-18
      相关资源
      最近更新 更多