How to use keras layers in custom keras layer

前端 未结 4 907
野性不改
野性不改 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:47

    This works for me and is clean, concise, and readable.

    import tensorflow as tf
    
    
    class MyDense(tf.keras.layers.Layer):
        def __init__(self, **kwargs):
            super(MyDense, self).__init__(kwargs)
            self.dense = tf.keras.layers.Dense(2, tf.keras.activations.relu)
    
        def call(self, inputs, training=None):
            return self.dense(inputs)
    
    
    inputs = tf.keras.Input(shape=10)
    outputs = MyDense(trainable=True)(inputs)
    model = tf.keras.Model(inputs=inputs, outputs=outputs, name='test')
    model.compile(loss=tf.keras.losses.MeanSquaredError())
    model.summary()
    

    Output:

    Model: "test"
    _________________________________________________________________
    Layer (type)                 Output Shape              Param #   
    =================================================================
    input_1 (InputLayer)         [(None, 10)]              0         
    _________________________________________________________________
    my_dense (MyDense)           (None, 2)                 22        
    =================================================================
    Total params: 22
    Trainable params: 22
    Non-trainable params: 0
    _________________________________________________________________
    

    Note that trainable=True is needed. I have posted a questions about it here.

提交回复
热议问题