I have a Neural Network as Shown below.I want to Change  the  weights of Specific Unit in a Layer.
Can Anyone help me in doing that.Tried Couple of Ways but ended up in getting one or other errors as show below.
My Network:
from torch.autograd import Variable
import torch.nn as nn
import torch.nn.functional as F
class Net(nn.Module):
def init(self):
super(Net, self).init()
self.conv1 = nn.Conv2d(3, 6, 5)
self.pool = nn.MaxPool2d(2, 2)
self.conv2 = nn.Conv2d(6, 16, 5)
self.fc1 = nn.Linear(16 * 5 * 5, 120)
self.fc2 = nn.Linear(120, 84)
self.fc3 = nn.Linear(84, 10)
def forward(self, x):
    x = self.pool(F.relu(self.conv1(x)))
    x = self.pool(F.relu(self.conv2(x)))
    x = x.view(-1, 16 * 5 * 5)
    x = F.relu(self.fc1(x))
    x = F.relu(self.fc2(x))
    x = self.fc3(x)
    return x

net = Net()
net1 = Net()
-------------------------------My Net Parameters---------------------
list(net.parameters())
[Parameter containing:
(0 ,0 ,.,.) =
-0.1100  0.0130 -0.0843 -0.0370 -0.1042
-0.0367  0.0072 -0.0065 -0.0693 -0.0167
0.0077  0.0832 -0.0992 -0.0597 -0.0762
-0.0847  0.0508 -0.0456  0.0356  0.0980
-0.0963  0.1076  0.0592 -0.0351  0.0080
(0 ,1 ,.,.) =
-0.0156  0.0566 -0.0963  0.1048 -0.0486
-0.0253  0.0790 -0.0535  0.0253 -0.0657
0.1035  0.0032 -0.1077 -0.0432  0.0934
0.0898 -0.0429  0.0928  0.0699 -0.0923
0.0489 -0.0067 -0.0029  0.0157 -0.0516
(0 ,2 ,.,.) =
0.0106  0.0951 -0.1137  0.0523  0.0359
0.0038 -0.0027  0.1060  0.0237  0.0649
-0.0763 -0.0639  0.0383 -0.0302  0.0918
0.0884  0.0614 -0.0081 -0.0620 -0.1018
-0.0967  0.0398 -0.0124  0.0124 -0.0921
(1 ,0 ,.,.) =
0.0522  0.0783 -0.1055  0.0551  0.0186
0.1088 -0.0123 -0.0914 -0.0519 -0.1009
-0.0276  0.0157 -0.0070 -0.0125  0.0871
-0.0090 -0.0358  0.0671  0.0343  0.0907
-0.0651  0.0638 -0.0926  0.1066 -0.0920
(1 ,1 ,.,.) =
0.0099  0.0280 -0.0231  0.0206 -0.0914
-0.0403 -0.0513  0.0567  0.1030 -0.0065
-0.0921 -0.0461  0.0221  0.0309  0.0035
0.0254 -0.0429 -0.0279 -0.0231  0.0169
-0.0751 -0.0468 -0.0288  0.0291  0.0480
(1 ,2 ,.,.) =
-0.1099 -0.0732 -0.0379 -0.1008 -0.0680
-0.0036 -0.0204 -0.0781 -0.0728  0.0557
0.0366 -0.0821  0.0537  0.0709  0.1103
-0.0869  0.0099  0.0826  0.0694 -0.0517
0.0295 -0.0811  0.0184  0.0451  0.1046
(2 ,0 ,.,.) =
0.0741 -0.0936 -0.0278  0.0159  0.1003
-0.1083  0.0306  0.0030  0.0974 -0.0852
0.0986 -0.0494  0.0264 -0.0380  0.0348
0.0396 -0.0359  0.1048 -0.0521 -0.0761
0.0728  0.1095 -0.1067  0.0511 -0.0712
-------------------------I Want to Update or Change these Weights----Especially net.conv1.weight[0] ----
print(net.conv1.weight[0])
Variable containing:
(0 ,.,.) =
0.0598  0.1560  0.2013  0.2555  0.2412
0.0162 -0.0772 -0.0445 -0.0157  0.1638
0.0557  0.0092 -0.2006 -0.1387 -0.0813
-0.1007 -0.0165 -0.1483 -0.1167 -0.0058
-0.0567  0.0700 -0.0206 -0.1318 -0.0349
(1 ,.,.) =
-0.3358 -0.3046 -0.2996 -0.0561 -0.1415
-0.2289 -0.2550 -0.3419 -0.1695 -0.1451
0.0601 -0.1424 -0.2787 -0.2117 -0.0380
0.0657 -0.0954  0.0011 -0.0948 -0.2423
0.1006 -0.0162 -0.0526 -0.0723 -0.1139
(2 ,.,.) =
-0.0812 -0.0360 -0.1169  0.0640  0.0750
0.0450 -0.0970  0.0353  0.0237  0.1496
0.1201  0.0088  0.0553 -0.0245  0.1247
0.3033  0.2118  0.0703 -0.0622 -0.0904
0.2083  0.2437  0.1262  0.1088  0.0217
[torch.FloatTensor of size 3x5x5]
–Methods Tried to Update and errors -----Method1----------
b=(net.conv1.weight[0]).data
print(b)
b1=torch.FloatTensor(b)
b2=torch.nn.Parameter(b1)
#net.conv1.weight[0]
(net.conv1.weight).index_add_(0,0,b)
Error:
TypeError: index_add_ received an invalid combination of arguments - got (int, int, torch.FloatTensor), but expected (int dim, torch.LongTensor index, torch.FloatTensor source)
------Method 2-----------
import torch.nn as nn
import torch
from torch.nn.parameter import Parameter
b=(net.conv1.weight[0]).data
b1=torch.FloatTensor(b)
bias = Parameter(b1)+1
net.conv1.weight[0]= bias
net.conv1.weight[0]
Error:
RuntimeError: in-place operations can be only used on variables that don’t share storage with any other variables, but detected that there are 3 objects sharing it
—Even Tried Updating single cell but same error----
import torch.nn as nn
import torch
from torch.nn.parameter import Parameter
b=(net.conv1.weight[0,0]).data
b1=torch.FloatTensor(b)
bias = Parameter(b1)+1
net.conv1.weight[0,0]= bias
net.conv1.weight[0,0]
Can anybody let me know how to do it.