LSTM的神经网络keras实现

加载keras模块

from keras.models import Sequential
from keras.layers import LSTM, Dense
from keras.datasets import mnist
from keras.utils import np_utils
from keras import initializations

def init_weights(shape, name=None):return initializations.normal(shape, scale=0.01, name=name)

绘制模型,需要加载plot

from keras.utils.visualize_util import plot

变量初始化

# Hyper parameters
batch_size = 128
nb_epoch = 10

# Parameters for MNIST dataset
img_rows, img_cols = 28, 28
nb_classes = 10

# Parameters for LSTM network
nb_lstm_outputs = 30
nb_time_steps = img_rows
dim_input_vector = img_cols

准备数据

# Load MNIST dataset
(X_train, y_train), (X_test, y_test) = mnist.load_data()
print('X_train original shape:', X_train.shape)
input_shape = (nb_time_steps, dim_input_vector)

X_train = X_train.astype('float32') / 255.
X_test = X_test.astype('float32') / 255.
Y_train = np_utils.to_categorical(y_train, nb_classes)
Y_test = np_utils.to_categorical(y_test, nb_classes)

print('X_train shape:', X_train.shape)
print(X_train.shape[0], 'train samples')
print(X_test.shape[0], 'test samples')

建立模型

# Build LSTM network
model = Sequential()
model.add(LSTM(nb_lstm_outputs, input_shape=input_shape))
model.add(Dense(nb_classes, activation='softmax', init=init_weights))

打印模型

model.summary()

绘制模型结构图,并保存成图片

plot(model, to_file='lstm_model.png')

编译模型

model.compile(optimizer='rmsprop', loss='categorical_crossentropy',
 metrics=['accuracy'])

迭代训练

history = model.fit(X_train, Y_train, nb_epoch=nb_epoch, 
batch_size=batch_size, shuffle=True, verbose=1)

模型评估

score = model.evaluate(X_test, Y_test, verbose=1)
print('Test score:', score[0])
print('Test accuracy:', score[1])






你可能感兴趣的:(工作实践,人工智能)