【问题标题】:ValueError: cannot reshape array of size 78543360 into shape (51135,4,32,32)ValueError:无法将大小为 78543360 的数组重塑为形状 (51135,4,32,32)
【发布时间】:2021-03-21 23:19:28
【问题描述】:

遵循来自https://machinelearningmastery.com/how-to-develop-rnn-models-for-human-activity-recognition-time-series-classification/的本教程

我正在尝试在我的时间序列数据上实现 CNN-LSTM 网络模型。

verbose, epochs, batch_size = 0, 25, 64
n_timesteps, n_features, n_outputs = 
X_train.shape[1], X_train.shape[2], y_train.shape[1]
# reshape data into time steps of sub-sequences
n_steps, n_length = 4, 32
X_train = X_train.reshape((X_train.shape[0], n_steps, 
n_length, n_features))
X_test = X_test.reshape((X_test.shape[0], n_steps, 
n_length, n_features))
# define model
model = Sequential()
model.add(TimeDistributed(Conv1D(filters=64, 
kernel_size=3, activation='relu'), input_shape= 
(None,n_length,n_features)))
model.add(TimeDistributed(Conv1D(filters=64, 
kernel_size=3, activation='relu')))
model.add(TimeDistributed(Dropout(0.5)))
model.add(TimeDistributed(MaxPooling1D(pool_size=2)))
model.add(TimeDistributed(Flatten()))
model.add(LSTM(100))
model.add(Dropout(0.5))
model.add(Dense(100, activation='relu'))
model.add(Dense(n_outputs, activation='sigmoid'))
model.compile(loss='binary_crossentropy', 
optimizer='adam', metrics=['accuracy'])
model.fit(X_train, y_train, epochs=epochs, 
batch_size=batch_size, verbose=verbose)

以下是使用 reshape 之前的形状:

print(X_train.shape, y_train.shape, X_test.shape, 
y_test.shape)
(51135, 128, 12) (51135, 1) (21915, 128, 12) (21915, 1)

这是我遇到错误的代码部分: ValueError: cannot reshape array of size 78543360 into shape (51135, 4,32,32)

我该如何解决这个问题?

【问题讨论】:

    标签: python tensorflow time-series conv-neural-network lstm


    【解决方案1】:

    我无权访问您的数据,因此我为具有您指定的维度的数据创建了虚拟 np.zero 数组。运行它以查看维度发生了什么。没有遇到你描述的错误。我使用的代码如下。跑步和训练,使尺寸看起来正确。

    X_train=np.zeros((51135, 128, 12))
    y_train=np.zeros((51135, 1))
    X_test=np.zeros((21915, 128, 12))
    y_test=np.zeros((21915, 1))
    print(X_train.shape, y_train.shape, X_test.shape,y_test.shape)
    verbose, epochs, batch_size = 0, 25, 64
    n_timesteps, n_features, n_outputs = X_train.shape[1], X_train.shape[2], y_train.shape[1]
    print(n_timesteps, n_features, n_outputs)
    # reshape data into time steps of sub-sequences
    n_steps, n_length = 4, 32
    X_train = X_train.reshape((X_train.shape[0], n_steps, n_length, n_features))
    X_test = X_test.reshape((X_test.shape[0], n_steps,n_length, n_features))
    print (X_train.shape, X_test.shape)
    # define model
    model = Sequential()
    model.add(TimeDistributed(Conv1D(filters=64, 
    kernel_size=3, activation='relu'), input_shape= 
    (None,n_length,n_features)))
    model.add(TimeDistributed(Conv1D(filters=64, 
    kernel_size=3, activation='relu')))
    model.add(TimeDistributed(Dropout(0.5)))
    model.add(TimeDistributed(MaxPooling1D(pool_size=2)))
    model.add(TimeDistributed(Flatten()))
    model.add(LSTM(100))
    model.add(Dropout(0.5))
    model.add(Dense(100, activation='relu'))
    model.add(Dense(n_outputs, activation='sigmoid'))
    model.compile(loss='binary_crossentropy',optimizer='adam', metrics=['accuracy'])
    model.summary()
    model.fit(X_train, y_train, epochs=epochs, batch_size=batch_size, verbose=1)
    

    这是打印出来的数据。当然,全零训练数据是没有意义的,但维度适合 model.fit 执行

    (51135, 128, 12) (51135, 1) (21915, 128, 12) (21915, 1)
    128 12 1
    (51135, 4, 32, 12) (21915, 4, 32, 12)
    Epoch 1/25
    799/799 [==============================] - 9s 11ms/step - loss: 0.6051 - accuracy: 1.0000
    Epoch 2/25
    799/799 [==============================] - 5s 6ms/step - loss: 0.3479 - accuracy: 1.0000
    

    【讨论】:

      【解决方案2】:

      您正在尝试将 X_train 重塑为 (51135, 4,32,32)。这是不可能的,因为 X_train 的形状是 (51135, 128, 12)。 51135 x 128 x 12 != 51135 x 4 x 32 x 32

      【讨论】:

      • 嗨@Andrey,感谢您的回答。你能建议我如何解决这个错误吗?
      • @Alanda X_train 是什么?为什么会有这样的形状?
      猜你喜欢
      • 2021-08-02
      • 2021-03-20
      • 2018-10-22
      • 2019-11-29
      • 2020-01-29
      • 2021-07-18
      • 1970-01-01
      相关资源
      最近更新 更多