idx = int(random.random() * x_test.size(0)) recon = model(x_test[idx].view(1, -1)).squeeze() show_image(x_test[idx]) show_image(recon) # Hidden Space if config.btl_size == 2: color_map = ['brown', 'red', 'orange', 'yellow', 'green', 'blue', 'navy', 'purple', 'gray','black', ] plt.figure(figsize=(20, 10)) with torch.no_grad(): latents = model.encoder(x_test[:1000]) for i in range(10): target_latents = latents[y_test[:1000] == i] y_target = y_test[:1000][y_test[:1000] == i] plt.scatter(target_latents[:, 0], target_latents[:, 1], marker='o', color=color_map[i], label=i) plt.legend() plt.grid(axis='both') plt.show()
plt.xlabel('epoch') plt.ylabel('loss') plt.grid() plt.savefig('./{}/loss.png'.format(log_dir)) # visualize latent space test_dataset = MNIST(data_dir, download=True, train=False, transform=img_transform) test_loader = DataLoader(test_dataset, batch_size=10000, shuffle=False) x, labels = iter(test_loader).next() x = x.view(x.size(0), -1) if use_gpu: x = Variable(x).cuda() z = model.encoder(x).cpu().data.numpy() else: x = Variable(x) z = model.encoder(x).data.numpy() plt.figure(figsize=(10, 10)) plt.scatter(z[:, 0], z[:, 1], marker='.', c=labels.numpy(), cmap=plt.cm.jet) plt.colorbar() plt.grid() plt.savefig('./{}/latent_space.png'.format(log_dir)) # save result np.save('./{}/loss_list.npy'.format(log_dir), np.array(loss_list)) torch.save(model.state_dict(), './{}/model_weights.pth'.format(log_dir))