when define a new net I find that I would use an object of a nn.Module class for multiple times so I just defined this object one time and use it in every nn.Module as follow:
feature = FeatureExtracter(SUBMODEL)
class Net1(nn.Module):
def forward(self,x):
s_feats =feature(x,LAYER)
......#do something
return ......
class Net2(nn.Module):
def forward(self,x):
s_feats =feature(x,LAYER)
......#do something
return ......
was that a bad programming habit ? or is there any reason in pytorch I shouldn’t do that, and instead define new object in every Net like this:
class Net1(nn.Module):
def __init__(self):
super().__init__()
self.feature = FeatureExtracter(SUBMODEL)
def forward(self,x):
s_feats = self.feature(x,LAYER)
......
return ......
class Net2(nn.Module):
def __init__(self):
super().__init__()
self.feature = FeatureExtracter(SUBMODEL)
def forward(self,x):
s_feats = self.feature(x,LAYER)
......
return ......