-
Notifications
You must be signed in to change notification settings - Fork 0
/
gan.py
135 lines (118 loc) · 4.58 KB
/
gan.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
# train a generative adversarial network on a one-dimensional function
from numpy import hstack
import numpy as np
from numpy import zeros
from numpy import ones
from numpy.random import rand
from numpy.random import randn
from keras.models import Sequential
from keras import Input
from keras.layers import Dense, LSTM
from matplotlib import pyplot
import matplotlib.pyplot as plt
LENGTH_INPUT = 300
# define the standalone discriminator model
def define_discriminator(n_inputs=LENGTH_INPUT):
model = Sequential()
model.add(Dense(LENGTH_INPUT, activation="relu", input_dim=n_inputs))
model.add(Dense(250, activation="relu", input_dim=n_inputs))
model.add(Dense(100, activation="relu"))
model.add(Dense(1, activation="sigmoid"))
# compile model
model.compile(loss="binary_crossentropy", optimizer="adam", metrics=["accuracy"])
return model
# define the standalone generator model
def define_generator(latent_dim, n_outputs=LENGTH_INPUT):
model = Sequential()
model.add(Input(shape=(latent_dim, 1)))
model.add(LSTM(150))
model.add(Dense(LENGTH_INPUT, activation="linear"))
model.compile(
loss="mean_absolute_error", optimizer="adam", metrics=["mean_absolute_error"]
)
return model
# define the combined generator and discriminator model, for updating the generator
def define_gan(generator, discriminator):
# make weights in the discriminator not trainable
discriminator.trainable = False
# connect them
model = Sequential()
model.add(generator)
model.add(discriminator)
model.compile(loss="binary_crossentropy", optimizer="adam")
return model
# generate n real samples with class labels
def generate_real_samples(n):
amps = np.arange(0.1, 10, 0.1)
bias = np.arange(0.1, 10, 0.1)
freqs = np.linspace(1, 2, 1000)
X2 = np.linspace(-5, 5, LENGTH_INPUT)
X1 = []
for x in range(n):
noise = np.random.normal(size=len(X2))
X1.append(
np.random.choice(amps) * np.sin(X2 * np.random.choice(freqs))
+ np.random.choice(bias)
+ 0.3 * noise
)
X1 = np.array(X1).reshape(n, LENGTH_INPUT)
# generate class labels
y = ones((n, 1))
return X1, y
# generate points in latent space as input for the generator
def generate_latent_points(latent_dim, n):
# generate points in the latent space
x_input = randn(latent_dim * n)
# reshape into a batch of inputs for the network
x_input = x_input.reshape(n, latent_dim)
return x_input
# use the generator to generate n fake examples, with class labels
def generate_fake_samples(generator, latent_dim, n):
# generate points in latent space
x_input = generate_latent_points(latent_dim, n)
# predict outputs
X = generator.predict(x_input, verbose=0)
# create class labels
y = zeros((n, 1))
# print(x_input)
return X, y
# train the generator and discriminator
def train(
g_model, d_model, gan_model, latent_dim, n_epochs=10000, n_batch=128, n_eval=200
):
# determine half the size of one batch, for updating the discriminator
half_batch = int(n_batch / 2)
# manually enumerate epochs
for i in range(n_epochs):
# prepare real samples
x_real, y_real = generate_real_samples(half_batch)
# prepare fake examples
x_fake, y_fake = generate_fake_samples(g_model, latent_dim, half_batch)
# update discriminator
d_model.train_on_batch(x_real, y_real)
d_model.train_on_batch(x_fake, y_fake)
# prepare points in latent space as input for the generator
x_gan = generate_latent_points(latent_dim, n_batch)
# create inverted labels for the fake samples
y_gan = ones((n_batch, 1))
# update the generator via the discriminator's error
gan_model.train_on_batch(x_gan, y_gan)
# evaluate the model every n_eval epochs
if (i + 1) % n_eval == 0:
plt.title("Number of epochs = %i" % (i + 1))
pred_data = generate_fake_samples(generator, latent_dim, latent_dim)[0]
real_data = generate_real_samples(latent_dim)[0]
plt.plot(pred_data[0], ".", label="Random Fake Sample", color="firebrick")
plt.plot(real_data[0], ".", label="Random Real Sample", color="navy")
plt.legend(fontsize=10)
plt.show()
# size of the latent space
latent_dim = 3
# create the discriminator
discriminator = define_discriminator()
# create the generator
generator = define_generator(latent_dim)
# create the gan
gan_model = define_gan(generator, discriminator)
# train model
train(generator, discriminator, gan_model, latent_dim)