tensorflow中的lstm的state

   

考虑 state_is_tuple

   

Output, new_state = cell(input, state)

   

state其实是两个 一个 c state,一个m(对应下图的hidden 或者h) 其中m(hidden)其实也就是输出

   

   

   

   

new_state = (LSTMStateTuple(c, m) if self._state_is_tuple

else array_ops.concat(1, [c, m]))

return m, new_state

   

   

def basic_rnn_seq2seq(

encoder_inputs, decoder_inputs, cell, dtype=dtypes.float32, scope=None):

with variable_scope.variable_scope(scope or "basic_rnn_seq2seq"):

_, enc_state = rnn.rnn(cell, encoder_inputs, dtype=dtype)

return rnn_decoder(decoder_inputs, enc_state, cell)

   

   

def rnn_decoder(decoder_inputs, initial_state, cell, loop_function=None,

scope=None):

with variable_scope.variable_scope(scope or "rnn_decoder"):

state = initial_state

outputs = []

prev = None

for i, inp in enumerate(decoder_inputs):

if loop_function is not None and prev is not None:

with variable_scope.variable_scope("loop_function", reuse=True):

inp = loop_function(prev, i)

if i > 0:

variable_scope.get_variable_scope().reuse_variables()

output, state = cell(inp, state)

outputs.append(output)

if loop_function is not None:

prev = output

return outputs, state

   

   

这里decoder用了encoder的最后一个state 作为输入

   

然后输出结果是decoder过程最后的state 加上所有ouput的集合(也就是hidden的集合)

注意ouputs[-1]其实数值和state里面的m是一致的

当然有可能后面outputs dynamic rnn 会补0

   

encode_feature, state = melt.rnn.encode(

cell,

inputs,

seq_length,

encode_method=0,

output_method=3)

   

encode_feature.eval()

array([[[ 4.27834410e-03, 1.45841937e-03, 1.25767402e-02,
5.00775501e-03],
[ 6.24437723e-03, 2.60074623e-03, 2.32168660e-02,
9.47457738e-03],
[ 7.59789022e-03, -5.34060055e-05, 1.64511874e-02,
-5.71310846e-03],
[ 0.00000000e+00, 0.00000000e+00, 0.00000000e+00,
0.00000000e+00]]], dtype=float32)

   

   

state[1].eval()

array([[ 7.59789022e-03, -5.34060055e-05, 1.64511874e-02,
-5.71310846e-03]], dtype=float32)

   

   

   

TensorFlow中,LSTM的参数设置可以通过定义LSTM层时传入的参数来完成。其中一些重要的参数包括: 1. units:指定LSTM中的隐藏单元数量。 2. activation:指定激活函数,默认为tanh函数。 3. recurrent_activation:指定递归激活函数,默认为sigmoid函数。 4. use_bias:指定是否使用偏置,默认为True。 5. kernel_initializer:指定权重矩阵的初始化方法,默认为glorot_uniform方法。 6. recurrent_initializer:指定循环权重矩阵的初始化方法,默认为orthogonal方法。 7. bias_initializer:指定偏置向量的初始化方法,默认为zeros方法。 8. unit_forget_bias:指定是否为遗忘门添加偏置,默认为True。 9. dropout:指定输入和循环状态的丢弃率,默认为0.0,即不丢弃。 10. recurrent_dropout:指定循环状态的丢弃率,默认为0.0,即不丢弃。 11. return_sequences:指定是否返回每个时间步的输出序列,默认为False。 12. return_state:指定是否返回最后一个时间步的输出和状态,默认为False。 13. go_backwards:指定是否从后向前计算,默认为False。 14. stateful:指定是否在批处理之间保持LSTM状态,默认为False。 你可以使用tf.keras.layers.LSTM函数来定义LSTM层,并在其中传入相应的参数来设置LSTM参数。例如: ``` lstm_layer = tf.keras.layers.LSTM(units=128, activation='tanh', recurrent_activation='sigmoid', use_bias=True, kernel_initializer='glorot_uniform', recurrent_initializer='orthogonal', bias_initializer='zeros', unit_forget_bias=True, dropout=0.2, return_sequences=True) ``` 在这个示例中,我们定义了一个具有128个隐藏单元的LSTM层,使用tanh作为激活函数,sigmoid作为递归激活函数,使用偏置,权重矩阵和循环权重矩阵使用glorot_uniform和orthogonal初始化,偏置向量使用zeros初始化,为遗忘门添加偏置,丢弃率为0.2,并返回每个时间步的输出序列。 通过设置这些参数,你可以根据需要调整LSTM模型的性能和行为。<span class="em">1</span><span class="em">2</span><span class="em">3</span><span class="em">4</span>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值