Python tensorflow.contrib.rnn.static_bidirectional_rnn() Examples

The following are 6 code examples of tensorflow.contrib.rnn.static_bidirectional_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: ops.py    From easy-tensorflow with MIT License 6 votes vote down vote up
def BiRNN(x, weights, biases, timesteps, num_hidden):
    # Prepare data shape to match `rnn` function requirements
    # Current data input shape: (batch_size, timesteps, n_input)
    # Required shape: 'timesteps' tensors list of shape (batch_size, num_input)

    # Unstack to get a list of 'timesteps' tensors of shape (batch_size, num_input)
    x = tf.unstack(x, timesteps, 1)

    # Define lstm cells with tensorflow
    # Forward direction cell
    lstm_fw_cell = rnn.BasicLSTMCell(num_hidden, forget_bias=1.0)
    # Backward direction cell
    lstm_bw_cell = rnn.BasicLSTMCell(num_hidden, forget_bias=1.0)

    # Get BiRNN cell output
    outputs, _, _ = rnn.static_bidirectional_rnn(lstm_fw_cell, lstm_bw_cell, x,
                                                 dtype=tf.float32)

    # Linear activation, using rnn inner loop last output
    return tf.matmul(outputs[-1], weights) + biases 
Example #2
Source File: base_controller.py    From EAS with MIT License 5 votes vote down vote up
def build(self):
		self._define_input()
		
		output = self.input_seq
		output = embedding(output, self.vocab.size, self.embedding_dim, name='layer_embedding')
		input_dim = self.embedding_dim
		
		# Prepare data shape to match rnn function requirements
		# Current data input shape: [batch_size, num_steps, input_dim]
		# Required shape: 'num_steps' tensors list of shape [batch_size, input_dim]
		output = tf.transpose(output, [1, 0, 2])
		output = tf.reshape(output, [-1, input_dim])
		output = tf.split(output, self.num_steps, 0)
		
		if self.bidirectional:
			# 'num_steps' tensors list of shape [batch_size, rnn_units * 2]
			fw_cell = build_cell(self.rnn_units, self.cell_type, self.rnn_layers)
			bw_cell = build_cell(self.rnn_units, self.cell_type, self.rnn_layers)
			output, state_fw, state_bw = rnn.static_bidirectional_rnn(
				fw_cell, bw_cell, output, dtype=tf.float32, sequence_length=self.seq_len, scope='encoder')
			
			if isinstance(state_fw, tf.contrib.rnn.LSTMStateTuple):
				encoder_state_c = tf.concat([state_fw.c, state_bw.c], axis=1, name='bidirectional_concat_c')
				encoder_state_h = tf.concat([state_fw.h, state_bw.h], axis=1, name='bidirectional_concat_h')
				state = tf.contrib.rnn.LSTMStateTuple(c=encoder_state_c, h=encoder_state_h)
			elif isinstance(state_fw, tf.Tensor):
				state = tf.concat([state_fw, state_bw], axis=1, name='bidirectional_concat')
			else:
				raise ValueError
		else:
			# 'num_steps' tensors list of shape [batch_size, rnn_units]
			cell = build_cell(self.rnn_units, self.cell_type, self.rnn_layers)
			output, state = rnn.static_rnn(cell, output, dtype=tf.float32, sequence_length=self.seq_len,
										   scope='encoder')
		
		output = tf.stack(output, axis=0)  # [num_steps, batch_size, rnn_units]
		output = tf.transpose(output, [1, 0, 2])  # [batch_size, num_steps, rnn_units]
		self.encoder_output = output
		self.encoder_state = state
		return output, state 
Example #3
Source File: base_controller.py    From auptimizer with GNU General Public License v3.0 5 votes vote down vote up
def build(self):
		self._define_input()
		
		output = self.input_seq
		output = embedding(output, self.vocab.size, self.embedding_dim, name='layer_embedding')
		input_dim = self.embedding_dim
		
		# Prepare data shape to match rnn function requirements
		# Current data input shape: [batch_size, num_steps, input_dim]
		# Required shape: 'num_steps' tensors list of shape [batch_size, input_dim]
		output = tf.transpose(output, [1, 0, 2])
		output = tf.reshape(output, [-1, input_dim])
		output = tf.split(output, self.num_steps, 0)
		
		if self.bidirectional:
			# 'num_steps' tensors list of shape [batch_size, rnn_units * 2]
			fw_cell = build_cell(self.rnn_units, self.cell_type, self.rnn_layers)
			bw_cell = build_cell(self.rnn_units, self.cell_type, self.rnn_layers)
			output, state_fw, state_bw = rnn.static_bidirectional_rnn(
				fw_cell, bw_cell, output, dtype=tf.float32, sequence_length=self.seq_len, scope='encoder')
			
			if isinstance(state_fw, tf.contrib.rnn.LSTMStateTuple):
				encoder_state_c = tf.concat([state_fw.c, state_bw.c], axis=1, name='bidirectional_concat_c')
				encoder_state_h = tf.concat([state_fw.h, state_bw.h], axis=1, name='bidirectional_concat_h')
				state = tf.contrib.rnn.LSTMStateTuple(c=encoder_state_c, h=encoder_state_h)
			elif isinstance(state_fw, tf.Tensor):
				state = tf.concat([state_fw, state_bw], axis=1, name='bidirectional_concat')
			else:
				raise ValueError
		else:
			# 'num_steps' tensors list of shape [batch_size, rnn_units]
			cell = build_cell(self.rnn_units, self.cell_type, self.rnn_layers)
			output, state = rnn.static_rnn(cell, output, dtype=tf.float32, sequence_length=self.seq_len,
										   scope='encoder')
		
		output = tf.stack(output, axis=0)  # [num_steps, batch_size, rnn_units]
		output = tf.transpose(output, [1, 0, 2])  # [batch_size, num_steps, rnn_units]
		self.encoder_output = output
		self.encoder_state = state
		return output, state 
Example #4
Source File: bidirectional_RNN_1.py    From Deep-Learning-with-TensorFlow with MIT License 5 votes vote down vote up
def BiRNN(x, weights, biases):
    x = tf.transpose(x, [1, 0, 2])
    x = tf.reshape(x, [-1, n_input])
    x = tf.split(axis=0, num_or_size_splits=n_steps, value=x)
    lstm_fw_cell = rnn.BasicLSTMCell(n_hidden, forget_bias=1.0)
    lstm_bw_cell = rnn.BasicLSTMCell(n_hidden, forget_bias=1.0)
    try:
        outputs, _, _ = rnn.static_bidirectional_rnn(lstm_fw_cell, lstm_bw_cell, x,
                                              dtype=tf.float32)
    except Exception: # Old TensorFlow version only returns outputs not states
        outputs = rnn.static_bidirectional_rnn(lstm_fw_cell, lstm_bw_cell, x,
                                        dtype=tf.float32)
    return tf.matmul(outputs[-1], weights['out']) + biases['out'] 
Example #5
Source File: bidirectional_RNN_1.py    From Deep-Learning-with-TensorFlow with MIT License 5 votes vote down vote up
def BiRNN(x, weights, biases):
    x = tf.transpose(x, [1, 0, 2])
    x = tf.reshape(x, [-1, n_input])
    x = tf.split(axis=0, num_or_size_splits=n_steps, value=x)
    lstm_fw_cell = rnn.BasicLSTMCell(n_hidden, forget_bias=1.0)
    lstm_bw_cell = rnn.BasicLSTMCell(n_hidden, forget_bias=1.0)
    try:
        outputs, _, _ = rnn.static_bidirectional_rnn(lstm_fw_cell, lstm_bw_cell, x,
                                              dtype=tf.float32)
    except Exception: # Old TensorFlow version only returns outputs not states
        outputs = rnn.static_bidirectional_rnn(lstm_fw_cell, lstm_bw_cell, x,
                                        dtype=tf.float32)
    return tf.matmul(outputs[-1], weights['out']) + biases['out'] 
Example #6
Source File: multi_task_model.py    From pynlp with MIT License 4 votes vote down vote up
def generate_rnn_output(self):
        """
        Generate RNN state outputs with word embeddings as inputs
        """
        with tf.variable_scope("generate_seq_output"):
            if self.bidirectional_rnn:
                embedding = tf.get_variable("embedding",
                                            [self.source_vocab_size,
                                             self.word_embedding_size])
                encoder_emb_inputs = [tf.nn.embedding_lookup(embedding, encoder_input) \
                                      for encoder_input in self.encoder_inputs]
                rnn_outputs = static_bidirectional_rnn(self.cell_fw,
                                                       self.cell_bw,
                                                       encoder_emb_inputs,
                                                       sequence_length=self.sequence_length,
                                                       dtype=tf.float32)
                encoder_outputs, encoder_state_fw, encoder_state_bw = rnn_outputs
                # with state_is_tuple = True, if num_layers > 1,
                # here we simply use the state from last layer as the encoder state
                state_fw = encoder_state_fw[-1]
                state_bw = encoder_state_bw[-1]
                encoder_state = tf.concat([tf.concat(state_fw, 1),
                                           tf.concat(state_bw, 1)], 1)
                top_states = [tf.reshape(e, [-1, 1, self.cell_fw.output_size \
                                             + self.cell_bw.output_size])
                              for e in encoder_outputs]
                attention_states = tf.concat(top_states, 1)
            else:
                embedding = tf.get_variable("embedding",
                                            [self.source_vocab_size,
                                             self.word_embedding_size])
                encoder_emb_inputs = [tf.nn.embedding_lookup(embedding, encoder_input) \
                                      for encoder_input in self.encoder_inputs]
                rnn_outputs = static_rnn(self.cell_fw,
                                         encoder_emb_inputs,
                                         sequence_length=self.sequence_length,
                                         dtype=tf.float32)
                encoder_outputs, encoder_state = rnn_outputs
                # with state_is_tuple = True, if num_layers > 1,
                # here we use the state from last layer as the encoder state
                state = encoder_state[-1]
                encoder_state = tf.concat(state, 1)
                top_states = [tf.reshape(e, [-1, 1, self.cell_fw.output_size])
                              for e in encoder_outputs]
                attention_states = tf.concat(top_states, 1)
            return encoder_outputs, encoder_state, attention_states