forked from mansimov/unsupervised-videos
-
Notifications
You must be signed in to change notification settings - Fork 1
/
logreg.py
72 lines (57 loc) · 2.09 KB
/
logreg.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
"""Implementation of logistic regression. Used as a baseline."""
from util import *
class LogReg(object):
def __init__(self, logreg_config):
self.name_ = logreg_config.name
self.num_dims_ = logreg_config.num_inputs
self.num_outputs_ = logreg_config.num_outputs
self.w_ = Param((self.num_outputs_, self.num_dims_), logreg_config.w)
self.b_ = Param((1, self.num_outputs_), logreg_config.b)
self.param_list_ = [
('%s:w' % self.name_, self.w_),
('%s:b' % self.name_, self.b_),
]
self.dropprob_ = logreg_config.dropprob
def __str__(self):
return 'W: %s \n b : %s' % (self.w_.__str__(), self.b_.__str__())
def Load(self, f):
for name, p in self.param_list_:
p.Load(f, name)
def Save(self, f):
for name, p in self.param_list_:
p.Save(f, name)
def SetBatchSize(self, batch_size):
self.o_ = cm.empty((batch_size, self.num_outputs_))
self.o_deriv_ = cm.empty((batch_size, self.num_outputs_))
self.c_ = cm.empty((batch_size, 1))
#self.c_ = cm.empty((batch_size, self.num_outputs_))
def Fprop(self, data, train=False):
if self.dropprob_ and train > 0:
data.dropout(self.dropprob_, scale=1.0/(1-self.dropprob_))
cm.dot(data, self.w_.GetW().T, target=self.o_)
self.o_.add_row_vec(self.b_.GetW())
self.o_.apply_softmax_row_major()
def GetPredictions(self):
return self.o_
def ComputeDeriv(self, t):
self.o_.apply_softmax_grad_row_major(t, target=self.o_deriv_)
#self.o_deriv_.assign(1)
def Bprop(self, deriv):
cm.dot(self.o_deriv_, self.w_.GetW(), target=deriv)
def Outp(self, data):
cm.dot(self.o_deriv_.T, data, target=self.w_.GetdW())
self.o_deriv_.sum(axis=0, target=self.b_.GetdW())
def Update(self):
self.w_.Update()
self.b_.Update()
def GetCorrect(self, t):
self.o_.get_softmax_correct_row_major(t, self.c_)
return self.c_.sum()
def GetLoss(self, t):
""" Linear loss for grad check."""
self.o_.subtract(t, target=self.c_)
return self.c_.sum()
def GetParams(self):
return self.param_list_
def GetOutputDims(self):
return self.num_outputs_