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))
#可以
keras 中的lstm
最新推荐文章于 2024-06-19 10:06:39 发布
![](https://img-home.csdnimg.cn/images/20240711042549.png)