keras ConvLSTM2D 的简单应用

本文深入探讨了ConvLSTM2D层在Keras中的应用,通过实例详细解析了return_sequences与return_state参数的作用,展示了如何获取不同时间点的状态及最终状态。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

示例1: 返回各时刻状态 

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

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

lstm_out1 = ConvLSTM2D(filters=1,kernel_size=[5,5],strides=(1,1),padding='valid',activation='relu',
                       input_shape=(6,30,30,3),return_sequences=True)(lstm_input)
lstm_out2 = ConvLSTM2D(filters=2,kernel_size=[5,5],strides=(1,1),padding='valid',activation='relu',
                       return_sequences=True)(lstm_out1)
lstm_out3 = ConvLSTM2D(filters=3,kernel_size=[5,5],strides=(1,1),padding='valid',activation='relu',
                       return_sequences=True)(lstm_out2)

with tf.Session() as sess:
    sess.run(tf.global_variables_initializer())
    lstm_out1_,lstm_out2_,lstm_out3_ = sess.run([lstm_out1,lstm_out2,lstm_out3])
    print(lstm_out1_.shape)
    print(lstm_out2_.shape)
    print(lstm_out3_.shape)

"""
返回:
(4, 6, 26, 26, 1)
(4, 6, 22, 22, 2)
(4, 6, 18, 18, 3)
"""

备注:

return_sequences: 默认是False,控制LSTM的输出:

  • False: 仅返回最后一个时刻的状态(hidden_state),shape = [B,H,W,C],是一个4维张量
  • True: 返回所有时刻的输出(hidden_state),shape = [B,Clip_len,H,W,C],是一个5维张量


示例2: 同时返回各时刻状态与最后一个时刻的state(包含state.h,state.c)

用return_state=True控制 

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

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

lstm_out,state_h,state_c = 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)(lstm_input)


with tf.Session() as sess:
    sess.run(tf.global_variables_initializer())
    lstm_out_,state_h_,state_c_= sess.run([lstm_out,state_h,state_c])
    print(lstm_out_==state_h_)
    print(lstm_out_.shape)
    print(state_h_.shape)
    print(state_c_.shape)

"""
返回:
   [ True]]]]
(4, 26, 26, 1)
(4, 26, 26, 1)
(4, 26, 26, 1)
"""

综上:

return_sequences: 决定是否返回所有时刻的状态

return_state:决定是否返回最后一个时刻的cell状态,由示例2结果可见,最后一个时刻的state = [h,c]

注意:在keras 中文文档中,在介绍ConvLSTM2D时,没有介绍 return_state 参数,该参数在LSTM的介绍中介绍,但是在ConvLSTM2D中通用。。。自己差点以为ConvLSTM2D中没有这个功能。。。

评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值