tf rnn输入输出的维度和权重的维度

本文通过示例介绍了TensorFlow中LSTM层的return_sequences和return_state参数。return_sequences决定是否返回整个输出序列,而return_state用于决定是否返回最后一个状态。示例展示了这四个不同设置下的输出形状,帮助理解LSTM在网络中的行为。
摘要由CSDN通过智能技术生成
demo
import tensorflow as tf
from tensorflow import keras
from tensorflow.keras import Sequential, Model
from tensorflow.keras.layers import Input, LSTM, Dense

import numpy as np

x = np.random.random(size=(10,4,2))

inputs = Input(shape=(4,2))
lstm1 = LSTM(units=3, return_sequences=False, return_state=False)(inputs)
lstm2 = LSTM(units=3, return_sequences=True, return_state=False)(inputs)
lstm3 = LSTM(units=3, return_sequences=True, return_state=True)(inputs)
lstm4 = LSTM(units=3, return_sequences=False, return_state=True)(inputs)

model1 = Model(inputs=inputs, outputs=lstm1)
model2 = Model(inputs=inputs, outputs=lstm2)
model3 = Model(inputs=inputs, outputs=lstm3)
model4 = Model(inputs=inputs, outputs=lstm4)
res1 = model1.predict(x)
res2 = model2.predict(x)
res3 = model3.predict(x)
res4 = model4.predict(x)

print("res1只有一个返回值")
print(res1.shape)

print("res2只有一个返回值")
print(res2.shape)

print("res3有三个返回值")
for i in res3:
	print(i.shape)

上面的代码表示一个
x i   [ 10 , 2 ] ∗ w x   [ 2 , 3 ] = [ 10 , 3 ] x_i ~ [10, 2] * w_x ~ [2, 3] = [10,3] xi [10,2]wx [2,3]=[10,3]
h i   [ 10 , 3 ] ∗ w h [ 3 , 3 ] = [ 10 , 3 ] h_i ~ [10,3] * w_h [3, 3] = [10, 3] hi [10,3]wh[3,3]=[10,3]

return参数

lstm中有两个参数
return_sequences
return_state

return_sequences
  return_sequences: Boolean. Whether to return the last output in the output sequence, or the full sequence. Default: `False`.

这里是选择是否返回last_output,那么什么是last_output
output就是每个cell的output总和,每一个cell的输出为 [ 10 , 3 ] [10,3] [10,3],所有的output为 [ 10 , 4 , 3 ] [10,4,3] [10,4,3]
last_output就是所有cell总和中最后一个输出项

return_state
return_state: Boolean. Whether to return the last state in addition to the output. Default: `False`

state就是 h h h,它的维度是 [ 10 , 3 ] [10,3] [10,3]

ref

参考一

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值