我想创建一个有状态的LSTM
我的数据是68871 x 43,其中的功能在列号中。 1-42并在第5栏中标注。 43
我的keras LSTM代码用于分类数据
import numpy
import matplotlib.pyplot as plt
import math
from keras.models import Sequential
from keras.layers import Dense
from keras.layers import LSTM, Dropout
from sklearn.preprocessing import MinMaxScaler
from sklearn.metrics import mean_squared_error
# convert an array of values into a dataset matrix
def create_dataset(dataset, look_back):
dataX, dataY= [], []
for i in range(len(dataset)-look_back-1):
a = dataset[i:(i+look_back), 0:43]
dataX.append(a)
dataY.append(dataset[i + look_back, 43])
return numpy.array(dataX), numpy.array(dataY)
# fix random seed for reproducibility
#numpy.random.seed(7)
# load the dataset
look_back=5
dataset = numpy.loadtxt("Source.txt", delimiter=" ")
# normalize the dataset
scaler = MinMaxScaler(feature_range=(0, 1))
dataset[:,0:43] = scaler.fit_transform(dataset[:,0:43])
# split into train and test sets
train_size = int(len(dataset) * 0.67)
test_size = len(dataset) - train_size
train, test = dataset[0:train_size,:], dataset[train_size:len(dataset),:]
trainX, trainY = create_dataset(train, look_back)
testX, testY = create_dataset(test, look_back)
print trainX.shape
#trainX=numpy.reshape(trainX,(46117,43,25))
# create and fit the LSTM network
model = Sequential()
model.add(LSTM(10, input_dim=43))
#model.add(Dropout(0.3))
model.add(Dense(1))
model.compile(loss='mean_squared_error', optimizer='adagrad')
model.fit(trainX, trainY, nb_epoch=10, batch_size=5)
model.evaluate(testX, testY, batch_size=1)
请建议为使LSTM有状态所做的更改 非常感谢提前!!!!!
答案 0 :(得分:0)
model.add(LSTM(10, input_shape=(5,43), stateful=True))
是你想要的。请参阅here for documentation。
此外,如果您想使用有状态的lstm,那么对训练数据进行随机播放是没有意义的(这是默认值)。因此也要修改此行:
model.fit(trainX, trainY, nb_epoch=10, batch_size=5, shuffle=False)