1
2
3
4
5 """
6 Computes a partial step for a specific function at a specific point, acting like a decorator for other steps
7 """
8
9 import random
10 import numpy
11
13 """
14 A partial step
15 """
16 - def __init__(self, step, nb_chunks, indice = None):
17 """
18 Allows only a part of the set of parameters to be updated
19 - step is the step that will be decorated
20 - nb_chunks is the number of chunks in the parameter set
21 - indice is the chunk to update each time. if None, a random indice is drawn each time
22 """
23 self.step = step
24 self.nb_chunks = nb_chunks
25 self.indice = indice
26
27 - def __call__(self, function, point, state):
28 """
29 Computes a step based on a function and a point
30 """
31 step = self.step(function, point, state)
32 state['old_direction'] = step
33 if self.indice is None:
34 indice = random.randint(0, self.nb_chunks - 1)
35 else:
36 indice = self.indice
37 new_step = numpy.zeros(step.shape)
38 new_step.shape = (self.nb_chunks, -1)
39 step.shape = (self.nb_chunks, -1)
40 new_step[indice] = step[indice]
41
42 new_step.shape = -1
43 step.shape = -1
44 state['direction'] = new_step
45 return new_step
46