-
Notifications
You must be signed in to change notification settings - Fork 0
/
logisticRegression.py
148 lines (125 loc) · 5.77 KB
/
logisticRegression.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
import numpy as np
import theano
import theano.tensor as T
class LogisticRegression(object):
def __init__(self, input, n_in, n_out, W=None, b=None):
self.inp = input
# initialize with 0 the weights W as a matrix of shape (n_in, n_out)
if W is None:
self.W = theano.shared(
value=np.zeros((n_in, n_out), dtype=theano.config.floatX),
name='W')
else:
self.W = W
# initialize the baises b as a vector of n_out 0s
if b is None:
self.b = theano.shared(
value=np.zeros((n_out,), dtype=theano.config.floatX),
name='b')
else:
self.b = b
# compute vector of class-membership probabilities in symbolic form
self.p_y_given_x = T.nnet.softmax(T.dot(input, self.W) + self.b)
# compute prediction as class whose probability is maximal in
# symbolic form
self.y_pred = T.argmax(self.p_y_given_x, axis=1)
# parameters of the model
self.params = [self.W, self.b]
def negative_log_likelihood(self, y):
return -T.mean(T.log(self.p_y_given_x)[T.arange(y.shape[0]), y])
def errors(self, y):
if y.ndim != self.y_pred.ndim:
raise TypeError('y should have the same shape as self.y_pred',
('y', target.type, 'y_pred', self.y_pred.type))
# check if y is of the correct datatype
if y.dtype.startswith('int'):
# the T.neq operator returns a vector of 0s and 1s, where 1
# represents a mistake in prediction
return T.sum(T.neq(self.y_pred, y))
else:
raise NotImplementedError()
def errorsFull(self, y):
if y.ndim != self.y_pred.ndim:
raise TypeError('y should have the same shape as self.y_pred',
('y', target.type, 'y_pred', self.y_pred.type))
# check if y is of the correct datatype
if y.dtype.startswith('int'):
# the T.neq operator returns a vector of 0s and 1s, where 1
# represents a mistake in prediction
return T.neq(self.y_pred, y)
else:
raise NotImplementedError()
def pred_proba_mine(self):
pp = T.nnet.softmax(T.dot(self.inp, self.W) + self.b)
return pp
def getSVM(self):
iii = self.inp
return iii
class LogisticRegression2d(object):
def __init__(self, input, nk, n_in, n_out, W=None, b=None):
self.inp = input
# initialize with 0 the weights W as a matrix of shape (n_in, n_out)
if W is None:
self.W = theano.shared(
value=np.zeros((n_in, n_out), dtype=theano.config.floatX),
name='W')
else:
self.W = W
# initialize the baises b as a vector of n_out 0s
if b is None:
self.b = theano.shared(
value=np.zeros((n_out,), dtype=theano.config.floatX),
name='b')
else:
self.b = b
# compute vector of class-membership probabilities in symbolic form
# self.p_y_given_x = T.nnet.softmax(T.dot(input, self.W) + self.b)
# o, updates = theano.scan(fn = lambda x: T.nnet.softmax(T.dot(x, WSoft) + bSoft),
# outputs_info=None,
# sequences=[self.outputH],
# )
# self.output = o
print input.shape
# in2 = T.tensordot(input.dimshuffle(0,2,1), T.tile(self.W, (19, 1)), axes=[[1,2],[0,1]]) + T.tile(self.b, [19])
o =[ T.nnet.softmax(T.dot(input[z][i], self.W) + self.b) for i in range(19) for z in range(1000)]
# o, updates = theano.scan(fn = lambda x: T.nnet.softmax(T.dot(x.T, self.W) + self.b),
# outputs_info=None,
# sequences= [ in2 ] ,
# )
#o = T.nnet.softmax(T.tensordot(input.dimshuffle(0,2,1), T.tile(self.W, (19, 1)), axes=[[1,2],[0,1]]) + T.tile(self.b, [19]))
self.p_y_given_x = T.mean(o, axis = 2)
# compute prediction as class whose probability is maximal in
# symbolic form
self.y_pred = T.argmax(self.p_y_given_x, axis=1)
# parameters of the model
self.params = [self.W, self.b]
def negative_log_likelihood(self, y):
return -T.mean(T.log(self.p_y_given_x)[T.arange(y.shape[0]), y])
def errors(self, y):
if y.ndim != self.y_pred.ndim:
raise TypeError('y should have the same shape as self.y_pred',
('y', target.type, 'y_pred', self.y_pred.type))
# check if y is of the correct datatype
if y.dtype.startswith('int'):
# the T.neq operator returns a vector of 0s and 1s, where 1
# represents a mistake in prediction
return T.sum(T.neq(self.y_pred, y))
else:
raise NotImplementedError()
def errorsFull(self, y):
if y.ndim != self.y_pred.ndim:
raise TypeError('y should have the same shape as self.y_pred',
('y', target.type, 'y_pred', self.y_pred.type))
# check if y is of the correct datatype
if y.dtype.startswith('int'):
# the T.neq operator returns a vector of 0s and 1s, where 1
# represents a mistake in prediction
return T.neq(self.y_pred, y)
else:
raise NotImplementedError()
def pred_proba_mine(self):
pp = T.nnet.softmax(T.dot(self.inp, self.W) + self.b)
return pp
def getSVM(self):
iii = self.inp
return iii