keras 双向LSTM 简单示例[通俗易懂]

keras 双向LSTM 简单示例[通俗易懂]importtensorflow.compat.v1astffromkeras.layersimportConvLSTM2D,TimeDistributed,Conv2D,Bidirectionalimportnumpyasnpinputs_np=tf.convert_to_tensor(np.random.random((4,6,256,256,3)).astype(np.float32))#shape=[5,6,10,10,3]conv1=TimeDi.

大家好,又见面了,我是你们的朋友全栈君。

示例1: 仅返回各个时刻的状态 

import tensorflow.compat.v1 as tf
from keras.layers import ConvLSTM2D,TimeDistributed,Conv2D,Bidirectional
import numpy as np


inputs_np = tf.convert_to_tensor(np.random.random((4,6,256,256,3)).astype(np.float32))  # shape = [5,6,10,10,3]

conv1 = TimeDistributed(Conv2D(filters=10,kernel_size=(3,3),strides=(1,1)),input_shape=(6,256,256,3))(inputs_np)
lstm_outs= Bidirectional(ConvLSTM2D(filters=4,kernel_size=(3,3),strides=(1,1),padding='valid',activation='tanh',return_sequences=True),merge_mode=None)(conv1)

with tf.Session() as sess:
	sess.run(tf.global_variables_initializer())
	lstm_out_1,lstm_out_2 = sess.run(lstm_outs)

	print(lstm_out_1.shape)
	

注意: 在Bidirectional中,参数merge_mode有5种选择[“sum”,”mul”,”concat”,”ave”,None],默认是“concat”模式,两个LSTM的输出沿channel维度串联。 选择None时,输出不会被结合,作为一个列表返回。

 示例2:同时返回各个时刻的输出,与最后一个时刻的状态(注意输出的排序)

import tensorflow as tf
import numpy as np
import keras
from keras.layers import ConvLSTM2D,Bidirectional

lstm_input = np.random.random((4,6,30,30,3)).astype(np.float32)
lstm_input = tf.convert_to_tensor(lstm_input)

lstm_out1,lstm_out2,h1,c1,h2,c2 = Bidirectional(ConvLSTM2D(filters=1,kernel_size=[5,5],strides=(1,1),padding='valid',
                                                           activation='relu',batch_input_shape=(-1,6,30,30,3),
                                                           return_sequences=False,return_state=True),
                                                merge_mode=None)(lstm_input)


with tf.Session() as sess:
    sess.run(tf.global_variables_initializer())
    lstm_out1,lstm_out2,h1,c1,h2,c2= sess.run([lstm_out1,lstm_out2,h1,c1,h2,c2])
    print(lstm_out1==h1)
    print(lstm_out2==h2)

可见,在双向LSTM中,如果输出LSTM的最后一个时刻的cell状态, 得到的输出的排序是:lstm_out1, lstm_out2,  h1, c1, h2, c2。

其中lstm_out1,h1,c1是前向LSTM的输出,lstm_out2,h2,c2是后向LSTM的输出。

参考:https://keras.io/zh/layers/wrappers/#bidirectional

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请联系我们举报,一经查实,本站将立刻删除。

发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/151619.html原文链接:https://javaforall.net

(0)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号