def train(args): print("[%s] Preparing dialog data in %s" % (args.model_name, args.data_dir)) setup_workpath(workspace=args.workspace) train_data, dev_data, _ = data_utils.prepare_dialog_data( args.data_dir, args.vocab_size) #### GET DATA ###### inti beer def get_gold(workspace=args.workspace): data_dir = "%s/data" % (workspace) full_path = str(sys.path[-1]) + "/" + data_dir + "/train/chat.txt.gz" print(full_path) with gzip.open(full_path, 'rb') as zi: test_sentences = zi.read() test_sentences = test_sentences.decode().split("\n") zi.close() return test_sentences ######get data data_ = get_gold() if args.reinforce_learn: args.batch_size = 1 # We decode one sentence at a time. gpu_options = tf.GPUOptions(per_process_gpu_memory_fraction=args.gpu_usage) with tf.Session(config=tf.ConfigProto(gpu_options=gpu_options)) as sess: # Create model. print("Creating %d layers of %d units." % (args.num_layers, args.size)) model = seq2seq_model_utils.create_model(sess, args, forward_only=False) # Read data into buckets and compute their sizes. print("Reading development and training data (limit: %d)." % args.max_train_data_size) dev_set = data_utils.read_data(dev_data, args.buckets, reversed=args.rev_model) train_set = data_utils.read_data(train_data, args.buckets, args.max_train_data_size, reversed=args.rev_model) train_bucket_sizes = [ len(train_set[b]) for b in xrange(len(args.buckets)) ] train_total_size = float(sum(train_bucket_sizes)) # A bucket scale is a list of increasing numbers from 0 to 1 that we'll use # to select a bucket. Length of [scale[i], scale[i+1]] is proportional to # the size if i-th training bucket, as used later. train_buckets_scale = [ sum(train_bucket_sizes[:i + 1]) / train_total_size for i in xrange(len(train_bucket_sizes)) ] # This is the training loop. step_time, loss = 0.0, 0.0 current_step = 0 previous_losses = [] # Load vocabularies. vocab_path = os.path.join(args.data_dir, "vocab%d.in" % args.vocab_size) vocab, rev_vocab = data_utils.initialize_vocabulary(vocab_path) while True: # Choose a bucket according to data distribution. We pick a random number # in [0, 1] and use the corresponding interval in train_buckets_scale. random_number_01 = np.random.random_sample() bucket_id = min([ i for i in xrange(len(train_buckets_scale)) if train_buckets_scale[i] > random_number_01 ]) # Get a batch and make a step. start_time = time.time() encoder_inputs, decoder_inputs, target_weights, encoder_input, decoder_input = model.get_batch( train_set, bucket_id) print("[shape]", np.shape(encoder_inputs), np.shape(decoder_inputs), np.shape(target_weights)) if args.reinforce_learn: _, step_loss, _ = model.step_rf(args, sess, encoder_inputs, decoder_inputs, target_weights, bucket_id, data_, encoder_input, decoder_input, rev_vocab=rev_vocab, forward_only=False) else: _, step_loss, _ = model.step(sess, encoder_inputs, decoder_inputs, target_weights, bucket_id, forward_only=False, force_dec_input=True) step_time += (time.time() - start_time) / args.steps_per_checkpoint loss += step_loss / args.steps_per_checkpoint current_step += 1 print("Current step: " + str(current_step)) # Once in a while, we save checkpoint, print statistics, and run evals. if current_step % args.steps_per_checkpoint == 0: #and (not args.reinforce_learn): # Print statistics for the previous epoch. perplexity = math.exp(loss) if loss < 300 else float('inf') print( "global step %d learning rate %.4f step-time %.2f perplexity %.2f @ %s" % (model.global_step.eval(), model.learning_rate.eval(), step_time, perplexity, datetime.now())) # Decrease learning rate if no improvement was seen over last 3 times. if len(previous_losses) > 2 and loss > max( previous_losses[-3:]): sess.run(model.learning_rate_decay_op) previous_losses.append(loss) # # Save checkpoint and zero timer and loss. checkpoint_path = os.path.join(args.model_dir, "model.ckpt") model.saver.save(sess, checkpoint_path, global_step=model.global_step) step_time, loss = 0.0, 0.0 # Run evals on development set and print their perplexity. for bucket_id in xrange(len(args.buckets)): encoder_inputs, decoder_inputs, target_weights, _, _ = model.get_batch( dev_set, bucket_id) _, eval_loss, _ = model.step(sess, encoder_inputs, decoder_inputs, target_weights, bucket_id, forward_only=True, force_dec_input=False) eval_ppx = math.exp( eval_loss) if eval_loss < 300 else float('inf') print(" eval: bucket %d perplexity %.2f" % (bucket_id, eval_ppx)) sys.stdout.flush()
def train(): print("Preparing dialog data in %s" % FLAGS.data_dir) train_data, dev_data, _ = data_utils.prepare_dialog_data( FLAGS.data_dir, FLAGS.vocab_size) config = tf.ConfigProto() config.gpu_options.allow_growth = True with tf.Session(config=config) as sess: record_file_name = "seq2seq_loss" record_file = open(pjoin(FLAGS.results_dir, record_file_name), mode="ab", buffering=0) # Create model. print("Creating %d layers of %d units." % (FLAGS.num_layers, FLAGS.hidden_size)) model = create_model(sess, forward_only=False) # Read data into buckets and compute their sizes. print("Reading development and training data (limit: %d)." % FLAGS.max_train_data_size) dev_set = read_data(dev_data) train_set = read_data(train_data, FLAGS.max_train_data_size) train_bucket_sizes = [len(train_set[b]) for b in xrange(len(BUCKETS))] train_total_size = float(sum(train_bucket_sizes)) # A bucket scale is a list of increasing numbers from 0 to 1 that we'll use # to select a bucket. Length of [scale[i], scale[i+1]] is proportional to # the size if i-th training bucket, as used later. train_buckets_scale = [ sum(train_bucket_sizes[:i + 1]) / train_total_size for i in xrange(len(train_bucket_sizes)) ] # This is the training loop. step_time, total_time, loss = 0.0, 0.0, 0.0 previous_losses = [] print("start train...") while (True): # Choose a bucket according to data distribution. We pick a random number # in [0, 1] and use the corresponding interval in train_buckets_scale. random_number_01 = np.random.random_sample() bucket_id = min([ i for i in xrange(len(train_buckets_scale)) if train_buckets_scale[i] > random_number_01 ]) # Get a batch and make a step. start_time = time.time() encoder_inputs, decoder_inputs, target_weights = model.get_batch( train_set, bucket_id, "train") _, step_loss, _ = model.step(sess, encoder_inputs, decoder_inputs, target_weights, bucket_id, forward_only=False) step_time += (time.time() - start_time) / FLAGS.steps_per_checkpoint loss += step_loss / FLAGS.steps_per_checkpoint # Once in a while, we save checkpoint, print statistics, and run evals. if model.global_step.eval() % FLAGS.steps_per_checkpoint == 0: print("\nTraining...") if loss > 6: print("inf !!!") sys.exit(0) total_time = step_time * FLAGS.steps_per_checkpoint print( "global step %d learning rate %.4f step-time %.2f total_time %.4f, loss %0.7f" % (model.global_step.eval(), model.learning_rate.eval(), step_time, total_time, loss)) # Decrease learning rate if no improvement was seen over last 3 times. if len(previous_losses) > 2 and loss > max( previous_losses[-3:]): sess.run(model.learning_rate_decay_op) previous_losses.append(loss) # Run evals on development set and print their perplexity. for bucket_id in xrange(len(BUCKETS)): print("Testing...") encoder_inputs, decoder_inputs, target_weights = model.get_batch( dev_set, bucket_id, "train") _, eval_loss, _ = model.step(sess, encoder_inputs, decoder_inputs, target_weights, bucket_id, True) if eval_loss > 6: print("inf !!!") sys.exit(0) print("eval: bucket %d, loss %0.5f" % (bucket_id, eval_loss)) sys.stdout.flush() record_file.write("%d\t%.5f\t%.5f\n" % (model.global_step.eval(), loss, eval_loss)) # Save checkpoint and zero timer and loss. if model.global_step.eval( ) % FLAGS.steps_per_predictpoint == 0: checkpoint_path = os.path.join(FLAGS.model_dir, "model.ckpt") model.saver.save(sess, checkpoint_path, global_step=model.global_step) step_time, loss = 0.0, 0.0
def train(): print("Preparing dialog data in %s" % FLAGS.data_dir) train_data, dev_data, _ = data_utils.prepare_dialog_data( FLAGS.data_dir, FLAGS.vocab_size) with tf.Session() as sess: # Create model. print("Creating %d layers of %d units." % (FLAGS.num_layers, FLAGS.size)) model = create_model(sess, forward_only=False) print("Reading development and training data (limit: %d)." % FLAGS.max_train_data_size) dev_set = read_data(dev_data) train_set = read_data(train_data, FLAGS.max_train_data_size) train_bucket_sizes = [len(train_set[b]) for b in xrange(len(BUCKETS))] train_total_size = float(sum(train_bucket_sizes)) train_buckets_scale = [ sum(train_bucket_sizes[:i + 1]) / train_total_size for i in xrange(len(train_bucket_sizes)) ] # This is the training loop. print("Start training ...") step_time, loss = 0.0, 0.0 current_step = 0 previous_losses = [] while True: random_number_01 = np.random.random_sample() bucket_id = min([ i for i in xrange(len(train_buckets_scale)) if train_buckets_scale[i] > random_number_01 ]) # Get a batch and make a step. start_time = time.time() encoder_inputs, decoder_inputs, target_weights = model.get_batch( train_set, bucket_id) _, step_loss, _ = model.step(sess, encoder_inputs, decoder_inputs, target_weights, bucket_id, forward_only=False) step_time += (time.time() - start_time) / FLAGS.steps_per_checkpoint loss += step_loss / FLAGS.steps_per_checkpoint current_step += 1 if current_step % FLAGS.steps_per_checkpoint == 0: perplexity = math.exp(loss) if loss < 300 else float('inf') print( "global step %d learning rate %.4f step-time %.2f perplexity %.2f" % (model.global_step.eval(), model.learning_rate.eval(), step_time, perplexity)) if len(previous_losses) > 2 and loss > max( previous_losses[-3:]): sess.run(model.learning_rate_decay_op) previous_losses.append(loss) checkpoint_path = os.path.join(FLAGS.model_dir, "model.ckpt") model.saver.save(sess, checkpoint_path, global_step=model.global_step) step_time, loss = 0.0, 0.0 for bucket_id in xrange(len(BUCKETS)): encoder_inputs, decoder_inputs, target_weights = model.get_batch( dev_set, bucket_id) _, eval_loss, _ = model.step(sess, encoder_inputs, decoder_inputs, target_weights, bucket_id, True) eval_ppx = math.exp( eval_loss) if eval_loss < 300 else float('inf') print(" eval: bucket %d perplexity %.2f" % (bucket_id, eval_ppx)) sys.stdout.flush()
def train(args): print("[%s] Preparing dialog data in %s" % (args.model_name, args.data_dir)) setup_workpath(workspace=args.workspace) train_data, dev_data, _ = data_utils.prepare_dialog_data(args.data_dir, args.vocab_size) if args.reinforce_learn: args.batch_size = 1 # We decode one sentence at a time. gpu_options = tf.GPUOptions(per_process_gpu_memory_fraction=args.gpu_usage) with tf.Session(config=tf.ConfigProto(gpu_options=gpu_options)) as sess: # Create model. print("Creating %d layers of %d units." % (args.num_layers, args.size)) model = seq2seq_model_utils.create_model(sess, args, forward_only=False) # Read data into buckets and compute their sizes. print("Reading development and training data (limit: %d)." % args.max_train_data_size) dev_set = data_utils.read_data(dev_data, args.buckets, reversed=args.rev_model) train_set = data_utils.read_data(train_data, args.buckets, args.max_train_data_size, reversed=args.rev_model) train_bucket_sizes = [len(train_set[b]) for b in xrange(len(args.buckets))] train_total_size = float(sum(train_bucket_sizes)) # A bucket scale is a list of increasing numbers from 0 to 1 that we'll use # to select a bucket. Length of [scale[i], scale[i+1]] is proportional to # the size if i-th training bucket, as used later. train_buckets_scale = [sum(train_bucket_sizes[:i + 1]) / train_total_size for i in xrange(len(train_bucket_sizes))] # This is the training loop. step_time, loss = 0.0, 0.0 current_step = 0 previous_losses = [] # Load vocabularies. vocab_path = os.path.join(args.data_dir, "vocab%d.in" % args.vocab_size) vocab, rev_vocab = data_utils.initialize_vocabulary(vocab_path) while True: # Choose a bucket according to data distribution. We pick a random number # in [0, 1] and use the corresponding interval in train_buckets_scale. random_number_01 = np.random.random_sample() bucket_id = min([i for i in xrange(len(train_buckets_scale)) if train_buckets_scale[i] > random_number_01]) # Get a batch and make a step. start_time = time.time() encoder_inputs, decoder_inputs, target_weights = model.get_batch( train_set, bucket_id) # print("[shape]", np.shape(encoder_inputs), np.shape(decoder_inputs), np.shape(target_weights)) if args.reinforce_learn: _, step_loss, _ = model.step_rf(args, sess, encoder_inputs, decoder_inputs, target_weights, bucket_id, rev_vocab=rev_vocab) else: _, step_loss, _ = model.step(sess, encoder_inputs, decoder_inputs, target_weights, bucket_id, forward_only=False, force_dec_input=True) step_time += (time.time() - start_time) / args.steps_per_checkpoint loss += step_loss / args.steps_per_checkpoint current_step += 1 # Once in a while, we save checkpoint, print statistics, and run evals. if (current_step % args.steps_per_checkpoint == 0) and (not args.reinforce_learn): # Print statistics for the previous epoch. perplexity = math.exp(loss) if loss < 300 else float('inf') print ("global step %d learning rate %.4f step-time %.2f perplexity %.2f @ %s" % (model.global_step.eval(), model.learning_rate.eval(), step_time, perplexity, datetime.now())) # Decrease learning rate if no improvement was seen over last 3 times. if len(previous_losses) > 2 and loss > max(previous_losses[-3:]): sess.run(model.learning_rate_decay_op) previous_losses.append(loss) # # Save checkpoint and zero timer and loss. checkpoint_path = os.path.join(args.model_dir, "model.ckpt") model.saver.save(sess, checkpoint_path, global_step=model.global_step) step_time, loss = 0.0, 0.0 # Run evals on development set and print their perplexity. for bucket_id in xrange(len(args.buckets)): encoder_inputs, decoder_inputs, target_weights = model.get_batch(dev_set, bucket_id) _, eval_loss, _ = model.step(sess, encoder_inputs, decoder_inputs, target_weights, bucket_id, forward_only=True, force_dec_input=False) eval_ppx = math.exp(eval_loss) if eval_loss < 300 else float('inf') print(" eval: bucket %d perplexity %.2f" % (bucket_id, eval_ppx)) sys.stdout.flush()