Loading caption-lib/.vscode/settings.json +1 −0 Original line number Diff line number Diff line Loading @@ -11,4 +11,5 @@ "python.linting.flake8Args": [ "--ignore=E302,E303,E304,E305,E501,E251,E128", ], "files.trimTrailingWhitespace": true } No newline at end of file caption-lib/lstm/configuration.py +6 −10 Original line number Diff line number Diff line Loading @@ -29,7 +29,6 @@ class default_validator(object): validator_on = True input_data_type = ".json" class default_trainer(object): train_on = True padding = 0 Loading @@ -39,8 +38,8 @@ class default_trainer(object): encoder_hidden_units = 20 decoder_hidden_units = encoder_hidden_units * 2 max_batches = 300 batches_in_epoch = 100 batches_in_epoch = 10 batch_size = 10 ''' Loading @@ -51,15 +50,12 @@ NOTE: later on there will be other settings like small/medium/high that can over ''' class current(default_reader, default_validator, default_trainer, default_paths): # Custom presets at bottom path = default_paths reader = default_reader trainer = default_trainer validator = default_validator # e.g. reader.input_train = "/train2" # e.g. validator.read_test = "/test1234" reader.reader_on = False reader.verbose = False # e.g. reader.input_train = "/train2" validator = default_validator validator.validator_on = False # e.g. validator.read_test = "/test1234" trainer = default_trainer trainer.train_on = True caption-lib/lstm/lstm_train.py +23 −37 Original line number Diff line number Diff line Loading @@ -62,22 +62,22 @@ def train(): ) # decoder decoder_cell = LSTMCell(decoder_hidden_units) decoder_cell = LSTMCell(config.trainer.decoder_hidden_units) encoder_max_time, config.trainer.batch_size = tf.unstack(tf.shape(encoder_inputs)) decoder_lengths = encoder_inputs_length + 3 # output projects # weights and biases # SOFT ATTENTION W = tf.Variable(tf.random_uniform([config.trainer.decoder_hidden_units], vocab_size), -1, 1, 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.zeroes([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([batch_size], dtype = tf.int32, name = 'EOS') pad_time_slice = tf.ones([batch_size], dtype = tf.int32, name = 'PAD') eos_time_slice = tf.ones([config.traer.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) Loading Loading @@ -115,14 +115,15 @@ def train(): sess.run(tf.global_variables_initializer()) loss_track = [] # training the real deal executes here: try: for batch in range(config.trainer.max_batches): fd = # TODO get a sequence to learn from seq2seq model next_feed() _, l = sess.run((([train_op], loss), fd) loss_track.append(l) fd = ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '<EOS>'] # TODO get a sequence to learn from seq2seq model next_feed() _, loss_current = sess.run(([train_op], loss), fd) loss_track.append(loss_current) if(batch == 0 or batch % config.trainer.batches_in_epoch == 0) if(batch == 0 or batch % config.trainer.batches_in_epoch == 0): print('batch {}' .format(batch)) print(' minibatch loss: {}' .format(sess.run(loss, fd))) predict_ = sess.run(decoder_prediction, fd) Loading @@ -140,26 +141,13 @@ def train(): # 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 def next_feed(): batch = next(batches) encoder_inputs_, encoder_input_lengths_ = helpers.batch(batch) decoder_targets_, _ = helpers.batch( [(sequence) + [EOS] + [PAD] * 2 for sequence in batch] ) return { encoder_inputs: encoder_inputs_, encoder_inputs_length: encoder_input_lengths_, decoder_targets: decoder_targets_, } # we define and return these values, no operations occure here def loop_fn_initial(): initial_elements_finished = (0 >= decoder_lengths) 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 initial_input = _eos_step_embedded # last time steps cell state initial_cell_state = encoder_final_state initial_cell_state = _encoder_final_state # none initial_cell_output = None # none Loading @@ -168,23 +156,22 @@ def loop_fn_initial(): initial_input, initial_cell_state, initial_cell_output, initial_loop_state ) 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): 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) 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) prediction = tf.argmax(output_logits, axis=1) next_inpuut = tf.nn.embedding_lookup(_embeddings, prediction) return next_inpuut elements_finished = (time >= decoder_lengths) elements_finished = (time >= _decoder_lengths) finished = tf.reduce_all(elements_finished) input = tf.cond(finished, lambda: pad_step_embedded, get_next_input) input = tf.cond(finished, lambda: _pad_step_embedded, get_next_input) state = previous_state output = previous_output Loading @@ -194,15 +181,14 @@ def loop_fn_transition(time, previous_output, previous_state, previous_loop_stat input, state, output, loop_state ) loop_state) def loop_fn(time, previous_output, previous_state, Previous_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) return loop_fn_transition(time, previous_output, previous_state, previous_loop_state, _w, _b, _decoder_lengths, _pad_step_embedded) Loading Loading
caption-lib/.vscode/settings.json +1 −0 Original line number Diff line number Diff line Loading @@ -11,4 +11,5 @@ "python.linting.flake8Args": [ "--ignore=E302,E303,E304,E305,E501,E251,E128", ], "files.trimTrailingWhitespace": true } No newline at end of file
caption-lib/lstm/configuration.py +6 −10 Original line number Diff line number Diff line Loading @@ -29,7 +29,6 @@ class default_validator(object): validator_on = True input_data_type = ".json" class default_trainer(object): train_on = True padding = 0 Loading @@ -39,8 +38,8 @@ class default_trainer(object): encoder_hidden_units = 20 decoder_hidden_units = encoder_hidden_units * 2 max_batches = 300 batches_in_epoch = 100 batches_in_epoch = 10 batch_size = 10 ''' Loading @@ -51,15 +50,12 @@ NOTE: later on there will be other settings like small/medium/high that can over ''' class current(default_reader, default_validator, default_trainer, default_paths): # Custom presets at bottom path = default_paths reader = default_reader trainer = default_trainer validator = default_validator # e.g. reader.input_train = "/train2" # e.g. validator.read_test = "/test1234" reader.reader_on = False reader.verbose = False # e.g. reader.input_train = "/train2" validator = default_validator validator.validator_on = False # e.g. validator.read_test = "/test1234" trainer = default_trainer trainer.train_on = True
caption-lib/lstm/lstm_train.py +23 −37 Original line number Diff line number Diff line Loading @@ -62,22 +62,22 @@ def train(): ) # decoder decoder_cell = LSTMCell(decoder_hidden_units) decoder_cell = LSTMCell(config.trainer.decoder_hidden_units) encoder_max_time, config.trainer.batch_size = tf.unstack(tf.shape(encoder_inputs)) decoder_lengths = encoder_inputs_length + 3 # output projects # weights and biases # SOFT ATTENTION W = tf.Variable(tf.random_uniform([config.trainer.decoder_hidden_units], vocab_size), -1, 1, 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.zeroes([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([batch_size], dtype = tf.int32, name = 'EOS') pad_time_slice = tf.ones([batch_size], dtype = tf.int32, name = 'PAD') eos_time_slice = tf.ones([config.traer.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) Loading Loading @@ -115,14 +115,15 @@ def train(): sess.run(tf.global_variables_initializer()) loss_track = [] # training the real deal executes here: try: for batch in range(config.trainer.max_batches): fd = # TODO get a sequence to learn from seq2seq model next_feed() _, l = sess.run((([train_op], loss), fd) loss_track.append(l) fd = ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '<EOS>'] # TODO get a sequence to learn from seq2seq model next_feed() _, loss_current = sess.run(([train_op], loss), fd) loss_track.append(loss_current) if(batch == 0 or batch % config.trainer.batches_in_epoch == 0) if(batch == 0 or batch % config.trainer.batches_in_epoch == 0): print('batch {}' .format(batch)) print(' minibatch loss: {}' .format(sess.run(loss, fd))) predict_ = sess.run(decoder_prediction, fd) Loading @@ -140,26 +141,13 @@ def train(): # 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 def next_feed(): batch = next(batches) encoder_inputs_, encoder_input_lengths_ = helpers.batch(batch) decoder_targets_, _ = helpers.batch( [(sequence) + [EOS] + [PAD] * 2 for sequence in batch] ) return { encoder_inputs: encoder_inputs_, encoder_inputs_length: encoder_input_lengths_, decoder_targets: decoder_targets_, } # we define and return these values, no operations occure here def loop_fn_initial(): initial_elements_finished = (0 >= decoder_lengths) 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 initial_input = _eos_step_embedded # last time steps cell state initial_cell_state = encoder_final_state initial_cell_state = _encoder_final_state # none initial_cell_output = None # none Loading @@ -168,23 +156,22 @@ def loop_fn_initial(): initial_input, initial_cell_state, initial_cell_output, initial_loop_state ) 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): 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) 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) prediction = tf.argmax(output_logits, axis=1) next_inpuut = tf.nn.embedding_lookup(_embeddings, prediction) return next_inpuut elements_finished = (time >= decoder_lengths) elements_finished = (time >= _decoder_lengths) finished = tf.reduce_all(elements_finished) input = tf.cond(finished, lambda: pad_step_embedded, get_next_input) input = tf.cond(finished, lambda: _pad_step_embedded, get_next_input) state = previous_state output = previous_output Loading @@ -194,15 +181,14 @@ def loop_fn_transition(time, previous_output, previous_state, previous_loop_stat input, state, output, loop_state ) loop_state) def loop_fn(time, previous_output, previous_state, Previous_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) return loop_fn_transition(time, previous_output, previous_state, previous_loop_state, _w, _b, _decoder_lengths, _pad_step_embedded) Loading