【问题标题】:Can a neural network be configured to output a matrix in Keras?可以将神经网络配置为在 Keras 中输出矩阵吗?
【发布时间】:2021-01-06 22:17:23
【问题描述】:

我正在预测 3 个维度的时间序列,并且想知道是否可以配置模型以在 Keras 中输出矩阵。

目前,我有 3 个回归模型我一个接一个地训练,一个用于预测每个输出维度。例如,对于 10 个样本的预测范围,每个模型都输出一个 10x1 的向量。但是,似乎使用单个模型可以更有效地完成此操作。

谢谢

【问题讨论】:

    标签: python tensorflow keras neural-network


    【解决方案1】:

    我找到了一种更好的方法来使用 Keras 核心层 Reshape 来实现这一点。对于通过预测变量大小输出的预测范围,在具有该形状的密集层之后添加一个 Reshape 层

    from keras.layers import Dense, Reshape, Sequential, LSTM
    
    model = Sequential()
    model.add(LSTM(100, activation='relu', return_sequences=True, input_shape=(n_steps_in, n_features)))
    model.add(LSTM(100, activation='relu'))
    model.add(Dense(n_steps_out*n_features))
    model.add(Reshape((n_steps_out,n_features)))
    
    model.compile(optimizer='adam', loss='mse')
    

    【讨论】:

      【解决方案2】:

      我想出了一个非常简单的解决方法。我只是在进场时重塑目标,在离场时重塑预测。

          input_data = input_data.reshape((num_simulations,input_samples*3))
          target_data = target_data.reshape((num_simulations,horizon*3))
      
          model.fit(input_data, target_data, validation_split=0.2, epochs=epochs, 
                  batch_size=batch_size, verbose=0, shuffle=True)
      
          prediction = model.predict(input_data, batch_size=batch_size)
          prediction = prediction.reshape((num_simulations,horizon,3))
      

      【讨论】:

        猜你喜欢
        • 2021-09-22
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2018-01-24
        • 1970-01-01
        • 2017-06-14
        • 2020-10-21
        • 1970-01-01
        相关资源
        最近更新 更多