Bi-LSTM的理解以及 Tensorflow实现

Bidirectional LSTM,由两个LSTMs上下叠加在 一起组成。输出由这两个LSTMs的隐藏层的状态决定。

def bilstm(self,x):
        
        # 输入的数据格式转换
        # x.shape [batch_size, time_steps, input_size]
        x=tf.transpose(x,[1,0,2])
        fw_x = tf.reshape(x, [-1, self.n_input_text]) # step*batch, feature
        fw_x = tf.split(0, self.n_step_text, fw_x)
        
    
        with tf.variable_scope('bilstm_lt'):
            
            #定义Cell,单层LSTM
            lstm_fw_cell = rnn_cell.BasicLSTMCell(self.n_hidden_text, forget_bias=1.0, state_is_tuple=True)#前向的lstm cell
            lstm_bw_cell = rnn_cell.BasicLSTMCell(self.n_hidden_text, forget_bias=1.0, state_is_tuple=True)#反向的rnn cell
            
            #dropout
            lstm_fw_cell = rnn_cell.DropoutWrapper(cell=lstm_fw_cell, input_keep_prob=1.0, output_keep_prob=keep_prob)
            lstm_bw_cell = rnn_cell.DropoutWrapper(cell=lstm_bw_cell, input_keep_prob=1.0, output_keep_prob=keep_prob)
            
            #构建双向的RNN网络
            with tf.variable_scope('fw_lt'):
                 (output_fw, state_fw) = rnn.rnn(lstm_fw_cell,fw_x,dtype=tf.float32)
                 t=tf.convert_to_tensor(output_fw)
                 print (t.get_shape().as_list())
            with tf.variable_scope('bw_lt'):
                bw_x = tf.reverse(x, [True,False,False])# reverse time dim
                bw_x = tf.reshape(bw_x, [-1, self.n_input_text])  # step*batch, feature
                bw_x = tf.split(0, self.n_step_text, bw_x)
                (output_bw, state_bw) = rnn.rnn(lstm_bw_cell,bw_x,dtype=tf.float32)
                
            # output_bw.shape = [timestep_size, batch_size, hidden_size]
            output_bw = tf.reverse(output_bw, [True,False,False])
            output = tf.concat(2,[output_fw, output_bw])#在第2个维度上,将output_fw, output_bw拼接
        return output#返回值:(outputs, output_states:最后一层隐藏层)

 

  • 2
    点赞
  • 45
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值