How to use keras layers in custom keras layer

前端 未结 4 905
野性不改
野性不改 2021-02-05 15:14

I am trying to write my own keras layer. In this layer, I want to use some other keras layers. Is there any way to do something like this:

class MyDenseLayer(tf.         


        
4条回答
  •  情深已故
    2021-02-05 15:55

    If you look at the documentation for how to add custom layers, they recommend that you use the .add_weight(...) method. This method internally places all weights in self._trainable_weights. So to do what you want, you mush first define the keras layers you want to use, build them, copy the weights and then build your own layer. If I update your code it should be something like

    class mylayer(tf.keras.layers.Layer):
        def __init__(self, num_outputs, num_outputs2):
            self.num_outputs = num_outputs
            super(mylayer, self).__init__()
    
        def build(self, input_shape):
            self.fc = tf.keras.layers.Dense(self.num_outputs)
            self.fc.build(input_shape)
            self._trainable_weights = self.fc.trainable_weights
            super(mylayer, self).build(input_shape)
    
        def call(self, input):
            return self.fc(input)
    
    layer = mylayer(10)
    input = tf.keras.layers.Input(shape=(16, ))
    output = layer(input)
    model = tf.keras.Model(inputs=[input], outputs=[output])
    model.summary()
    

    You should then get what you want

提交回复
热议问题