klds) print("epoch {0} --------------------- Time {1:.2f}s".format( epoch, time.time() - tt)) print( "train loss:{0:.3f}, train mse:{1:.3f}, train kld:{2:.3f}".format( train_loss, train_mse, train_kld)) print("test loss:{0:.3f}, test mse:{1:.3f}, test kld:{2:.3f}".format( test_loss, test_mse, test_kld)) if epoch % FLAGS.save_interval == 0: saver.save(sess, FLAGS.save_dir + '/params_' + 'celeba' + '.ckpt') data = next(test_data) feed_dict = make_feed_dict(data, test_mgen) sample_x = sess.run(x_hats, feed_dict=feed_dict) sample_x = np.concatenate(sample_x, axis=0) test_data.reset() img_tile = plotting.img_tile(sample_x[:25], aspect_ratio=1.0, border_color=1.0, stretch=True) img = plotting.plot_img(img_tile, title=FLAGS.data_set + ' samples') plotting.plt.savefig( os.path.join(FLAGS.save_dir, '%s_vae_sample%d.png' % (FLAGS.data_set, epoch)))
sess.run(initializer) feed_dict = make_feed_dict( train_data.next(args.init_batch_size), init=True ) # manually retrieve exactly init_batch_size examples sess.run(init_pass, feed_dict) # generate samples from the model sample_x = [] for i in range(args.num_samples): sample_x.append(sample_from_model(sess)) sample_x = np.concatenate(sample_x, axis=0) img_tile = plotting.img_tile(sample_x[:100], aspect_ratio=1.0, border_color=1.0, stretch=True) img = plotting.plot_img(img_tile, title=args.data_set + ' samples') plotting.plt.savefig( os.path.join(args.save_dir, '%s_sample%d.png' % (args.data_set, epoch))) plotting.plt.close('all') np.savez( os.path.join(args.save_dir, '%s_sample%d.npz' % (args.data_set, epoch)), sample_x) print('starting training') # train for one epoch train_losses = [] cnt = 0 for d in train_data:
# compute likelihood over test data test_losses = [] for d in test_data: feed_dict = make_feed_dict(d) l = sess.run(bits_per_dim_test, feed_dict) test_losses.append(l) test_loss_gen = np.mean(test_losses) test_bpd.append(test_loss_gen) # log progress to console print("Iteration %d, time = %ds, train bits_per_dim = %.4f, test bits_per_dim = %.4f" % (epoch, time.time()-begin, train_loss_gen, test_loss_gen)) sys.stdout.flush() if epoch % args.save_interval == 0: # generate samples from the model sample_x = [] for i in range(args.num_samples): sample_x.append(sample_from_model(sess)) sample_x = np.concatenate(sample_x,axis=0) img_tile = plotting.img_tile(sample_x[:100], aspect_ratio=1.0, border_color=1.0, stretch=True) img = plotting.plot_img(img_tile, title=dset_dump_name + ' samples') plotting.plt.savefig(os.path.join(args.save_dir,'%s_sample%d.png' % (dset_dump_name, epoch))) plotting.plt.close('all') np.savez(os.path.join(args.save_dir,'%s_sample%d.npz' % (dset_dump_name, epoch)), sample_x) # save params saver.save(sess, args.save_dir + '/params_' + dset_dump_name + '.ckpt', global_step=epoch) np.savez(args.save_dir + '/test_bpd_' + dset_dump_name + '.npz', test_bpd=np.array(test_bpd))