def transfer(model, decoder, sess, args, vocab, data0, data1, out_path): batches, order0, order1 = get_batches(data0, data1, vocab.word2id, args.batch_size) data0_tsf, data1_tsf = [], [] losses = Losses(len(batches)) for batch in batches: ori, tsf = decoder.rewrite(batch) half = batch['size'] / 2 data0_tsf += tsf[:half] data1_tsf += tsf[half:] loss, loss_g, loss_d, loss_d0, loss_d1 = sess.run( [ model.loss, model.loss_g, model.loss_d, model.loss_d0, model.loss_d1 ], feed_dict=feed_dictionary(model, batch, args.rho, args.gamma_min)) losses.add(loss, loss_g, loss_d, loss_d0, loss_d1) n0, n1 = len(data0), len(data1) data0_tsf = reorder(order0, data0_tsf)[:n0] data1_tsf = reorder(order1, data1_tsf)[:n1] if out_path: write_sent(data0_tsf, out_path + '.0' + '.tsf') write_sent(data1_tsf, out_path + '.1' + '.tsf') return losses
def transfer(model, sess, args, vocab, data0, data1, out_path): batches, order0, order1 = get_batches(data0, data1, vocab.word2id, args.batch_size) data0_tsf, data1_tsf = [], [] losses = Losses(len(batches)) for batch in batches: ori, tsf, loss, loss_g, loss_d, loss_d0, loss_d1 = rewrite( model, sess, args, vocab, batch) half = batch['size'] / 2 data0_tsf += tsf[:half] data1_tsf += tsf[half:] losses.add(loss, loss_g, loss_d, loss_d0, loss_d1) n0, n1 = len(data0), len(data1) data0_tsf = reorder(order0, data0_tsf)[:n0] data1_tsf = reorder(order1, data1_tsf)[:n1] if out_path: write_sent(data0_tsf, out_path + '.0' + '.tsf') write_sent(data1_tsf, out_path + '.1' + '.tsf') return losses
loss_d1, _ = sess.run([model.loss_d1, model.optimizer_d1], feed_dict=feed_dict) # do not back-propagate from the discriminator # when it is too poor if loss_d0 < 1.2 and loss_d1 < 1.2: optimizer = model.optimizer_all else: optimizer = model.optimizer_ae loss, loss_g, loss_d, _ = sess.run( [model.loss, model.loss_g, model.loss_d, optimizer], feed_dict=feed_dict) step += 1 losses.add(loss, loss_g, loss_d, loss_d0, loss_d1) if step % args.steps_per_checkpoint == 0: losses.output('step %d, time %.0fs,' % (step, time.time() - start_time)) losses.clear() if args.dev: dev_losses = transfer(model, decoder, sess, args, vocab, dev0, dev1, args.output + '.epoch%d' % epoch) dev_losses.output('dev') if dev_losses.loss < best_dev: best_dev = dev_losses.loss print 'saving model...' model.saver.save(sess, args.model)