本文最后更新于:3 个月前
书接上回,我们说到用cnn神经网络搭建一个mnist手写数字识别,这回还是使用keras搭建一个lstm神经网络模型。之前用tensorflow搭建过一个二进制加法识别模型,今天用keras复现一下上次的模型。
写在前面 在之前的机器学习:第一讲 LSTM神经网络简单入门 里面已经说的比较清楚了,这里只是简单的用keras和LSTM重构这个项目。
环境搭建
Python 3.7.4
Keras 2.2.4
Tensorflow 1.13.1
具体代码 导入模块 from keras.models import Sequentialfrom keras.layers import Densefrom keras.layers import Dropoutfrom keras.layers import LSTMfrom keras import lossesimport numpy as np
参数设置 BINARY_DIM = 8 epochs = 100 data_size = 10000
使用的函数 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 def int_2_binary (number, binary_dim ): binary_list = list (map (lambda x: int (x), bin (number)[2 :])) number_dim = len (binary_list) result_list = [0 ]*(binary_dim-number_dim)+binary_list return result_listdef binary2int (binary_array ): out = 0 for index, x in enumerate (reversed (binary_array)): out += x*pow (2 , index) return out binary = np.array([int_2_binary(x, BINARY_DIM) for x in range (2 **BINARY_DIM)])def batch_generation (largest_number ): n1 = np.random.randint(0 , largest_number) n2 = np.random.randint(0 , largest_number) add = n1 +n2 return np.append(binary[n1], binary[n2]),int_2_binary(add, BINARY_DIM+1 ) dataX = [] dataY = []for i in range (data_size): inputX,inputY=batch_generation(2 **BINARY_DIM) dataX.append(inputX) dataY.append(inputY) X = np.reshape(dataX, (len (dataX), 2 *BINARY_DIM, 1 )) Y = np.array(dataY)
搭建模型 model = Sequential() model.add(LSTM(256 , input_shape=(X.shape[1 ], X.shape[2 ]))) model.add(Dropout(0.2 )) model.add(Dense(Y.shape[1 ], activation='relu' )) model.compile (loss=losses.mean_squared_error, optimizer='adam' ) model.summary() model.fit(X, Y, epochs=epochs, batch_size=128 ) mp = r'./LSTM_Operation.h5' model.save(mp)
模型结构 _________________________________________________________________ Layer (type) Output Shape Param # ================================================================= lstm_1 (LSTM) (None, 256) 264192 _________________________________________________________________ dropout_1 (Dropout) (None, 256) 0 _________________________________________________________________ dense_1 (Dense) (None, 9) 2313 =================================================================
验证模型 dataX = [] dataY = []for i in range (1000 ): inputX,inputY=batch_generation(2 **BINARY_DIM) dataX.append(inputX) dataY.append(inputY) X = np.reshape(dataX, (len (dataX), 2 *BINARY_DIM, 1 )) Y = np.array(dataY) test_acc = model.evaluate(X, Y) print(str ((1 -test_acc)*100 )+"%" )
最后的准确率是99.98892793562263%,应该还算可以吧。(其实一共就65536种可能,常规算法只要运算65536次就可以达到100%的准确率)
总结 LSTM的难度主要还是在于模型的理解,而不是使用。 这一篇算是之前的翻版,使用keras来搭建模型确实省事很多。
懒人链接:点击下载