update to torch 0.4
This commit is contained in:
@ -3,13 +3,12 @@ View more, visit my tutorial page: https://morvanzhou.github.io/tutorials/
|
||||
My Youtube Channel: https://www.youtube.com/user/MorvanZhou
|
||||
|
||||
Dependencies:
|
||||
torch: 0.1.11
|
||||
torch: 0.4
|
||||
numpy
|
||||
matplotlib
|
||||
"""
|
||||
import torch
|
||||
import torch.nn as nn
|
||||
from torch.autograd import Variable
|
||||
import numpy as np
|
||||
import matplotlib.pyplot as plt
|
||||
|
||||
@ -35,7 +34,7 @@ def artist_works(): # painting from the famous artist (real target)
|
||||
a = np.random.uniform(1, 2, size=BATCH_SIZE)[:, np.newaxis]
|
||||
paintings = a * np.power(PAINT_POINTS, 2) + (a-1)
|
||||
paintings = torch.from_numpy(paintings).float()
|
||||
return Variable(paintings)
|
||||
return paintings
|
||||
|
||||
G = nn.Sequential( # Generator
|
||||
nn.Linear(N_IDEAS, 128), # random ideas (could from normal distribution)
|
||||
@ -57,7 +56,7 @@ plt.ion() # something about continuous plotting
|
||||
|
||||
for step in range(10000):
|
||||
artist_paintings = artist_works() # real painting from artist
|
||||
G_ideas = Variable(torch.randn(BATCH_SIZE, N_IDEAS)) # random ideas
|
||||
G_ideas = torch.randn(BATCH_SIZE, N_IDEAS) # random ideas
|
||||
G_paintings = G(G_ideas) # fake painting from G (random ideas)
|
||||
|
||||
prob_artist0 = D(artist_paintings) # D try to increase this prob
|
||||
@ -67,7 +66,7 @@ for step in range(10000):
|
||||
G_loss = torch.mean(torch.log(1. - prob_artist1))
|
||||
|
||||
opt_D.zero_grad()
|
||||
D_loss.backward(retain_variables=True) # retain_variables for reusing computational graph
|
||||
D_loss.backward(retain_graph=True) # reusing computational graph
|
||||
opt_D.step()
|
||||
|
||||
opt_G.zero_grad()
|
||||
@ -79,9 +78,9 @@ for step in range(10000):
|
||||
plt.plot(PAINT_POINTS[0], G_paintings.data.numpy()[0], c='#4AD631', lw=3, label='Generated painting',)
|
||||
plt.plot(PAINT_POINTS[0], 2 * np.power(PAINT_POINTS[0], 2) + 1, c='#74BCFF', lw=3, label='upper bound')
|
||||
plt.plot(PAINT_POINTS[0], 1 * np.power(PAINT_POINTS[0], 2) + 0, c='#FF9359', lw=3, label='lower bound')
|
||||
plt.text(-.5, 2.3, 'D accuracy=%.2f (0.5 for D to converge)' % prob_artist0.data.numpy().mean(), fontdict={'size': 15})
|
||||
plt.text(-.5, 2, 'D score= %.2f (-1.38 for G to converge)' % -D_loss.data.numpy(), fontdict={'size': 15})
|
||||
plt.ylim((0, 3));plt.legend(loc='upper right', fontsize=12);plt.draw();plt.pause(0.01)
|
||||
plt.text(-.5, 2.3, 'D accuracy=%.2f (0.5 for D to converge)' % prob_artist0.data.numpy().mean(), fontdict={'size': 13})
|
||||
plt.text(-.5, 2, 'D score= %.2f (-1.38 for G to converge)' % -D_loss.data.numpy(), fontdict={'size': 13})
|
||||
plt.ylim((0, 3));plt.legend(loc='upper right', fontsize=10);plt.draw();plt.pause(0.01)
|
||||
|
||||
plt.ioff()
|
||||
plt.show()
|
||||
Reference in New Issue
Block a user