检查目标时出错:预期的activation_1的形状为(1,),但数组的形状为(10,)

时间:2019-01-19 17:28:50

标签: python tensorflow keras forecasting

我对此模型有疑问,该模型正试图在未来10天预测股市:

model = Sequential()
model.add(LSTM(input_shape=(None, INPUT_DIM), 
          units=UNROLL_LENGTH, return_sequences=True))
model.add(Dropout(0.2))
model.add(LSTM(128, return_sequences=False))
model.add(Dropout(0.2))
model.add(Dense(10, activation='softmax'))
model.add(Activation('linear'))

start = time.time()
model.compile(loss='sparse_categorical_crossentropy', optimizer='adam')
model.fit(x_train_unroll, y_train_unroll, batch_size=BATCH_SIZE,epochs=EPOCHS, verbose=2, validation_split=0.05)

错误:

  

ValueError:检查目标:预期的activation_1时出错   形状(1,),但数组的形状为(10,)

numpy数组的形状:

  

x_train(1968,50,3),y_train(1968,10),x_test(450,50,3),y_test   (450,10)

*X_TRAIN_UNROLL*

[[[0.12339965 0.1352139  0.11937183]
[0.12231633 0.16698145 0.12354637]
[0.12261178 0.13978988 0.11837789]
 ...
[0.04057514 0.16677908 0.03448961]
[0.03998424 0.16039329 0.03439022]
[0.03407524 0.18277416 0.03906172]]


*Y_TRAIN_UNROLL*

[[0.06529447 0.06007485 0.06165058 ... 0.06342328 0.0627339 0.05465826]
 [0.06007485 0.06165058 0.06204451 ... 0.0627339  0.05465826 0.05515068]
[0.06165058 0.06204451 0.06135513 ... 0.05465826 0.05515068 0.04687808]
...
[0.68505023 0.67096711 0.66988379 ... 0.66525507 0.66289147 0.64171755]
[0.67096711 0.66988379 0.66968682 ... 0.66289147 0.64171755 0.65195982]
[0.66988379 0.66968682 0.67234587 ... 0.64171755 0.65195982 0.64250542]]

1 个答案:

答案 0 :(得分:0)

您的输出未经过稀疏编码,因此应将categorical_crossentropy用作损失函数,而不要使用sparse_categorical_crossentropy。另外,可以删除模型末尾的Linear激活,在此不执行任何操作。