This repository has been archived by the owner on Oct 26, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 7
/
acgan.py
187 lines (163 loc) · 9.23 KB
/
acgan.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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
"""
Auxiliary classifier DCGAN for pecan dataset using Keras
"""
import datetime
import keras
import numpy as np
from keras.layers import Input, Conv1D, LeakyReLU, BatchNormalization, Flatten, Dense, Lambda
from keras.layers import Reshape, Conv2DTranspose, Activation, Embedding, Concatenate
from keras.models import Model
from keras.optimizers import Adam
from keras.utils import to_categorical
from tensorboardX import SummaryWriter
from tqdm import tqdm
from data import date_format_day
from metric import mmd_loss
class ACGAN(object):
def __init__(self, input_dim, window_length, weight_path, code_size=64, learning_date=1e-4,
batch_size=32):
self.input_dim = input_dim
self.code_size = code_size
assert window_length % 8 == 0, 'This DCGAN architecture requires window length to be multiple of 8'
self.window_length = window_length
self.learning_rate = learning_date
self.batch_size = batch_size
self.weight_path = weight_path
self.generator = self._create_generator()
self.discriminator = self._create_discriminator()
self.discriminator_generator = self._combine_generator_discriminator()
def _create_generator(self):
final_window_length = int(self.window_length / 8)
noise = Input(shape=(self.code_size,))
month_label_input = Input(shape=(1,), dtype='int32')
day_label_input = Input(shape=(1,), dtype='int32')
self.month_embedding_layer = Embedding(input_dim=12, output_dim=self.code_size)
self.day_embedding_layer = Embedding(input_dim=7, output_dim=self.code_size)
month_embedding = Flatten()(self.month_embedding_layer(month_label_input))
day_embedding = Flatten()(self.day_embedding_layer(day_label_input))
x = Concatenate(axis=-1)([noise, month_embedding, day_embedding])
x = Dense(final_window_length * 64)(x)
x = Reshape(target_shape=(final_window_length, 1, 64))(x)
x = BatchNormalization()(x)
x = LeakyReLU(alpha=0.2)(x)
x = Conv2DTranspose(filters=32, kernel_size=(4, 1), strides=(2, 1), padding='same')(x)
x = BatchNormalization()(x)
x = LeakyReLU(alpha=0.2)(x)
x = Conv2DTranspose(filters=16, kernel_size=(4, 1), strides=(2, 1), padding='same')(x)
x = BatchNormalization()(x)
x = LeakyReLU(alpha=0.2)(x)
x = Conv2DTranspose(filters=self.input_dim, kernel_size=(4, 1), strides=(2, 1), padding='same')(x)
x = Lambda(lambda x: keras.backend.squeeze(x, axis=-2))(x)
output = Activation('sigmoid')(x)
model = Model(inputs=[noise, month_label_input, day_label_input], outputs=output)
model.compile(optimizer=Adam(lr=self.learning_rate, beta_1=0.5), loss='binary_crossentropy')
return model
def _create_discriminator(self):
time_series_input = Input(shape=(self.window_length, self.input_dim))
x = Conv1D(filters=16, kernel_size=4, strides=2, padding='same')(time_series_input)
x = LeakyReLU(alpha=0.2)(x)
x = Conv1D(filters=32, kernel_size=4, strides=2, padding='same')(x)
x = BatchNormalization()(x)
x = LeakyReLU(alpha=0.2)(x)
x = Conv1D(filters=64, kernel_size=4, strides=2, padding='same')(x)
x = BatchNormalization()(x)
x = LeakyReLU(alpha=0.2)(x)
x = Flatten()(x)
fake = Dense(1, activation='sigmoid')(x)
month_label_output = Dense(12, activation='softmax')(x)
day_label_output = Dense(7, activation='softmax')(x)
model = Model(inputs=time_series_input, outputs=[fake, month_label_output, day_label_output])
model.compile(optimizer=Adam(lr=self.learning_rate, beta_1=0.5, decay=1e-6),
loss=['binary_crossentropy', 'categorical_crossentropy', 'categorical_crossentropy'])
return model
def _combine_generator_discriminator(self):
latent = Input(shape=(self.code_size,))
month_label_input = Input(shape=(1,), dtype='int32')
day_label_input = Input(shape=(1,), dtype='int32')
generated_data = self.generator([latent, month_label_input, day_label_input])
self.discriminator.trainable = False
fake, month_label_output, day_label_output = self.discriminator(generated_data)
combined = Model([latent, month_label_input, day_label_input], [fake, month_label_output, day_label_output])
combined.compile(optimizer=Adam(lr=self.learning_rate, beta_1=0.5, decay=1e-6),
loss=['binary_crossentropy', 'categorical_crossentropy', 'categorical_crossentropy'])
return combined
def train(self, x_train, x_val, num_epoch=5):
summary_writer = SummaryWriter()
self.gen_losses = []
self.dis_losses = []
self.mmd_losses = []
train_samples, month_label, day_label = x_train
num_train = train_samples.shape[0]
step = 0
index_array = np.arange(num_train)
validation_data = x_val
for epoch in range(num_epoch):
np.random.shuffle(index_array)
for i in tqdm(range(num_train // self.batch_size), desc='Epoch {}: '.format(epoch + 1)):
current_index = index_array[i * self.batch_size: (i + 1) * self.batch_size]
# get image
time_series_batch = train_samples[current_index]
# get label
month_label_batch = month_label[current_index]
day_label_batch = day_label[current_index]
# get noise
noise = np.random.normal(0., 1., [self.batch_size, self.code_size])
# get a batch of fake images
generated_time_series = self.generator.predict(
[noise, month_label_batch.reshape((-1, 1)), day_label_batch.reshape((-1, 1))], verbose=0)
soft_zero, soft_one = 0, 0.95
dis_loss_image = self.discriminator.train_on_batch(time_series_batch,
[np.array([soft_one] * self.batch_size),
to_categorical(month_label_batch, 12),
to_categorical(day_label_batch, 7)])
dis_loss_noise = self.discriminator.train_on_batch(
generated_time_series, [[soft_zero] * self.batch_size, to_categorical(month_label_batch, 12),
to_categorical(day_label_batch, 7)])
dis_loss = dis_loss_image + dis_loss_noise
dis_loss = np.sum(dis_loss)
# print(dis_loss)
noise = np.random.normal(0., 1., (2 * self.batch_size, self.code_size))
# get sample labels
month_sampled_labels = np.random.randint(0, 12, 2 * self.batch_size)
day_sampled_labels = np.random.randint(0, 7, 2 * self.batch_size)
trick = np.ones(2 * self.batch_size) * soft_one
gen_loss = self.discriminator_generator.train_on_batch(
[noise, month_sampled_labels, day_sampled_labels],
[trick, to_categorical(month_sampled_labels, 12), to_categorical(day_sampled_labels, 7)])
gen_loss = np.sum(gen_loss)
summary_writer.add_scalars('data/train_loss', {'gen': gen_loss,
'dis': dis_loss},
global_step=step)
step += 1
# sample a batch and calculate mmd loss with validation data
x_val = validation_data[0]
y_val = validation_data[1:3]
x_generated = self.generate(y_val)
mmd_loss_vec = np.zeros(shape=(x_val.shape[-1]))
for j in range(x_val.shape[-1]):
mmd_loss_vec[j] = mmd_loss(x_val[:, :, j], x_generated[:, :, j], weight=1.)
summary_writer.add_scalars('data/mmd_loss', {'load': mmd_loss_vec[0],
'pv': mmd_loss_vec[1]},
global_step=epoch)
self.save_weight()
def _generate(self, x):
return self.generator.predict(x)
def generate(self, labels):
num_samples = labels[0].shape[0]
z = np.random.normal(0, 1, size=[num_samples, self.code_size])
return self._generate([z] + labels)
def generate_by_date(self, num_samples, starting_date_str='2013-01-01'):
month_labels = np.zeros(shape=(num_samples))
day_labels = np.zeros(shape=(num_samples))
starting_date = datetime.datetime.strptime(starting_date_str, date_format_day)
for i in range(num_samples):
current_date = starting_date + datetime.timedelta(i)
month_labels[i] = current_date.month - 1
day_labels[i] = current_date.weekday()
return self.generate([month_labels, day_labels])
def save_weight(self):
self.generator.save_weights(self.weight_path + '_acgan_generator.h5')
self.discriminator.save_weights(self.weight_path + '_acgan_discriminator.h5')
def load_weight(self):
self.generator.load_weights(self.weight_path + '_acgan_generator.h5')
self.discriminator.load_weights(self.weight_path + '_acgan_discriminator.h5')