如何在keras模型中获得置信区间?

o4tp2gmn  于 2021-08-25  发布在  Java
关注(0)|答案(0)|浏览(325)

我目前为timeseries预测建立了keras lstm模型。我对模型做了拟合数据并做了一些预测。我还输出了rmse值(如果有帮助的话)。
如何计算预测的置信区间?非常感谢。

def fit_model(train, batch_size, num_epochs, neurons):
  X, y = train[:, 0:-1], train[:, -1]
  X = X.reshape(X.shape[0], 1, X.shape[1])
  model = Sequential()
  model.add(LSTM(neurons, batch_input_shape=(batch_size, X.shape[1], X.shape[2]), stateful=True))
  model.add(Dense(1))
  model.compile(loss='mean_squared_error', optimizer='adam')
  for i in range(num_epochs):
      model.fit(X, y, epochs=1, batch_size=batch_size, verbose=0, shuffle=False)
      model.reset_states()
  return model

error_scores = list()
repeats = 5
for r in range(repeats):
    # fit the model
    batch_size = 1
    epochs = 200
    model = fit_model(train_scaled, batch_size, epochs, neurons)
    # forecast the entire training dataset to build up state for forecasting
    train_reshaped = train_scaled[:, 0].reshape(len(train_scaled), 1, 1)
    model.predict(train_reshaped, batch_size=batch_size)
    # forecast test dataset
    test_reshaped = test_scaled[:, 0].reshape(len(test_scaled), 1, 1)
    output = model.predict(test_reshaped, batch_size=batch_size)
    predictions = list()
    for i in range(len(output)):
        # make one-step forecast
        yhat = output[i,0]
        X = test_scaled[i, 0:-1]
        # invert scaling
        yhat = invert_scale(scaler, X, yhat)
        # invert differencing
        yhat = inverse_difference_series(raw_values, yhat, len(test_scaled)+1-i)
        # store forecast
        predictions.append(yhat)

      rmse = sqrt(mean_squared_error(raw_values[-80:], predictions))
      print('%d) Test RMSE: %.3f' % (r+1, rmse))
      error_scores.append(rmse)

暂无答案!

目前还没有任何答案,快来回答吧!

相关问题