So I'm currently learning how to create a model in Tensorflow using subclassing. According to the tutorial, the following snippet of code should run perfectly:
#Defining the classclass FeatureExtractor(Layer): def __init__(self): super().__init__() self.conv_1 = Conv2D(filters = 6, kernel_size = 4, padding = "valid", activation = "relu") self.batchnorm_1 = BatchNormalization() self.maxpool_1 = MaxPool2D(pool_size = 2, strides=2) self.conv_2 = Conv2D(filters = 16, kernel_size = 4, padding = "valid", activation = "relu") self.batchnorm_2 = BatchNormalization() self.maxpool_2 = MaxPool2D(pool_size = 2, strides=2) def call(self, x): x = self.conv_1(x) x = self.batchnorm_1(x) x = self.maxpool_1(x) x = self.conv_2(x) x = self.batchnorm_2(x) x = self.maxpool_2(x) return x#Calling and using the classfeature_extractor = FeatureExtractor()func_input = Input(shape=(IMG_SIZE, IMG_SIZE, 3), name="Input_Image")x = feature_extractor(func_input)
And it does indeed run flawlessly. But then I realized that in __init__()
, the BatchNormalization()
and MaxPool2D()
look the same but are defined twice instead of being reused, so I did some research and asked on Stackoverflow and came to the conclusion that when layers are called in call()
, they adapt to the dimension of the input, and then keep that dimension for later calls, which prevents these layers to being able to be reused.
But as Fynn pointed out in my previous question, the maxpool
member can actually be reused, while batchnorm
cannot. So some layers adapt to the dimension of the input (like batchnorm
) and some don't (like maxpool
)? Or is there a property of these layers that causes this behavior? (that I can look up in the documentation)