Custom Layers

http://d2l.ai/chapter_deep-learning-computation/custom-layer.html

Hello,

As a start, thanks for this nice book.

I have defined the following layer which adds 2:

class add_2(Model):
    def __init__(self):
        #initiate class
        super().__init__()
    
    def call(self, X):
        return X+2

I then defined this layer which adds takes a parameter K as input and adds it:

class add_K(Model):
    def __init__(self, K):
        #initiate class
        super().__init__()
        self.K = K

    def call(self, X):
        return X+self.K

Now, assume I realise I always want to add 2 to layers, I can do that:

class add_2_to_layer(Model):
    def __init__(self, layer):
        #initiate class
        super().__init__()
        self.layer = layer
    
    def call(self, X):
        return self.layer(X)+2

If I do add_2_to_layer(add_K(K=3)) it works fine.

Now, assume I always want to add 2 on add_K, implicitly, without calling add_2_to_layer, then I think I need some kind of ‘‘decorator’’ (though I am not familiar with them). I have tried:

class add_2_to_layer_class(Model):
    def __init__(self, layer):
        #initiate class
        super().__init__()
        self.layer = layer
    
    def call(self, X):
        return self.layer(X)+2

def add_2_to_layer_func(layer):
    return add_2_to_layer_class(layer)
    
@add_2_to_layer_func
class add_K(Model):
    def __init__(self, K):
        #initiate class
        super().__init__()
        self.K = K

    def call(self, X):
        return X+self.K    

and

class add_2_to_layer(Model):
    def __init__(self, layer):
        #initiate class
        super().__init__()
        self.layer = layer
    
    def call(self, X):
        return self.layer(X)+2

@add_2_to_layer
class add_K(Model):
    def __init__(self, K):
        #initiate class
        super().__init__()
        self.K = K

    def call(self, X):
        return X+self.K 

but none of them work.

I could define a new class:

class add_K_with_2(Model):
    def __init__(self, K):
        #initiate class
        super().__init__()
        self.K = K

        self.layer = add_2_to_layer(add_K(K=self.K))

    def call(self, X):
        return self.layer(X)  

but it’s not very elegant nor versatile (if I want to add 2 to another layer type, I have to write another piece of code). Any idea?

1 Like