python 之LSTM 预测模型
dated
如果遇到这个错误:ValueError: Error when checking input: the list of Numpy arrays that you are passing to your model is not the size the model expected. Expected to e 1 array(s), but instead got the following list of 25000 arrays: [array([[11],...........]]是因为数据⼤⼩或者长度等超出了限制⽽导致的。所以⽤limit_imdb_datalength(x_train)来限制数据中的数值⼤⼩。
1、数据标准化:
必修二政治
如果使⽤sigmoid或者tanh作为激活函数的时候,将数据缩放到⼀个指定范围,避免输⼊数据的尺度敏感。
拖泥带水MinMaxScaler将数据缩放到⼀个指定的最⼤和最⼩值(通常是1-0)之间。作⽤:对⽅差⾮常⼩的属性可以增强其稳定性;维持稀疏
矩阵中为0的条⽬。
正则化,将每个样本缩放到单位范数(每个样本的范数为1), 如果后⾯要使⽤如⼆次型(点积)或者其它核⽅法计算两个样本之间的相似性这个⽅法会很有⽤。normalize,L1范数表⽰向量中所有元素的绝对值和,L2范数表⽰欧⽒距离,p范数表⽰向量元素绝对值的p
次⽅和的1/p次幂。
2、模型数据导⼊
IMDB影评数据导⼊,可以直接从Keras.datats中进⾏导⼊,imdb.load_data()进⾏在线下载导⼊(下载好的也可以导⼊),也可以⾃⼰编写数据导⼊函数进⾏数据导⼊,下⾯包含两个函数,⼀个是导⼊函数,⼀个是对导⼊的IMDB数据进⾏截取。1
from sklearn.preprocessing import MinMaxScaler 2
data = [[-1, 2], [-0.5, 6], [0, 10], [1, 18]]3
scaler=MinMaxScaler()4铁道运输
# scaler.fit(data)5
# ansform(data)6
data=scaler.fit_transform(data)#等于fit 加transform ,标准化数据7
data=scaler.inver_transform(data)#与fit_transform 相反,还原标准化后的数据8# scikit-learn/stable/modules/generated/sklearn.preprocessing.MinMaxScaler.html
1
from sklearn.preprocessing import normalize 2
#L1范数表⽰向量中所有元素的绝对值和,L2范数表⽰欧⽒距离3
x=normalize(data,norm='l1')#norm='l1', 'l2', or 'max', optional ('l2' by default)4# print(x)5# scikit-learn/stable/modules/generated/alize.html#alize
1
def load_imdb_data(path='imdb.npz',choodata='train',num_words=None):2
"""3 imdb.npz 数据提取
4 :param path: imdb数据地址
5 :param choodata: {'all','train','test','trainandtest'}
6 :param num_words:数据值上限
7 :return:x,y:数据标签
8 """
9 with np.load(path) as f:#提取数据
10 x_train, y_train = f['x_train'], f['y_train']
11 x_test, y_test = f['x_test'], f['y_test']
12 #测试数据提取
13 indices = np.arange(len(x_train))#数据长度
14 np.random.shuffle(indices)#随机打乱顺序
15 x_train = x_train[indices]#提取打乱顺序的训练数据
16 y_train = y_train[indices]#提取打乱顺序的训练数据的标签
17 #训练数据提取
18 indices = np.arange(len(x_test))
19 np.random.shuffle(indices)
20 x_test = x_test[indices]
21 y_test = y_test[indices]
22 #去除数据中超过上限的值,下限为0
23 if num_words is not None:
24 x_train = [[w for w in x if 0 <= w < num_words] for x in x_train]
25 x_test = [[w for w in x if 0 <= w < num_words] for x in x_test]
26 if choodata=='train':
27 return x_train,y_train
28 elif choodata=='test':补妆怎么补
29 return x_test,y_test
30 elif choodata=='trainandtest':
31 return (x_train,y_train),(x_test,y_test)
32 elif choodata=='all':
33 #数据拼接
34 xs = np.concatenate([x_train, x_test])
35 ys = np.concatenate([y_train, y_test])
36 return xs,ys
37
38def limit_imdb_datalength(datax,maxlen=500,truncating='post',dtype='int32'): 39 """
40限定数据集的长度,对过长的进⾏剪切,不⾜的⽤0填充
41 :param datax:输⼊数据⼆维数组
42 :param maxlen:限定数据集的长度
43 :param truncating:前向限定还是后向限定{'pre','post'}
44 :param dtype:数据类型
45 :return:x:处理后的数据
46 """
47 #构建⼀个相同的矩阵来装改变后的数据
48 num_samples=len(datax)
49 x = np.full((num_samples, maxlen), 0., dtype=dtype)
50 #对数据进⾏长度判断,并将处理后的数据装在新数组x中
51 for idx, s in enumerate(datax):
52 if not len(s):
53 continue
54 if len(s)>maxlen:
55 if truncating == 'post':
56 trunc = s[:maxlen]
57 x[idx, :len(trunc)] = trunc
58 elif truncating == 'pre':
59 trunc = s[-maxlen:]
60 x[idx, -len(trunc):] = trunc
61 el:
62 trunc=s
63 if truncating == 'post':
64 x[idx, :len(trunc)] = trunc
65 elif truncating == 'pre':
66 x[idx, -len(trunc):] = trunc
67 return x
国际旅⾏⼈数预测的数据导⼊,需要从⽹上下载international-airline-pasngers.csv数据,当然也可以写函数进⾏导⼊,也可以直接通过pandas函数中的read_csv函数进⾏导⼊,CSV格式可以直接通过office打开,看着和xls差不多,⾃⼰写导⼊函数的话可以根据Excel表格读取的数据特点来进⾏程序编写。在此直接调⽤read_csv进⾏数据导⼊,并对导⼊的数据进⾏归⼀化。
1data=read_csv('international-airline-pasngers.csv',ucols=[1],engine='python',skipfooter=3)
2data=data.values.astype('float32')
3scaler=MinMaxScaler()
4datat=scaler.fit_transform(data)#归⼀化
如果⽤国际旅⾏⼈数来进⾏建⽴的话,导⼊的数据还需要进⾏处理,导⼊的数据只是不同时间的⼈数,我们需要让不同时间段的⼈数有关系,将数据关联起来,如果当前时间是t,需要知道前两个时间t-1,t-2,来预测下⼀个时间t+1;并对导⼊的数据进⾏了训练数据和测试数据的分割,⽤⼀定数据的数据来进⾏训练,⽤⼀定数据的数据来进⾏预测。
1def create_data(data,look_back=3,splitsize=0.7):
2 """
3给定当前时间t,预测t+1,使⽤数据(t-2,t-1,t)
4 :param data:数据
凡鸟偏从末世来5 :param look_back:输出数据集的格式,默认3[1,2,3],如果改成4则为[1,2,3,4]
6 :return: (x_train,y_train),(x_test,y_test):实际数据,结果,训练数据,结果
7 """
8 datax,datay=[],[]
9 for i in range(len(data)-look_back-1):
10 x=data[i:i+look_back,0]#每次去look_back个数据
11 datax.append(x)
12 y=data[i+look_back,0]#如果x是0,1,2,则y是3
13 datay.append(y)
14 train_size=int(len(datax)*splitsize)
15 # test_size=int(len(datax)-train_size)
16 # 前半截数据⽤于训练,后半截数据⽤于测试
17 x_train,y_train=np.array(datax[0:train_size]),np.array(datay[0:train_size])
18 x_train = np.reshape(x_train, (x_train.shape[0], x_train.shape[1], 1))#样本、时间步长、特征
19 x_test,y_test=np.array(datax[train_size:len(datax)]),np.array(datay[train_size:len(datax)])
20 x_test = np.reshape(x_test, (x_test.shape[0], x_test.shape[1], 1))
21 return (x_train,y_train),(x_test,y_test)
3、国际旅⾏⼈数预测模型建⽴
2 #多层感知器模型,hidden_layer_num隐藏层层数
3 """这⾥导⼊的数据不需要进⾏reshpe改变,直接⽤datax.append(x),datay.append(y)的数据就⾏"""
4 model=Sequential()
5 model.add(Den(units=hidden_layer_num,input_dim=look_back,activation='relu'))
6 model.add(Den(units=hidden_layer_num, activation='relu'))
7 model.add(Den(units=1))
8 pile(loss='mean_squared_error',optimizer='adam')
9 return model
10
11def time_model():
12 #LSTM搭建的LSTM回归模型
13 model=Sequential()
14 model.add(LSTM(units=hidden_layer_num,input_shape=(1,look_back)))#四个隐藏层或者更多
wifi热点
15 model.add(Den(units=1))
16 pile(loss='mean_squared_error',optimizer='adam')
17 return model
18
19def time_step_model():
20 #使⽤时间步长的LSTM回归模型
21 model=Sequential()
22 model.add(LSTM(units=hidden_layer_num,input_shape=(look_back,1)))
23 model.add(Den(units=1))
24 pile(loss='mean_squared_error',optimizer='adam')
25 return model
26
27def memory_batches_model():
28 #LSTM的批次时间记忆模型
29 model=Sequential()
30 # 通过设置stateful为True来保证LSTM层内部的状态,从⽽获得更好的控制
31 model.add(LSTM(units=hidden_layer_num,batch_input_shape=(batch_size,look_back,1),stateful=True))
32 model.add(Den(units=1))
33 pile(loss='mean_squared_error',optimizer='adam')
34 return model
35
36def stack_memory_batches_model():
37 # 两个叠加的LSTM的批次时间记忆模型
38 model=Sequential()
39 #通过设施return_quences等于True来完成每个LSTM层之前的LSTM层必须返回序列,将LSTM扩展位两层
40 model.add(LSTM(units=hidden_layer_num,batch_input_shape=(batch_size,look_back,1),stateful=True,return_quences=True))
41 # 通过设置stateful为True来保证LSTM层内部的状态,从⽽获得更好的控制
42 model.add(LSTM(units=hidden_layer_num,input_shape=(batch_size,look_back,1),stateful=True))
43 model.add(Den(units=1))
44 pile(loss='mean_squared_error',optimizer='adam')
45 return model
4、IMDB预测模型建⽴
最简单的IMDB模型,有⼀个词嵌⼊层,⼀个LSTM层,⼀个输出层组成,为提⾼准确率,可以加⼊Dropout损失,卷积池化等,搭建复杂的⽹络。
2 model=Sequential()
3 model.add(Embedding(top_words,out_dimension,input_length=max_words))
4 model.add(LSTM(units=100))
5 model.add(Den(units=1,activation='sigmoid'))
6 pile(loss='binary_crosntropy',optimizer='adam',metrics=['accuracy'])
7 model.summary()
8 return model
9
10def dropout_imdb_model():
11 model=Sequential()
12 model.add(Embedding(top_words,out_dimension,input_length=max_words))
13 model.add(Dropout(dropout_rate))
14 model.add(LSTM(units=100))
15 # model.add(LSTM(units=100,dropout=0.2,recurrent_dropout=0.2))
16 model.add(Dropout(dropout_rate))
17 model.add(Den(units=1,activation='sigmoid'))
18 pile(loss='binary_crosntropy',optimizer='adam',metrics=['accuracy'])
19 model.summary()
20 return model
21
22def lstm_cnn_imdb_model():
23 model=Sequential()
24 model.add(Embedding(top_words,out_dimension,input_length=max_words))
25 model.add(Conv1D(filters=32,kernel_size=3,padding='same',activation='relu'))
26 model.add(MaxPooling1D(pool_size=2))
27 model.add(LSTM(units=100))
28 model.add(Den(units=1,activation='sigmoid'))
29 pile(loss='binary_crosntropy',optimizer='adam',metrics=['accuracy'])
30 model.summary()
31 return model
5、训练测试模型
1model=memory_batches_model()
现实主义画派2for i in range(epochs):
3 history = model.fit(X_train, y_train, epochs=epochs, batch_size=batch_size, verbo=0, shuffle=Fal)
4 mean_loss = np.mean(history.history['loss'])
5 print('mean loss %.5f for loop %s' % (mean_loss, str(i)))
6 _states()#重置模型中所有层的状态
1 (x_train,y_train),(x_test,y_test) = load_imdb_data(choodata='trainandtest',num_words=max_words)
2 x_train = limit_imdb_datalength(x_train,truncating='pre')
3 x_test = limit_imdb_datalength(x_test,truncating='pre')
4 model=easy_imdb_model()
5 model.fit(x_train,y_train,batch_size=batch_size,epochs=epochs,verbo=2)
6 scores=model.evaluate(x_test,y_test,verbo=2)
7 print("Accuracy:%.2f%%"%(scores[1]*100))