【问题标题】:Error when checking target: expected dense_2 to have 2 dimensions, but got array with shape (1, 1226, 2)检查目标时出错:预期 dense_2 有 2 个维度,但得到了形状为 (1, 1226, 2) 的数组
【发布时间】:2019-03-25 07:12:27
【问题描述】:

这是我要运行的代码:

y = Df[['label']]

y_train = np.column_stack((y_train['label']))

y_test = np.column_stack((y_test['label']))

data_dim = 18
timesteps = 1
num_classes = 2

model = Sequential()

model.add(LSTM(19, return_sequences=True,
               input_shape=(timesteps, data_dim)))  
model.add(LSTM(19, return_sequences=True))  
model.add(LSTM(19))  # return a single vector of dimension 30
model.add(Dense(1, activation='softmax'))

model.compile(loss='binary_crossentropy',
              optimizer='rmsprop',
              metrics=['accuracy'])

model.summary()

model.fit(X_train, y_train, batch_size = 400, epochs = 20, verbose = 

accuracy_train = accuracy_score(y_train, model.predict(X_train))
accuracy_test = accuracy_score(y_test, model.predict(X_test))
print('\nTrain Accuracy:{: .2f}%'.format(accuracy_train*100))
print('Test Accuracy:{: .2f}%'.format(accuracy_test*100))

我收到以下错误:

Error when checking target: expected dense_1 to have shape (1,) but got array with shape (1226,)

模型总结:

_________________________________________________________________
Layer (type)                 Output Shape              Param #   
=================================================================
lstm_37 (LSTM)               (None, 1, 19)             2888      
_________________________________________________________________
lstm_38 (LSTM)               (None, 1, 19)             2964      
_________________________________________________________________
lstm_39 (LSTM)               (None, 19)                2964      
_________________________________________________________________
dense_13 (Dense)             (None, 1)                 20        
=================================================================
Total params: 8,836
Trainable params: 8,836
Non-trainable params: 0

X_train

[[[-0.02444971  0.02444971  1.          1.          1.
    1.         -1.          2.          3.          4.
    3.          1.          0.          0.06938705 -0.04329106
    0.02458854  0.06025883  0.01439807]]]
[[[ 0.00733477  0.02033006 -1.          1.          1.
    1.         -1.          0.          1.          2.
    1.          0.          0.          0.03837079 -0.00829683
   -0.00734757  0.00985466 -0.04543226]]]

y_train

[[ 1  1 -1 ...  1  1 -1]]

【问题讨论】:

标签: python machine-learning keras time-series lstm


【解决方案1】:

看来你正在做二进制分类。因此,有几点需要修复:

  1. y_train 应该由零和一组成。以下会将所有 -1 标签转换为零:

    y_train = (y_train == 1).astype('float32')
    
  2. 重新调整标签的形状,使其具有(n_samples, 1) 的形状:

    y_train = y_train.reshape(-1, 1)
    
  3. 使用'sigmoid'作为最后一层的激活:

    model.add(Dense(1, activation='sigmoid'))
    

【讨论】:

    【解决方案2】:

    你应该在最后一层使用'sigmoid'激活函数,因为它是二分类

    【讨论】:

    • 您的答案可以通过额外的支持信息得到改进。请edit 添加更多详细信息,例如引用或文档,以便其他人可以确认您的答案是正确的。你可以找到更多关于如何写好答案的信息in the help center
    猜你喜欢
    • 1970-01-01
    • 2018-04-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-09-26
    • 2018-09-12
    相关资源
    最近更新 更多