Unable to get nn.Parameter as a parameter in model.parameters

Hi!

I am unable to get the parameter that I had created in the def __init__(self): function the parameter is : self.q_params = nn.Parameter(q_delta * torch.randn(train_Rdepth * n_qubits, requires_grad=True))

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.conv1 = self.conv1.to(device)

        self.pool = nn.MaxPool2d(2, 2)
        #self.pool = self.pool.to(device)

        self.conv2 = nn.Conv2d(6, 8, 6)
        #self.conv2 = self.conv2.to(device)

        self.q_params = nn.Parameter(q_delta * torch.randn(train_Rdepth * n_qubits, requires_grad=True))
        #self.q_params = self.q_params.to(device)

        #print(self.q_params.requires_grad)
        #self.q_params = torch.nn.ModuleList(self.q_params)

        self.fc1 = nn.Linear(1 * 4 * 4, 120)
        self.fc1 = self.fc1.to(device)
        self.fc2 = nn.Linear(120, 84)
        self.fc2 = self.fc2.to(device)
        self.fc3 = nn.Linear(84, 10)
        self.fc3 = self.fc3.to(device)

    def forward(self, x):
        x = self.pool(F.relu(self.conv1(x)))
        x = self.pool(F.relu(self.conv2(x)))
        #print(x.shape)
        #print(type(x))
        #print(x)
        pre_out = x 

        q_in = torch.tanh(pre_out) * np.pi / 2.0   
        
        # Apply the quantum circuit to each element of the batch, and append to q_out
        #q_out = torch.Tensor(0, n_qubits)
        q_out = torch.Tensor(0,1, q_in.shape[2], q_in.shape[3])
        q_out = q_out.to(device)
        for elem in q_in:
            # q_out_elem = q_net(elem,self.q_params).float().unsqueeze(0)
            q_out_elem = torch.from_numpy(q_net(elem,self.q_params)).float()
            q_out = torch.cat((q_out, q_out_elem.to(device).reshape((1,1,q_in.shape[2], q_in.shape[3]))))
       
        x = q_out.to(device)
        x = x.view(-1, 1 * 4 * 4)
        #x = x.to(device)
        #self.hidden = torch.nn.ModuleList(self.hidden)
        x = F.relu(self.fc1(x))
        #x = x.to(device)
        x = F.relu(self.fc2(x))
        x = self.fc3(x)
        return x


net = Net()

The output from net.parameters() is :

<bound method Module.parameters of Net(
  (conv1): Conv2d(3, 6, kernel_size=(5, 5), stride=(1, 1))
  (pool): MaxPool2d(kernel_size=2, stride=2, padding=0, dilation=1, ceil_mode=False)
  (conv2): Conv2d(6, 8, kernel_size=(6, 6), stride=(1, 1))
  (fc1): Linear(in_features=16, out_features=120, bias=True)
  (fc2): Linear(in_features=120, out_features=84, bias=True)
  (fc3): Linear(in_features=84, out_features=10, bias=True)
)>

You can see that the parameter I had created is not present in the output so even if I pass into my :

import torch.optim as optim

criterion = nn.CrossEntropyLoss()
optimizer = optim.SGD(net.parameters(), lr=0.001, momentum=0.9)

So my self.q_params will not train…

Can anybody help to resolve this issue?

The parameter is shown using your code snippet:

device = 'cpu'
net = Net()
print(dict(net.named_parameters()).keys())
> dict_keys(['q_params', 'conv1.weight', 'conv1.bias', 'conv2.weight', 'conv2.bias', 'fc1.weight', 'fc1.bias', 'fc2.weight', 'fc2.bias', 'fc3.weight', 'fc3.bias'])

Thank you @ptrblck But when I used net.named_parameters() in optimizer it is throwing error.

My code:

import torch.optim as optim

criterion = nn.CrossEntropyLoss()

optimizer = optim.SGD(net.named_parameters(), lr=0.001, momentum=0.9)

The error:

TypeError                                 Traceback (most recent call last)
<ipython-input-28-a87dc351f2a6> in <module>()
      2 
      3 criterion = nn.CrossEntropyLoss()
----> 4 optimizer = optim.SGD(net.named_parameters(), lr=0.001, momentum=0.9)

2 frames
/usr/local/lib/python3.7/dist-packages/torch/optim/optimizer.py in add_param_group(self, param_group)
    254             if not isinstance(param, torch.Tensor):
    255                 raise TypeError("optimizer can only optimize Tensors, "
--> 256                                 "but one of the params is " + torch.typename(param))
    257             if not param.is_leaf:
    258                 raise ValueError("can't optimize a non-leaf Tensor")

TypeError: optimizer can only optimize Tensors, but one of the params is tuple

Some results that might help you understand the net model output:

for name, param in net.named_parameters():
  print(name)
  print(param)

The output of above code:

q_params
Parameter containing:
tensor([ 0.0018,  0.0002,  0.0068,  0.0068, -0.0049,  0.0123,  0.0101,  0.0001,
        -0.0109,  0.0065, -0.0125,  0.0142, -0.0052, -0.0076, -0.0177, -0.0220,
         0.0137,  0.0022,  0.0108,  0.0043,  0.0008, -0.0044, -0.0133,  0.0184,
         0.0011,  0.0171, -0.0056,  0.0056,  0.0107, -0.0080,  0.0157, -0.0036],
       device='cuda:0', requires_grad=True)
conv1.weight
Parameter containing:
tensor([[[[ 0.0431, -0.0828, -0.0412,  0.0164,  0.0124],
          [-0.1011,  0.1121, -0.0506, -0.0241,  0.0576],
          [ 0.0298, -0.0694, -0.0125, -0.0784, -0.0376],
          [-0.1059,  0.0874,  0.0784, -0.0475,  0.0240],
          [-0.0520,  0.0776,  0.0590, -0.0345, -0.0362]],

         [[ 0.0240,  0.0388,  0.0400, -0.0685, -0.0319],
          [ 0.0074,  0.0131, -0.0667, -0.0095,  0.0256],
          [-0.0304, -0.0971,  0.0359,  0.0022, -0.0342],
          [-0.1098, -0.0721, -0.0919, -0.1078,  0.0312],
          [-0.1081,  0.0867,  0.0993, -0.0550, -0.0050]],

         [[-0.0367,  0.0076,  0.0426,  0.0485,  0.0879],
          [ 0.0158,  0.0255,  0.0382, -0.0945, -0.1088],
          [ 0.0217, -0.0935,  0.0871,  0.1006, -0.0460],
          [-0.0314, -0.0505,  0.0300, -0.0401,  0.0177],
          [ 0.1089, -0.0622,  0.0887, -0.0106, -0.0237]]],


        [[[-0.1150, -0.0757,  0.0256,  0.0925,  0.0804],
          [-0.0752, -0.0254, -0.1137, -0.0933, -0.0243],
          [-0.1025, -0.0235, -0.0161,  0.0628, -0.0867],
          [ 0.0309,  0.0805, -0.0750, -0.0624, -0.0255],
          [ 0.0007,  0.0371, -0.0163, -0.1120,  0.0472]],

         [[-0.0637,  0.0722,  0.0584, -0.0336, -0.1009],
          [ 0.0831,  0.0167,  0.0679,  0.0911, -0.0115],
          [ 0.0578, -0.0776, -0.0660,  0.0306,  0.0625],
          [ 0.0294,  0.1066, -0.1041, -0.0459, -0.0687],
          [ 0.0327, -0.1017, -0.0522, -0.0799, -0.0361]],

         [[-0.0204, -0.0530, -0.0066,  0.0794,  0.0242],
          [-0.1094,  0.0468,  0.0700, -0.0704, -0.0486],
          [ 0.0546,  0.0378, -0.0420, -0.0705, -0.0414],
          [ 0.0028,  0.0243,  0.1060,  0.0631,  0.0645],
          [-0.0723,  0.0355,  0.0911, -0.1019, -0.0774]]],


        [[[-0.0378, -0.0691,  0.0460,  0.0649,  0.0719],
          [ 0.0630,  0.0958, -0.0972,  0.0035, -0.0016],
          [ 0.0498,  0.0730,  0.0616, -0.1139, -0.0701],
          [ 0.0138, -0.1122, -0.0895, -0.0971, -0.0913],
          [ 0.0270,  0.0755, -0.0932,  0.0789, -0.0097]],

         [[ 0.0351,  0.0020,  0.0666,  0.0151, -0.0547],
          [-0.0951,  0.0673,  0.0058, -0.0824, -0.0577],
          [ 0.0801, -0.0044,  0.0410, -0.0673,  0.0362],
          [-0.0219, -0.0163, -0.0136, -0.0944, -0.0551],
          [-0.0569, -0.0907,  0.0772, -0.0883, -0.0357]],

         [[-0.0702, -0.0381, -0.0585,  0.0325, -0.0561],
          [-0.0840, -0.0026, -0.0280, -0.0403,  0.0784],
          [-0.1137,  0.1119,  0.0611, -0.0655,  0.1097],
          [-0.0815,  0.0952, -0.1085, -0.0152,  0.0047],
          [-0.0774, -0.0856, -0.0024, -0.0688, -0.0274]]],


        [[[-0.0783,  0.0788,  0.1111, -0.0657, -0.0291],
          [-0.0924,  0.0736,  0.0735, -0.0186,  0.0768],
          [-0.0336,  0.1000,  0.0446,  0.1065,  0.0593],
          [-0.0016,  0.0936,  0.0858, -0.0876, -0.1034],
          [ 0.0690,  0.0550,  0.0077,  0.0132,  0.0576]],

         [[ 0.0331, -0.0444, -0.0866, -0.0204, -0.0367],
          [-0.0246, -0.0126, -0.1033, -0.0750,  0.0177],
          [ 0.0388,  0.1009,  0.0467,  0.0514, -0.0192],
          [ 0.1050, -0.0762, -0.0584, -0.0784,  0.0041],
          [ 0.0702,  0.0709,  0.0009,  0.1063, -0.0742]],

         [[-0.1138, -0.0240, -0.0844,  0.0790, -0.0636],
          [-0.1144,  0.0709,  0.0983, -0.0904,  0.0683],
          [ 0.0180, -0.0584,  0.0302, -0.0390, -0.0897],
          [-0.0137,  0.0871,  0.0892, -0.1001, -0.0989],
          [ 0.0241,  0.0002,  0.0786, -0.0495, -0.1068]]],


        [[[ 0.0227, -0.0532,  0.0344,  0.0822,  0.0571],
          [ 0.0465,  0.0887,  0.0244, -0.0198, -0.0102],
          [ 0.0486,  0.0187,  0.1068, -0.0991, -0.0627],
          [ 0.0067,  0.0843,  0.0888,  0.0913, -0.0162],
          [ 0.0369, -0.0050,  0.0854, -0.1059,  0.0267]],

         [[ 0.0310, -0.0762, -0.0202,  0.0553,  0.0460],
          [ 0.1021,  0.0587, -0.0282,  0.0233, -0.0889],
          [-0.0308,  0.0751, -0.0090, -0.0200,  0.0947],
          [ 0.0590, -0.0139,  0.0122, -0.1143,  0.0752],
          [-0.0661,  0.0842,  0.0760,  0.0791,  0.0676]],

         [[ 0.0770,  0.0869, -0.0506,  0.0633,  0.0680],
          [ 0.1060,  0.0151,  0.0547,  0.0650,  0.0197],
          [ 0.0168, -0.0241,  0.0306, -0.0844, -0.0925],
          [ 0.0472, -0.0848,  0.0161, -0.0722,  0.0682],
          [-0.0940, -0.0730,  0.1104, -0.0245,  0.0300]]],


        [[[ 0.0764, -0.0742,  0.0558,  0.0783, -0.0205],
          [ 0.0772,  0.1005,  0.0931, -0.0677,  0.1049],
          [ 0.0029, -0.0797, -0.0954,  0.0399, -0.0105],
          [ 0.0584, -0.0909, -0.0228,  0.0935, -0.0538],
          [-0.0760, -0.0572,  0.0847,  0.0124, -0.1028]],

         [[ 0.0992, -0.0520, -0.0934, -0.0901, -0.0275],
          [ 0.0616,  0.0023,  0.1001, -0.0259,  0.0392],
          [-0.0395, -0.0769,  0.0077,  0.0185,  0.0992],
          [-0.0960,  0.0842,  0.0160,  0.0919, -0.0965],
          [-0.0485,  0.1026,  0.0697,  0.0309, -0.0570]],

         [[ 0.0271, -0.0732,  0.0880, -0.0612,  0.0996],
          [ 0.0407, -0.0795,  0.0191,  0.1090,  0.0617],
          [-0.0239, -0.0895, -0.0687, -0.0871, -0.0799],
          [ 0.0723, -0.0348,  0.0854, -0.0281, -0.0608],
          [-0.0726, -0.0566, -0.0446,  0.0345,  0.0424]]]], device='cuda:0',
       requires_grad=True)
conv1.bias
Parameter containing:
tensor([ 0.0738,  0.0443, -0.0615,  0.0343, -0.0381,  0.0755], device='cuda:0',
       requires_grad=True)
conv2.weight
Parameter containing:
tensor([[[[ 3.4021e-02, -8.1530e-03,  4.6262e-04,  3.0741e-02,  2.1451e-02,
            3.4138e-02],
          [-3.5612e-02, -1.6960e-02, -2.0097e-02, -6.4952e-02, -2.1277e-02,
            4.2644e-02],
          [ 3.4957e-02, -3.9221e-02,  4.5643e-02,  1.6085e-03,  6.5308e-02,
           -1.4750e-02],
          [ 4.2648e-02,  5.8520e-02,  5.5542e-02,  1.5110e-02,  4.5127e-02,
            8.3337e-03],
          [-4.4736e-03, -6.3364e-02,  4.3984e-02,  6.5143e-02, -4.5883e-02,
            4.4148e-02],
          [-6.1665e-02,  3.9649e-02, -3.1362e-02, -6.7043e-02, -8.7041e-03,
           -5.7258e-02]],

         [[-5.9093e-02, -3.9984e-02,  4.2934e-02,  2.6916e-02,  2.9312e-02,
           -9.0438e-03],
          [-6.0938e-02, -4.0273e-03, -1.2030e-02, -1.7449e-02, -5.8476e-02,
            4.8105e-02],
          [ 2.1846e-02,  5.3420e-02,  7.6082e-03,  3.0828e-02, -1.0419e-02,
           -8.6992e-03],
          [-3.7032e-02, -1.1896e-02, -3.9974e-02, -4.8045e-02, -2.7677e-02,
           -7.4075e-03],
          [-4.3165e-02, -7.3700e-03,  1.8909e-02,  5.6602e-02,  6.0600e-02,
            5.1607e-02],
          [-5.4237e-02, -5.7838e-02,  3.2613e-02,  5.2814e-02, -6.2673e-02,
            2.3883e-02]],

         [[ 3.7553e-02, -2.5199e-02, -6.7474e-02,  5.1738e-02, -1.2495e-02,
           -3.0692e-02],
          [-8.8318e-03, -4.2293e-02, -6.6782e-02,  5.2414e-02, -6.2887e-02,
            9.1557e-04],
          [-2.7443e-02,  5.0216e-02,  6.0764e-02, -1.1556e-02,  5.1522e-02,
            5.3330e-02],
          [ 3.1449e-02,  1.0108e-02, -2.2822e-02, -1.3821e-02,  4.5250e-02,
            3.6671e-03],
          [-6.1234e-02,  4.5404e-02,  1.6192e-02,  5.7136e-02,  5.0886e-02,
            6.0709e-02],
          [ 4.5057e-03, -2.7105e-02,  5.2065e-02,  5.5814e-02,  2.3365e-02,
            1.4663e-02]],

         [[-4.1800e-02, -2.9898e-03, -6.7015e-02,  1.6291e-02,  7.7644e-03,
            2.1124e-02],
          [-3.7753e-02, -4.6833e-02,  1.8346e-02, -5.4014e-02,  9.3810e-03,
            5.4488e-02],
          [-3.8349e-02,  2.3971e-02, -1.2987e-02,  3.6422e-02, -5.9863e-02,
           -2.9146e-02],
          [-1.3513e-02, -3.8371e-02, -2.0003e-02,  6.2172e-02,  2.6359e-02,
            3.2120e-02],
          [-1.1676e-02, -6.2881e-02, -1.1803e-02, -1.7807e-02,  1.5395e-02,
            6.0106e-02],
          [ 6.6531e-03,  4.8172e-03, -2.2972e-02,  1.6072e-02, -4.4307e-02,
            3.3247e-02]],

         [[ 3.6360e-02,  1.0231e-02, -3.4925e-02,  1.7691e-02, -2.7136e-03,
            6.6460e-02],
          [ 5.6086e-02,  6.4013e-02,  6.7961e-02, -3.6489e-02, -1.8469e-02,
            6.4756e-03],
          [ 1.5573e-02, -3.2893e-02, -3.3775e-02, -5.7747e-02,  1.4172e-02,
            6.1171e-02],
          [-6.7532e-02, -6.0183e-02,  4.7064e-02,  4.0328e-02, -4.4040e-02,
           -3.6295e-03],
          [-1.3500e-02,  5.9019e-02,  6.1489e-02, -5.8316e-02,  4.9221e-03,
           -5.9068e-02],
          [ 6.5703e-02, -2.4619e-02, -5.1524e-03, -3.7909e-02,  3.2536e-02,
            4.7023e-02]],

         [[-4.6377e-02, -9.2006e-04,  5.5396e-02, -1.0831e-02,  2.4205e-02,
           -5.2667e-02],
          [-6.7365e-02, -3.2582e-02, -2.5132e-02, -5.6445e-02, -4.2807e-02,
            4.7557e-02],
          [ 4.2270e-02, -3.9933e-02, -5.9613e-02,  2.4708e-02, -2.5175e-02,
            3.0176e-02],
          [ 5.5319e-02, -5.3412e-02,  6.1620e-02,  5.5952e-02, -3.4110e-02,
           -5.3009e-02],
          [ 5.8944e-02,  4.8874e-04,  9.4294e-03, -5.2722e-02, -2.8208e-02,
            6.4685e-02],
          [-3.7952e-02, -5.1314e-02,  8.0136e-03,  3.7031e-02,  4.6432e-02,
            3.9671e-02]]],


        [[[ 5.8651e-02,  4.1898e-02, -2.9471e-02,  3.7678e-02, -6.2234e-02,
            5.3933e-02],
          [ 1.5744e-02, -3.8757e-02,  7.6668e-03, -4.9580e-02,  4.5845e-02,
           -2.7830e-02],
          [-4.9905e-02, -6.5088e-02, -5.5736e-02, -6.0459e-02, -3.0742e-02,
           -1.3147e-02],
          [-2.6607e-03,  1.1761e-02,  3.3049e-02,  6.1704e-02, -3.4256e-02,
           -3.9880e-02],
          [ 2.2260e-02,  6.5521e-02, -4.9279e-02, -1.8872e-02, -5.6985e-02,
            4.4972e-02],
          [-6.0551e-02, -1.8046e-02,  3.5077e-02, -4.2605e-02,  3.1459e-02,
           -1.9867e-02]],

         [[-4.6360e-02, -4.9482e-02,  4.2976e-02, -5.1743e-03, -6.6319e-02,
           -4.5098e-02],
          [-4.1253e-03, -1.4461e-02,  3.4009e-03,  6.7957e-02,  3.6245e-02,
           -1.4352e-02],
          [ 5.5707e-03, -3.0379e-02, -4.2180e-02, -1.9232e-02,  5.7133e-02,
            3.3216e-03],
          [ 6.0146e-02, -5.3574e-02, -6.8312e-03,  4.4925e-02,  3.4638e-02,
           -3.8007e-02],
          [-2.5674e-03, -2.4308e-02, -3.1558e-02,  2.5169e-02, -5.3324e-02,
            6.0365e-02],
          [ 6.2735e-02, -1.7691e-02, -4.8957e-02,  2.9018e-02, -1.1731e-02,
           -4.1415e-02]],

         [[-5.1690e-02, -1.7597e-03, -6.4901e-02,  4.1923e-02,  1.8556e-02,
            5.8412e-02],
          [ 3.9794e-02,  6.5335e-02, -2.7487e-02, -5.1026e-02,  3.4734e-02,
            5.7003e-02],
          [-2.3167e-02, -5.2969e-02, -2.0822e-02, -4.7864e-02, -4.4006e-02,
            2.6421e-02],
          [ 4.6411e-02,  1.8531e-02, -3.2962e-02, -3.0861e-02,  5.8308e-02,
            2.9809e-02],
          [-1.2799e-02, -1.6351e-02,  8.1764e-03, -3.8940e-02, -2.8578e-02,
            3.7489e-02],
          [ 3.3330e-02, -1.1045e-02, -2.7753e-03, -3.7217e-02,  5.2454e-02,
           -4.8543e-02]],

         [[-4.7015e-02,  3.6067e-02, -5.2694e-02, -4.1495e-02,  1.7116e-02,
           -5.8048e-02],
          [ 1.7880e-02,  5.3420e-02, -3.9910e-02, -1.9712e-02,  5.2425e-02,
           -2.2675e-02],
          [ 2.3855e-02,  2.6532e-02,  4.0023e-02,  3.4148e-02,  5.3037e-02,
           -1.2042e-02],
          [ 3.0454e-02,  5.5629e-03,  5.1930e-02,  6.1931e-02, -2.6818e-02,
           -3.2057e-02],
          [-2.3171e-02, -2.3073e-02, -9.6216e-03,  6.4929e-02,  8.9837e-03,
            3.3352e-02],
          [ 9.1110e-03,  3.9694e-02,  4.1281e-02,  4.1720e-02,  2.1290e-02,
            6.6281e-02]],

         [[-6.6090e-02,  4.0820e-02,  5.9227e-02, -2.4618e-02, -4.8149e-04,
           -1.2643e-02],
          [ 4.7050e-02,  4.8698e-02,  1.2401e-02,  2.0487e-02,  5.4961e-02,
            1.0526e-02],
          [-1.0708e-02, -5.6358e-02,  2.7883e-02, -2.9937e-02, -3.2686e-02,
            3.6615e-04],
          [ 2.8013e-02,  4.0101e-02,  1.3556e-03, -4.4060e-02,  1.9481e-02,
           -4.5195e-02],
          [ 3.4449e-02,  4.7998e-02, -3.9239e-02,  1.6052e-02, -3.7272e-02,
            3.9724e-02],
          [-4.0421e-02,  6.5823e-02,  3.7484e-02, -3.9382e-03,  2.4101e-02,
            2.3868e-02]],

         [[-3.6967e-02,  2.5271e-02,  3.2770e-02,  3.9550e-02,  5.3392e-02,
           -5.5768e-02],
          [ 5.3781e-02,  1.0321e-02, -1.1471e-02,  5.3727e-02, -3.1423e-02,
           -3.9318e-02],
          [-1.6151e-03, -2.2172e-03,  6.4186e-02,  6.4440e-03,  2.5530e-02,
            4.0752e-02],
          [ 2.7171e-02,  1.6856e-02,  1.5860e-04, -6.4760e-02,  5.9463e-02,
           -1.9234e-02],
          [ 2.3627e-02,  1.9661e-03, -3.7440e-02, -5.6950e-02, -4.5346e-02,
           -1.9206e-02],
          [ 3.5384e-02, -4.3581e-03, -5.5281e-02,  1.1943e-02, -4.3813e-02,
            2.5459e-02]]],


        [[[ 5.0282e-02, -4.6607e-02,  6.3649e-02, -6.7850e-02, -2.1214e-02,
            1.5324e-02],
          [ 7.7585e-03, -2.5559e-02, -4.4435e-02,  9.3370e-03,  4.0947e-02,
           -9.3001e-03],
          [ 3.0154e-02,  3.9013e-02, -1.0915e-02,  3.0304e-02,  1.1094e-02,
           -1.8436e-03],
          [-2.4667e-02, -9.2862e-03, -2.3787e-02, -2.0218e-02, -6.4283e-02,
           -1.0397e-02],
          [-5.7685e-02,  2.5345e-03,  5.4018e-02, -3.3478e-02,  4.0567e-02,
            6.0704e-02],
          [-1.0486e-02,  2.1917e-02, -6.5314e-02, -5.8087e-02, -5.1728e-02,
            4.0483e-02]],

         [[ 5.2686e-02, -4.3673e-02,  4.1443e-02,  6.4302e-02, -4.7340e-02,
            3.5576e-02],
          [-6.2619e-02,  6.3953e-02, -2.4049e-02,  2.7150e-02, -2.0687e-02,
            6.0942e-02],
          [ 9.7461e-03,  5.2419e-02,  2.0310e-02,  4.4437e-02, -4.1434e-02,
            3.6659e-02],
          [-2.6101e-02, -2.5870e-02,  1.2478e-02,  4.2969e-03, -5.1419e-03,
            1.2988e-02],
          [ 6.1165e-02, -3.4421e-03,  2.3489e-02, -2.6428e-02, -6.7631e-02,
            3.4123e-02],
          [ 1.5775e-02, -1.6422e-02,  1.6504e-02, -2.6171e-03, -2.5550e-02,
            5.1954e-02]],

         [[-5.5952e-02, -6.2114e-02, -2.9033e-02,  9.7867e-03,  6.0566e-02,
            3.6322e-02],
          [ 4.5390e-03,  4.6046e-02,  2.8275e-02,  4.2587e-02,  5.8608e-02,
           -1.7871e-03],
          [-4.6120e-03,  4.4561e-02,  4.4045e-02,  5.4618e-02,  5.4773e-02,
           -3.6090e-02],
          [-1.3526e-02,  5.8113e-03, -6.7282e-02, -4.3480e-02,  5.0594e-02,
           -3.9919e-03],
          [ 6.0029e-02,  3.5228e-02,  3.2290e-03,  3.7547e-02,  9.0943e-03,
            3.7780e-02],
          [-3.3874e-03, -2.8848e-02,  1.1336e-02, -3.0376e-02,  4.5568e-02,
            5.2692e-02]],

         [[-6.4418e-02,  1.8007e-02,  5.9143e-02, -5.9627e-02,  4.2518e-02,
           -5.8506e-02],
          [-6.6957e-02, -5.0980e-02,  2.5619e-02,  6.2819e-02, -1.5467e-02,
            3.6354e-02],
          [-6.6236e-02,  1.2732e-02, -6.6256e-02,  4.5470e-02, -4.3355e-02,
            4.0108e-02],
          [-2.9504e-02,  5.6920e-03,  1.2776e-02, -2.7132e-02,  6.5659e-02,
            6.5530e-05],
          [ 2.8102e-02, -1.1201e-02,  1.1171e-02,  5.6588e-02, -5.9475e-02,
            3.2435e-02],
          [-5.4065e-02, -5.5738e-02,  4.9094e-02, -5.5426e-02,  2.2601e-02,
           -1.3511e-04]],

         [[-5.2612e-02, -2.9760e-02,  5.2480e-02,  4.4128e-02, -2.4441e-02,
            4.3765e-02],
          [-5.6266e-02,  1.8057e-02,  3.6927e-02, -5.5090e-02, -3.9980e-02,
            9.5967e-03],
          [ 8.2806e-03,  6.7399e-02, -4.6316e-03,  6.2925e-02,  1.4781e-02,
           -5.4584e-02],
          [-5.7102e-02, -1.4766e-02,  4.8011e-02, -2.6943e-02, -2.4832e-02,
            5.2528e-02],
          [-5.2394e-02,  2.6625e-02,  2.6826e-02, -4.8256e-02,  2.4807e-02,
            6.6693e-02],
          [ 4.8849e-02,  5.1029e-02,  3.2441e-02, -2.8934e-02, -6.3950e-03,
           -2.8721e-02]],

         [[ 6.7883e-02,  4.1604e-02, -1.2163e-02, -5.8261e-03,  1.4964e-02,
            2.3182e-02],
          [ 6.0082e-02,  5.0245e-02,  6.7235e-02, -4.0800e-02,  6.1524e-02,
           -2.8888e-02],
          [ 5.5434e-02,  2.9905e-02, -2.0776e-02,  5.8204e-02, -1.7626e-02,
            4.4553e-02],
          [-5.7778e-02,  2.8391e-03,  1.4783e-02, -6.0455e-02,  8.5516e-03,
           -1.4838e-03],
          [ 3.5731e-02, -5.2179e-02, -3.4146e-02,  6.1390e-02,  6.5144e-02,
           -1.7195e-02],
          [ 3.8293e-02, -8.9802e-03,  7.2851e-03,  1.6451e-02, -8.2517e-03,
            8.3227e-03]]],


        ...,


        [[[-6.1033e-02, -6.2231e-02, -1.7732e-02, -4.7730e-02, -1.0310e-02,
            3.3293e-02],
          [ 6.6693e-02, -5.2335e-02,  3.3698e-02,  5.0717e-04, -3.7277e-02,
            4.6151e-03],
          [ 8.8201e-03, -3.4051e-02,  2.1081e-02, -5.4400e-02, -6.2689e-02,
            5.4935e-02],
          [-2.1240e-02,  5.8260e-02,  5.4442e-03, -6.5432e-02,  3.0565e-02,
           -2.9650e-02],
          [-6.3032e-02, -6.3728e-02,  7.9715e-03, -3.4735e-02, -1.3813e-02,
           -1.3967e-02],
          [ 3.4695e-02,  4.3078e-02, -2.9233e-02,  4.4320e-02, -5.3949e-02,
            5.8573e-02]],

         [[ 3.2612e-02,  4.1423e-02, -2.8045e-02,  1.0483e-02, -3.8973e-02,
           -5.2981e-02],
          [ 3.9109e-03, -4.5248e-02,  2.7728e-02, -4.3742e-02, -1.8613e-02,
            4.5885e-02],
          [-4.5885e-02, -6.4162e-02,  6.4738e-02, -6.9644e-03,  3.2731e-03,
           -2.7960e-02],
          [ 4.7241e-02,  6.8040e-02, -5.3925e-02,  2.3406e-02, -1.0548e-02,
            1.8321e-03],
          [ 7.4228e-03,  3.0310e-02,  6.7140e-02,  5.8473e-02, -5.7597e-02,
           -3.7725e-03],
          [-4.9229e-02, -3.3430e-02, -4.9466e-02, -4.2944e-02, -7.6428e-03,
           -1.4526e-02]],

         [[-1.1183e-02, -4.7027e-02, -4.4681e-02,  4.9006e-02,  4.4983e-03,
            3.6591e-02],
          [-6.2649e-02,  5.6368e-02,  5.7103e-02,  3.8548e-02, -4.7619e-02,
            2.5656e-02],
          [-4.9495e-02, -6.2330e-02, -5.7398e-02,  6.4545e-03,  6.0369e-02,
           -3.1203e-02],
          [-2.8834e-02,  3.7447e-02,  1.8729e-04,  5.6350e-02, -5.0937e-02,
            6.8147e-03],
          [ 9.8059e-03,  2.4477e-02, -6.4389e-02, -1.8404e-02,  4.2428e-02,
            6.7612e-02],
          [-4.6467e-03, -5.2623e-02,  4.5291e-02, -2.3482e-02,  4.4858e-02,
           -9.0489e-04]],

         [[-2.7999e-02, -2.2931e-02,  4.7835e-02, -2.5446e-02, -3.0041e-02,
            1.4024e-02],
          [-2.8088e-02,  3.0817e-02,  5.5440e-02, -4.5636e-02, -4.3630e-02,
           -5.0684e-02],
          [ 6.5562e-02,  3.8413e-02,  4.6479e-02,  4.3743e-03,  7.4613e-03,
           -1.8037e-02],
          [ 1.2478e-03,  5.8622e-02,  4.8563e-02, -2.8324e-02,  5.3664e-02,
            2.1385e-02],
          [ 9.2907e-03,  1.2440e-02,  4.7265e-02, -5.8044e-02, -1.6102e-02,
           -3.8088e-02],
          [-5.3717e-02,  4.5452e-02, -4.1998e-02, -5.9259e-02, -3.0864e-02,
           -2.3862e-02]],

         [[-2.7153e-02, -6.3103e-02,  5.5046e-02,  2.0721e-02, -2.4297e-02,
            6.0049e-02],
          [-2.8586e-02,  1.5998e-02, -8.7942e-03,  4.9794e-02, -9.3429e-03,
            5.1555e-02],
          [-4.2563e-03,  4.4887e-02,  4.7527e-03,  6.1778e-02,  1.2934e-03,
            1.2843e-02],
          [-1.4324e-02,  3.7526e-02, -4.0256e-02,  3.7373e-02, -1.5643e-02,
           -1.1986e-02],
          [ 6.3757e-02,  1.2777e-02,  3.5772e-02, -2.0934e-02,  7.2438e-03,
            4.8680e-02],
          [ 4.9542e-03, -2.5416e-02, -2.5119e-02,  1.4701e-02,  5.7594e-02,
           -3.0032e-02]],

         [[-2.0427e-02, -3.2456e-02,  1.5126e-02,  5.8987e-02, -3.2970e-02,
           -3.0400e-02],
          [ 2.5519e-02, -6.3037e-03, -2.3003e-02, -4.7833e-02, -5.5122e-02,
           -6.2180e-02],
          [ 4.4371e-02,  2.2316e-02,  1.3596e-02, -6.4803e-02,  5.0075e-02,
           -4.1990e-02],
          [ 1.7316e-03,  2.6644e-02, -5.1837e-02, -1.2279e-02,  2.8196e-02,
            6.6416e-02],
          [-2.9089e-02, -3.5379e-02,  4.1916e-02,  6.6213e-02, -1.4923e-02,
            5.3729e-02],
          [-4.4773e-03, -3.1954e-02,  4.8463e-02,  5.3065e-02, -5.8841e-02,
            3.0893e-02]]],


        [[[-6.3198e-02,  5.8580e-02, -3.9888e-02,  1.3634e-02,  4.9508e-02,
            1.5051e-02],
          [-9.6018e-03,  4.5728e-03, -2.5196e-02, -5.0983e-02, -4.8849e-02,
            2.4862e-02],
          [-2.7147e-02, -3.9915e-03,  1.0636e-03,  4.0989e-02,  1.0051e-03,
           -5.4415e-02],
          [ 1.4473e-03,  1.8123e-02,  6.6056e-03, -6.4474e-03,  1.0976e-02,
            4.3017e-02],
          [-4.8711e-02,  2.5606e-02,  2.9522e-02, -6.1847e-03,  1.0273e-03,
           -1.9533e-02],
          [-3.3082e-02, -1.3037e-02, -3.0129e-03, -4.0231e-02,  4.0907e-03,
           -2.0879e-02]],

         [[-2.9791e-02, -1.6329e-02, -1.8673e-02, -4.2083e-02, -1.8384e-02,
           -8.4345e-03],
          [-2.2081e-02,  1.1988e-02, -3.5258e-02,  1.3488e-02,  4.1661e-02,
            3.2792e-04],
          [-2.3439e-02,  5.3573e-02,  2.7882e-02, -3.1874e-02, -5.1036e-02,
           -4.5294e-02],
          [ 5.1135e-02, -1.6443e-02, -6.3578e-02,  3.8854e-02,  6.2287e-02,
           -4.3854e-02],
          [-1.2678e-02, -5.3853e-02, -1.9657e-02, -4.4930e-02, -3.8327e-03,
            5.3365e-02],
          [-1.2165e-02,  5.9267e-02,  7.1698e-03,  4.2204e-02, -2.6801e-03,
            4.6924e-02]],

         [[-6.2153e-02, -3.6933e-02,  3.5344e-02,  1.1651e-02,  5.7333e-02,
           -6.2557e-02],
          [-1.4140e-02, -1.4334e-02,  5.2528e-03,  2.5644e-03,  6.7630e-03,
            4.6064e-02],
          [ 5.6076e-02,  3.0145e-02, -3.0068e-02, -3.6243e-02,  6.2382e-02,
            1.3515e-02],
          [-5.6745e-02, -5.0754e-02, -4.7718e-02, -2.6241e-02, -2.9486e-02,
           -1.1007e-02],
          [ 1.7331e-02, -4.7643e-02,  5.8668e-02,  3.5160e-02,  4.5782e-02,
            6.0914e-02],
          [-6.2339e-02, -1.1028e-02,  9.3684e-05,  1.8538e-02, -3.3827e-02,
           -2.6005e-02]],

         [[-1.0177e-02, -4.2442e-02,  2.2631e-02,  1.2926e-03, -1.7958e-02,
           -1.0231e-03],
          [-4.0959e-02, -5.2364e-03, -2.9106e-02,  4.5870e-02, -4.4999e-02,
           -3.3874e-02],
          [-3.4285e-02,  2.3284e-02,  6.3463e-02,  7.4099e-03,  4.1683e-02,
           -2.0354e-02],
          [-5.8693e-02, -4.4849e-02,  5.5784e-02,  2.6528e-02, -4.6216e-02,
            5.9381e-02],
          [ 9.3299e-03,  2.5779e-02,  3.0300e-02, -8.0332e-03,  2.0562e-02,
           -4.2041e-02],
          [ 2.2309e-02,  3.3367e-03, -1.2218e-02, -3.0558e-03, -3.7603e-02,
           -3.4858e-03]]],


        [[[-1.7878e-02,  1.6290e-02, -2.0958e-02,  8.6858e-03, -4.4613e-02,
           -6.6590e-03],
          [-3.4006e-03,  3.0360e-04, -2.0582e-03, -3.8802e-02,  4.2682e-02,
            2.8087e-02],
          [-3.3807e-02,  5.9136e-02,  3.1198e-02,  2.6892e-02, -3.0326e-03,
            5.1533e-02],
          [ 5.7860e-02, -2.8579e-02, -6.5466e-02,  1.5547e-02,  3.5922e-02,
            6.3173e-02],
          [-7.7903e-03,  3.7570e-02, -6.0163e-02,  1.0796e-02,  5.0663e-02,
           -2.7680e-02],
          [ 8.9746e-03, -6.1034e-02,  5.7320e-02, -2.3462e-02,  1.2268e-02,
            5.4938e-02]],

         [[-5.2393e-03,  6.3352e-02,  5.4104e-02,  4.3946e-02, -6.0433e-02,
            5.0296e-02],
          [ 4.3445e-02, -6.7252e-03,  1.2625e-02, -3.1242e-02,  4.5316e-02,
            5.7103e-02],
          [ 1.2401e-03, -5.2470e-02,  5.8256e-02,  8.8942e-03,  2.8809e-02,
            5.7434e-02],
          [-1.7459e-02,  5.4265e-02, -5.7987e-02,  6.5110e-02, -1.1622e-02,
            3.8874e-02],
          [ 2.1374e-03, -4.7434e-02,  5.7053e-03, -5.6108e-02,  2.3738e-02,
           -5.6660e-02],
          [-1.9472e-02,  1.4523e-02,  6.7930e-02, -3.1108e-03,  3.1468e-03,
           -3.3623e-03]],
         [[-4.5703e-02, -5.0825e-02, -2.4130e-02, -9.0071e-03,  2.6277e-03,
           -6.2414e-02],
          [ 2.5200e-02,  1.1748e-02,  5.1698e-02,  3.0960e-02, -2.7416e-02,
            5.3391e-02],
          [ 3.0464e-02,  4.7903e-02,  6.4730e-03, -3.2426e-02,  2.0975e-02,
           -4.6186e-02],
          [-3.8858e-02, -1.4183e-02,  8.5696e-03,  2.3206e-02, -6.6595e-02,
           -1.4909e-02],
          [ 4.5485e-02,  1.3218e-02,  3.1149e-02, -1.3894e-02, -3.6772e-02,
            5.5798e-04],
          [ 1.2914e-02,  2.4075e-02, -3.9304e-02, -4.0275e-02, -1.2261e-02,
            5.9580e-02]],

         [[-1.1269e-02,  5.7410e-02,  5.3175e-03,  4.9887e-02,  2.1414e-02,
           -4.5186e-02],
          [ 4.7050e-02, -5.1185e-02,  1.4047e-02, -3.3591e-02,  5.0442e-02,
            6.7868e-02],
          [-6.7813e-02, -3.6417e-02, -5.9848e-02, -4.2991e-02,  8.3231e-03,
            4.8056e-02],
          [ 3.5584e-02, -3.1576e-03,  2.0225e-02,  1.5989e-02, -5.2238e-02,
           -6.1330e-02],
          [ 1.3347e-03, -6.4301e-02,  4.9421e-02, -3.0799e-02,  5.8497e-02,
           -4.9394e-02],
          [-2.4522e-02, -2.6627e-02,  7.7586e-03,  1.3444e-02, -1.3654e-02,
           -4.7028e-02]]]], device='cuda:0', requires_grad=True)
conv2.bias
Parameter containing:
tensor([-0.0629,  0.0634, -0.0304,  0.0623,  0.0191,  0.0079,  0.0282, -0.0173],
       device='cuda:0', requires_grad=True)
fc1.weight
Parameter containing:
tensor([[-0.1559, -0.1208,  0.0885,  ...,  0.0397, -0.1065, -0.1679],
        [ 0.1346,  0.1687,  0.0600,  ...,  0.0837,  0.1431,  0.1889],
        [-0.0069, -0.2389, -0.0327,  ..., -0.1500, -0.1122, -0.0209],
        ...,
        [ 0.1137, -0.1526, -0.1878,  ...,  0.0303,  0.1267, -0.2314],
        [ 0.0977,  0.2230,  0.1495,  ...,  0.1344,  0.1374, -0.0711],
        [-0.2340,  0.0246,  0.0386,  ..., -0.0133,  0.0496,  0.1986]],
       device='cuda:0', requires_grad=True)
fc1.bias
Parameter containing:
tensor([ 0.0619,  0.1791, -0.1679,  0.2440,  0.0490,  0.1529, -0.1567, -0.0005,
         0.1662, -0.1294,  0.0871,  0.2116,  0.0184,  0.1915, -0.0915,  0.1252,
        -0.0583,  0.0445,  0.1300, -0.0429, -0.1291,  0.0331,  0.1861,  0.0701,
         0.2385,  0.2299, -0.1124, -0.1594,  0.0863, -0.1881, -0.1308, -0.2110,
        -0.0572,  0.1537,  0.0826,  0.2306, -0.1304, -0.2150, -0.0329,  0.1007,
         0.0899,  0.0996, -0.0150,  0.1174, -0.0235,  0.0060, -0.1755, -0.1267,
         0.1460,  0.1452, -0.1040, -0.1271,  0.0275,  0.0774, -0.2313,  0.0059,
         0.0134,  0.1079,  0.0210,  0.2181, -0.0628, -0.2224,  0.1316, -0.1728,
        -0.2123,  0.2285,  0.0195,  0.1577, -0.1488,  0.1079,  0.0152, -0.2156,
         0.0354, -0.1788,  0.1208, -0.0957,  0.1112,  0.0205, -0.0429,  0.0444,
        -0.0651,  0.0184,  0.0605,  0.1974, -0.1610,  0.0308, -0.2419,  0.1713,
         0.1454, -0.1610,  0.1335, -0.0013,  0.2250,  0.2300, -0.1112, -0.1200,
         0.1380, -0.1661,  0.0880, -0.2137, -0.1442,  0.0772,  0.0070, -0.1650,
        -0.0672,  0.0849, -0.2365,  0.0596, -0.2335, -0.1349, -0.0755,  0.1505,
         0.1003, -0.1385,  0.2489, -0.1639,  0.2245, -0.1308,  0.1818,  0.2290],
       device='cuda:0', requires_grad=True)
fc2.weight
Parameter containing:
tensor([[-0.0614, -0.0558,  0.0538,  ..., -0.0669,  0.0884,  0.0865],
        [-0.0813,  0.0791,  0.0873,  ..., -0.0474, -0.0628,  0.0097],
        [-0.0336,  0.0455, -0.0774,  ..., -0.0617,  0.0774, -0.0146],
        ...,
        [ 0.0396,  0.0592,  0.0102,  ..., -0.0187,  0.0747,  0.0681],
        [ 0.0713, -0.0297, -0.0537,  ...,  0.0293,  0.0568,  0.0836],
        [ 0.0634, -0.0679, -0.0424,  ..., -0.0670, -0.0123,  0.0617]],
       device='cuda:0', requires_grad=True)
fc2.bias
Parameter containing:
tensor([ 0.0237, -0.0757,  0.0325,  0.0618, -0.0896, -0.0326,  0.0608,  0.0743,
        -0.0175,  0.0041,  0.0267,  0.0061,  0.0392, -0.0683,  0.0439, -0.0675,
         0.0522, -0.0184, -0.0228, -0.0423, -0.0386, -0.0401,  0.0539, -0.0002,
        -0.0674,  0.0120, -0.0305, -0.0314,  0.0569,  0.0887, -0.0116, -0.0579,
         0.0191, -0.0813, -0.0512, -0.0052,  0.0690,  0.0853,  0.0273,  0.0077,
        -0.0354, -0.0502, -0.0792, -0.0252,  0.0314, -0.0289,  0.0335,  0.0897,
         0.0649, -0.0375,  0.0749,  0.0351,  0.0243,  0.0794,  0.0471, -0.0746,
        -0.0713,  0.0213, -0.0021,  0.0181,  0.0685, -0.0195, -0.0471, -0.0098,
         0.0669,  0.0101,  0.0503,  0.0268, -0.0177,  0.0867,  0.0870, -0.0116,
        -0.0289,  0.0066, -0.0829, -0.0036, -0.0171,  0.0416, -0.0553, -0.0582,
         0.0249,  0.0116, -0.0046, -0.0599], device='cuda:0',
       requires_grad=True)
fc3.weight
Parameter containing:
tensor([[-7.4545e-02, -8.1495e-02, -8.8644e-02,  9.8536e-02, -7.7196e-02,
         -2.4417e-02,  1.7350e-02, -1.1084e-02,  1.0872e-02, -8.3333e-03,
          7.1090e-02, -1.0064e-01, -1.0632e-01,  4.9437e-02,  8.6292e-02,
          1.5347e-02, -5.1232e-02, -8.4426e-02,  3.8898e-02, -2.8575e-02,
         -6.2655e-02,  7.2709e-02, -4.3537e-02, -3.4669e-02,  6.6329e-02,
         -2.5820e-02, -8.8458e-03, -6.3773e-02,  1.9198e-02, -9.8671e-02,
          4.7000e-02,  4.4877e-02, -5.6892e-02, -2.8685e-02,  3.9047e-02,
         -7.5403e-02,  4.1356e-02,  6.6993e-02, -1.0709e-02,  2.3813e-02,
         -8.0735e-02, -2.4629e-02,  5.4321e-02,  3.8917e-02, -6.1421e-02,
          4.2220e-03,  1.9904e-02, -7.6355e-02, -8.8523e-02, -1.8404e-02,
         -3.4845e-02, -5.0796e-02,  8.4714e-02,  4.3445e-02, -2.6623e-02,
          3.9873e-02, -5.1395e-02, -1.0440e-01,  9.1349e-02, -1.7097e-02,
         -6.6794e-02, -2.5095e-02, -4.9208e-02,  6.6839e-02, -8.0035e-02,
         -6.4631e-02,  5.9116e-02,  1.7576e-02,  6.6645e-02,  3.7677e-02,
         -1.4034e-02,  1.0024e-01, -9.1758e-02,  9.1857e-02, -8.2432e-02,
         -5.7745e-02,  7.6147e-02, -4.2291e-03,  4.9467e-02, -2.5684e-02,
         -4.3005e-02, -5.7229e-02, -6.8184e-02,  4.2769e-02]], device='cuda:0',
       requires_grad=True)
fc3.bias
Parameter containing:
tensor([ 0.0605, -0.0577,  0.0898,  0.0678,  0.0655,  0.0693, -0.0772, -0.0127,
         0.0198, -0.0719], device='cuda:0', requires_grad=True)

If we pass net.parameters() in optim.SGD then it will optimize my net.parameters() only which doesn’t contain q_params parameters and due to this my q_params parameters will not optimize.

How to Reslove this problem in pytorch @ptrblck

The issue is most likely created by the usage of numpy arrays in the undefined q_net.
Since you are calling from_numpy on the output of q_net here:

torch.from_numpy(q_net(elem,self.q_params)).float()

I assume that internally numpy will be used, which will detach these operations from the computation graph and self.q_params won’t get any gradients.
You could either use PyTorch methods in q_net or write custom autograd.Functions as described here.

Thank you @ptrblck for your response!

Actually the error is not due to that and for removing the above confusion of numpy, I had done some changes in my code:

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, 8, 6)

        self.q_params = nn.Parameter(q_delta * torch.randn(train_Rdepth * n_qubits, requires_grad=True))

        self.fc1 = nn.Linear(1 * 4 * 4, 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)))
        
        pre_out = x 

        q_in = torch.tanh(pre_out) * np.pi / 2.0   

        q_out = torch.Tensor(0,1, q_in.shape[2], q_in.shape[3])
        q_out = q_out.to(device)
        for elem in q_in:
            
            q_out_elemm = q_net(elem,self.q_params)
           
            q_out_elem = q_out_elemm.float().reshape((1,1,q_in.shape[2], q_in.shape[3]))
           
            q_out = torch.cat((q_out, q_out_elem))
         
        x = q_out
        x = x.view(-1, 1 * 4 * 4)
        
        x = F.relu(self.fc1(x))
       
        x = F.relu(self.fc2(x))
        x = self.fc3(x)
        return x


net = Net()
net.to(device)

Now you can clearly see there is no numpy separately used here and also i assure that in q_net alos , i don’t have numpy and I had used only tensors everywhere to remove the confusion.

But still I am getting same error.

Later, I try to run the colab notebook TRAINING A CLASSIFIER which is the tutorial and in that I run all cells in it and everything is fine.

But as soon as I change the part

import torch.optim as optim

criterion = nn.CrossEntropyLoss()
optimizer = optim.SGD(net.parameters(), lr=0.001, momentum=0.9)

TO

import torch.optim as optim

criterion = nn.CrossEntropyLoss()
optimizer = optim.SGD(net.named_parameters(), lr=0.001, momentum=0.9)

It is throwing error :

---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-6-a87dc351f2a6> in <module>()
      2 
      3 criterion = nn.CrossEntropyLoss()
----> 4 optimizer = optim.SGD(net.named_parameters(), lr=0.001, momentum=0.9)

2 frames
/usr/local/lib/python3.7/dist-packages/torch/optim/optimizer.py in add_param_group(self, param_group)
    254             if not isinstance(param, torch.Tensor):
    255                 raise TypeError("optimizer can only optimize Tensors, "
--> 256                                 "but one of the params is " + torch.typename(param))
    257             if not param.is_leaf:
    258                 raise ValueError("can't optimize a non-leaf Tensor")

TypeError: optimizer can only optimize Tensors, but one of the params is tuple

Which is exactly same as my above error!

But as you mentioned earlier that it is present in net.named_parameters() which i personally also found the same output.

dict_keys(['q_params', 'conv1.weight', 'conv1.bias', 'conv2.weight', 'conv2.bias', 'fc1.weight', 'fc1.bias', 'fc2.weight', 'fc2.bias', 'fc3.weight', 'fc3.bias'])

Can you please help @ptrblck

Actually the net.parameters include q_params also…
We can verify by iterating in each element of net.parameters() and in both the net.parameters() and net.named_parameters() contain same number of variables of tensors in it.

Thank you @ptrblck for your previous responses!