-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathlayers.py
49 lines (33 loc) · 1.24 KB
/
layers.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
import numpy as np
def sigmoid(eta):
return 1. / (1. + np.exp(-eta))
def draw_bernoulli(m):
""" Returns array A of same shape as m with A[x] = True with probability m[x] """
return np.random.rand(*m.shape) < m
class Layer(object):
def __init__(self, size, initial_bias=None):
self.size = size
if initial_bias is not None:
assert(initial_bias.size == size)
self.bias = initial_bias
else:
self.bias = np.zeros(size)
def expectation(self, activations):
raise NotImplementedError("No expectation method defined")
def sample(self, activations):
raise NotImplementedError("No sampling method defined")
def gradient(self, positive, negative):
return positive.sum(axis=0) - negative.sum(axis=0)
def gradient_update(self, update):
self.bias += update
def __repr__(self):
return str(self.bias)
def __getitem__(self, key):
return self.bias[key]
class BinaryLayer(Layer):
def expectation(self, activations):
return sigmoid(activations + self.bias)
def sample(self, activations):
return draw_bernoulli(self.expectation(activations))
def sample_exp(self, exp):
return draw_bernoulli(exp)