-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathGAN.py
More file actions
193 lines (116 loc) · 5.03 KB
/
GAN.py
File metadata and controls
193 lines (116 loc) · 5.03 KB
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
188
189
190
191
192
#!/usr/bin/env python
# coding: utf-8
# # CE-40719: Deep Learning
# ## HW5 - GAN (100 points)
#
# #### Name:
# #### Student No.:
# ### 1) Import Libraries
# In[ ]:
import numpy as np
import torch
import torch.nn as nn
import torch.nn.functional as F
import torch.optim as optim
from torchvision import datasets, transforms
import matplotlib.pyplot as plt
get_ipython().run_line_magic('matplotlib', 'inline')
plt.rcParams['figure.figsize'] = (10, 3) # set default size of plots
# ### 2) Loading Dataset (10 points)
#
# In this notebook, you will use `MNIST` dataset to train your GAN. You can see more information about this dataset [here](http://yann.lecun.com/exdb/mnist/). This dataset is a 10 class dataset. It contains 60000 grayscale images (50000 for train and 10000 for test or validation) each with shape (3, 28, 28). Every image has a corresponding label which is a number in range 0 to 9.
# In[ ]:
# MNIST Dataset
train_dataset = datasets.MNIST(root='./mnist/', train=True, transform=transforms.ToTensor(), download=True)
test_dataset = datasets.MNIST(root='./mnist/', train=False, transform=transforms.ToTensor(), download=True)
# In[ ]:
# Device configuration
device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
################ Problem 01 (5 pts) ################
# define hyper parameters
batch_size = None
d_lr = None
g_lr = None
n_epochs = None
####################### End ########################
z_dim = 100
# In[ ]:
################ Problem 02 (5 pts) ################
# Define Dataloaders
train_loader = None
test_loader = None
####################### End ########################
train_loader = torch.utils.data.DataLoader(dataset=train_dataset, batch_size=batch_size, shuffle=True)
test_loader = torch.utils.data.DataLoader(dataset=test_dataset, batch_size=batch_size, shuffle=False)
# ### 3) Defining Network (30 points)
# At this stage, you should define a network that improves your GAN training and prevents problems such as mode collapse and vanishing gradients.
# In[ ]:
class Discriminator(nn.Module):
def __init__(self):
super().__init__()
self.discriminator = nn.Sequential(
################ Problem 03 (15 pts) ################
# use linear or convolutional layer
# use arbitrary techniques to stabilize training
####################### End ########################
)
def forward(self, x):
return self.discriminator(x)
class Generator(nn.Module):
def __init__(self):
super().__init__()
self.generator = nn.Sequential(
################ Problem 04 (15 pts) ################
# use linear or convolutional layer
# use arbitrary techniques to stabilize training
####################### End ########################
)
def forward(self, z):
return self.generator(z)
# ### 4) Train the Network
# At this step, you are going to train your network.
# In[ ]:
################ Problem 05 (5 pts) ################
# Create instances of modules (discriminator and generator)
# don't forget to put your models on device
discriminator = None
generator = None
####################### End ########################
# In[ ]:
################ Problem 06 (5 pts) ################
# Define two optimizer for discriminator and generator
d_optimizer = None
g_optimizer = None
####################### End ########################
# In[ ]:
plot_frequency = None
for epoch in range(n_epochs):
for i, (images, labels) in enumerate(train_loader):
################ Problem 07 (15 pts) ################
# put your inputs on device
# Prepare what you need for training, like inputs for modules and variables for computing loss
z = None
####################### End ########################
################ Problem 08 (10 pts) ################
# calculate discriminator loss and update it
d_loss = None
####################### End ########################
################ Problem 09 (10 pts) ################
# calculate generator loss and update it
g_loss = None
####################### End ########################
################ Problem 10 (10 pts) ################
# plot some of the generated pictures based on plot frequency variable
if (epoch % plot_frequency == 0):
pass
####################### End ########################
print("epoch: {} \t discriminator last batch loss: {} \t generator last batch loss: {}".format(epoch + 1,
d_loss.item(),
g_loss.item())
)
# ### 5) Save Generator
# Save your final generator parameters. Upload it with your other files.
# In[ ]:
################ Problem 11 (5 pts) ################
# save state dict of your generator
####################### End ########################