python类init函数及call函数简单用法
本博客用于本人自身学习及记录,如有错误,欢迎指正~
init函数相当于构造函数,下面是ResNet一个小模块类的init函数
def __init__(self, filter_num, stride=1):
super(BasicBlock, self).__init__()
self.conv1 = layers.Conv2D(filter_num, (3, 3), strides=stride, padding='same')
self.bn1 = layers.BatchNormalization()
self.relu = layers.Activation('relu')
self.conv2 = layers.Conv2D(filter_num, (3, 3), strides=1, padding='same')
self.bn2 = layers.BatchNormalization()
if stride != 1:
self.downsample = Sequential()
self.downsample.add(layers.Conv2D(filter_num, (1, 1), strides=stride))
else:
self.downsample = lambda x:x
python类中的call函数作用
call函数可以把类当作函数来调用,例如:
class Demo():
def __init__(self, name):
self.name = name
def __call__(self):
print(self.name)
Demo('这里是泡芙的博客')() # 输出:这里是泡芙的博客