-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathloss.py
executable file
·100 lines (69 loc) · 2.35 KB
/
loss.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
#!/usr/bin/env python
import sys
sys.path.append("/u/lambalex/DeepLearning/dreamprop/lib")
import theano
import theano.tensor as T
class ConsiderConstant(theano.compile.ViewOp):
def grad(self, args, g_outs):
return [T.zeros_like(g_out) for g_out in g_outs]
consider_constant = ConsiderConstant()
def cast(inp):
return T.cast(inp, 'float32')
def crossent(p,y):
return -T.mean(T.log(p)[T.arange(y.shape[0]), y])
def nll(p,y,n):
return -T.sum(T.log(p)*expand(y,n))
def accuracy(p,y):
return T.mean(cast(T.eq(cast(T.argmax(p, axis = 1)),cast(y))))
def expand(y,n):
return T.extra_ops.to_one_hot(y, n)
def clip_updates(updates, params):
new_updates = []
for (p,u) in updates.items():
if p in params:
print "UPDATING P IN PARAMS"
u = T.clip(u, -0.01, 0.01)
new_updates.append((p,u))
return new_updates
def lsgan_loss(D_q_lst, D_p_lst, bs=True):
dloss = 0.0
gloss = 0.0
max_len = max(len(D_q_lst), len(D_p_lst))
for i in range(len(D_p_lst)):
D_p = D_p_lst[i]
dloss += T.mean(T.sqr(0.0 - D_p))
gloss += T.mean(T.sqr(1.0 - D_p))
for i in range(len(D_q_lst)):
D_q = D_q_lst[i]
dloss += T.mean(T.sqr(1.0 - D_q))
gloss += T.mean(T.sqr(0.0 - D_q))
return dloss / max_len, gloss / max_len
def improvement_loss(D1lst, D2lst):
new_loss = 0.0
for i in range(len(D1lst)):
D1 = D1lst[i]
D2 = D2lst[i]
print "loss - just push up D2"
new_loss += T.mean(T.switch(T.lt(D2,D1)*T.lt(D2,0.9),-1.0*D2,0.0))
new_loss += T.mean(T.switch(T.gt(D2,1.0), 1.0*D2, 0.0))
return new_loss
def wgan_loss(D_q_lst, D_p_lst):
dloss = 0.0
gloss = 0.0
#for dloss, push up D_p and push down D_q
#for gloss, push up D_q and push down D_p
for i in range(len(D_q_lst)):
D_q = D_q_lst[i]
D_p = D_p_lst[i]
dloss += T.mean(D_q) + T.mean(-1.0 * D_p)
gloss += T.mean(D_p) + T.mean(-1.0 * D_q)
return dloss / len(D_q_lst), gloss / len(D_q_lst)
if __name__ == "__main__":
p = T.matrix()
y = T.ivector()
f = theano.function([p,y], [crossent(p,y), nll(p,y)], allow_input_downcast=True)
print f([[0.9,0.1],[0.1,0.9]], [0,0])
print f([[0.9,0.1]], [1])
print f([[0.7,0.3]], [0])
print f([[0.5,0.5]], [0])
print f([[0.1,0.9]], [0])