Skip to content

Commit 084648c

Browse files
MorvanZhouMorvan Zhou
authored and
Morvan Zhou
committed
update
1 parent 504f4b4 commit 084648c

File tree

4 files changed

+186
-1
lines changed

4 files changed

+186
-1
lines changed

tutorial-contents/GAN.py renamed to tutorial-contents/406_GAN.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ def artist_works(): # painting from the famous artist (real target)
6161

6262
plt.ion() # something about continuous plotting
6363
plt.show()
64-
for step in range(10000):
64+
for step in range(5000):
6565
artist_paintings = artist_works() # real painting from artist
6666
G_ideas = np.random.randn(BATCH_SIZE, N_IDEAS)
6767
G_paintings, pa0, Dl = sess.run([G_out, prob_artist0, D_loss, train_D, train_G], # train and get results
+101
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
"""
2+
Know more, visit my Python tutorial page: https://morvanzhou.github.io/tutorials/
3+
My Youtube Channel: https://www.youtube.com/user/MorvanZhou
4+
5+
Dependencies:
6+
tensorflow: 1.1.0
7+
matplotlib
8+
numpy
9+
"""
10+
import tensorflow as tf
11+
import numpy as np
12+
import matplotlib.pyplot as plt
13+
14+
tf.set_random_seed(1)
15+
np.random.seed(1)
16+
17+
# Hyper Parameters
18+
BATCH_SIZE = 64
19+
LR_G = 0.0001 # learning rate for generator
20+
LR_D = 0.0001 # learning rate for discriminator
21+
N_IDEAS = 5 # think of this as number of ideas for generating an art work (Generator)
22+
ART_COMPONENTS = 15 # it could be total point G can draw in the canvas
23+
PAINT_POINTS = np.vstack([np.linspace(-1, 1, ART_COMPONENTS) for _ in range(BATCH_SIZE)])
24+
25+
# show our beautiful painting range
26+
plt.plot(PAINT_POINTS[0], 2 * np.power(PAINT_POINTS[0], 2) + 1, c='#74BCFF', lw=3, label='upper bound')
27+
plt.plot(PAINT_POINTS[0], 1 * np.power(PAINT_POINTS[0], 2) + 0, c='#FF9359', lw=3, label='lower bound')
28+
plt.legend(loc='upper right')
29+
plt.show()
30+
31+
32+
def artist_works(): # painting from the famous artist (real target)
33+
a = np.random.uniform(1, 2, size=BATCH_SIZE)[:, np.newaxis]
34+
paintings = a * np.power(PAINT_POINTS, 2) + (a-1)
35+
labels = (a - 1) > 0.5 # upper paintings (1), lower paintings (0), two classes
36+
labels = labels.astype(np.float32)
37+
return paintings, labels
38+
39+
art_labels = tf.placeholder(tf.float32, [None, 1])
40+
with tf.variable_scope('Generator'):
41+
G_in = tf.placeholder(tf.float32, [None, N_IDEAS]) # random ideas (could from normal distribution)
42+
G_art = tf.concat((G_in, art_labels), 1) # combine ideas with labels
43+
G_l1 = tf.layers.dense(G_art, 128, tf.nn.relu)
44+
G_out = tf.layers.dense(G_l1, ART_COMPONENTS) # making a painting from these random ideas
45+
46+
with tf.variable_scope('Discriminator'):
47+
real_in = tf.placeholder(tf.float32, [None, ART_COMPONENTS], name='real_in') # receive art work from the famous artist + label
48+
real_art = tf.concat((real_in, art_labels), 1) # art with labels
49+
D_l0 = tf.layers.dense(real_art, 128, tf.nn.relu, name='l')
50+
prob_artist0 = tf.layers.dense(D_l0, 1, tf.nn.sigmoid, name='out') # probability that the art work is made by artist
51+
# reuse layers for generator
52+
G_art = tf.concat((G_out, art_labels), 1) # art with labels
53+
D_l1 = tf.layers.dense(G_art, 128, tf.nn.relu, name='l', reuse=True) # receive art work from a newbie like G
54+
prob_artist1 = tf.layers.dense(D_l1, 1, tf.nn.sigmoid, name='out', reuse=True) # probability that the art work is made by artist
55+
56+
D_loss = -tf.reduce_mean(tf.log(prob_artist0) + tf.log(1-prob_artist1))
57+
G_loss = tf.reduce_mean(tf.log(1-prob_artist1))
58+
59+
train_D = tf.train.AdamOptimizer(LR_D).minimize(
60+
D_loss, var_list=tf.get_collection(tf.GraphKeys.TRAINABLE_VARIABLES, scope='Discriminator'))
61+
train_G = tf.train.AdamOptimizer(LR_G).minimize(
62+
G_loss, var_list=tf.get_collection(tf.GraphKeys.TRAINABLE_VARIABLES, scope='Generator'))
63+
64+
sess = tf.Session()
65+
sess.run(tf.global_variables_initializer())
66+
67+
plt.ion() # something about continuous plotting
68+
plt.show()
69+
for step in range(7000):
70+
artist_paintings, labels = artist_works() # real painting from artist
71+
G_ideas = np.random.randn(BATCH_SIZE, N_IDEAS)
72+
G_paintings, pa0, Dl = sess.run([G_out, prob_artist0, D_loss, train_D, train_G], # train and get results
73+
{G_in: G_ideas, real_in: artist_paintings, art_labels: labels})[:3]
74+
75+
if step % 50 == 0: # plotting
76+
plt.cla()
77+
plt.plot(PAINT_POINTS[0], G_paintings[0], c='#4AD631', lw=3, label='Generated painting',)
78+
bound = [0, 0.5] if labels[0, 0] == 0 else [0.5, 1]
79+
plt.plot(PAINT_POINTS[0], 2 * np.power(PAINT_POINTS[0], 2) + bound[1], c='#74BCFF', lw=3, label='upper bound')
80+
plt.plot(PAINT_POINTS[0], 1 * np.power(PAINT_POINTS[0], 2) + bound[0], c='#FF9359', lw=3, label='lower bound')
81+
plt.text(-.5, 2.3, 'D accuracy=%.2f (0.5 for D to converge)' % pa0.mean(), fontdict={'size': 15})
82+
plt.text(-.5, 2, 'D score= %.2f (-1.38 for G to converge)' % -Dl, fontdict={'size': 15})
83+
plt.text(-.5, 1.7, 'Class = %i' % int(labels[0, 0]), fontdict={'size': 15})
84+
plt.ylim((0, 3))
85+
plt.legend(loc='upper right', fontsize=12)
86+
plt.draw()
87+
plt.pause(0.1)
88+
89+
plt.ioff()
90+
91+
# plot a generated painting for upper class
92+
plt.figure(2)
93+
z = np.random.randn(1, N_IDEAS)
94+
label = np.array([[1.]]) # for upper class
95+
G_paintings = sess.run(G_out, {G_in: z, art_labels: label})
96+
plt.plot(PAINT_POINTS[0], G_paintings[0], c='#4AD631', lw=3, label='G painting for upper class',)
97+
plt.plot(PAINT_POINTS[0], 2 * np.power(PAINT_POINTS[0], 2) + bound[1], c='#74BCFF', lw=3, label='upper bound (class 1)')
98+
plt.plot(PAINT_POINTS[0], 1 * np.power(PAINT_POINTS[0], 2) + bound[0], c='#FF9359', lw=3, label='lower bound (class 1)')
99+
plt.ylim((0, 3))
100+
plt.legend(loc='upper right', fontsize=12)
101+
plt.show()

tutorial-contents/501_dropout.py

+84
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
"""
2+
Know more, visit my Python tutorial page: https://morvanzhou.github.io/tutorials/
3+
My Youtube Channel: https://www.youtube.com/user/MorvanZhou
4+
5+
Dependencies:
6+
tensorflow: 1.1.0
7+
matplotlib
8+
numpy
9+
"""
10+
import tensorflow as tf
11+
import numpy as np
12+
import matplotlib.pyplot as plt
13+
14+
tf.set_random_seed(1)
15+
np.random.seed(1)
16+
17+
# Hyper parameters
18+
N_SAMPLES = 20
19+
N_HIDDEN = 300
20+
LR = 0.01
21+
22+
# training data
23+
x = np.linspace(-1, 1, N_SAMPLES)[:, np.newaxis]
24+
y = x + 0.3*np.random.randn(N_SAMPLES)[:, np.newaxis]
25+
26+
# test data
27+
test_x = x.copy()
28+
test_y = test_x + 0.3*np.random.randn(N_SAMPLES)[:, np.newaxis]
29+
30+
# show data
31+
plt.scatter(x, y, c='magenta', s=50, alpha=0.5, label='train')
32+
plt.scatter(test_x, test_y, c='cyan', s=50, alpha=0.5, label='test')
33+
plt.legend(loc='upper left')
34+
plt.ylim((-2.5, 2.5))
35+
plt.show()
36+
37+
# tf placeholders
38+
tf_x = tf.placeholder(tf.float32, [None, 1])
39+
tf_y = tf.placeholder(tf.float32, [None, 1])
40+
tf_is_training = tf.placeholder(tf.bool, None) # to control dropout when training and testing
41+
42+
# overfitting net
43+
o1 = tf.layers.dense(tf_x, N_HIDDEN, tf.nn.relu)
44+
o2 = tf.layers.dense(o1, N_HIDDEN, tf.nn.relu)
45+
o_out = tf.layers.dense(o2, 1)
46+
o_loss = tf.losses.mean_squared_error(tf_y, o_out)
47+
o_train = tf.train.AdamOptimizer(LR).minimize(o_loss)
48+
49+
# dropout net
50+
d1 = tf.layers.dense(tf_x, N_HIDDEN, tf.nn.relu)
51+
d1 = tf.layers.dropout(d1, rate=0.5, training=tf_is_training) # drop out 50% of inputs
52+
d2 = tf.layers.dense(d1, N_HIDDEN, tf.nn.relu)
53+
d2 = tf.layers.dropout(d2, rate=0.5, training=tf_is_training) # drop out 50% of inputs
54+
d_out = tf.layers.dense(d2, 1)
55+
d_loss = tf.losses.mean_squared_error(tf_y, d_out)
56+
d_train = tf.train.AdamOptimizer(LR).minimize(d_loss)
57+
58+
sess = tf.Session()
59+
sess.run(tf.global_variables_initializer())
60+
61+
plt.ion() # something about plotting
62+
plt.show()
63+
64+
for t in range(500):
65+
sess.run([o_train, d_train], {tf_x: x, tf_y: y, tf_is_training: True}) # train, set is_training=True
66+
67+
if t % 10 == 0:
68+
# plotting
69+
plt.cla()
70+
o_loss_, d_loss_, o_out_, d_out_ = sess.run(
71+
[o_loss, d_loss, o_out, d_out], {tf_x: test_x, tf_y: test_y, tf_is_training: False} # test, set is_training=False
72+
)
73+
plt.scatter(x, y, c='magenta', s=50, alpha=0.3, label='train')
74+
plt.scatter(test_x, test_y, c='cyan', s=50, alpha=0.3, label='test')
75+
plt.plot(test_x, o_out_, 'r-', lw=3, label='overfitting')
76+
plt.plot(test_x, d_out_, 'b--', lw=3, label='dropout(50%)')
77+
plt.text(0, -1.2, 'overfitting loss=%.4f' % o_loss_, fontdict={'size': 20, 'color': 'red'})
78+
plt.text(0, -1.5, 'dropout loss=%.4f' % d_loss_, fontdict={'size': 20, 'color': 'blue'})
79+
plt.legend(loc='upper left')
80+
plt.ylim((-2.5, 2.5))
81+
plt.pause(0.1)
82+
83+
plt.ioff()
84+
plt.show()

0 commit comments

Comments
 (0)