首页 > 解决方案 > tensorflow 我们可以对 Conv2D 或 Dense 等层进行子类化吗?

问题描述

我知道可以像这样在 tensorflow 2 中创建自定义层:

class CustomDense(tf.keras.layers.Layer):

    def __init__(self,unit,activation):
        super(CustomDense, self).__init__(name='my_layer')
        self.unit=unit
        self.activation=activation

    def build(self, input_shape):

        self.kernel = self.add_weight(shape=(input_shape[1],self.unit),trainable=Truename='kernel')
        self.bias = self.add_weight(shape=(self.unit,),name='bias')


    def call(self,inputs):
        x=tf.matmul(inputs,self.kernel) + self.bias
        output=self.activation(x)
        return output

但是是否可以创建一个自定义层,其父类是现有层,如 Dense 或 Conv2D?

class CustomDense(tf.keras.layers.Dense):

标签: tensorflow2.0

解决方案


推荐阅读