为 LSTM 建模设置 Keras 选项

Setup Keras options for LSTM modelling

我正在尝试弄清楚预测某些值的设置过程。目前,我无法理解以下代码中的问题:

        in_neurons = 1
        out_neurons = 1
        hidden_neurons = 20
        nb_features = 9

        # retrieve data
        y_train = train.pop(target).values
        X_train = pd.concat([train[['QTR_HR_START', 'QTR_HR_END', 'HOLIDAY_RANK_', 'SPECIAL_EVENT_RANK_',
                                    'IS_AM', 'IS_TOP_RANKED', 'AWARDS_WINS_ANY', 'YEARS_SINCE_RELEASE']],
                             pd.DataFrame({'DATETIME': pd.DatetimeIndex(train['DATETIME']).astype(np.int64)})])
        X_train = X_train.values

        y_test = test.pop(target).values
        X_test = pd.concat([test[['QTR_HR_START', 'QTR_HR_END', 'HOLIDAY_RANK_', 'SPECIAL_EVENT_RANK_',
                                    'IS_AM', 'IS_TOP_RANKED', 'AWARDS_WINS_ANY', 'YEARS_SINCE_RELEASE']],
             pd.DataFrame({'DATETIME': pd.DatetimeIndex(test['DATETIME']).astype(np.int64)})])
        X_test = X_test.values

        model = Sequential()
        model.add(TimeDistributed(Dense(8, input_shape=(X_train.shape[0], 100, nb_features), activation='softmax')))
        model.add(LSTM(4, dropout_W=0.2, dropout_U=0.2))
        model.add(Dense(1))
        model.add(Activation("sigmoid"))
        model.compile(loss="mean_squared_error", optimizer="rmsprop", metrics=['accuracy'])

在运行代码之后,我得到了一个异常:

raise Exception('The first layer in a Sequential model must ' Exception: The first layer in a Sequential model must get an input_shape or batch_input_shape argument.

哪里不对请指教

EDIT1: 我刚刚按照官方文档中的说明配置了模型 - http://keras.io/layers/recurrent/

model.add(LSTM(32, input_dim=nb_features, input_length=100))
model.compile(loss="mean_squared_error", optimizer="rmsprop", metrics=['accuracy'])

Exception: Error when checking model input: expected lstm_input_1 to have 3 dimensions, but got array with shape (48614, 9)

它很旧,但我发布以备将来使用。 Keras 作为输入需要 3D 数据,如错误所述。它是样本、时间步长、特征。尽管您有 (48614, 9),但 Keras 将其视为 2D - [样本、特征]。为了修复它,做这样的事情

def reshape_dataset(train):
    trainX = numpy.reshape(train, (train.shape[0], 1, train.shape[1]))
    return numpy.array(trainX)

x = reshape_dataset(your_dataset_48614, 9)

现在 X 应该是 48614,1, 9 即 [样本、时间步长、特征] - 3D