大家好,又见面了,我是你们的朋友全栈君。
示例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
发布者:全栈程序员-用户IM,转载请注明出处:https://javaforall.cn/151619.html原文链接:https://javaforall.cn
【正版授权,激活自己账号】: Jetbrains全家桶Ide使用,1年售后保障,每天仅需1毛
【官方授权 正版激活】: 官方授权 正版激活 支持Jetbrains家族下所有IDE 使用个人JB账号...