-
Notifications
You must be signed in to change notification settings - Fork 24
/
gd.py
668 lines (538 loc) · 24.2 KB
/
gd.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
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
# -*- coding: utf-8 -*-
"""
.. invisible:
_ _ _____ _ _____ _____
| | | | ___| | | ___/ ___|
| | | | |__ | | | |__ \ `--.
| | | | __|| | | __| `--. \
\ \_/ / |___| |___| |___/\__/ /
\___/\____/\_____|____/\____/
Created on Apr 15, 2013
Gradient descent units for **all-to-all** perceptron units with different \
activations.
* :class:`GradientDescent` couples with :class:`veles.znicz.all2all.All2All`
* :class:`GDTanh` couples with :class:`veles.znicz.all2all.All2AllTanh`
* :class:`GDSM` couples with :class:`veles.znicz.all2all.All2AllSoftmax`
* :class:`GDRELU` couples with :class:`veles.znicz.all2all.All2AllRELU`\
(NB: this ReLU is the smooth one from *Krizhevsky, Hinton et al.*,\
not the strict one from CAFFE)
███████████████████████████████████████████████████████████████████████████████
Licensed to the Apache Software Foundation (ASF) under one
or more contributor license agreements. See the NOTICE file
distributed with this work for additional information
regarding copyright ownership. The ASF licenses this file
to you under the Apache License, Version 2.0 (the
"License"); you may not use this file except in compliance
with the License. You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing,
software distributed under the License is distributed on an
"AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
KIND, either express or implied. See the License for the
specific language governing permissions and limitations
under the License.
███████████████████████████████████████████████████████████████████████████████
"""
from __future__ import division
import cuda4py.blas as cublas
import numpy
from zope.interface import implementer
from veles.memory import reshape, Array
from veles.accelerated_units import IOpenCLUnit, ICUDAUnit, INumpyUnit
import veles.ocl_blas as ocl_blas
import veles.znicz.nn_units as nn_units
from collections import namedtuple
FastGDObjects = namedtuple("FastGDObjects", ("learning_rate",
"weights", "bias"))
AdaDeltaGDObjects = namedtuple("AdaDeltaGDObjects", ("momentum",
"weights",
"gweights",
"bias",
"gbias",
"adom", "epsilon"))
AdaGradGDObjects = namedtuple("AdaGradGDObjects", ("epsilon",
"weights",
"bias"))
@implementer(IOpenCLUnit, ICUDAUnit, INumpyUnit)
class GradientDescent(nn_units.GradientDescentBase):
"""Gradient Descent unit for :class:`veles.znicz.all2all.All2All`.
Attributes:
output: assign before `initialize`!
input: assign before `initialize`!
err_output: assign before `initialize`!
weights: assign before `initialize`!
bias: assign before `initialize`!
batch_size: assign before `initialize`!
err_input: updates after `run`
err_outpur: updates after `run`
weights: updates after `run`
bias: updates after `run`
err_input: **creates** within `initialize`
Attributes:
krn_err_input_: OpenCL kernel for matrix multiplication.
krn_weights_: OpenCL kernel for weights update.
krn_err_output_: OpenCL kernel for err_output update.
krn_bias_: OpenCL kernel for bias update.
self.variant_gradient - variant of the method using a gradient
0- old (gradient ->l1l2->add moment->new moment->upd weights)
1- new ( gradient-> new moment->l1l2->
add( adadelta adagard,fast and t.d.)->upd weights)
2- TODO : NESTEROV
3- Sparsing (different ways)
self.variant_moment_gradient -
variant of the method using a moment gradient
gradient_weights_with_moment -not the correct name
may be gradient_weights_with_moment
self.last_minibatch - need of loader
"""
MAPPING = {"all2all"}
SOLVERS = ("momentum", "adagrad", "adadelta", "fast")
@property
def solvers(self):
return self._solvers
@solvers.setter
def solvers(self, arr):
if "adagrad" in arr and "adadelta" in arr:
raise ValueError("This solver is not have adagrad and adadelta")
solvers = set()
for value in arr:
if value not in self.SOLVERS:
raise ValueError(
"This solver is not supported: %s. Select one of %s.",
value, ", ".join(self.SOLVERS))
solvers.add(value)
self._solvers.clear()
self._solvers.update(solvers)
def __init__(self, workflow, **kwargs):
self._solvers = set()
super(GradientDescent, self).__init__(workflow, **kwargs)
s = kwargs.get("solvers", set())
self.solvers = s
self.reduce_size = self.REDUCE_SIZE
self.krn_err_input_ = None
self.krn_weights_ = None
self.krn_err_output_ = None
self.krn_bias_ = None
self.krn_compute_col_sums_ = None
self.krn_err_output_name = None
self.demand("weights")
if self.include_bias:
self.demand("bias")
self.last_minibatch = None
self.variant_gradient = kwargs.get("variant_gradient", True)
self.variant_moment_gradient = (
kwargs.get("variant_moment_gradient", True))
if "fast" in self.solvers:
self.fast = FastGDObjects(kwargs.get("fast_learning_rate", 0.02),
Array(), Array())
if "adadelta" in self.solvers:
self.adadelta = AdaDeltaGDObjects(
kwargs.get("adadelta_momentum", 0.9),
Array(), Array(),
Array(), Array(),
kwargs.get("adadelta_adom", 0.3),
kwargs.get("adadelta_epsilon", 1e-8))
self.adadelta_adom = self.adadelta.adom
if "adagrad" in self.solvers:
self.adagrad = AdaGradGDObjects(
kwargs.get("adagrad_epsilon", 1e-8),
Array(), Array())
self.last_minibatch = kwargs.get("last_minibatch", False)
def initialize(self, device, **kwargs):
if not self.input:
return True
super(GradientDescent, self).initialize(device=device, **kwargs)
if "adadelta" in self.solvers:
for vec in (self.adadelta.weights, self.adadelta.gweights):
vec.reset(numpy.zeros_like(self.weights.mem))
for vec in (self.adadelta.bias, self.adadelta.gbias):
vec.reset(numpy.zeros_like(self.bias.mem))
if "fast" in self.solvers:
self.fast.bias.reset(numpy.zeros_like(self.bias.mem))
self.fast.weights.reset(numpy.zeros_like(self.weights.mem))
if "adagrad" in self.solvers:
self.adagrad.bias.reset(numpy.zeros_like(self.bias.mem))
self.adagrad.weights.reset(numpy.zeros_like(self.weights.mem))
if "fast" in self.solvers:
self.init_vectors(self.fast.weights, self.fast.bias)
if "adadelta" in self.solvers:
self.init_vectors(
self.adadelta.weights,
self.adadelta.gweights,
self.adadelta.bias,
self.adadelta.gbias)
if "adagrad" in self.solvers:
self.init_vectors(self.adagrad.weights, self.adagrad.bias)
if (any(s in self.solvers for s in ("fast", "adagrad", "adadelta")) and
not self.gradient_weights_with_moment):
raise ValueError("Some of the solvers need moment vectors")
def _gpu_init(self, blas_class):
dtype = self.err_output.dtype
self.gemm_ = blas_class.gemm(dtype)
self.np_alpha = numpy.ones(1, dtype=dtype)
self.np_beta = numpy.zeros(1, dtype=dtype)
# The following code is for computing gradient for weight and bias
if not self.need_gradient_weights:
return
self._weights_const = numpy.zeros(16, dtype=dtype)
self._bias_const = numpy.zeros(16, dtype=dtype)
side = self.weights_shape[0]
other = self.weights.size // side
assert side == self.err_output.sample_size
assert other == self.input.sample_size
batch = self.input.shape[0]
defines = {
"H": other,
"Y": side,
"BATCH": batch,
"APPLY_GRADIENT": int(self.apply_gradient),
"ACCUMULATE_GRADIENT": int(self.accumulate_gradient),
"WEIGHTS_TRANSPOSED": int(self.weights_transposed),
"REDUCE_SIZE": self.reduce_size
}
self.sources_["all2all/gradient_descent/weights_update"] = {
"USE_ORTHO": int(bool(self.factor_ortho)),
"USE_MOMENT": int(bool(self.gradient_weights_with_moment))
}
self.sources_["all2all/gradient_descent/bias_update"] = {
"BIAS_SIZE": side,
"OUTPUT_SIZE": batch,
"USE_MOMENT": int(bool(self.gradient_bias_with_moment))
}
self.build_program(defines, "%s_%d_%d_%d" % (
self.__class__.__name__, self.input.shape[0],
self.input.sample_size, self.err_output.sample_size),
dtype=dtype)
self.krn_weights_ = self.get_kernel("weights_update")
self.krn_weights_.set_args(self.weights.devmem,
self.gradient_weights.devmem,
self.accumulated_gradient_weights.devmem,
self.gradient_weights_with_moment.devmem)
if self.include_bias:
self.krn_bias_ = self.get_kernel("bias_update")
self.krn_bias_.set_args(
self.err_output.devmem, self.bias.devmem,
self.gradient_bias.devmem,
self.accumulated_gradient_bias.devmem,
self.gradient_bias_with_moment.devmem)
if self.factor_ortho:
self.krn_compute_col_sums_ = self.get_kernel("compute_col_sums")
self.krn_compute_col_sums_.set_args(self.weights.devmem,
self.col_sums.devmem)
self.krn_weights_.set_arg(13, self.col_sums.devmem)
def ocl_init(self):
ocl_blas.OCLBLAS.attach_to_device(self.device)
self._gpu_init(ocl_blas.OCLBLAS)
if not self.need_gradient_weights:
return
side = self.weights_shape[0]
other = self.weights.size // side
self._global_size_weights = (self.weights.size,)
self._local_size_weights = None
if self.include_bias:
self._global_size_bias = (side * self.reduce_size,)
self._local_size_bias = (self.reduce_size,)
self._global_size_ortho = (other * self.reduce_size,)
self._local_size_ortho = (self.reduce_size,)
def cuda_init(self):
self._gpu_init(cublas.CUBLAS)
if not self.need_gradient_weights:
return
side = self.weights_shape[0]
other = self.weights.size // side
block_size = self.device.suggest_block_size(self.krn_weights_)
self._global_size_weights = (int(numpy.ceil(
self.weights.size / block_size)), 1, 1)
self._local_size_weights = (block_size, 1, 1)
if self.include_bias:
self._global_size_bias = (side, 1, 1)
self._local_size_bias = (self.reduce_size, 1, 1)
self._global_size_ortho = (other, 1, 1)
self._local_size_ortho = (self.reduce_size, 1, 1)
def moment_use(self, gradient_w_moment, grad):
if gradient_w_moment:
if self.variant_moment_gradient:
gradients = (grad +
gradient_w_moment.mem * self.gradient_moment)
else:
gradients = (
(1 - self.gradient_moment) * grad +
gradient_w_moment.mem * self.gradient_moment)
gradient_w_moment.mem[:] = gradients[:]
else:
gradients = grad
return gradients
def apply_gradient_f(self, gradient, vec, transposed):
if self.apply_gradient:
vec.mem += gradient
def numpy_update(self, s):
f_ortho_use = False if s == 'bias' else self.factor_ortho
if s == 'weights':
self.gradient_weights.map_read()
for vec in (self.weights,
self.accumulated_gradient_weights,
self.gradient_weights_with_moment):
vec.map_write()
v_trans = getattr(self, s + "_transposed")
elif s == 'bias':
self.gradient_bias.map_read()
for vec in (self.bias,
self.accumulated_gradient_bias,
self.gradient_bias_with_moment):
vec.map_write()
v_trans = False
vec = getattr(self, s)
grad_vec = getattr(self, "gradient_" + s)
acc_vec = getattr(self, "accumulated_gradient_" + s)
vec_old = getattr(self, "gradient_%s_with_moment" % s)
if "fast" in self.solvers:
f_vec = getattr(self.fast, s)
if "adagrad" in self.solvers:
adagard_vec = getattr(self.adagrad, s)
if "adadelta" in self.solvers:
adadelta_vec = getattr(self.adadelta, s)
adadelta_gvec = getattr(self.adadelta, "g" + s)
lr = self.learning_rate
factor_l12 = self.weights_decay
l1_vs_l2 = self.l1_vs_l2
if self.variant_gradient:
gradient = -nn_units.GradientDescentBase.numpy_gradient_step(
vec.mem, grad_vec.mem, lr, factor_l12, l1_vs_l2, f_ortho_use,
v_trans)
gradient = self.accumulate_gradient_f(acc_vec, gradient)
# if "momentum" in self.solvers:
gradient = self.moment_use(vec_old, gradient)
else:
# it is RNN
gradient = self.accumulate_gradient_f(acc_vec, grad_vec)
gradient = self.moment_use(vec_old, gradient)
gradient = -nn_units.GradientDescentBase.numpy_gradient_step(
vec.mem, gradient, lr, factor_l12, l1_vs_l2, f_ortho_use,
v_trans)
if "adagrad" in self.solvers:
gradient = self.apply_adagrad(adagard_vec, vec_old, gradient)
if "adadelta" in self.solvers:
gradient = self.apply_adadelta(adadelta_vec, adadelta_gvec,
vec_old, gradient)
if "fast" in self.solvers:
self.apply_fast(f_vec, vec_old)
self.apply_gradient_f(gradient, vec, v_trans)
if "fast" in self.solvers and self.apply_gradient and not v_trans:
vec.mem -= f_vec.mem
def apply_fast(self, f_vec, vec_old):
f_vec.mem *= 0.95
f_vec.mem[:] = f_vec + self.fast.learning_rate * vec_old.mem
def apply_adagrad(self, adagard_vec, vec_old, gradient):
adagard_vec.map_write()
adagard_vec.mem += (vec_old.mem ** 2)
adagard_vec.map_read()
gradient *= numpy.sqrt(adagard_vec.mem + self.adagrad.epsilon)
return gradient
def apply_adadelta(self, adadelta_vec, adadelta_gvec, vec_old, gradient):
adadelta_vec.map_write()
adadelta_gvec.map_write()
adadelta_gvec.mem = (self.adadelta.adom * adadelta_gvec.mem +
(1 - self.adadelta.adom) * vec_old.mem ** 2)
s1, s2 = (numpy.sqrt(m.mem + self.adadelta.epsilon)
for m in (adadelta_vec, adadelta_gvec))
gradient *= s1 / s2
adadelta_vec.mem = (self.adadelta_adom * adadelta_vec.mem +
(1 - self.adadelta_adom) * gradient ** 2)
self.adadelta_adom = 0 if (
self.last_minibatch) else self.adadelta.momentum
return gradient
def numpy_weights_update(self):
if not self.need_gradient_weights:
return
self.input.map_read()
self.output.map_read()
self.err_output.map_write()
err_output = reshape(
self.err_output.mem,
[self.err_output.shape[0], self.err_output.sample_size])
inp = reshape(
self.input.mem, [self.input.shape[0], self.input.sample_size])
self.gradient_weights.map_write()
if self.weights_transposed:
numpy.dot(inp.transpose(), err_output, self.gradient_weights.mem)
else:
numpy.dot(err_output.transpose(), inp, self.gradient_weights.mem)
self.numpy_update('weights')
def numpy_bias_update(self):
if not self.need_gradient_weights or not self.include_bias:
return
self.err_output.map_read()
self.gradient_bias.map_write()
self.gradient_bias.mem[:] = self.err_output.mem.sum(axis=0)
self.numpy_update('bias')
def numpy_err_input_update(self):
"""Backpropagate error (will compute err_input).
"""
if not self.need_err_input:
return
self.err_input.map_invalidate()
self.err_output.map_read()
self.weights.map_read()
err_output = reshape(
self.err_output.mem,
[self.err_output.shape[0], self.err_output.sample_size])
err_input = reshape(
self.err_input.mem,
[self.err_input.shape[0], self.err_input.sample_size])
if self.weights_transposed:
bp = numpy.dot(err_output, self.weights.mem.transpose())
else:
bp = numpy.dot(err_output, self.weights.mem)
bp *= self.err_input_alpha
err_input *= self.err_input_beta
err_input += bp
def numpy_run(self):
"""Do gradient descent.
"""
self.numpy_err_output_update()
self.numpy_err_input_update()
self.numpy_weights_update()
self.numpy_bias_update()
self.print_debug_data()
def ocl_run(self):
"""Do gradient descent.
"""
self.gpu_err_output_update()
# TODO(a.kazantsev): remove intel_opencl_workaround flag.
if self.intel_opencl_workaround:
self.numpy_err_input_update()
else:
self.gpu_err_input_update()
self.gpu_weights_update()
self.gpu_bias_update()
self.print_debug_data()
def cuda_run(self):
"""Do gradient descent.
"""
self.gpu_err_output_update()
self.gpu_err_input_update()
self.gpu_weights_update()
self.gpu_bias_update()
self.print_debug_data()
def gpu_err_input_update(self):
if not self.need_err_input:
return
self.unmap_vectors(self.err_output, self.weights, self.err_input)
self.np_alpha[0] = self.err_input_alpha
self.np_beta[0] = self.err_input_beta
self.gemm_(
self.device.blas, cublas.CUBLAS_OP_T
if self.weights_transposed else cublas.CUBLAS_OP_N,
cublas.CUBLAS_OP_N,
self.err_input.sample_size, self.err_output.shape[0],
self.err_output.sample_size,
self.np_alpha, self.weights.devmem, self.err_output.devmem,
self.np_beta, self.err_input.devmem)
def gpu_weights_update(self):
if not self.need_gradient_weights:
return
self.unmap_vectors(self.err_output, self.gradient_weights, self.input)
self.np_alpha[0] = 1.0
self.np_beta[0] = 0.0
if self.weights_transposed:
self.gemm_(
self.device.blas, cublas.CUBLAS_OP_N, cublas.CUBLAS_OP_T,
self.err_output.sample_size, self.input.sample_size,
self.err_output.shape[0],
self.np_alpha, self.err_output.devmem, self.input.devmem,
self.np_beta, self.gradient_weights.devmem)
else:
self.gemm_(
self.device.blas, cublas.CUBLAS_OP_N, cublas.CUBLAS_OP_T,
self.input.sample_size, self.err_output.sample_size,
self.err_output.shape[0],
self.np_alpha, self.input.devmem, self.err_output.devmem,
self.np_beta, self.gradient_weights.devmem)
# Accumulate/apply gradient
super(GradientDescent, self).gpu_weights_update()
class GDSoftmax(GradientDescent):
"""Gradient Descent for :class:`veles.znicz.all2all.All2AllSoftmax`.
We minimize cross-entropy error function for softmax, so gradient descent
is the same as in :class:`veles.znicz.gd.GradientDescent`.
"""
MAPPING = {"softmax"}
class GDTanh(nn_units.GradientDescentWithActivation, GradientDescent):
"""Gradient Descent for
:math:`f(x) = 1.7159 \\tanh(0.6666 s), s = (W x + b)`,
:math:`y = a \cdot \\tanh(b s)`
:math:`f'(s) = (a \\cdot \\tanh(b s))' = a \\cdot \\tanh'(b s) \\cdot b`
:math:`= a (1 - \\tanh^2(b s)) * b = a b - a * b * \\tanh^2(b s)`
:math:`= a b - y * y * b / a = y^2 (-b / a) + (a \\cdot b)`
:math:`z = y^2 (-0.388484177) + 1.14381894`
"""
MAPPING = {"all2all_tanh"}
def numpy_err_output_update(self):
"""Multiply err_output by activation derivative
by s in terms of output.
"""
self.output.map_read()
self.err_output.map_write()
output = self.output.mem
self.err_output.mem *= output * output * (-0.388484177) + 1.14381894
def initialize(self, device, **kwargs):
self.sources_["gradient_descent_tanh"] = {
"ERR_OUTPUT_SIZE": self.err_output.size}
self.krn_err_output_name = "err_y_update"
return super(GDTanh, self).initialize(device=device, **kwargs)
class GDRELU(nn_units.GradientDescentWithActivation, GradientDescent):
"""
Gradient Descent for :math:`f(x) = \\log(1 + \\exp(s))`
:math:`s = (W x + b)`
:math:`y = \\log(1.0 + \\exp(s))`
:math:`f'(s) = \\frac{1}{1 + \\exp(-s)} = 1 - \\exp(-y)`
"""
MAPPING = {"all2all_relu"}
def numpy_err_output_update(self):
"""Multiply err_output by activation derivative by s
in terms of output.
"""
self.output.map_read()
self.err_output.map_write()
output = self.output.mem
self.err_output.mem *= 1.0 - numpy.exp(-output)
def initialize(self, device, **kwargs):
self.sources_["gradient_descent_relu"] = {
"ERR_OUTPUT_SIZE": self.err_output.size}
self.krn_err_output_name = "err_y_update"
return super(GDRELU, self).initialize(device=device, **kwargs)
class GDStrictRELU(nn_units.GradientDescentWithActivation, GradientDescent):
"""Gradient Descent for strict ReLU (like in CAFFE)
:math:`f(x) = \\max(x, 0)`
:math:`f'(s) = \\begin{cases}1 & s > 0 \\\\ 0 & else. \\\\ \\end{cases}`
"""
MAPPING = {"all2all_str"}
def numpy_err_output_update(self):
"""Multiply err_output by activation derivative by s
in terms of output.
"""
self.output.map_read()
self.err_output.map_write()
output = self.output.mem
self.err_output.mem *= numpy.greater(output, 0)
def initialize(self, device, **kwargs):
self.sources_["gradient_descent_strict_relu"] = {
"ERR_OUTPUT_SIZE": self.err_output.size}
self.krn_err_output_name = "err_y_update"
return super(GDStrictRELU, self).initialize(device=device, **kwargs)
class GDSigmoid(nn_units.GradientDescentWithActivation, GradientDescent):
"""Gradient Descent for Sigmoid activation.
"""
MAPPING = {"all2all_sigmoid"}
def numpy_err_output_update(self):
"""Multiply err_output by activation derivative
by s in terms of output.
"""
self.output.map_read()
self.err_output.map_write()
output = self.output.mem
self.err_output.mem *= output * (1.0 - output)
def initialize(self, device, **kwargs):
self.sources_["gradient_descent_sigmoid"] = {
"ERR_OUTPUT_SIZE": self.err_output.size}
self.krn_err_output_name = "err_y_update"
return super(GDSigmoid, self).initialize(device=device, **kwargs)