-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsoftmax.py
241 lines (196 loc) · 7.98 KB
/
softmax.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
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
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
import numpy as np
class SoftmaxRegression(object):
"""Softmax regression classifier.
Parameters
------------
eta : float (default: 0.01)
Learning rate (between 0.0 and 1.0)
epochs : int (default: 50)
Passes over the training dataset.
Prior to each epoch, the dataset is shuffled
if `minibatches > 1` to prevent cycles in stochastic gradient descent.
l2 : float
Regularization parameter for L2 regularization.
No regularization if l2=0.0.
minibatches : int (default: 1)
The number of minibatches for gradient-based optimization.
If 1: Gradient Descent learning
If len(y): Stochastic Gradient Descent (SGD) online learning
If 1 < minibatches < len(y): SGD Minibatch learning
n_classes : int (default: None)
A positive integer to declare the number of class labels
if not all class labels are present in a partial training set.
Gets the number of class labels automatically if None.
random_seed : int (default: None)
Set random state for shuffling and initializing the weights.
Attributes
-----------
w_ : 2d-array, shape={n_features, 1}
Model weights after fitting.
b_ : 1d-array, shape={1,}
Bias unit after fitting.
cost_ : list
List of floats, the average cross_entropy for each epoch.
"""
def __init__(self, eta=0.01, epochs=50,
l2=0.0,
minibatches=1,
n_classes=None,
random_seed=None):
self.eta = eta
self.epochs = epochs
self.l2 = l2
self.minibatches = minibatches
self.n_classes = n_classes
self.random_seed = random_seed
def _fit(self, X, y, init_params=True):
if init_params:
if self.n_classes is None:
self.n_classes = int(np.max(y) + 1)
self._n_features = X.shape[1]
self.b_, self.w_ = self._init_params(
weights_shape=(self._n_features, self.n_classes),
bias_shape=(self.n_classes,),
random_seed=self.random_seed)
self.cost_ = []
y_enc = self._one_hot(y=y, n_labels=self.n_classes, dtype=np.float)
for i in range(self.epochs):
for idx in self._yield_minibatches_idx(
n_batches=self.minibatches,
data_ary=y,
shuffle=True):
# givens:
# w_ -> n_feat x n_classes
# b_ -> n_classes
# net_input, softmax and diff -> n_samples x n_classes:
net = self._net_input(X[idx], self.w_, self.b_)
softm = self._softmax(net)
diff = softm - y_enc[idx]
mse = np.mean(diff, axis=0)
# gradient -> n_features x n_classes
grad = np.dot(X[idx].T, diff)
# update in opp. direction of the cost gradient
self.w_ -= (self.eta * grad +
self.eta * self.l2 * self.w_)
self.b_ -= (self.eta * np.sum(diff, axis=0))
# compute cost of the whole epoch
net = self._net_input(X, self.w_, self.b_)
softm = self._softmax(net)
cross_ent = self._cross_entropy(output=softm, y_target=y_enc)
cost = self._cost(cross_ent)
self.cost_.append(cost)
return self
def fit(self, X, y, init_params=True):
"""Learn model from training data.
Parameters
----------
X : {array-like, sparse matrix}, shape = [n_samples, n_features]
Training vectors, where n_samples is the number of samples and
n_features is the number of features.
y : array-like, shape = [n_samples]
Target values.
init_params : bool (default: True)
Re-initializes model parametersprior to fitting.
Set False to continue training with weights from
a previous model fitting.
Returns
-------
self : object
"""
if self.random_seed is not None:
np.random.seed(self.random_seed)
self._fit(X=X, y=y, init_params=init_params)
self._is_fitted = True
return self
def _predict(self, X):
probas = self.predict_proba(X)
return self._to_classlabels(probas)
def predict(self, X):
"""Predict targets from X.
Parameters
----------
X : {array-like, sparse matrix}, shape = [n_samples, n_features]
Training vectors, where n_samples is the number of samples and
n_features is the number of features.
Returns
----------
target_values : array-like, shape = [n_samples]
Predicted target values.
"""
if not self._is_fitted:
raise AttributeError('Model is not fitted, yet.')
return self._predict(X)
def predict_proba(self, X):
"""Predict class probabilities of X from the net input.
Parameters
----------
X : {array-like, sparse matrix}, shape = [n_samples, n_features]
Training vectors, where n_samples is the number of samples and
n_features is the number of features.
Returns
----------
Class probabilties : array-like, shape= [n_samples, n_classes]
"""
net = self._net_input(X, self.w_, self.b_)
softm = self._softmax(net)
return softm
def _net_input(self, X, W, b):
return (X.dot(W) + b)
def _softmax(self, z):
return (np.exp(z.T) / np.sum(np.exp(z), axis=1)).T
def _cross_entropy(self, output, y_target):
return - np.sum(np.log(output) * (y_target), axis=1)
def _cost(self, cross_entropy):
L2_term = self.l2 * np.sum(self.w_ ** 2)
cross_entropy = cross_entropy + L2_term
return 0.5 * np.mean(cross_entropy)
def _to_classlabels(self, z):
return z.argmax(axis=1)
def _init_params(self, weights_shape, bias_shape=(1,), dtype='float64',
scale=0.01, random_seed=None):
"""Initialize weight coefficients."""
if random_seed:
np.random.seed(random_seed)
w = np.random.normal(loc=0.0, scale=scale, size=weights_shape)
b = np.zeros(shape=bias_shape)
return b.astype(dtype), w.astype(dtype)
def _one_hot(self, y, n_labels, dtype):
"""Returns a matrix where each sample in y is represented
as a row, and each column represents the class label in
the one-hot encoding scheme.
Example:
y = np.array([0, 1, 2, 3, 4, 2])
mc = _BaseMultiClass()
mc._one_hot(y=y, n_labels=5, dtype='float')
np.array([[1., 0., 0., 0., 0.],
[0., 1., 0., 0., 0.],
[0., 0., 1., 0., 0.],
[0., 0., 0., 1., 0.],
[0., 0., 0., 0., 1.],
[0., 0., 1., 0., 0.]])
"""
mat = np.zeros((len(y), n_labels))
for i, val in enumerate(y):
mat[i, int(val)] = 1
return mat.astype(dtype)
def _yield_minibatches_idx(self, n_batches, data_ary, shuffle=True):
indices = np.arange(data_ary.shape[0])
if shuffle:
indices = np.random.permutation(indices)
if n_batches > 1:
remainder = data_ary.shape[0] % n_batches
if remainder:
minis = np.array_split(indices[:-remainder], n_batches)
minis[-1] = np.concatenate((minis[-1],
indices[-remainder:]),
axis=0)
else:
minis = np.array_split(indices, n_batches)
else:
minis = (indices,)
for idx_batch in minis:
yield idx_batch
def _shuffle_arrays(self, arrays):
"""Shuffle arrays in unison."""
r = np.random.permutation(len(arrays[0]))
return [ary[r] for ary in arrays]