-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSG.py
25 lines (19 loc) · 778 Bytes
/
SG.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
from topo import numbergen
import numpy
def SG(K,X,Y,lscsm,der,bounds,learning_rate=0.1,num_steps=100,batch_size=100):
num_pres,num_neurons = numpy.shape(X)
r = numbergen.UniformRandom(seed=513)
mins = numpy.array(zip(*bounds)[0])
maxs = numpy.array(zip(*bounds)[1])
for i in xrange(0,num_steps):
K = K - learning_rate*der(K[:])
K = numpy.array(K)
above_bounds = K > maxs
below_bounds = K < mins
if (numpy.sum(above_bounds)+numpy.sum(below_bounds)) > 0:
K[numpy.nonzero(above_bounds)] = maxs[numpy.nonzero(above_bounds)]
K[numpy.nonzero(below_bounds)] = mins[numpy.nonzero(below_bounds)]
index = int(r()*(num_pres-batch_size))
lscsm.X.value = X[index:index+batch_size,:]
lscsm.Y.value = Y[index:index+batch_size,:]
return K