Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Keras: Custom layer without inputs

I want to implement a Keras custom layer without any input, just trainable weights.

Here is the code so far:

class Simple(Layer):

    def __init__(self, output_dim, **kwargs):
       self.output_dim = output_dim
       super(Simple, self).__init__(**kwargs)

    def build(self):
       self.kernel = self.add_weight(name='kernel', shape=self.output_dim, initializer='uniform', trainable=True)
       super(Simple, self).build()  

    def call(self):
       return self.kernel

    def compute_output_shape(self):
       return self.output_dim

X = Simple((1, 784))()

I am getting an error message:

__call__() missing 1 required positional argument: 'inputs'

Is there a workaround for building a custom layer without inputs in Keras?

like image 207
nairouz mrabah Avatar asked Mar 21 '19 14:03

nairouz mrabah


People also ask

When should you create a custom layer versus a custom model?

If you are building a new model architecture using existing keras/tf layers then build a custom model. If you are implementing your own custom tensor operations with in a layer, then build a custom layer.

How do I create a custom layer in Tensorflow?

Implementing custom layers Layer class and implementing: __init__ , where you can do all input-independent initialization. build , where you know the shapes of the input tensors and can do the rest of the initialization. call , where you do the forward computation.

What is model subclassing?

In Model Sub-Classing there are two most important functions __init__ and call. Basically, we will define all the trainable tf. keras layers or custom implemented layers inside the __init__ method and call those layers based on our network design inside the call method which is used to perform a forward propagation.


1 Answers

You can do the following,

from tensorflow.keras.layers import Layer

class Simple(Layer):

    def __init__(self, output_dim, **kwargs):
       self.output_dim = output_dim
       super(Simple, self).__init__(**kwargs)

    def build(self, input_shapes):
       self.kernel = self.add_weight(name='kernel', shape=self.output_dim, initializer='uniform', trainable=True)
       super(Simple, self).build(input_shapes)  

    def call(self, inputs):
       return self.kernel

    def compute_output_shape(self):
       return self.output_dim

X = Simple((1, 784))([])
print(X.shape)

Which produces

>>> (1, 784)
like image 79
thushv89 Avatar answered Nov 10 '22 20:11

thushv89