import numpy from keras.models import Sequential from keras.layers import Dense from keras.layers import LSTM from keras.utils import np_utils # fix random seed for reproducibility numpy.random.seed(7) def process_data(): # define the raw dataset alphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZ" # create mapping of characters to integers (0-25) and the reverse char_to_int = dict((c, i) for i, c in enumerate(alphabet)) int_to_char = dict((i, c) for i, c in enumerate(alphabet)) seq_length =3 sample_length=len(alphabet) dataX = [] dataY = [] for i in range(0, len(alphabet) - seq_length, 1): seq_in = alphabet[i:i + seq_length] # seq_in = alphabet[i] seq_out = alphabet[i + seq_length] dataX.append([char_to_int[char] for char in seq_in]) dataY.append(char_to_int[seq_out]) # print (seq_in, '->', seq_out) # print(dataX) # reshape X to be [samples, time steps, features] a=len(dataX) b=a/2 # print(a/2) # X = numpy.reshape(dataX, (len(dataX), 1,seq_length))#timesteps这个参数,我们设置了1 X = numpy.reshape(dataX, (len(dataX),seq_length, 1))#timesteps这个参数,此处设置了3 # X = numpy.reshape(dataX, (12, 1,seq_length ))#lstm要求三维的输入,所以需要将原始数据转成3维的,这里将原始数据做成了24个矩阵,每个矩阵是1行1列的, #当然可以做成12个矩阵,每个矩阵是1行2列的 # normalize,归一化 X = X / float(len(alphabet)) #可以把这个问题当作是一个序列的分类问题,26个不同的字母代表了不同的类别,我们用keras的内置的 to_categorical()函数把datay进行 one——hot编码,作为输出层的结果。 # one hot encode the output variable y = np_utils.to_categorical(dataY) # print(X.shape[1], X.shape[2]) return X,y,int_to_char,dataX,sample_length def buile_model(): # create and fit the model model = Sequential() model.add(LSTM(128, input_shape=(X.shape[1], X.shape[2])))#确定输入数据是多少行,多少列的,在单层的lstm下,若神经元个数为32训练313个epoch后,准确度达到100% #若神经元个数达到128,可以在训练到175个epoch后,准确度达到100% # model.add(LSTM(32)) model.add(Dense(y.shape[1], activation='softmax'))#输出应该是多少类,是由输出的字母类别数目决定的 model.compile(loss='categorical_crossentropy', optimizer='adam', metrics=['accuracy']) return model def train_model(model,X, y): for i in range(0,500): model.fit(X, y, epochs=1, batch_size=1, verbose=2)#模型进行训练 if i% 10 == 0: model.save_weights("./save/abcd.h5")#每10个迭代保存依次模型 # summarize performance of the model scores = model.evaluate(X, y, verbose=0) print("Model Accuracy: %.2f%%" % (scores[1]*100)) print('here',i) def predict(dataX,model,sample_length): # demonstrate some model predictions model.load_weights("./save/abcd.h5")#预测时先载入训练好的权重 for pattern in dataX: # print(pattern) # x = numpy.reshape(pattern, (1, 1, len(pattern)))#这个是在time_step=1时用 x = numpy.reshape(pattern, (1, len(pattern),1 ))#这个是在time_step=3时用 x = x / float(sample_length) prediction = model.predict(x, verbose=0) index = numpy.argmax(prediction) result = int_to_char[index] seq_in = [int_to_char[value] for value in pattern] print (seq_in, "->", result) if __name__ == '__main__': X, y,int_to_char,dataX,sample_length=process_data() model=buile_model() train_model(model,X, y) # predict(dataX,model,sample_length)
''' authour:wanghua date:2018/3/16 探讨下输入,预测之间维度的关系 如何增加多层的lstm ''' import numpy from keras.models import Sequential from keras.layers import Dense from keras.layers import LSTM from keras.utils import np_utils # fix random seed for reproducibility numpy.random.seed(7) def process_data(): # define the raw dataset alphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZ" # create mapping of characters to integers (0-25) and the reverse char_to_int = dict((c, i) for i, c in enumerate(alphabet)) int_to_char = dict((i, c) for i, c in enumerate(alphabet)) seq_length =3 sample_length=len(alphabet) dataX = [] dataY = [] for i in range(0, len(alphabet) - seq_length, 1): seq_in = alphabet[i:i + seq_length] # seq_in = alphabet[i] seq_out = alphabet[i + seq_length] dataX.append([char_to_int[char] for char in seq_in]) dataY.append(char_to_int[seq_out]) # print (seq_in, '->', seq_out) # print(dataX) # reshape X to be [samples, time steps, features] a=len(dataX) b=a/2 # print(a/2) # X = numpy.reshape(dataX, (len(dataX), 1,seq_length))#timesteps这个参数,我们设置了1 X = numpy.reshape(dataX, (len(dataX),seq_length, 1))#timesteps这个参数,此处设置了3 # X = numpy.reshape(dataX, (12, 1,seq_length ))#lstm要求三维的输入,所以需要将原始数据转成3维的,这里将原始数据做成了24个矩阵,每个矩阵是1行1列的, #当然可以做成12个矩阵,每个矩阵是1行2列的 # normalize,归一化 X = X / float(len(alphabet)) #可以把这个问题当作是一个序列的分类问题,26个不同的字母代表了不同的类别,我们用keras的内置的 to_categorical()函数把datay进行 one——hot编码,作为输出层的结果。 # one hot encode the output variable y = np_utils.to_categorical(dataY) # print(X.shape[1], X.shape[2]) return X,y,int_to_char,dataX,sample_length def buile_model(): # create and fit the model model = Sequential() model.add(LSTM(128,dropout_W=0.2, dropout_U=0.2, input_shape=(X.shape[1], X.shape[2]),return_sequences=True))#确定输入数据是多少行,多少列的,在单层的lstm下,若神经元个数为32训练313个epoch后,准确度达到100% #此处通过设置return_sequences=True,可以添加多层的lstm model.add(LSTM(256, return_sequences=False))#最后一层的lstm,return_sequences=False #若神经元个数达到128,可以在训练到175个epoch后,准确度达到100% # model.add(LSTM(32)) model.add(Dense(y.shape[1], activation='softmax'))#输出应该是多少类,是由输出的字母类别数目决定的 model.compile(loss='categorical_crossentropy', optimizer='adam', metrics=['accuracy']) return model def train_model(model,X, y): for i in range(0,500): model.fit(X, y, epochs=1, batch_size=1, verbose=2)#模型进行训练 if i% 10 == 0: model.save_weights("./save/abcd.h5")#每10个迭代保存依次模型 # summarize performance of the model scores = model.evaluate(X, y, verbose=0) print("Model Accuracy: %.2f%%" % (scores[1]*100)) print('here',i) def predict(dataX,model,sample_length): # demonstrate some model predictions model.load_weights("./save/abcd.h5")#预测时先载入训练好的权重 for pattern in dataX: # print(pattern) # x = numpy.reshape(pattern, (1, 1, len(pattern)))#这个是在time_step=1时用 x = numpy.reshape(pattern, (1, len(pattern),1 ))#这个是在time_step=3时用 x = x / float(sample_length) prediction = model.predict(x, verbose=0) index = numpy.argmax(prediction) result = int_to_char[index] seq_in = [int_to_char[value] for value in pattern] print (seq_in, "->", result) if __name__ == '__main__': X, y,int_to_char,dataX,sample_length=process_data() model=buile_model() train_model(model,X, y) # predict(dataX,model,sample_length)