It seems that I cannot call any additional functions or attributes. Would packaging (init.py) have anything to do with this? Seems weird that I cannot access any function or attribute besides the ones from superclass.
What I am ultimately trying to do is to have different weight initialization every time I create a model. That is why I tried to add the attribute. Since I cannot, I tried creating a global variable. However, I cannot import this variable in the main script and increment it.
For that I have weights_init() in my model script my_model.py
def weights_init(m, act_type='relu'):
global initialization_counter
classname = m.__class__.__name__
# 0: normal_
# 1: xavier_uniform_
if initialization_counter == 0:
print("Initializing weights using normal distribution.")
if classname.find('Conv') != -1:
if act_type == 'selu':
n = float(m.in_channels * m.kernel_size[0] * m.kernel_size[1])
m.weight.data.normal_(0.0, 1.0 / math.sqrt(n))
else:
m.weight.data.normal_(0.0, 0.02)
if hasattr(m.bias, 'data'):
m.bias.data.fill_(0)
elif classname.find('BatchNorm2d') != -1:
m.weight.data.normal_(1.0, 0.02)
m.bias.data.fill_(0)
if initialization_counter == 1:
print("Initializing weights using xavier uniform distribution.")
if classname.find('Conv') != -1:
init.xavier_uniform_(m.weight, gain=nn.init.calculate_gain('relu'))
if hasattr(m.bias, 'data'):
m.bias.data.fill_(0)
elif classname.find('BatchNorm2d') != -1:
m.weight.data.normal_(1.0, 0.02)
m.bias.data.fill_(0)
Executing this script by itself is working (I mean in the main method of this script).
However, in the learn.py I cannot import this global variable. Getting this error:
from models.my_model import net, weights_init, initialization_counter
ImportError: cannot import name 'initialization_counter' from 'models.my_model'
Based on your initial description this was the original issue while you are now trying to include global variables from other scripts if I understand your last description correctly.
Why don’t you use the model attribute as it seems to work?