Loading caption-lib/lstm/configuration.py +1 −0 Original line number Diff line number Diff line Loading @@ -24,6 +24,7 @@ class default_reader(object): # default reader config verbose = False read_raw_files = False # this will only read files use only for testing create_vocab = True # current_reader = "basic_text_reader.py" class default_validator(object): validator_on = True Loading caption-lib/lstm/lstm_train.py +74 −60 Original line number Diff line number Diff line Loading @@ -5,6 +5,7 @@ from lstm.configuration import current as config import lstm.Readers as reader from tensorflow.python.ops.rnn_cell import LSTMCell, LSTMStateTuple from tensorflow.python.framework import constant_op # def train(config) # instanziiert das model mit hife der config Loading @@ -14,6 +15,61 @@ from tensorflow.python.ops.rnn_cell import LSTMCell, LSTMStateTuple # speichert das trainierte model mit saver # unter einem bestimmtem path # manually spcifying loop function over time - to get initial cell state and input to RNN # normally we would just use dynamic_rnn, but lets get detailed here with raw_rnn # we define and return these values, no operations occure here def loop_fn_initial(_decoder_lengths, _eos_step_embedded, _encoder_final_state): initial_elements_finished = (0 >= _decoder_lengths) # end of sentence initial_input = _eos_step_embedded # last time steps cell state initial_cell_state = _encoder_final_state # none initial_cell_output = None # none initial_loop_state = None return(initial_elements_finished, initial_input, initial_cell_state, initial_cell_output, initial_loop_state) # attention mechanism --choose which previously generated token to pass as input in the next time step def loop_fn_transition(time, previous_output, previous_state, previous_loop_state, _embeddings, _W, _b, _decoder_lengths, _pad_step_embedded): def get_next_input(): output_logits = tf.add(tf.matmul(previous_output, _W), _b) # this next line is attention prediction = tf.argmax(output_logits, axis=1) next_inpuut = tf.nn.embedding_lookup(_embeddings, prediction) return next_inpuut elements_finished = (time >= _decoder_lengths) finished = tf.reduce_all(elements_finished) input = tf.cond(finished, lambda: _pad_step_embedded, get_next_input) state = previous_state output = previous_output loop_state = None return (elements_finished, input, state, output, loop_state) def loop_fn(time, previous_output, previous_state, previous_loop_state, _W, _b, _decoder_lengths, _pad_step_embedded, _eos_step_embedded, _encoder_final_state): if previous_state is None: assert previous_output is None and previous_state is None return loop_fn_initial(_decoder_lengths, _eos_step_embedded, _encoder_final_state) else: return loop_fn_transition(time, previous_output, previous_state, previous_loop_state, _W, _b, _decoder_lengths, _pad_step_embedded) def train(): # TODO split into sub functions # this is a tutorial of Siraj Raval! Loading Loading @@ -52,8 +108,8 @@ def train(): # bidirectional step1 # encoder_outputs = tf.concat((encoder_fw_outputs, encoder_bw_outputs, 2)) encoder_final_state_c = tf.concat((encoder_fw_final_state.c, encoder_bw_final_state.c, 1)) encoder_final_state_h = tf.concat((encoder_fw_final_state.h, encoder_bw_final_state.h, 1)) encoder_final_state_c = tf.concat((encoder_fw_final_state.c, encoder_bw_final_state.c), 1) encoder_final_state_h = tf.concat((encoder_fw_final_state.h, encoder_bw_final_state.h), 1) # TF Tuple by LSTM Cells for state size, zerostate and output state encoder_final_state = LSTMStateTuple( Loading @@ -69,21 +125,33 @@ def train(): # output projects # weights and biases # SOFT ATTENTION W = tf.Variable(tf.random_uniform([config.trainer.decoder_hidden_units], config.trainer.vocab_size), -1, 1, dtype = tf.float32) b = tf.Variable(tf.zeroes([config.trainer.vocab_size]), dtype = tf.float32) W = tf.Variable(tf.random_uniform([config.trainer.decoder_hidden_units, config.trainer.vocab_size], -1, 1), dtype=tf.float32) b = tf.Variable(tf.zeros([config.trainer.vocab_size]), dtype=tf.float32) # create padded inputs for the decoder from the word embeddings # we are telling the program to test a condition, and trigger an error if the condition is false assert config.trainer.end_of_sentence == 1 and config.trainer.padding == 0 eos_time_slice = tf.ones([config.traer.batch_size], dtype = tf.int32, name = 'EOS') eos_time_slice = tf.ones([config.trainer.batch_size], dtype = tf.int32, name = 'EOS') pad_time_slice = tf.ones([config.trainer.batch_size], dtype = tf.int32, name = 'PAD') # retrieves rows of the params tensor. The behaviour is similar to using indexing with arrays in numpy eos_step_embedded = tf.nn.embedding_lookup(embeddings, eos_time_slice) pad_step_embedded = tf.nn.embedding_lookup(embeddings, pad_time_slice) time = constant_op.constant(0, dtype=tf.int32) callable_loop_fn = loop_fn( time=time, previous_output=None, previous_state=None, previous_loop_state=None, _W=W, _b=b, _decoder_lengths=decoder_lengths, _pad_step_embedded=pad_step_embedded, _eos_step_embedded=eos_step_embedded, _encoder_final_state=encoder_final_state) # using the functions for the attention decoder decoder_outputs_ta, decoder_final_state, _ = tf.nn.raw_rnn(decoder_cell, loop_fn) decoder_outputs_ta, decoder_final_state, decoder_loop_state = tf.nn.raw_rnn(decoder_cell, callable_loop_fn) decoder_outputs = decoder_outputs_ta.stack() # unpacks the given dimension of a rank-R tensr into a R-1 tensor Loading Loading @@ -138,60 +206,6 @@ def train(): print('\n.\n.\n.\n...training interupted') # manually spcifying loop function over time - to get initial cell state and input to RNN # normally we would just use dynamic_rnn, but lets get detailed here with raw_rnn # we define and return these values, no operations occure here def loop_fn_initial(_decoder_lengths, _eos_step_embedded, _encoder_final_state): initial_elements_finished = (0 >= _decoder_lengths) # end of sentence initial_input = _eos_step_embedded # last time steps cell state initial_cell_state = _encoder_final_state # none initial_cell_output = None # none initial_loop_state = None return(initial_elements_finished, initial_input, initial_cell_state, initial_cell_output, initial_loop_state) # attention mechanism --choose which previously generated token to pass as input in the next time step def loop_fn_transition(time, previous_output, previous_state, previous_loop_state, _embeddings, _w, _b, _decoder_lengths, _pad_step_embedded): def get_next_input(): output_logits = tf.add(tf.matmul(previous_output, _w), _b) # this next line is attention prediction = tf.argmax(output_logits, axis=1) next_inpuut = tf.nn.embedding_lookup(_embeddings, prediction) return next_inpuut elements_finished = (time >= _decoder_lengths) finished = tf.reduce_all(elements_finished) input = tf.cond(finished, lambda: _pad_step_embedded, get_next_input) state = previous_state output = previous_output loop_state = None return (elements_finished, input, state, output, loop_state) def loop_fn(time, previous_output, previous_state, previous_loop_state, _w, _b, _decoder_lengths, _pad_step_embedded): if previous_state is None: assert previous_output is None and previous_state is None return loop_fn_initial() else: return loop_fn_transition(time, previous_output, previous_state, previous_loop_state, _w, _b, _decoder_lengths, _pad_step_embedded) """ # Embedding embedding_encoder = variable_scope.get_variable( Loading Loading
caption-lib/lstm/configuration.py +1 −0 Original line number Diff line number Diff line Loading @@ -24,6 +24,7 @@ class default_reader(object): # default reader config verbose = False read_raw_files = False # this will only read files use only for testing create_vocab = True # current_reader = "basic_text_reader.py" class default_validator(object): validator_on = True Loading
caption-lib/lstm/lstm_train.py +74 −60 Original line number Diff line number Diff line Loading @@ -5,6 +5,7 @@ from lstm.configuration import current as config import lstm.Readers as reader from tensorflow.python.ops.rnn_cell import LSTMCell, LSTMStateTuple from tensorflow.python.framework import constant_op # def train(config) # instanziiert das model mit hife der config Loading @@ -14,6 +15,61 @@ from tensorflow.python.ops.rnn_cell import LSTMCell, LSTMStateTuple # speichert das trainierte model mit saver # unter einem bestimmtem path # manually spcifying loop function over time - to get initial cell state and input to RNN # normally we would just use dynamic_rnn, but lets get detailed here with raw_rnn # we define and return these values, no operations occure here def loop_fn_initial(_decoder_lengths, _eos_step_embedded, _encoder_final_state): initial_elements_finished = (0 >= _decoder_lengths) # end of sentence initial_input = _eos_step_embedded # last time steps cell state initial_cell_state = _encoder_final_state # none initial_cell_output = None # none initial_loop_state = None return(initial_elements_finished, initial_input, initial_cell_state, initial_cell_output, initial_loop_state) # attention mechanism --choose which previously generated token to pass as input in the next time step def loop_fn_transition(time, previous_output, previous_state, previous_loop_state, _embeddings, _W, _b, _decoder_lengths, _pad_step_embedded): def get_next_input(): output_logits = tf.add(tf.matmul(previous_output, _W), _b) # this next line is attention prediction = tf.argmax(output_logits, axis=1) next_inpuut = tf.nn.embedding_lookup(_embeddings, prediction) return next_inpuut elements_finished = (time >= _decoder_lengths) finished = tf.reduce_all(elements_finished) input = tf.cond(finished, lambda: _pad_step_embedded, get_next_input) state = previous_state output = previous_output loop_state = None return (elements_finished, input, state, output, loop_state) def loop_fn(time, previous_output, previous_state, previous_loop_state, _W, _b, _decoder_lengths, _pad_step_embedded, _eos_step_embedded, _encoder_final_state): if previous_state is None: assert previous_output is None and previous_state is None return loop_fn_initial(_decoder_lengths, _eos_step_embedded, _encoder_final_state) else: return loop_fn_transition(time, previous_output, previous_state, previous_loop_state, _W, _b, _decoder_lengths, _pad_step_embedded) def train(): # TODO split into sub functions # this is a tutorial of Siraj Raval! Loading Loading @@ -52,8 +108,8 @@ def train(): # bidirectional step1 # encoder_outputs = tf.concat((encoder_fw_outputs, encoder_bw_outputs, 2)) encoder_final_state_c = tf.concat((encoder_fw_final_state.c, encoder_bw_final_state.c, 1)) encoder_final_state_h = tf.concat((encoder_fw_final_state.h, encoder_bw_final_state.h, 1)) encoder_final_state_c = tf.concat((encoder_fw_final_state.c, encoder_bw_final_state.c), 1) encoder_final_state_h = tf.concat((encoder_fw_final_state.h, encoder_bw_final_state.h), 1) # TF Tuple by LSTM Cells for state size, zerostate and output state encoder_final_state = LSTMStateTuple( Loading @@ -69,21 +125,33 @@ def train(): # output projects # weights and biases # SOFT ATTENTION W = tf.Variable(tf.random_uniform([config.trainer.decoder_hidden_units], config.trainer.vocab_size), -1, 1, dtype = tf.float32) b = tf.Variable(tf.zeroes([config.trainer.vocab_size]), dtype = tf.float32) W = tf.Variable(tf.random_uniform([config.trainer.decoder_hidden_units, config.trainer.vocab_size], -1, 1), dtype=tf.float32) b = tf.Variable(tf.zeros([config.trainer.vocab_size]), dtype=tf.float32) # create padded inputs for the decoder from the word embeddings # we are telling the program to test a condition, and trigger an error if the condition is false assert config.trainer.end_of_sentence == 1 and config.trainer.padding == 0 eos_time_slice = tf.ones([config.traer.batch_size], dtype = tf.int32, name = 'EOS') eos_time_slice = tf.ones([config.trainer.batch_size], dtype = tf.int32, name = 'EOS') pad_time_slice = tf.ones([config.trainer.batch_size], dtype = tf.int32, name = 'PAD') # retrieves rows of the params tensor. The behaviour is similar to using indexing with arrays in numpy eos_step_embedded = tf.nn.embedding_lookup(embeddings, eos_time_slice) pad_step_embedded = tf.nn.embedding_lookup(embeddings, pad_time_slice) time = constant_op.constant(0, dtype=tf.int32) callable_loop_fn = loop_fn( time=time, previous_output=None, previous_state=None, previous_loop_state=None, _W=W, _b=b, _decoder_lengths=decoder_lengths, _pad_step_embedded=pad_step_embedded, _eos_step_embedded=eos_step_embedded, _encoder_final_state=encoder_final_state) # using the functions for the attention decoder decoder_outputs_ta, decoder_final_state, _ = tf.nn.raw_rnn(decoder_cell, loop_fn) decoder_outputs_ta, decoder_final_state, decoder_loop_state = tf.nn.raw_rnn(decoder_cell, callable_loop_fn) decoder_outputs = decoder_outputs_ta.stack() # unpacks the given dimension of a rank-R tensr into a R-1 tensor Loading Loading @@ -138,60 +206,6 @@ def train(): print('\n.\n.\n.\n...training interupted') # manually spcifying loop function over time - to get initial cell state and input to RNN # normally we would just use dynamic_rnn, but lets get detailed here with raw_rnn # we define and return these values, no operations occure here def loop_fn_initial(_decoder_lengths, _eos_step_embedded, _encoder_final_state): initial_elements_finished = (0 >= _decoder_lengths) # end of sentence initial_input = _eos_step_embedded # last time steps cell state initial_cell_state = _encoder_final_state # none initial_cell_output = None # none initial_loop_state = None return(initial_elements_finished, initial_input, initial_cell_state, initial_cell_output, initial_loop_state) # attention mechanism --choose which previously generated token to pass as input in the next time step def loop_fn_transition(time, previous_output, previous_state, previous_loop_state, _embeddings, _w, _b, _decoder_lengths, _pad_step_embedded): def get_next_input(): output_logits = tf.add(tf.matmul(previous_output, _w), _b) # this next line is attention prediction = tf.argmax(output_logits, axis=1) next_inpuut = tf.nn.embedding_lookup(_embeddings, prediction) return next_inpuut elements_finished = (time >= _decoder_lengths) finished = tf.reduce_all(elements_finished) input = tf.cond(finished, lambda: _pad_step_embedded, get_next_input) state = previous_state output = previous_output loop_state = None return (elements_finished, input, state, output, loop_state) def loop_fn(time, previous_output, previous_state, previous_loop_state, _w, _b, _decoder_lengths, _pad_step_embedded): if previous_state is None: assert previous_output is None and previous_state is None return loop_fn_initial() else: return loop_fn_transition(time, previous_output, previous_state, previous_loop_state, _w, _b, _decoder_lengths, _pad_step_embedded) """ # Embedding embedding_encoder = variable_scope.get_variable( Loading