-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathshared.py
More file actions
44 lines (35 loc) · 1.35 KB
/
Copy pathshared.py
File metadata and controls
44 lines (35 loc) · 1.35 KB
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
import sys
from types import ModuleType
from multiprocessing.sharedctypes import Array
from ctypes import c_double
import numpy as np
temp_module_name = '__hogwildsgd__temp__'
class SharedWeights:
""" Class to create a temporary module with the gradient function inside
to allow multiprocessing to work for async weight updates.
"""
def __init__(self, size_w):
coef_shared = Array(c_double,
(np.random.normal(size=(size_w,1)) * 1./np.sqrt(size_w)).flat,
lock=False)
w = np.frombuffer(coef_shared)
w = w.reshape((len(w),1))
self.w = w
def __enter__(self, *args):
# Make temporary module to store shared weights
mod = ModuleType(temp_module_name)
mod.__dict__['w'] = self.w
sys.modules[mod.__name__] = mod
self.mod = mod
return self
def __exit__(self, *args):
# Clean up temporary module
del sys.modules[self.mod.__name__]
def mse_gradient_step(X, y, learning_rate):
""" Gradient for mean squared error loss function. """
w = sys.modules[temp_module_name].__dict__['w']
# Calculate gradient
err = y.reshape((len(y),1))-np.dot(X,w)
grad = -2.*np.dot(np.transpose(X),err)/ X.shape[0]
for index in np.where(abs(grad) > .01)[0]:
w[index] -= learning_rate*grad[index,0]