I want to use something related to input to init the property in my module, before first iter it is init to zeros in init()
class Model(nn.Module):
def __init__(self):
super(Model, self).__init__()
self.test_var = torch.zeros((1,3))
def forward(self, imgs):
print(self.test_var)
if torch.equal( self.test_var, torch.zeros(self.test_var.shape)):
print('zeros. ')
var = torch.tensor([1,1,1])
self.test_var = var + self.test_var
It should change the value of test_var to [1,1,1] after 1st iter, but I only find the output is enter image description here It means the change of test_var is invalid, and it is set to zero every iter. I'm confused. I guess maybe something wrong with DataParallel, because when I set CUDA_VISIEBLE_DEVICES=0, the question can be fixed.
What piece of code do you use to call the
model.forward()function?Could it be that you reinitialize the model after each epoch?
Also, your
imgsparam is not used in the code you provided.When I initialize the model and call
model.forward()like this......I get the following output that you also expect to come: In the first epoch, the ones are added because both tensors contain only zeros. In the following they are not added because the zero values are not equal to the one values.