我们从Python开源项目中,提取了以下12个代码示例,用于说明如何使用tensorflow.python.ops.rnn_cell.EmbeddingWrapper()。
def embedding_rnn_seq2seq(encoder_inputs, decoder_inputs, cell, num_encoder_symbols, num_decoder_symbols, embedding_size, output_projection=None, feed_previous=False, dtype=dtypes.float32, scope=None): with variable_scope.variable_scope(scope or "embedding_rnn_seq2seq"): # Encoder. encoder_cell = rnn_cell.EmbeddingWrapper( cell, embedding_classes=num_encoder_symbols, embedding_size=embedding_size) _, encoder_state = rnn.rnn(encoder_cell, encoder_inputs, dtype=dtype) # Decoder. if output_projection is None: cell = rnn_cell.OutputProjectionWrapper(cell, num_decoder_symbols) if isinstance(feed_previous, bool): return embedding_rnn_decoder( decoder_inputs, encoder_state, cell, num_decoder_symbols, embedding_size, output_projection=output_projection, feed_previous=feed_previous) # If feed_previous is a Tensor, we construct 2 graphs and use cond. def decoder(feed_previous_bool): reuse = None if feed_previous_bool else True with variable_scope.variable_scope(variable_scope.get_variable_scope(), reuse=reuse): outputs, state = embedding_rnn_decoder( decoder_inputs, encoder_state, cell, num_decoder_symbols, embedding_size, output_projection=output_projection, feed_previous=feed_previous_bool, update_embedding_for_previous=False) return outputs + [state] outputs_and_state = control_flow_ops.cond(feed_previous, lambda: decoder(True), lambda: decoder(False)) return outputs_and_state[:-1], outputs_and_state[-1]
def embedding_rnn_seq2seq(encoder_inputs, decoder_inputs, cell, num_encoder_symbols, num_decoder_symbols, embedding_size, output_projection=None, feed_previous=False, dtype=dtypes.float32, scope=None): """Embedding RNN sequence-to-sequence model. """ with variable_scope.variable_scope(scope or "embedding_rnn_seq2seq"): # Encoder. encoder_cell = rnn_cell.EmbeddingWrapper( cell, embedding_classes=num_encoder_symbols, embedding_size=embedding_size) _, encoder_state = rnn.rnn(encoder_cell, encoder_inputs, dtype=dtype) # Decoder. if output_projection is None: cell = rnn_cell.OutputProjectionWrapper(cell, num_decoder_symbols) if isinstance(feed_previous, bool): return embedding_rnn_decoder( decoder_inputs, encoder_state, cell, num_decoder_symbols, embedding_size, output_projection=output_projection, feed_previous=feed_previous) # If feed_previous is a Tensor, we construct 2 graphs and use cond. def decoder(feed_previous_bool): reuse = None if feed_previous_bool else True with variable_scope.variable_scope(variable_scope.get_variable_scope(), reuse=reuse): outputs, state = embedding_rnn_decoder( decoder_inputs, encoder_state, cell, num_decoder_symbols, embedding_size, output_projection=output_projection, feed_previous=feed_previous_bool, update_embedding_for_previous=False) return outputs + [state] outputs_and_state = control_flow_ops.cond(feed_previous, lambda: decoder(True), lambda: decoder(False)) return outputs_and_state[:-1], outputs_and_state[-1]
def sentence_embedding_rnn(_encoder_inputs, vocab_size, cell, embedding_size, mask=None, dtype=dtypes.float32, scope=None, reuse_scop=None): """ """ with variable_scope.variable_scope("embedding_rnn", reuse=reuse_scop): # encoder_cell = rnn_cell.EmbeddingWrapper( # cell, embedding_classes=vocab_size, # embedding_size=embedding_size) # Divde encoder_inputs by given input_mask if mask != None: encoder_inputs = [[] for _ in mask] _mask = 0 for num in range(len(_encoder_inputs)): encoder_inputs[_mask].append(_encoder_inputs[num]) if num == mask[_mask]: _mask += 1 else: encoder_inputs = [] encoder_inputs.append(_encoder_inputs) encoder_state = None encoder_states = [] for encoder_input in encoder_inputs: if encoder_state == []: _, encoder_state = rnn.dynamic_rnn(encoder_cell, encoder_input, dtype=dtype) else: _, encoder_state = rnn.dynamic_rnn(encoder_cell, encoder_input, encoder_state, dtype=dtype) encoder_states.append(encoder_state) return encoder_states # def def_feedforward_nn(input_size, l1_size, l2_size): # with tf.variable_scope("episodic"): # l1_weights = tf.get_variable("l1_weights", [input_size, l1_size]) # l1_biases = tf.get_variable("l1_biases", [l1_size]) # l2_weights = tf.get_variable("l2_weights", [l1_size, l2_size]) # l2_biases = tf.get_variable("l2_biases", [l2_size]) #def feedforward_nn(l1_input, input_size, l1_size, l2_size): # with tf.variable_scope("episodic"): # l1_weights = tf.get_variable("l1_weights", [input_size, l1_size]) # l1_biases = tf.get_variable("l1_biases", [l1_size]) # l2_weights = tf.get_variable("l2_weights", [l1_size, l2_size]) # l2_biases = tf.get_variable("l2_biases", [l2_size]) # l2_input = tf.tanh(tf.matmul(l1_input , l1_weights) + l1_biases) # gate_prediction = tf.matmul(l2_input , l2_weights) + l2_biases # return gate_prediction
def embedding_rnn_seq2seq(encoder_inputs, decoder_inputs, cell, num_encoder_symbols, num_decoder_symbols, embedding_size, output_projection=None, feed_previous=False, dtype=None, scope=None, beam_search=True, beam_size=10): with variable_scope.variable_scope(scope or "embedding_rnn_seq2seq") as scope: if dtype is not None: scope.set_dtype(dtype) else: dtype = scope.dtype # Encoder. encoder_cell = rnn_cell.EmbeddingWrapper( cell, embedding_classes=num_encoder_symbols, embedding_size=embedding_size) _, encoder_state = rnn.rnn(encoder_cell, encoder_inputs, dtype=dtype) # Decoder. if output_projection is None: cell = rnn_cell.OutputProjectionWrapper(cell, num_decoder_symbols) if isinstance(feed_previous, bool): return embedding_rnn_decoder(decoder_inputs, encoder_state, cell, num_decoder_symbols, embedding_size, output_projection=output_projection, feed_previous=feed_previous, scope=scope, beam_search=beam_search, beam_size=beam_size) # If feed_previous is a Tensor, we construct 2 graphs and use cond. def decoder(feed_previous_bool): reuse = None if feed_previous_bool else True with variable_scope.variable_scope( variable_scope.get_variable_scope(), reuse=reuse) as scope: outputs, state = embedding_rnn_decoder( decoder_inputs, encoder_state, cell, num_decoder_symbols, embedding_size, output_projection=output_projection, feed_previous=feed_previous_bool, update_embedding_for_previous=False, beam_search=beam_search, beam_size=beam_size) state_list = [state] if nest.is_sequence(state): state_list = nest.flatten(state) return outputs + state_list outputs_and_state = control_flow_ops.cond(feed_previous, lambda: decoder(True), lambda: decoder(False)) outputs_len = len(decoder_inputs) # Outputs length same as decoder inputs. state_list = outputs_and_state[outputs_len:] state = state_list[0] if nest.is_sequence(encoder_state): state = nest.pack_sequence_as(structure=encoder_state, flat_sequence=state_list) return outputs_and_state[:outputs_len], state