Python tensorflow.contrib.rnn.stack_bidirectional_dynamic_rnn() Examples
The following are 6
code examples of tensorflow.contrib.rnn.stack_bidirectional_dynamic_rnn().
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example.
You may also want to check out all available functions/classes of the module
tensorflow.contrib.rnn
, or try the search function
.
Example #1
Source File: model.py From document-ocr with Apache License 2.0 | 6 votes |
def bidirectionnal_rnn(self, input_tensor, input_sequence_length): lstm_num_units = self.config.lstm_num_units print("rnn input tensor ===> ", input_tensor) with tf.variable_scope('lstm_layers'): fw_cell_list = [rnn.BasicLSTMCell(nh, forget_bias=1.0, name='fw_cell_%d'%(nh)) for nh in [lstm_num_units] * 2] bw_cell_list = [rnn.BasicLSTMCell(nh, forget_bias=1.0, name='bw_cell_%d'%(nh)) for nh in [lstm_num_units] * 2] stack_lstm_layer, _, _ = rnn.stack_bidirectional_dynamic_rnn( cells_fw=fw_cell_list, cells_bw=bw_cell_list, inputs=input_tensor, sequence_length=input_sequence_length, dtype=tf.float32) hidden_num = lstm_num_units * 2 rnn_reshaped = tf.nn.dropout(stack_lstm_layer, keep_prob=self.keep_prob) w = tf.get_variable(initializer=tf.truncated_normal([hidden_num, self.num_classes], stddev=0.02), name="w") w_t = tf.tile(tf.expand_dims(w, 0),[self.batch_size,1,1]) logits = tf.matmul(rnn_reshaped, w_t, name="nn_logits") self.logits = tf.identity(tf.transpose(logits, (1, 0, 2)), name='logits') return logits
Example #2
Source File: crnn_model.py From 2019-CCF-BDCI-OCR-MCZJ-OCR-IdentificationIDElement with MIT License | 5 votes |
def _sequence_label(self, inputdata, name): with tf.variable_scope(name_or_scope=name): fw_cell_list = [tf.nn.rnn_cell.LSTMCell(nh, forget_bias=1.0) for nh in [self._hidden_nums] * self._layers_nums] # Backward direction cells bw_cell_list = [tf.nn.rnn_cell.LSTMCell(nh, forget_bias=1.0) for nh in [self._hidden_nums] * self._layers_nums] stack_lstm_layer, _, _ = rnn.stack_bidirectional_dynamic_rnn(fw_cell_list, bw_cell_list, inputdata, #sequence_length=CFG.ARCH.SEQ_LENGTH * np.ones(CFG.TRAIN.BATCH_SIZE), dtype=tf.float32) #stack_lstm_layer = self.dropout(inputdata=stack_lstm_layer, keep_prob=0.5,\ # is_training=self._is_training, name='sequence_drop_out') [batch_s, _, hidden_nums] = inputdata.get_shape().as_list() # [batch, width, 2*n_hidden] shape = tf.shape(stack_lstm_layer) rnn_reshaped = tf.reshape(stack_lstm_layer, [shape[0] * shape[1], shape[2]]) w = tf.get_variable(name='w',shape=[hidden_nums, self._num_classes],\ initializer=tf.truncated_normal_initializer(stddev=0.02),trainable=True) # Doing the affine projection logits = tf.matmul(rnn_reshaped, w, name='logits') logits = tf.reshape(logits, [shape[0], shape[1], self._num_classes], name='logits_reshape') raw_pred = tf.argmax(tf.nn.softmax(logits), axis=2, name='raw_prediction') # Swap batch and batch axis rnn_out = tf.transpose(logits, [1, 0, 2], name='transpose_time_major') # [width, batch, n_classes] return rnn_out, raw_pred
Example #3
Source File: crnn_model.py From uai-sdk with Apache License 2.0 | 5 votes |
def __sequence_label(self, inputdata): """ Implement the sequence label part of the network :param inputdata: :return: """ with tf.variable_scope('LSTMLayers'): # construct stack lstm rcnn layer # forward lstm cell fw_cell_list = [rnn.BasicLSTMCell(nh, forget_bias=1.0) for nh in [self.__hidden_nums, self.__hidden_nums]] # Backward direction cells bw_cell_list = [rnn.BasicLSTMCell(nh, forget_bias=1.0) for nh in [self.__hidden_nums, self.__hidden_nums]] stack_lstm_layer, _, _ = rnn.stack_bidirectional_dynamic_rnn(fw_cell_list, bw_cell_list, inputdata, dtype=tf.float32) if self.phase.lower() == 'train': stack_lstm_layer = self.dropout(inputdata=stack_lstm_layer, keep_prob=0.5) [batch_s, _, hidden_nums] = inputdata.get_shape().as_list() # [batch, width, 2*n_hidden] rnn_reshaped = tf.reshape(stack_lstm_layer, [-1, hidden_nums]) # [batch x width, 2*n_hidden] w = tf.Variable(tf.truncated_normal([hidden_nums, self.__num_classes], stddev=0.1), name="w") # Doing the affine projection logits = tf.matmul(rnn_reshaped, w) logits = tf.reshape(logits, [batch_s, -1, self.__num_classes]) raw_pred = tf.argmax(tf.nn.softmax(logits), axis=2, name='raw_prediction') # Swap batch and batch axis rnn_out = tf.transpose(logits, (1, 0, 2), name='transpose_time_major') # [width, batch, n_classes] return rnn_out, raw_pred
Example #4
Source File: crnn_model.py From uai-sdk with Apache License 2.0 | 5 votes |
def __sequence_label(self, inputdata): """ Implement the sequence label part of the network :param inputdata: :return: """ with tf.variable_scope('LSTMLayers'): # construct stack lstm rcnn layer # forward lstm cell fw_cell_list = [rnn.BasicLSTMCell(nh, forget_bias=1.0) for nh in [self.__hidden_nums, self.__hidden_nums]] # Backward direction cells bw_cell_list = [rnn.BasicLSTMCell(nh, forget_bias=1.0) for nh in [self.__hidden_nums, self.__hidden_nums]] stack_lstm_layer, _, _ = rnn.stack_bidirectional_dynamic_rnn(fw_cell_list, bw_cell_list, inputdata, dtype=tf.float32) if self.phase.lower() == 'train': stack_lstm_layer = self.dropout(inputdata=stack_lstm_layer, keep_prob=0.5) [batch_s, _, hidden_nums] = inputdata.get_shape().as_list() # [batch, width, 2*n_hidden] rnn_reshaped = tf.reshape(stack_lstm_layer, [-1, hidden_nums]) # [batch x width, 2*n_hidden] w = tf.Variable(tf.truncated_normal([hidden_nums, self.__num_classes], stddev=0.1), name="w") # Doing the affine projection logits = tf.matmul(rnn_reshaped, w) logits = tf.reshape(logits, [batch_s, -1, self.__num_classes]) raw_pred = tf.argmax(tf.nn.softmax(logits), axis=2, name='raw_prediction') # Swap batch and batch axis rnn_out = tf.transpose(logits, (1, 0, 2), name='transpose_time_major') # [width, batch, n_classes] return rnn_out, raw_pred
Example #5
Source File: model.py From deep_learning with MIT License | 5 votes |
def bi_gru(self, inputs): """build the bi-GRU network. 返回个所有层的隐含状态。""" cells_fw = [self.gru_cell() for _ in range(self.n_layer)] cells_bw = [self.gru_cell() for _ in range(self.n_layer)] initial_states_fw = [cell_fw.zero_state(self.batch_size, tf.float32) for cell_fw in cells_fw] initial_states_bw = [cell_bw.zero_state(self.batch_size, tf.float32) for cell_bw in cells_bw] outputs, _, _ = rnn.stack_bidirectional_dynamic_rnn(cells_fw, cells_bw, inputs, initial_states_fw=initial_states_fw, initial_states_bw=initial_states_bw, dtype=tf.float32) return outputs
Example #6
Source File: crnn_net.py From CRNN_Tensorflow with MIT License | 4 votes |
def _sequence_label(self, inputdata, name): """ Implements the sequence label part of the network :param inputdata: :param name: :return: """ with tf.variable_scope(name_or_scope=name): # construct stack lstm rcnn layer # forward lstm cell fw_cell_list = [tf.nn.rnn_cell.LSTMCell(nh, forget_bias=1.0) for nh in [self._hidden_nums] * self._layers_nums] # Backward direction cells bw_cell_list = [tf.nn.rnn_cell.LSTMCell(nh, forget_bias=1.0) for nh in [self._hidden_nums] * self._layers_nums] stack_lstm_layer, _, _ = rnn.stack_bidirectional_dynamic_rnn( fw_cell_list, bw_cell_list, inputdata, dtype=tf.float32 ) stack_lstm_layer = self.dropout( inputdata=stack_lstm_layer, keep_prob=0.5, is_training=self._is_training, name='sequence_drop_out' ) [batch_s, _, hidden_nums] = inputdata.get_shape().as_list() # [batch, width, 2*n_hidden] shape = tf.shape(stack_lstm_layer) rnn_reshaped = tf.reshape(stack_lstm_layer, [shape[0] * shape[1], shape[2]]) w = tf.get_variable( name='w', shape=[hidden_nums, self._num_classes], initializer=tf.truncated_normal_initializer(stddev=0.02), trainable=True ) # Doing the affine projection logits = tf.matmul(rnn_reshaped, w, name='logits') logits = tf.reshape(logits, [shape[0], shape[1], self._num_classes], name='logits_reshape') raw_pred = tf.argmax(tf.nn.softmax(logits), axis=2, name='raw_prediction') # Swap batch and batch axis rnn_out = tf.transpose(logits, [1, 0, 2], name='transpose_time_major') # [width, batch, n_classes] return rnn_out, raw_pred