使用keras R API训练RNN

时间:2018-07-25 05:43:57

标签: r keras

我想使用keras R API在时间序列数据的结果变量之前预测n个样本,实际上我正在使用Python编写代码,但必须将其转换为R。这部分python代码如下:< / p>

# fit an LSTM network to training data
def fit_lstm(train, n_lag, n_seq, n_batch, nb_epoch, n_neurons):
    # reshape training into [samples, timesteps, features]
    X, y = train[:, 0:n_lag], train[:, n_lag:]
    X = X.reshape(X.shape[0], 1, X.shape[1])
    # design network
    model = Sequential()
    model.add(LSTM(n_neurons, batch_input_shape=(n_batch, X.shape[1], X.shape[2]), stateful=True))
    model.add(Dense(y.shape[1]))
    model.compile(loss='mean_squared_error', optimizer='adam')
    # fit network
    for i in range(nb_epoch):
        model.fit(X, y, epochs=1, batch_size=n_batch, verbose=0, shuffle=False)
        model.reset_states()
    return model

现在我正在用R编写代码:

> data2=read.csv("PSA.csv")
> respTS=data2$outcome[1:190]
> data.train=data2[1:nrow(data2)*0.8,]
> data.test=data2[-(1:nrow(data2)*0.8),]
> data.train=as.matrix(data.train)
> data.test=as.matrix(data.test)
> model <- keras_model_sequential() 

现在我正在尝试使用keras构建LTSM层,但不确定如何进一步对其进行编码?

0 个答案:

没有答案