This repository has been archived by the owner on Nov 17, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
neurons.py
492 lines (388 loc) · 17.5 KB
/
neurons.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
#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""
En este módulo se implementa un esquema de "neuronas" para manejar los arreglos referidos
a pesos sinápticos en las capas de una red neuronal. Ello implica tanto operaciones algebraicas
sobre matrices y vectores como la aplicación de funciones de activación y costo.
"""
__author__ = 'leferrad'
from learninspy.utils.asserts import *
import pyspark.rdd
"""
# Queda experimental ...
class DistributedNeurons(object):
# Vectores son guardados como vectores columna
def __init__(self, mat, shape):
if isinstance(mat, pyspark.rdd.PipelinedRDD):
self.matRDD = mat
else: # mat es un np.array
if len(shape) == 1:
shape += (1,) # Le agrego la dimension que le falta
if shape[1] != 1:
mat = mat.T # Guardo por columnas
if shape[0] == 1:
shape = shape[::-1] # Invierto para que sea vector columna
self.matRDD = sc.parallelize(mat)
self.rows = shape[0]
self.cols = shape[1]
# OPERADORES:
def __mul__(self, other):
return DistributedNeurons(self.matRDD.map(lambda x: x * other), self.shape)
def __div__(self, other):
return DistributedNeurons(self.matRDD.map(lambda x: x / other), self.shape)
def __add__(self, other):
rdd = other.matrix()
return DistributedNeurons(self.matRDD.zip(rdd).map(lambda (x, y): x + y), self.shape)
def __sub__(self, other):
rdd = other.matrix()
return DistributedNeurons(self.matRDD.zip(rdd).map(lambda (x, y): x - y), self.shape)
def __pow__(self, power, modulo=None):
return DistributedNeurons(self.matRDD.map(lambda x: x ** power), self.shape)
# TRANSFORMACIONES:
@assert_sametype
@assert_samedimension
def mul_elemwise(self, rdd):
rdd = rdd.matrix()
return DistributedNeurons(self.matRDD.zip(rdd).map(lambda (x, y): x * y), self.shape)
@assert_sametype
@assert_samedimension
def sum_array(self, rdd):
rdd = rdd.matrix()
return DistributedNeurons(self.matRDD.zip(rdd).map(lambda (x, y): x + y), self.shape)
def activation(self, fun):
return DistributedNeurons(self.matRDD.map(lambda x: fun(x)), self.shape)
@assert_sametype
@assert_samedimension
def mse_d(self, y):
y = y.matrix()
n = self.count()
error = self.matRDD.zip(y).map(lambda (o, t): t - o)
errorDiv = error.map(lambda val: val * (2.0 / n))
return DistributedNeurons(errorDiv, self.shape)
def dropout(self, p):
# Aplicable para vectores unicamente
index = np.random.rand(*self.shape) > p
indexRDD = sc.parallelize(index) # Vector RDD de bools, que indican que valor "tirar"
# Notar que se escala el resultado diviendo por p
# Retorno arreglo con dropout, y el vector con indices de las unidades "tiradas"
zipped = self.matRDD.zip(indexRDD)
dropped = zipped.map(lambda (x, y): (x * y) / p)
return DistributedNeurons(dropped, self.shape), indexRDD
def zip(self, rdd):
return self.matRDD.zip(rdd)
# ACCIONES:
def mul_array(self, array): # Lento
# Ver decoradores para chequear dimensiones validas
if isinstance(array, np.ndarray):
array = DistributedNeurons(array, array.shape)
return self.mul_arrayrdd(array)
def mul_array2(self,array): #Uso operaciones de numpy
if isinstance(array, DistributedNeurons) or isinstance(array, pyspark.rdd.PipelinedRDD):
array = array.collect()
shape = self.rows, array.shape[1]
return DistributedNeurons(self.collect().dot(array), shape)
@assert_sametype
@assert_matchdimension
def mul_arrayrdd(self, rdd):
# Supongo que la matriz que multiplica a rdd esta transpuesta
shape = self.rows, rdd.cols # mul es un np.ndarray
rdd = rdd.matrix()
zipped = self.matRDD.zip(rdd)
mapped = zipped.map(lambda (col, row): np.outer(col, row))
mul = mapped.reduce(lambda x, y: x + y)
if shape[1] == 1: # Si es vector columna
mul = mul.reshape(-1) # lo redimensiono para que sea 1D
return DistributedNeurons(mul, shape)
@assert_sametype
@assert_matchdimension
def mul_arrayrdd2(self, rdd): # No anda bien pq devuelve un vector fila (en el reduce)
# Supongo que la matriz que multiplica a rdd esta transpuesta
shape = self.rows, rdd.cols # mul es un np.ndarray
rdd = rdd.matrix()
zipped = self.matRDD.zip(rdd)
mapped = zipped.map(lambda (col, row): (1, np.outer(col, row)))
mul = mapped.reduceByKey(lambda x, y: x + y).map(lambda (k, v): v)
if shape[1] == 1: # Si es vector columna, redimensiono para que sea 1D
mul = mul.map(lambda col: col.reshape(-1))
return DistributedNeurons(mul, shape)
def outer(self, array):
if isinstance(array, DistributedNeurons) or isinstance(array, pyspark.rdd.PipelinedRDD):
array = array.collect()
mat = self.collect()
res = np.outer(mat, array)
shape = res.shape
return DistributedNeurons(res, shape)
@assert_sametype
@assert_samedimension
def dot(self, rdd):
rdd = rdd.matrix()
return self.mul_elemwise(rdd).sum()
def l1(self):
gradient = self.matRDD.map(lambda x: np.sign(x))
cost = self.matRDD.map(lambda x: abs(x).sum()).sum()
return cost, DistributedNeurons(gradient, self.shape)
def l2(self):
gradient = self.matRDD
cost = self.matRDD.map(lambda x: (x ** 2).sum()).sum()
return cost, DistributedNeurons(gradient, self.shape)
def loss(self, fun, y):
y = DistributedNeurons(y, y.shape)
return self.mse(y)
def loss_d(self, fun, y):
y = DistributedNeurons(y, y.shape)
return self.mse_d(y)
@assert_sametype
@assert_samedimension
def mse(self, y):
y = y.matrix()
n = self.count()
error = self.matRDD.zip(y).map(lambda (o, t): (t - o) ** 2)
sumError = error.sum()
return sumError / (1.0 * n)
def softmax(self):
# Uso de tip de implementacion (http://ufldl.stanford.edu/wiki/index.php/Exercise:Softmax_Regression)
maxmat = self.matRDD.max()
matminus = self.matRDD.map(lambda x: x - maxmat)
exp = np.exp
matexp = matminus.map(lambda o: exp(o))
sumexp = matexp.sum()
softmat = matexp.map(lambda o: o / sumexp)
return DistributedNeurons(softmat, self.shape)
def collect(self):
return np.array(self.matRDD.collect()).T # Se transpone para retornar por filas
def sum(self):
return self.matRDD.sum()
# MISC
def persist(self):
self.matRDD.cache()
return
def unpersist(self):
self.matRDD.unpersist()
return
@property
def shape(self):
return self.rows, self.cols
def count(self):
rows, cols = self.shape
return rows * cols
def matrix(self):
return self.matRDD
"""
# ----------------------------
class LocalNeurons(object):
"""
Clase principal para representar los pesos sinápticos **W** de una red neuronal
y el vector de bias **b**. Provee funcionalidades algebraicas para operar matrices y vectores,
así como también normas regularizadoras y la aplicación de funciónes de activación y de costo.
No obstante, esta clase es usada directamente por :class:`.NeuralLayer`, por lo cual
no es herramienta de libre utilidad.
.. note:: Es preciso aclarar que su estructuración se debe a que está pensada para ser compatible con su par *DistributedNeurons*, pero que en esta versión se encuentra inhabilitada.
:param mat: numpy.array o list o pyspark.rdd.RDD, que corresponde a la matriz **W**
o vector **b** a alojar para operar.
:param shape: tuple, que corresponde a la dimensión que debe tener *mat*. Útil sólo cuando se
utilizan arreglos distribuidos.
>>> shape = (5,3)
>>> w = np.asarray(np.random.uniform(low=-np.sqrt(6.0 / (shape[0] + shape[1])), \ ...
>>> high=+np.sqrt(6.0 / (shape[0] + shape[1])),
>>> size=shape), dtype=np.dtype(float))
>>> weights = LocalNeurons(w, shape)
"""
# NOTA: Vectores son guardados como vectores columna
def __init__(self, mat, shape):
if isinstance(mat, pyspark.rdd.RDD):
mat = mat.collect()
if len(shape) == 1:
shape += (1,) # Le agrego la dimension que le falta
if isinstance(mat, list):
mat = np.array(mat)
self.matrix = mat.reshape(shape)
self.rows = shape[0]
self.cols = shape[1]
# ### Operadores ###
def __mul__(self, other):
if isinstance(other, LocalNeurons):
other = other.matrix
return LocalNeurons(self.matrix * other, self.shape)
def __div__(self, other):
if isinstance(other, LocalNeurons):
other = other.matrix
return LocalNeurons(self.matrix / other, self.shape)
def __sub__(self, other):
if isinstance(other, LocalNeurons):
other = other.matrix
return LocalNeurons(self.matrix - other, self.shape)
def __add__(self, other):
if isinstance(other, LocalNeurons):
other = other.matrix
return LocalNeurons(self.matrix + other, self.shape)
def __pow__(self, power, modulo=None):
return LocalNeurons(self.matrix ** power, self.shape)
def __eq__(self, other):
return np.array_equiv(self.matrix, other)
#def __sizeof__(self):
# return self.matrix.nbytes + sys.getsizeof(self.rows) + sys.getsizeof(self.cols) # TODO: mejorar estimación
# ### Operaciones algebraicas ###
@assert_matchdimension
def mul_array(self, array):
"""
Realiza el producto matricial entre el arreglo alojado *self.matrix* y *array*.
:param array: numpy.array o :class:`~learninspy.core.neurons.LocalNeurons`
:return: :class:`~learninspy.core.neurons.LocalNeurons`
"""
if isinstance(array, LocalNeurons):
array = array.matrix
arrshape = array.shape
if len(arrshape) == 1:
arrshape += (1,) # Le agrego la dimension que le falta
shape = self.rows, arrshape[1]
return LocalNeurons(self.matrix.dot(array), shape)
def outer(self, array):
"""
Producto exterior entre vectores. Equivalente a *numpy.outer*.
:param array: numpy.array o :class:`~learninspy.core.neurons.LocalNeurons`
:return: :class:`~learninspy.core.neurons.LocalNeurons`
"""
if isinstance(array, LocalNeurons):
array = array.matrix
res = np.outer(self.matrix, array)
shape = res.shape
return LocalNeurons(res, shape)
@assert_samedimension
def mul_elemwise(self, array):
"""
Producto elemento a elemento con *array*. Equivalente a utilizar *numpy.multiply* entre dos arreglos.
:param array: numpy.array o :class:`~learninspy.core.neurons.LocalNeurons`
:return: :class:`~learninspy.core.neurons.LocalNeurons`
.. note:: el arreglo *array* debe tener igual dimensión que el arreglo *self.matrix* alojado.
"""
if isinstance(array, LocalNeurons):
array = array.matrix
return LocalNeurons(np.multiply(self.matrix, array), self.shape)
@assert_samedimension
def sum_array(self, array):
"""
Suma elemento a elemento con *array*.
:param array: numpy.array o :class:`~learninspy.core.neurons.LocalNeurons`
:return: :class:`~learninspy.core.neurons.LocalNeurons`
.. note:: el arreglo *array* debe tener igual dimensión que el arreglo *self.matrix* alojado.
"""
if isinstance(array, LocalNeurons):
array = array.matrix
return LocalNeurons(self.matrix + array, self.shape)
def sum(self, axis=None):
"""
Suma de los elementos del arreglo alojado. Equivalente a *numpy.array.sum*
:param axis: None o int o tuple de ints, optional. Indicando dimensión/es a tomar en la suma.
:return: numpy.array
"""
return np.sum(self.matrix, axis=axis)
def transpose(self):
"""
Transpone el arreglo alojado en la instancia. Equivale a *numpy.array.transpose()*.
:return: :class:`~learninspy.core.neurons.LocalNeurons`
"""
return LocalNeurons(self.matrix.transpose(), self.shape[::-1])
# ### Funciones de activación ###
def activation(self, fun):
"""
Aplica una función de activación *fun* sobre cada entrada del arreglo *self.matrix* alojado.
:param fun: función soportada en :mod:`~learninspy.core.activations`.
:return: :class:`~learninspy.core.neurons.LocalNeurons`
"""
return LocalNeurons(map(lambda x: fun(x), self.matrix), self.shape)
def softmax(self):
"""
Aplica la función *Softmax* sobre el vector alojado.
Ver más info en Wikipedia: `Softmax function <https://en.wikipedia.org/wiki/Softmax_function>`_
:return: numpy.ndarray
"""
# Uso de tip de implementacion (http://ufldl.stanford.edu/wiki/index.php/Exercise:Softmax_Regression)
x = self.matrix
x = x - max(x) # Se previene llegar a valores muy grandes del exp(x)
exp_x = np.exp(x)
softmat = exp_x / sum(exp_x)
return LocalNeurons(softmat, self.shape)
# ### Funciones de error ###
def loss(self, fun, y):
"""
Aplica una función de error entre el vector almacenado y la entrada *y*.
:param fun: función soportada en :mod:`~learninspy.core.loss`
:param y: list o numpy.ndarray
:return: float
"""
return fun(self.matrix, y)
def loss_d(self, fun, y):
"""
Aplica una función derivada de error entre el vector almacenado y el vector *y*.
:param fun: función derivada soportada en :mod:`~learninspy.core.loss`
:param y: list o numpy.ndarray
:return: :class:`~learninspy.core.neurons.LocalNeurons`
"""
return LocalNeurons(fun(self.matrix, y), self.shape)
# ### Normas de regularización sobre pesos ###
def dropout(self, p, seed=123):
"""
Aplica DropOut [srivastava2014dropout]_ sobre el arreglo alojado, anulando sus elementos con una probabilidad *p*.
El resultado es un arreglo con entradas aleatoriamente anuladas con probabilidad *p*,
y la máscara o arreglo binario de igual dimensión que el anterior,
en donde se almacena *1* en las entradas donde respectivamente se aplicó DropOut y *0* en el resto.
:param p: float, tal que :math:`0<p<1`
:return: tuple de :class:`~learninspy.core.neurons.LocalNeurons`, numpy.ndarray
**Referencias**:
.. [srivastava2014dropout] Srivastava, N., Hinton, G., Krizhevsky, A., Sutskever, I., & Salakhutdinov, R. (2014):
"Dropout: A simple way to prevent neural networks from overfitting".
The Journal of Machine Learning Research, 15(1), 1929-1958.
"""
np.random.seed(seed)
# Aplicable para vectores unicamente
index = np.random.rand(*self.shape) > p # Vector de bools, que indican que valor "tirar"
# Notar que se escala el resultado diviendo por p
# Retorno arreglo con dropout, y el vector con indices de las unidades "tiradas"
dropped = self.matrix * index / p
return LocalNeurons(dropped, self.shape), index
def l1(self):
"""
Norma **L1** sobre la matriz almacenada.
Se retorna una tupla con el resultado y además el gradiente de dicha norma.
:math:`L_1(W)=\displaystyle\sum\limits_{i}^{n_{rows}} \sum\limits_{j}^{n_{cols}} |W_{i,j}|, \quad
\\dfrac{\partial}{\partial W_{i,j}} L_1(W)= sign(W_{i,j})`
:return: tuple de float, :class:`~learninspy.core.neurons.LocalNeurons`
.. note:: El cálculo no suele aplicarse a un vector de bias *b*,
ya que afecta poco en el resultado final.
"""
cost = sum(sum(abs(self.matrix)))
gradient = np.sign(self.matrix) # en x=0 no es diferenciable, pero que sea igual a 0 la derivada anda bien
return cost, LocalNeurons(gradient, self.shape)
def l2(self):
"""
Norma **L2** sobre la matriz almacenada.
Se retorna una tupla con el resultado y además el gradiente de dicha norma.
:math:`L_2(W)=\\dfrac{1}{2} \displaystyle\sum\limits_{i}^{n_{rows}} \sum\limits_{j}^{n_{cols}} (W_{i,j})^2, \quad
\\dfrac{\partial}{\partial W_{i,j}} L_2(W)= W_{i,j}`
:return: tuple de float, :class:`~learninspy.core.neurons.LocalNeurons`
.. note:: El cálculo no suele aplicarse a un vector de bias *b*,
ya que afecta poco en el resultado final.
"""
cost = sum(sum(self.matrix ** 2)) * 0.5 # Multiplico por 0.5 para hacer mas simple el gradiente
gradient = self.matrix
return cost, LocalNeurons(gradient, self.shape)
# ### Misc ###
@property
def shape(self):
"""
Dimensiones del arreglo alojado.
:return: tuple
"""
return self.rows, self.cols
def count(self):
"""
Cantidad de elementos de la matriz almacenada. Siendo MxN las dimensiones, retorna el producto de ambas.
:return: int
"""
return self.rows * self.cols
def collect(self):
"""
Retorna el arreglo alojado.
:return: numpy.ndarray
"""
return self.matrix