tensorflow2.0-自定义层

tensorflow2.0-自定义层

无参数的自定义层

无参数的自定义层可以使用:keras.layers.Lambda函数

customized_spftplus = keras.layers.Lambda(lambda x : tf.nn.softplus(x))
print(customized_spftplus([1.0,1.0,1.0,0.0,0.1,0.2]))
# tf.Tensor([1.3132616 1.3132616 1.3132616 0.6931472 0.7443967 0.7981388], shape=(6,), dtype=float32)

有参数的自定义层

有参数自定义层需要继承keras.layers.Layer类

	class CustomizedDenseLayer(keras.layers.Layer):
	'''
	 继承的时候必须有三个方法,__init__,build,call
	'''
	# 构造函数需要,继承父类的初始属性,这时候需要用到
	# super(CustomizedDenseLayer, self).__init__(**kwargs)
    def __init__(self, units, activation=None, **kwargs):
        self.units = units
        self.activation = keras.layers.Activation(activation)
        # 
        super(CustomizedDenseLayer, self).__init__(**kwargs)
    
    def build(self, input_shape):
    # 调用父类的方法,add_weight,添加weight
        self.kernel = self.add_weight(name='kernel',
                                      shape=(input_shape[1], self.units),
                                     initializer = 'uniform',
                                     trainable = True)
        self.bias = self.add_weight(name='bias',shape=(self.units,),
                                    initializer = 'zeros',
                                    trainable = True)
        super(CustomizedDenseLayer,self).build(input_shape)
    
    def call(self, x):
        return self.activation(x @ self.kernel + self.bias)

你可能感兴趣的:(Tensorflow)