def biLSTM_layer(self, lstm_inputs, lstm_dim, lengths, name=None): """ :param lstm_inputs: [batch_size, num_steps, emb_size] :return: [batch_size, 2*lstm_dim] """ with tf.variable_scope("char_BiLSTM" if not name else name): lstm_cell = {} for direction in ["forward", "backward"]: with tf.variable_scope(direction): lstm_cell[direction] = rnn.CoupledInputForgetGateLSTMCell( lstm_dim, use_peepholes=True, initializer=self.initializer, state_is_tuple=True) (outputs, (encoder_fw_final_state, encoder_bw_final_state)) = tf.nn.bidirectional_dynamic_rnn( lstm_cell["forward"], lstm_cell["backward"], lstm_inputs, dtype=tf.float32, sequence_length=lengths) final_state = tf.concat( (encoder_fw_final_state.h, encoder_bw_final_state.h), -1) return final_state
def biLSTM_layer(self, lstm_inputs, lstm_dim, lengths, name=None): """ :param lstm_inputs: [batch_size, num_steps, emb_size] :param lstm_dim: :param name: :return: [batch_size, num_steps, 2*lstm_dim] """ with tf.variable_scope("word_biLSTM" if not name else name): lstm_cell = {} for direction in ['forward', 'backward']: with tf.variable_scope(direction): lstm_cell[direction] = rnn.CoupledInputForgetGateLSTMCell( lstm_dim, use_peepholes=True, initializer=self.initializer, state_is_tuple=True ) outputs, final_status = tf.nn.bidirectional_dynamic_rnn( lstm_cell['forward'], lstm_cell['backward'], lstm_inputs, dtype=tf.float32, sequence_length=lengths ) return tf.concat(outputs, axis=2)
def biLSTM_layer(self, lstm_inputs, lstm_dim, lengths, name=None): """ :param lstm_inputs: [batch_size, num_steps, emb_size] :param lstm_dim: :param name: :return: [batch_size, num_steps, 2*lstm_dim] 为何返回是2*lstm_dim,因为其是双向的lstm。每个方向的输出为lstm_dim """ with tf.variable_scope("word_biLSTM" if not name else name): lstm_cell = {} for direction in ['forward', 'backward']: with tf.variable_scope(direction): lstm_cell[direction] = rnn.CoupledInputForgetGateLSTMCell( lstm_dim, use_peepholes=True, initializer=self.initializer, state_is_tuple=True) outputs, final_status = tf.nn.bidirectional_dynamic_rnn( lstm_cell['forward'], lstm_cell['backward'], lstm_inputs, dtype=tf.float32, sequence_length=lengths) # 因为单向的lstm输出的格式为[batch_size, num_steps,lstm_dim]。 # 2表示在lstm_dim这个维度进行拼接。 # 个人觉得outputs的输出格式为[[batch_size, num_steps,lstm_dim],[batch_size, num_steps,lstm_dim]] # 即是一个list。list里面的每一个元素是单向的lstm的输出 return tf.concat(outputs, axis=2)
def MultiRNN(self, x, weight, bias, initializer, activation, num_layers): x = tf.unstack(tf.reshape( x, [tf.shape(x)[0], self.STEPS, int(self.X_DIM / self.STEPS)]), self.STEPS, axis=1) lstm_cell = rnn.CoupledInputForgetGateLSTMCell(self.CELL_SIZE, forget_bias=10.0, use_peepholes=True, proj_clip=15.0, initializer=initializer, activation=activation) lstm_cell = rnn.MultiRNNCell([lstm_cell] * num_layers) outputs, _ = rnn.static_rnn(lstm_cell, x, dtype=tf.float32) return tf.matmul(outputs[-1], weight) + bias, outputs
def lstm_layer(): # B-LSTM 构建双向一层的LSTM # 定义两个LSTM网络 with tf.variable_scope('lstm_layer'): lstm_cell = {} for direction in ["forward", "backward"]: with tf.variable_scope(direction): lstm_cell[direction] = rnn.CoupledInputForgetGateLSTMCell( FLAGS.lstm_size, use_peepholes=True, initializer=initializers.xavier_initializer(), state_is_tuple=True) outputs, final_states = tf.nn.bidirectional_dynamic_rnn( lstm_cell["forward"], lstm_cell["backward"], embedding, dtype=tf.float32, sequence_length=lengths) return tf.concat(outputs, axis=2)
def biLSTM_layer(self): """ :param lstm_inputs: [batch_size, num_steps, emb_size] :return: [batch_size, num_steps, 2*lstm_dim] """ with tf.variable_scope("BiLSTM"): lstm_cell = {} for direction in ["forward", "backward"]: with tf.variable_scope(direction): lstm_cell[direction] = rnn.CoupledInputForgetGateLSTMCell( self.lstm_dim, use_peepholes=True, initializer=self.initializer, state_is_tuple=True) outputs, final_states = tf.nn.bidirectional_dynamic_rnn( lstm_cell["forward"], lstm_cell["backward"], self.embedding, dtype=tf.float32, sequence_length=self.lengths) return tf.concat(outputs, axis=2)
def biLSTM_layer(self, lstm_inputs, lstm_dim, lengths, name=None): with tf.variable_scope("char_BiLSTM", reuse=tf.AUTO_REUSE): lstm_cell = {} for direction in ["forward", "backward"]: with tf.variable_scope(direction): lstm_cell[direction] = rnn.CoupledInputForgetGateLSTMCell( lstm_dim, use_peepholes=True, initializer=self.initializer, state_is_tuple=True) (outputs, (encoder_fw_final_state, encoder_bw_final_state)) = tf.nn.bidirectional_dynamic_rnn( lstm_cell["forward"], lstm_cell["backward"], lstm_inputs, dtype=tf.float32, sequence_length=lengths) final_state = tf.concat( (encoder_fw_final_state.h, encoder_bw_final_state.h), -1) return tf.concat(outputs, axis=2), final_state
def biLSTM_layer(self, lstm_inputs, lstm_dim, lengths, name=None): with tf.variable_scope('char_BiLSTM', reuse=tf.AUTO_REUSE): lstm_cell = {} for direction in ["forward", "backward"]: with tf.variable_scope(direction): lstm_cell[direction] = rnn.CoupledInputForgetGateLSTMCell( lstm_dim, use_peepholes=True, initializer=self.initiaizer, state_is_tuple=True) (outputs, (encoder_fw_final_state, encoder_bw_final_state)) = tf.nn.bidirectional_dynamic_rnn( lstm_cell["forward"], lstm_cell["backward"], lstm_inputs, dtype=tf.float32, sequence_length=lengths) # 每句话经过当前cell后会得到一个state, # 经过多少个cell,就有多少个LSTMStateTuple,即每个cell都会输出一个 tuple(c, h) # state中的h跟output 的最后一个时刻的输出是一样的,即:output[:, -1, :] = state[0].h final_state = tf.concat( (encoder_fw_final_state.h, encoder_bw_final_state.h), -1) return tf.concat(outputs, -1), final_state
def basic_cell(hidden_size, initializer): _cell = rnn.CoupledInputForgetGateLSTMCell( hidden_size, use_peepholes=True, initializer=initializer) return _cell