def main(): parser = argparse.ArgumentParser() parser.add_argument('--model_path', type=str, default=None, help='Pre-Trained Model Path') parser.add_argument('--data_dir', type=str, default='Data', help='Data Directory') parser.add_argument('--seed', type=str, default="ANTONIO", help='seed') parser.add_argument('--num_char', type=int, default=1000, help='seed') parser.add_argument('--output_file', type=str, default='sample.txt', help='Output File') args = parser.parse_args() # model_config = json.loads( open('model_config.json').read() ) config = model_config.config model_options = { 'n_source_quant' : config['n_source_quant'], 'n_target_quant' : config['n_target_quant'], 'residual_channels' : config['residual_channels'], 'decoder_dilations' : config['decoder_dilations'], 'sample_size' : config['sample_size'], 'decoder_filter_width' : config['decoder_filter_width'], 'batch_size' : 1, } seed_ = [ ord(s) for s in args.seed ] seed_ = np.array(seed_, dtype='int32') seed_ = seed_.reshape([1, -1]) byte_net = model.Byte_net_model( model_options ) generator = byte_net.build_generator( len(args.seed) ) sess = tf.InteractiveSession() saver = tf.train.Saver() saver.restore(sess, args.model_path) input_batch = seed_ print "INPUT", input_batch for i in range(0, args.num_char): generator = byte_net.build_generator( input_batch.shape[1], reuse = True) prediction = sess.run( [generator['prediction']], feed_dict = { generator['source_sentence'] : input_batch }) prediction = prediction[0] last_prediction = prediction[ prediction.shape[0] - 1 ] last_prediction = last_prediction.reshape([1,-1]) input_batch = np.concatenate((input_batch, last_prediction), axis = 1) res = list_to_string(input_batch[0]) print res with open(args.output_file, 'wb') as f: f.write(res)
def main(): parser = argparse.ArgumentParser() parser.add_argument('--learning_rate', type=float, default=0.001, help='Learning Rate') parser.add_argument('--batch_size', type=int, default=1, help='Learning Rate') parser.add_argument('--max_epochs', type=int, default=1000, help='Max Epochs') parser.add_argument('--beta1', type=float, default=0.5, help='Momentum for Adam Update') parser.add_argument('--resume_model', type=str, default=None, help='Pre-Trained Model Path, to resume from') parser.add_argument('--data_dir', type=str, default='Data', help='Data Directory') args = parser.parse_args() # model_config = json.loads( open('model_config.json').read() ) config = model_config.predictor_config model_options = { 'n_source_quant': config['n_source_quant'], 'n_target_quant': config['n_target_quant'], 'residual_channels': config['residual_channels'], 'decoder_dilations': config['decoder_dilations'], 'sample_size': config['sample_size'], 'decoder_filter_width': config['decoder_filter_width'], 'batch_size': args.batch_size, } byte_net = model.Byte_net_model(model_options) bn_tensors = byte_net.build_prediction_model() optim = tf.train.AdamOptimizer(args.learning_rate, beta1=args.beta1).minimize( bn_tensors['loss'], var_list=bn_tensors['variables']) sess = tf.InteractiveSession() tf.initialize_all_variables().run() saver = tf.train.Saver() if args.resume_model: saver.restore(sess, args.resume_model) dl = data_loader.Data_Loader({ 'model_type': 'generator', 'dir_name': args.data_dir }) text_samples = dl.load_generator_data(config['sample_size']) print text_samples.shape for i in range(args.max_epochs): batch_no = 0 batch_size = args.batch_size while (batch_no + 1) * batch_size < text_samples.shape[0]: text_batch = text_samples[batch_no * batch_size:(batch_no + 1) * batch_size, :] _, loss, prediction = sess.run( [optim, bn_tensors['loss'], bn_tensors['prediction']], feed_dict={bn_tensors['sentence']: text_batch}) print "-------------------------------------------------------" print utils.list_to_string(prediction) print "Loss", i, batch_no, loss print "********************************************************" # print prediction batch_no += 1 if (batch_no % 500) == 0: save_path = saver.save( sess, "Data/Models/model_epoch_{}.ckpt".format(i))
def main(): parser = argparse.ArgumentParser() parser.add_argument('--model_path', type=str, default="Data/Models/model_epoch_6.ckpt", help='Pre-Trained Model Path') parser.add_argument('--data_dir', type=str, default='Data', help='Data Directory') parser.add_argument('--seed', type=str, default="ANTONIO", help='seed') parser.add_argument('--num_char', type=int, default=1000, help='seed') parser.add_argument('--output_file', type=str, default='sample.txt', help='Output File') args = parser.parse_args() config = model_config.config model_options = { 'n_source_quant': config['n_source_quant'], 'n_target_quant': config['n_target_quant'], 'residual_channels': config['residual_channels'], 'decoder_dilations': config['decoder_dilations'], 'sample_size': args.num_char, 'decoder_filter_width': config['decoder_filter_width'], 'batch_size': 1, } seed_ = [ord(s) for s in args.seed ] + [0 for i in range(args.num_char - len(args.seed))] seed_ = np.array(seed_, dtype='int32') seed_ = seed_.reshape([1, -1]) byte_net = model.Byte_net_model(model_options) generator = byte_net.build_generator(args.num_char) sess = tf.InteractiveSession() saver = tf.train.Saver() saver.restore(sess, args.model_path) input_batch = seed_ print "INPUT", input_batch for i in range(0, args.num_char - len(args.seed)): prediction, probs = sess.run( [generator['prediction'], generator['probs']], feed_dict={generator['source_sentence']: input_batch}) last_prediction = np.array( [utils.weighted_pick(probs[i + len(args.seed) - 1])]) last_prediction = last_prediction.reshape([1, -1]) input_batch[:, i + len(args.seed)] = last_prediction res = utils.list_to_string(input_batch[0, 0:i + len(args.seed) + 1]) if i % 100 == 0: print res with open(args.output_file, 'wb') as f: f.write(res)
def main(): parser = argparse.ArgumentParser() parser.add_argument('--model_path', type=str, default='Data/Models/model_translation_epoch_1.ckpt', help='Pre-Trained Model Path') parser.add_argument('--data_dir', type=str, default='Data', help='Data Directory') parser.add_argument('--num_char', type=int, default=1000, help='seed') parser.add_argument('--output_file', type=str, default='sample.txt', help='Output File') args = parser.parse_args() config = model_config.translator_config source_sentence = None with open('Data/MachineTranslation/news-commentary-v11.de-en.de') as f: source_sentences = f.read().decode("utf-8").split('\n') with open('Data/MachineTranslation/news-commentary-v11.de-en.en') as f: target_sentences = f.read().decode("utf-8").split('\n') source_sentence = source_sentences[4] target_sentence = target_sentences[4] print source_sentence print target_sentence data_loader_options = { 'model_type' : 'translation', 'source_file' : 'Data/MachineTranslation/news-commentary-v11.de-en.de', 'target_file' : 'Data/MachineTranslation/news-commentary-v11.de-en.en' 'bucket_quant' : 25, } dl = data_loader_v2.Data_Loader(data_loader_options) # buckets, source_vocab, target_vocab, frequent_keys = dl.load_translation_data() with open('source.txt', 'wb') as f: f.write(source_sentence.encode('utf8')) source = [ dl.source_vocab(s) for s in source_sentence ] source += [ dl.source_vocab['eol'] ] new_length = len(source) bucket_quant = data_loader.BUCKET_QUANT if new_length % bucket_quant > 0: new_length = ((new_length/bucket_quant) + 1 ) * bucket_quant for i in range(len(source), new_length): source += [ dl.source_vocab['padding'] ] target = [ dl.target_vocab['target_init'] ] for j in range(1, new_length): target += [ dl.target_vocab['padding'] ] print "SL", len(source) print "TL", len(target) source = np.array(source, dtype='int32') source = source.reshape([1, -1]) target = np.array(target, dtype='int32') target = target.reshape([1, -1]) model_options = { 'n_source_quant' : len(dl.source_vocab), 'n_target_quant' : len(dl.target_vocab), 'residual_channels' : config['residual_channels'], 'decoder_dilations' : config['decoder_dilations'], 'encoder_dilations' : config['encoder_dilations'], 'sample_size' : 10, 'decoder_filter_width' : config['decoder_filter_width'], 'encoder_filter_width' : config['encoder_filter_width'], 'batch_size' : args.batch_size, 'source_mask_chars' : [ dl.source_vocab['padding'] ], 'target_mask_chars' : [ dl.target_vocab['padding'] ] } byte_net = model.Byte_net_model( model_options ) translator = byte_net.build_translator( new_length ) sess = tf.InteractiveSession() saver = tf.train.Saver() saver.restore(sess, args.model_path) input_batch = target print "INPUT", input_batch print "Source", source for i in range(0, 1000): prediction, encoder_output = sess.run( [translator['prediction'], translator['encoder_output']], feed_dict = { translator['source_sentence'] : source, translator['target_sentence'] : input_batch, }) # prediction = prediction[0] print "encoder" print encoder_output last_prediction = prediction[i] last_prediction = np.array( [ last_prediction ]) last_prediction = last_prediction.reshape([1,-1]) input_batch[:,i+1] = last_prediction[:,0] res = dl.inidices_to_string(input_batch[0], dl.target_vocab) print "RES" print res with open('sample.txt', 'wb') as f: f.write(res)
def main(): parser = argparse.ArgumentParser() parser.add_argument('--learning_rate', type=float, default=0.001, help='Learning Rate') parser.add_argument('--batch_size', type=int, default=1, help='Learning Rate') parser.add_argument('--max_epochs', type=int, default=1000, help='Max Epochs') parser.add_argument('--beta1', type=float, default=0.5, help='Momentum for Adam Update') parser.add_argument('--resume_model', type=str, default=None, help='Pre-Trained Model Path, to resume from') parser.add_argument('--data_dir', type=str, default='Data', help='Data Directory') parser.add_argument('--log_dir', type=str, default='logs', help='Path to TensorBoard logs') args = parser.parse_args() # model_config = json.loads( open('model_config.json').read() ) config = model_config.predictor_config model_options = { 'n_source_quant': config['n_source_quant'], 'n_target_quant': config['n_target_quant'], 'residual_channels': config['residual_channels'], 'decoder_dilations': config['decoder_dilations'], 'sample_size': config['sample_size'], 'decoder_filter_width': config['decoder_filter_width'], 'batch_size': args.batch_size, } byte_net = model.Byte_net_model(model_options) bn_tensors = byte_net.build_prediction_model() # Set up logging for TensorBoard writer = tf.summary.FileWriter(args.log_dir, graph=tf.get_default_graph()) run_metadata = tf.RunMetadata() summaries = tf.summary.merge_all() optim = tf.train.AdamOptimizer(args.learning_rate, beta1 = args.beta1) \ .minimize(bn_tensors['loss'], var_list=bn_tensors['variables']) sess = tf.InteractiveSession() tf.global_variables_initializer().run() saver = tf.train.Saver() if args.resume_model: saver.restore(sess, args.resume_model) dl = data_loader.Data_Loader({ 'model_type': 'generator', 'dir_name': args.data_dir }) text_samples = dl.load_generator_data(config['sample_size']) print(text_samples.shape) models_path = "Data/Models/" if not os.path.exists(models_path): os.makedirs(models_path) for epoch in range(args.max_epochs): step = 0 batch_size = args.batch_size while (step + 1) * batch_size < text_samples.shape[0]: text_batch = text_samples[step * batch_size:(step + 1) * batch_size, :] _, summary, loss, prediction = sess.run([ optim, summaries, bn_tensors['loss'], bn_tensors['prediction'] ], feed_dict={ bn_tensors['sentence']: text_batch }) print("-------------------------------------------------------") print(utils.list_to_string(prediction)) print("Epoch", epoch, " Step", step, " Loss", loss) print("********************************************************") writer.add_summary(summary, step) writer.add_run_metadata( run_metadata, 'epoch_{:04d}, step_{:04d}'.format(epoch, step)) step += 1 if step % 500 == 0: saver.save(sess, models_path + "model_epoch_{}.ckpt".format(epoch))
def main(): args = get_args() dl = get_data_loader(args) buckets, source_vocab, target_vocab, frequent_keys = dl.load_translation_data( ) config = model_config.translator_config model_options = get_model_options(args, config, source_vocab, target_vocab) last_saved_model_path = None if args.resume_model: last_saved_model_path = args.resume_model print("Number Of Buckets", len(buckets)) for i in range(1, args.max_epochs): cnt = 0 for _, key in frequent_keys: cnt += 1 print("KEY", cnt, key) if key > 400: continue if len(buckets[key]) < args.batch_size: print("BUCKET TOO SMALL", key) continue sess = tf.InteractiveSession() batch_no = 0 batch_size = args.batch_size byte_net = model.Byte_net_model(model_options) bn_tensors = byte_net.build_translation_model(sample_size=key) adam = tf.train.AdamOptimizer(args.learning_rate, beta1=args.beta1) optim = adam.minimize(bn_tensors['loss'], var_list=bn_tensors['variables']) train_writer = tf.train.SummaryWriter('logs/', sess.graph) tf.initialize_all_variables().run() saver = tf.train.Saver() if last_saved_model_path: saver.restore(sess, last_saved_model_path) while (batch_no + 1) * batch_size < len(buckets[key]): source, target = dl.get_batch_from_pairs( buckets[key][batch_no * batch_size:(batch_no + 1) * batch_size]) _, loss, prediction, summary, source_gradient, target_gradient = sess.run( [ optim, bn_tensors['loss'], bn_tensors['prediction'], bn_tensors['merged_summary'], bn_tensors['source_gradient'], bn_tensors['target_gradient'] ], feed_dict={ bn_tensors['source_sentence']: source, bn_tensors['target_sentence']: target, }) train_writer.add_summary(summary, batch_no * (cnt + 1)) print("Loss", loss, batch_no, len(buckets[key]) / batch_size, i, cnt, key) print("******") print("Source ", dl.inidices_to_string(source[0], source_vocab)) print("---------") print("Target ", dl.inidices_to_string(target[0], target_vocab)) print("----------") print("Prediction ", dl.inidices_to_string(prediction[0:key], target_vocab)) print("******") batch_no += 1 if batch_no % 1000 == 0: save_path = saver.save( sess, "Data/Models/model_translation_epoch_{}_{}.ckpt". format(i, cnt)) last_saved_model_path = "Data/Models/model_translation_epoch_{}_{}.ckpt".format( i, cnt) save_path = saver.save( sess, "Data/Models/model_translation_epoch_{}.ckpt".format(i)) last_saved_model_path = "Data/Models/model_translation_epoch_{}.ckpt".format( i) tf.reset_default_graph() sess.close()
def main(): args, config = get_args_and_config() source_sentence = None with open('Data/MachineTranslation/news-commentary-v11.de-en.de') as f: source_sentences = f.read().decode("utf-8").split('\n') with open('Data/MachineTranslation/news-commentary-v11.de-en.en') as f: target_sentences = f.read().decode("utf-8").split('\n') idx = 0 for i in range(len(source_sentences)): if 'NEW YORK' in target_sentences[i][0:40]: print(target_sentences[i]) idx = i break source_sentences = source_sentences[idx:idx + 1] target_sentences = target_sentences[idx:idx + 1] print(source_sentences) print(target_sentences) data_loader_options = { 'model_type': 'translation', 'source_file': 'Data/MachineTranslation/news-commentary-v11.de-en.de', 'target_file': 'Data/MachineTranslation/news-commentary-v11.de-en.en', 'bucket_quant': 25, } dl = data_loader.Data_Loader(data_loader_options) # buckets, source_vocab, target_vocab, frequent_keys = dl.load_translation_data() source, target = prepare_source_target_arrays(args, dl, source_sentences) model_options = { 'n_source_quant': len(dl.source_vocab), 'n_target_quant': len(dl.target_vocab), 'residual_channels': config['residual_channels'], 'decoder_dilations': config['decoder_dilations'], 'encoder_dilations': config['encoder_dilations'], 'sample_size': 10, 'decoder_filter_width': config['decoder_filter_width'], 'encoder_filter_width': config['encoder_filter_width'], 'batch_size': 1, 'source_mask_chars': [dl.source_vocab['padding']], 'target_mask_chars': [dl.target_vocab['padding']] } byte_net = model.Byte_net_model(model_options) translator = byte_net.build_translation_model(args.translator_max_length) sess = tf.InteractiveSession() saver = tf.train.Saver() saver.restore(sess, args.model_path) input_batch = target print("INPUT", input_batch) print("Source", source) for i in range(0, 1000): prediction, probs = sess.run( [translator['prediction'], translator['probs']], feed_dict={ translator['source_sentence']: source, translator['target_sentence']: input_batch, }) # prediction = prediction[0] last_prediction = np.array([utils.weighted_pick(probs[i])]) last_prediction = last_prediction.reshape([1, -1]) input_batch[:, i + 1] = last_prediction[:, 0] res = dl.inidices_to_string(input_batch[0], dl.target_vocab) print("RES") print(res)
def main(): parser = argparse.ArgumentParser() parser.add_argument('--learning_rate', type=float, default=0.001, help='Learning Rate') parser.add_argument('--batch_size', type=int, default=32, help='Learning Rate') parser.add_argument('--bucket_quant', type=int, default=25, help='Learning Rate') parser.add_argument('--max_epochs', type=int, default=1000, help='Max Epochs') parser.add_argument('--beta1', type=float, default=0.5, help='Momentum for Adam Update') parser.add_argument('--resume_model', type=str, default=None, help='Pre-Trained Model Path, to resume from') parser.add_argument( '--source_file', type=str, default='Data/MachineTranslation/news-commentary-v11.de-en.de', help='Source File') parser.add_argument( '--target_file', type=str, default='Data/MachineTranslation/news-commentary-v11.de-en.en', help='Target File') args = parser.parse_args() data_loader_options = { 'model_type': 'translation', 'source_file': args.source_file, 'target_file': args.target_file, 'bucket_quant': args.bucket_quant, #'max_sentences' : 1000 } dl = data_loader_v2.Data_Loader(data_loader_options) buckets, source_vocab, target_vocab, frequent_keys = dl.load_translation_data( ) config = model_config.translator_config model_options = { 'n_source_quant': len(source_vocab), 'n_target_quant': len(target_vocab), 'residual_channels': config['residual_channels'], 'decoder_dilations': config['decoder_dilations'], 'encoder_dilations': config['encoder_dilations'], 'sample_size': 10, 'decoder_filter_width': config['decoder_filter_width'], 'encoder_filter_width': config['encoder_filter_width'], 'batch_size': args.batch_size, 'source_mask_chars': [source_vocab['padding']], 'target_mask_chars': [target_vocab['padding']] } # temp # byte_net = model.Byte_net_model( model_options ) # bn_tensors = byte_net.build_translation_model(sample_size = 100) last_saved_model_path = None if args.resume_model: last_saved_model_path = args.resume_model print "Number Of Buckets", len(buckets) for i in range(1, args.max_epochs): cnt = 0 for _, key in frequent_keys: cnt += 1 print "KEY", cnt, key if len(buckets[key]) < args.batch_size: print "BUCKET TOO SMALL", key continue sess = tf.InteractiveSession() batch_no = 0 batch_size = args.batch_size byte_net = model.Byte_net_model(model_options) bn_tensors = byte_net.build_translation_model(sample_size=key) adam = tf.train.AdamOptimizer(args.learning_rate, beta1=args.beta1) optim = adam.minimize(bn_tensors['loss'], var_list=bn_tensors['variables']) train_writer = tf.train.SummaryWriter('logs/', sess.graph) tf.initialize_all_variables().run() saver = tf.train.Saver() if last_saved_model_path: saver.restore(sess, last_saved_model_path) while (batch_no + 1) * batch_size < len(buckets[key]): source, target = dl.get_batch_from_pairs( buckets[key][batch_no * batch_size:(batch_no + 1) * batch_size]) _, loss, prediction, summary, source_gradient, target_gradient = sess.run( [ optim, bn_tensors['loss'], bn_tensors['prediction'], bn_tensors['merged_summary'], bn_tensors['source_gradient'], bn_tensors['target_gradient'] ], feed_dict={ bn_tensors['source_sentence']: source, bn_tensors['target_sentence']: target, }) train_writer.add_summary(summary, batch_no * (cnt + 1)) print "Loss", loss, batch_no, len( buckets[key]) / batch_size, i, cnt, key print "$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$" print "Source ", dl.inidices_to_string(source[0], source_vocab) print "---------" print "Target ", dl.inidices_to_string(target[0], target_vocab) print "----------" print "Prediction ", dl.inidices_to_string( prediction[0:key], target_vocab) print "*****" print "Source Gradients", np.mean(source_gradient[0][0, :], axis=1) print " " print "Target Gradients", np.mean(target_gradient[0][0, :], axis=1) print "$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$" batch_no += 1 if batch_no % 1000 == 0: save_path = saver.save( sess, "Data/Models/model_translation_epoch_{}_{}.ckpt". format(i, cnt)) last_saved_model_path = "Data/Models/model_translation_epoch_{}_{}.ckpt".format( i, cnt) save_path = saver.save( sess, "Data/Models/model_translation_epoch_{}.ckpt".format(i)) last_saved_model_path = "Data/Models/model_translation_epoch_{}.ckpt".format( i) tf.reset_default_graph() sess.close()
def main(): parser = argparse.ArgumentParser() parser.add_argument('--model_path', type=str, default='Data/Models/model_translation_epoch_1.ckpt', help='Pre-Trained Model Path') parser.add_argument('--data_dir', type=str, default='Data', help='Data Directory') parser.add_argument('--num_char', type=int, default=1000, help='seed') parser.add_argument('--translator_max_length', type=int, default=500, help='translator_max_length') parser.add_argument('--output_file', type=str, default='sample.txt', help='Output File') args = parser.parse_args() config = model_config.translator_config source_sentence = None with open('Data/MachineTranslation/news-commentary-v11.de-en.de') as f: source_sentences = f.read().decode("utf-8").split('\n') with open('Data/MachineTranslation/news-commentary-v11.de-en.en') as f: target_sentences = f.read().decode("utf-8").split('\n') idx = 0 for i in range(len(source_sentences)): if 'NEW YORK' in target_sentences[i][0:40]: print target_sentences[i] idx = i break source_sentences = source_sentences[idx:idx + 1] target_sentences = target_sentences[idx:idx + 1] print source_sentences print target_sentences data_loader_options = { 'model_type': 'translation', 'source_file': 'Data/MachineTranslation/news-commentary-v11.de-en.de', 'target_file': 'Data/MachineTranslation/news-commentary-v11.de-en.en', 'bucket_quant': 25, } dl = data_loader_v2.Data_Loader(data_loader_options) # buckets, source_vocab, target_vocab, frequent_keys = dl.load_translation_data() source_ = [] target_ = [] for i in range(len(source_sentences)): source_sentence = source_sentences[i] source = [dl.source_vocab[s] for s in source_sentence] source += [dl.source_vocab['eol']] new_length = args.translator_max_length # bucket_quant = args.bucket_quant # if new_length % bucket_quant > 0: # new_length = ((new_length/bucket_quant) + 1 ) * bucket_quant for i in range(len(source), new_length): source += [dl.source_vocab['padding']] target = [dl.target_vocab['init']] for j in range(1, new_length + 1): target += [dl.target_vocab['padding']] source_.append(source) target_.append(target) source = np.array(source_) target = np.array(target_) # print source_ # source = np.array(source_, dtype='int32') # target = np.array(target_, dtype='int32') # print source # print target model_options = { 'n_source_quant': len(dl.source_vocab), 'n_target_quant': len(dl.target_vocab), 'residual_channels': config['residual_channels'], 'decoder_dilations': config['decoder_dilations'], 'encoder_dilations': config['encoder_dilations'], 'sample_size': 10, 'decoder_filter_width': config['decoder_filter_width'], 'encoder_filter_width': config['encoder_filter_width'], 'batch_size': 1, 'source_mask_chars': [dl.source_vocab['padding']], 'target_mask_chars': [dl.target_vocab['padding']] } byte_net = model.Byte_net_model(model_options) translator = byte_net.build_translation_model(args.translator_max_length) sess = tf.InteractiveSession() saver = tf.train.Saver() saver.restore(sess, args.model_path) input_batch = target print "INPUT", input_batch print "Source", source for i in range(0, 1000): prediction, probs = sess.run( [translator['prediction'], translator['probs']], feed_dict={ translator['source_sentence']: source, translator['target_sentence']: input_batch, }) # prediction = prediction[0] last_prediction = np.array([utils.weighted_pick(probs[i])]) last_prediction = last_prediction.reshape([1, -1]) # prediction = np.reshape(prediction, ) # print "encoder" # print encoder_output # last_prediction = prediction[i] # last_prediction = np.array( [ last_prediction ]) # last_prediction = last_prediction.reshape([1,-1]) input_batch[:, i + 1] = last_prediction[:, 0] res = dl.inidices_to_string(input_batch[0], dl.target_vocab) print "RES" print res
def main(): parser = argparse.ArgumentParser() parser.add_argument('--learning_rate', type=float, default=0.001, help='Learning Rate') parser.add_argument('--batch_size', type=int, default=1, help='Learning Rate') parser.add_argument('--max_epochs', type=int, default=1000, help='Max Epochs') parser.add_argument('--beta1', type=float, default=0.5, help='Momentum for Adam Update') parser.add_argument('--resume_model', type=str, default=None, help='Pre-Trained Model Path, to resume from') parser.add_argument('--data_dir', type=str, default='Data', help='Data Directory') parser.add_argument('--output_dir', type=str, default='Data', help='Data Directory') args = parser.parse_args() # model_config = json.loads( open('model_config.json').read() ) config = model_config.config model_options = { 'n_source_quant' : config['n_source_quant'], 'n_target_quant' : config['n_target_quant'], 'residual_channels' : config['residual_channels'], 'decoder_dilations' : config['decoder_dilations'], 'sample_size' : config['sample_size'], 'decoder_filter_width' : config['decoder_filter_width'], 'batch_size' : args.batch_size, } byte_net = model.Byte_net_model( model_options ) bn_tensors = byte_net.build_prediction_model() optim = tf.train.AdamOptimizer( args.learning_rate, beta1 = args.beta1).minimize(bn_tensors['loss'], var_list=bn_tensors['variables']) sess = tf.InteractiveSession() tf.initialize_all_variables().run() saver = tf.train.Saver(max_to_keep=0) if args.resume_model: saver.restore(sess, args.resume_model) text_samples = data_loader.load_text_samples(args.data_dir, model_config.config['sample_size']) print text_samples.shape for i in range(args.max_epochs): batch_no = 0 batch_size = args.batch_size while (batch_no+1) * batch_size < text_samples.shape[0]: text_batch = text_samples[batch_no*batch_size : (batch_no + 1)*batch_size, :] _, loss, prediction = sess.run( [optim, bn_tensors['loss'], bn_tensors['prediction']], feed_dict = { bn_tensors['sentence'] : text_batch }) # print "-------------------------------------------------------" # print list_to_string(prediction) print "Loss" print i, batch_no, loss print "********************************************************" # print prediction batch_no += 1 txt = STARTED_DATESTRING+"\n\nN Source Quant: "+str(config['n_source_quant'])+"\nN Target Quant: "+str(config['n_target_quant'])+"\nSample Size: "+ str(config['sample_size'])+"\nBatch Size: "+ str(args.batch_size)+"\nResidual Channels: "+str(config['residual_channels'])+"\nDecoder Filter Width: "+str(config['decoder_filter_width'])+"\nDecoder Dilations: "+str(config['decoder_dilations'])+"\n\nEpoch: "+str(i)+"\nBatch: "+str(batch_no)+"\nLoss: "+str(loss)+"\n\n***<###>***\n\n"+list_to_string(prediction) txt_filename = os.path.join(args.output_dir, "{}_Epoch_{}_Batch_{}.txt".format(STARTED_DATESTRING, i, batch_no)) with open(txt_filename, 'wb') as f: print "Saving:", txt_filename f.write(txt) if (batch_no % 100) == 0: save_path = saver.save(sess, "Data/Models/model_epoch_{}.ckpt".format(i)) if loss < 0.7 : print ("SAVING", STARTED_DATESTRING+"model_epoch_{}.ckpt".format(i)) save_path = saver.save(sess, "Data/Models/"+STARTED_DATESTRING+"model_epoch_{}.ckpt".format(i))