In my CNN at some stage I want to multiply a feature map with some scalar which should be learnt by the network. Which of the following is the best way to do it or all are same? The scalar has to be initialised to 5.
# Method 1
def __init__(self):
super(..., self).__init__()
...
...
alpha = nn.Parameter(5)
...
def forward(self, x):
...
x = x * alpha
return x
# Method 2
def __init__(self):
super(..., self).__init__()
...
...
alpha = nn.Parameter(torch.tensor(5))
...
def forward(self, x):
...
x = x * alpha
return x
# Method 3
def __init__(self):
super(..., self).__init__()
...
...
alpha = nn.Parameter(torch.ones(1)*5)
...
def forward(self, x):
...
x = x * alpha
return x
If all are same I would prefer Method 1 and let CNN learn the appropriate multiplier alpha for the feature map x. I hope in all cases alpha will be a float32 tensor initialised to 5. I am using PyTorch 1.3.1
Yours sincerely,
Mohit