-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtrain_func.py
43 lines (29 loc) · 931 Bytes
/
train_func.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
"""
Functions for training the Generator and Discriminator
"""
from torch import ones, zeros
from torch.autograd import Variable
def train_d(opt, trueData, falseData):
trueN = trueData.size(0)
falseN = falseData.size(0)
opt.zero_grad()
# Train on true data
truePred = discriminator(trueData)
trueError = loss(truePred, Variable(ones(trueN, 1)))
trueError.backward()
# Train on generated data
falsePred = discriminator(falseData)
falseError = loss(falsePred, Variable(zeros(falseN, 1)))
falseError.backward()
opt.step()
return trueError + falseError
def train_g(opt, falseData):
falseN = falseData.size(0)
opt.zero_grad()
# Get response from Discriminator
pred = discriminator(falseData)
# Pretend that the false data is true data
error = loss(pred, Variable(ones(falseN, 1)))
error.backward()
opt.step()
return error