-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrbflayer.py
103 lines (87 loc) · 3.64 KB
/
rbflayer.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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
# Author: Petra Vidnerova (https://github.com/PetraVidnerova)
# Source: https://github.com/PetraVidnerova/rbf_keras
import random
from keras import backend as K
from keras.engine.topology import Layer
from keras.initializers import RandomUniform, Initializer, Orthogonal, Constant
import numpy as np
class InitCentersRandom(Initializer):
""" Initializer for initialization of centers of RBF network
as random samples from the given data set.
# Arguments
X: matrix, dataset to choose the centers from (random rows
are taken as centers)
"""
def __init__(self, X):
self.X = X
def __call__(self, shape, dtype=None):
print("--------------X is --------------\n",self.X)
print(self.X.shape[1])
print(shape[1])
assert shape[1] == self.X.shape[1]
print(self.X.shape[0])
print(shape[0])
idx = np.random.randint(self.X.shape[0], size=shape[0])
print(idx)
return self.X[idx, :]
class RBFLayer(Layer):
""" Layer of Gaussian RBF units.
# Example
```python
model = Sequential()
model.add(RBFLayer(10,
initializer=InitCentersRandom(X),
betas=1.0,
input_shape=(1,)))
model.add(Dense(1))
```
# Arguments
output_dim: number of hidden units (i.e. number of outputs of the layer)
initializer: instance of initiliazer to initialize centers
betas: float, initial value for betas
"""
def __init__(self, output_dim, initializer=None, betas=1.0, **kwargs):
self.output_dim = output_dim
self.init_betas = betas
if not initializer:
self.initializer = RandomUniform(0.0, 1.0)
# self.initializer = Orthogonal()
else:
self.initializer = initializer
super(RBFLayer, self).__init__(**kwargs)
def build(self, input_shape):
print("in build out dim ",self.output_dim," input shape ",input_shape[1])
print("input shape ", input_shape)
print("weight size should be (",self.output_dim,",", input_shape[1],")")
self.centers = self.add_weight(name='centers',
shape=(self.output_dim, input_shape[-1]),
initializer=self.initializer,
trainable=True)
print("CENTERS ", self.centers)
self.betas = self.add_weight(name='betas',
shape=(self.output_dim,),
initializer=Constant(value=self.init_betas),
# initializer='ones',
trainable=True)
super(RBFLayer, self).build(input_shape)
def call(self, x):
"""
C = K.expand_dims(self.centers)
print("C is ",C)
H = K.transpose(C - K.transpose(x))
return K.exp(-self.betas * K.sum(H ** 2, axis=1))
"""
C = self.centers[np.newaxis, :, :]
X = x[:, np.newaxis, :]
diffnorm = K.sum((C-X)**2, axis=-1)
ret = K.exp( - self.betas * diffnorm)
return ret
def compute_output_shape(self, input_shape):
return (input_shape[0], self.output_dim)
def get_config(self):
# have to define get_config to be able to use model_from_json
config = {
'output_dim': self.output_dim
}
base_config = super(RBFLayer, self).get_config()
return dict(list(base_config.items()) + list(config.items()))