【发布时间】:2021-06-19 09:13:02
【问题描述】:
我想实现一个 RBFN,并在 StackOverflow 上找到了这段代码。虽然我确实理解了一些代码,但我不明白 gamma、kwargs 和整个 call 函数是什么。
谁能给我解释一下?
from keras.layers import Layer
from keras import backend as K
class RBFLayer(Layer):
def __init__(self, units, gamma, **kwargs):
super(RBFLayer, self).__init__(**kwargs)
self.units = units
self.gamma = K.cast_to_floatx(gamma)
def build(self, input_shape):
self.mu = self.add_weight(name='mu',
shape=(int(input_shape[1]), self.units),
initializer='uniform',
trainable=True)
super(RBFLayer, self).build(input_shape)
def call(self, inputs):
diff = K.expand_dims(inputs) - self.mu
l2 = K.sum(K.pow(diff,2), axis=1)
res = K.exp(-1 * self.gamma * l2)
return res
def compute_output_shape(self, input_shape):
return (input_shape[0], self.units)
【问题讨论】:
标签: python tensorflow machine-learning deep-learning neural-network