基于Keras构建LSTM

前言:

之前构建LSTM网络是通过Tensorflow,虽然也不错,可是接触到keras后觉得Keras真香!(虽然Tensorflow为默认后端)

Keras是一个高层神经网络API,Keras由纯Python编写而成并基TensorflowTheano以及CNTK后端。Keras 为支持快速实验而生,能够把你的idea迅速转换为结果,如果你有如下需求,请选择Keras:

  • 简易和快速的原型设计(keras具有高度模块化,极简,和可扩充特性)
  • 支持CNN和RNN,或二者的结合
  • 无缝CPU和GPU切换

Keras适用的Python版本是:Python 2.7-3.6

1.构建LSTM实例 

# design network
model = Sequential()
model.add(LSTM(30, input_shape=(train_x.shape[1], train_x.shape[2]),return_sequences=True))
model.add(LSTM(15, return_sequences=False))
model.add(Dropout(0.5)) #防止过拟合,每次更新参数时随机断开一定百分比(rate)的输入神经元
model.add(Dense(6, activation='softmax'))
model.summary() #打印出模型概况

 以上几句便已经构建好了用于分类的LSTM网络,网络结构如下图所示

接着是训练和测试网络

# fit network
history = model.fit(train_x,train_y, epochs=1000, batch_size=72, validation_data=(test_x, test_y), verbose=2, shuffle=True)
# test the model
score = model.evaluate(test_x, test_y, verbose=2) #evaluate函数按batch计算在某些输入数据上模型的误差
print('Test loss:', score[0])
print('Test accuracy:', score[1])
score = model.evaluate(train_x, train_y, verbose=2) #evaluate函数按batch计算在某些输入数据上模型的误差
print('Train loss:', score[0])
print('Train accuracy:', score[1])

然后可以利用history存储的训练信息,看到随着迭代次数的变化得到的模型在测试集和训练集上的表现

# plot history
pyplot.plot(history.history['loss'], label='train')
pyplot.plot(history.history['val_loss'], label='test')
pyplot.legend()
pyplot.title("label_lstm")
pyplot.show()
pyplot.plot(history.history['acc'], label='train')
pyplot.plot(history.history['val_acc'], label='test')
pyplot.legend()
pyplot.title("label_lstm")
pyplot.show()

loss及accuracy随着迭代次数的变化曲线图: 

 

 


2.LSTM层

keras.layers.recurrent.LSTM(units, activation='tanh', recurrent_activation='hard_sigmoid', use_bias=True, kernel_initializer='glorot_uniform', recurrent_initializer='orthogonal', bias_initializer='zeros', unit_forget_bias=True, kernel_regularizer=None, recurrent_regularizer=None, bias_regularizer=None, activity_regularizer=None, kernel_constraint=None, recurrent_constraint=None, bias_constraint=None, dropout=0.0, recurrent_dropout=0.0)

 2.1.参数:

  • units:输出维度,也是LSTM的门结构里隐藏层节点数目
  • input_dim输入维度,如果该lstm为网络第一层时,需要指定
  • input_length:输入的序列长度,

input_dim和input_length可以用input_shape统一代劳,

# as the first layer in a Sequential model
model = Sequential()
model.add(LSTM(32, input_shape=(10, 64)))
# now model.output_shape == (None, 32)
# note: `None` is the batch dimension.
-----------------------------------------------------------

# the following is identical:
model = Sequential()
model.add(LSTM(32, input_dim=64, input_length=10))

------------------------------------------------------------

# for subsequent layers, no need to specify the input size:
model.add(LSTM(16))
  • return_sequences:布尔值,默认False,控制返回类型。若为True则返回整个序列,否则仅返回输出序列的最后一个输出 

2.2函数输入及输出shape:

输入shape :
(samples,timesteps,input_dim)3D张量

输出shape:

如果return_sequences=True:返回(samples,timesteps,output_dim)的3D张量

否则,返回(samples,output_dim)的2D张量,return_sequences默认为False

所以当LSTM层后面仍然有LSTM层的话,需要设置return_sequences=True

# to stack recurrent layers, you must use return_sequences=True
# on any recurrent layer that feeds into another recurrent layer.
# note that you only need to specify the input size on the first layer.
model = Sequential()
model.add(LSTM(64, input_dim=64, input_length=10, return_sequences=True))
model.add(LSTM(32, return_sequences=True))
model.add(LSTM(10))

基于keras的lstm完整代码(含数据)见https://github.com/fff2zrx/lstm_example 

参考:

 

  • 11
    点赞
  • 65
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 13
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 13
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

fff2zrx

谢谢老板

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值