forked from puzzlelib/PuzzleLib
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Variable.py
57 lines (36 loc) · 1.02 KB
/
Variable.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
from PuzzleLib import Config
from PuzzleLib.Backend import gpuarray
class Variable:
index = 0
def __init__(self, data, name=None, withgrad=True, grad=None, updater=None, postUpdater=None):
if name is None:
self.name = str(type(self).index)
type(self).index += 1
else:
self.name = name
self.data = data
self.updater = updater
if updater is not None:
return
self.postUpdater = postUpdater
self.grad = None
if grad is not None:
self.grad = grad
elif withgrad and not Config.globalEvalMode:
self.grad = gpuarray.zeros(shape=self.data.shape, dtype=self.data.dtype)
self.learnRate, self.momRate = 1.0, 1.0
self.wc = 0.0
@property
def hasUpdater(self):
return self.updater is not None
@property
def hasPostUpdater(self):
return self.postUpdater is not None
def update(self, learnRate):
self.updater(self, learnRate)
def postUpdate(self):
self.postUpdater(self)
def set(self, variable):
self.data.set(variable.data)
if self.grad is not None:
self.grad.set(variable.grad)