我正在使用股市数据训练LSTM模型。当我使用“ model.fit()”训练模型时,会得到一些损失值。但是,当我使用相同的训练集评估模型时,损失值与训练过程中的最佳损失值不同。谁能指出我在哪里出错?代码如下。
def create_model(optimizer,num_neurons,input_timesteps,batch_size,input_dims):
model = Sequential()
model.add (LSTM (num_neurons,activation = 'tanh',dropout=0.2,stateful=True,
return_sequences=True, batch_input_shape =(batch_size,input_timesteps, input_dims) ))
model.add (LSTM (num_neurons,activation = 'tanh',dropout=0.2,stateful=True,
return_sequences=False, batch_input_shape =(batch_size,input_timesteps, input_dims) ))
model.add(Dense(1, activation='linear'))
model.compile(loss='mean_squared_error', optimizer=optimizer)
return model
erl_stop = EarlyStopping( monitor='val_loss', mode='min',restore_best_weights=True)
model.fit(x_train,y_train,epochs=num_epochs,batch_size=c['batch_size'],shuffle=False,callbacks=[erl_stop])
train_score=model.evaluate(x_train,y_train,batch_size=c['batch_size'])