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):
super().__init__(**kwargs)
self.output_dim = output_dim
# betas is either initializer object or float
if isinstance(betas, Initializer):
self.betas_initializer = betas
else:
self.betas_initializer = Constant(value=betas)
self.initializer = initializer if initializer else RandomUniform(0.0, 1.0)
self.count = 0
def build(self, input_shape):
self.centers = self.add_weight(name='centers',
shape=(self.output_dim, input_shape[1]),
initializer=self.initializer,
trainable=True)
self.betas = self.add_weight(name='betas',
shape=(self.output_dim,),
initializer=self.betas_initializer,
# initializer='ones',
trainable=True)
# super(RBFLayer, self).build(input_shape)
def call(self, x):
C = tf.expand_dims(self.centers, -1) # inserts a dimension of 1
H = tf.transpose(C - tf.transpose(x)) # matrix of differences
return tf.exp(-self.betas * tf.math.reduce_sum(H ** 2, axis=1))
# C = tf.expand_dims(self.centers, -1)
# XC = tf.transpose(tf.transpose(x) - C)
# D = tf.expand_dims(tf.sqrt(tf.reduce_mean(XC ** 2, axis=0)), 0)
# H = XC / D
# return tf.exp(-self.betas * tf.reduce_sum(H ** 2, axis=1))
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().get_config()
return dict(list(base_config.items()) + list(config.items()))
:tensorflow:5 out of the last 13 calls to <function Model.make_test_function.<locals>.test_function at 0x000002A8D0BB3E50> triggered tf.function retracing. Tracing is expensive and the excessive number of tracings could be due to (1) creating @tf.function repeatedly in a loop, (2) passing tensors with different shapes, (3) passing Python objects instead of tensors. For (1), please define your @tf.function outside of the loop. For (2), @tf.function has reduce_retracing=True option that can avoid unnecessary retracing. For (3), please refer to https://www.tensorflow.org/guide/function#controlling_retracing and https://www.tensorflow.org/api_docs/python/tf/function for more details.
再用tensorflow2 循环构建并训练模型过程时,有时候某个循环就会跳出如上信息,是什么问题,怎么解决?