0

我需要有关使用 MLP 神经网络进行预测的帮助。我使用 keras 实现了网络,并使用缩放的monthly_sunspots 数据集对其进行了训练:

inputs = Input(shape=(look_back,))
inner = Dense(64)(inputs)
inner = Activation("relu")(inner)
inner = Dense(32)(inputs)
inner = Activation("relu")(inner)
inner = Dense(16)(inputs)
inner = Activation("relu")(inner)
outputs = Dense(1)(inner)
outputs = Activation("linear")(outputs)
#outputs = ReLU(max_value=max_range)(outputs)
mlp_model = Model(inputs=inputs, outputs=outputs)
mlp_opt  = Adam(lr=0.001)
mlp_model.compile(mlp_opt, loss="mean_squared_error")
mlp_history = mlp_model.fit(X_train, y_train, epochs=100, verbose=2, validation_data=(X_test, y_test), shuffle=False, batch_size=1)

有了这个结果:

Train Score: 0.00 MSE (0.06 RMSE) Test Score: 0.01 MSE (0.08 RMSE)

然后我绘制了train_set和test_set,结果很好:

这些是训练和测试趋势,以及损失和 val_loss 趋势

问题是当我尝试使用新数据进行预测时。我创建了一个函数,它使用窗口方法从种子中计算样本。这是功能:

def forward(model, seed_start, steps=3, lib="keras"):
  assert seed_start.shape[0] == 1, "Insert only one element"
  predictions = []
  series = []
  seed_start = seed_start.copy()

  def shift_array(array, nb_pos=1, default_value="NaN", fill=True):
    indexes = list(np.arange(nb_pos))
    new_arr = []
    for index, element in enumerate(array):
      if(index in indexes):
        pass
      else:
        new_arr.append(element)
    if(fill is True):
      while(len(new_arr) < len(array)):
        new_arr.append(default_value)
    return new_arr

  if(len(seed_start.shape) == 3):
    series.append(seed_start[0].reshape(1, -1)[0])
    #Hese is for recurrent newtorks
    for _ in range(steps):
      res = model.predict(seed_start)[0][0]
      sub_arr = seed_start[0].reshape(1, -1)[0]
      predictions.append([res])
      new_arr = shift_array(sub_arr)
      new_arr[len(new_arr) - 1] = res
      new_arr = np.array(new_arr)
      series.append(new_arr)
      new_arr = np.expand_dims(new_arr, axis=0)
      new_arr = np.expand_dims(new_arr, axis=2)
      seed_start = new_arr
  elif(len(seed_start.shape) == 2):
    #Here is for multilayer perceptron networks
    for i in range(steps):
      if(lib=="keras"):
        res = model.predict(seed_start)[0][0]
      elif(lib=="sklearn"):
        res = model.predict(seed_start)[0]
      else:
        print("lib not managed")
        return
      sub_arr = seed_start[0]
      predictions.append([res])
      new_arr = shift_array(sub_arr)
      new_arr[len(new_arr) - 1] = res
      new_arr = np.array(new_arr)
      series.append(new_arr)
      new_arr = np.expand_dims(new_arr, axis=0)
      seed_start = new_arr
  else:
    print("Dimension not managed")

  predictions = np.array(predictions)
  series = np.array(series)
  return predictions, series

给出了这个函数的一个例子:

seed=[[1, 2, 3]] ==> prediction=[4] new_seed=[[2, 3, 4]] ==> prediction=[5] 等等...

问题是预测收敛到一个唯一值:

这是转发的趋势

知道为什么吗?谢谢大家的回复。

4

0 回答 0