因此,我目前正在学习如何使用子类化在TensorFlow中创建模型.根据本教程,以下代码片段应该可以完美运行:
#Defining the class
class 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 class
feature_extractor = FeatureExtractor()
func_input = Input(shape=(IMG_SIZE, IMG_SIZE, 3), name="Input_Image")
x = feature_extractor(func_input)
而且它确实运行得无懈可击.但后来我意识到,在__init__()
中,BatchNormalization()
和MaxPool2D()
看起来是一样的,但被定义了两次,而不是被重复使用,所以我做了一些研究和asked on Stackoverflow,得出了when layers are called in 104, they adapt to the dimension of the input, and then keep that dimension for later calls的结论,这阻止了这些层能够被重复使用.
但正如Fynn在我的previous question中指出的那样,maxpool
个成员实际上可以重复使用,而batchnorm
个成员不能.那么,一些层适应输入的维度(比如batchnorm
),而另一些不适应(比如maxpool
)?或者是这些层的某个属性导致了这种行为?(我可以在文档中查找)