-
Notifications
You must be signed in to change notification settings - Fork 0
/
synthetic_gaussian_mapping.py
32 lines (25 loc) · 1.05 KB
/
synthetic_gaussian_mapping.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
import numpy as np
class SyntheticGaussianMapping:
'''generate synthetic feature extractor'''
def __init__(self, params_map):
self.__dict__.update(params_map)
self.initialize_weights()
self.activation = lambda z: np.exp(-z**2)
def initialize_weights(self):
'''initialize the network from normal distribution'''
np.random.seed(self.seed)
self.dims = [self.dim_in, *self.dim_layers, self.dim_out]
self.num_layers = len(self.dims) - 1
self.weights = {}
for l in range(self.num_layers):
self.weights[l] = np.random.randn(self.dims[l] + 1, self.dims[l+1])
def propagate(self, x):
'''propagate input through the network'''
z = np.array(x, ndmin=2)
for l in range(self.num_layers):
z = np.concatenate([np.ones((z.shape[0],1)), z], axis=1)
if l < self.num_layers - 1:
z = self.activation(np.matmul(z, self.weights[l]))
else:
z = np.tanh(np.matmul(z, self.weights[l]))
return z