3
import torch
import torch.nn as nn
from torch.optim import Adam

class NN_Network(nn.Module):
    def __init__(self,in_dim,hid,out_dim):
        super(NN_Network, self).__init__()
        self.linear1 = nn.Linear(in_dim,hid)
        self.linear2 = nn.Linear(hid,out_dim)


    def forward(self, input_array):
        h = self.linear1(input_array)
        y_pred = self.linear2(h)
        return y_pred

in_d = 5
hidn = 2
out_d = 3
net = NN_Network(in_d, hidn, out_d)

list(net.parameters())

The result was :

[Parameter containing:
 tensor([[-0.2948, -0.1261,  0.2525, -0.4162,  0.3067],
         [-0.2483, -0.3600, -0.4090,  0.0844, -0.2772]], requires_grad=True),
 Parameter containing:
 tensor([-0.2570, -0.3754], requires_grad=True),
 Parameter containing:
 tensor([[ 0.4550, -0.4577],
         [ 0.1782,  0.2454],
         [ 0.6931, -0.6003]], requires_grad=True),
 Parameter containing:
 tensor([ 0.4181, -0.2229, -0.5921], requires_grad=True)]

Without using nn.Parameter, list(net.parmeters()) results as a parameters.

What I am curious is that :

  1. I didn't used nn.Parameter command, why does it results? And to check any network's layers' parameters, then is .parameters() only way to check it?

  2. Maybe the result was self.linear1(in_dim,hid)'s weight, bias and so on, respectively.
    But is there any way to check what it is?

JAEMTO
  • 209
  • 3
  • 15

1 Answers1

4

Instead of .parameters(), you can use .named_parameters() to get more information about the model:

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

Result:

linear1.weight tensor([[ 0.3727,  0.2522,  0.2381,  0.3115,  0.0656],
        [-0.3322,  0.2024,  0.1089, -0.3370,  0.3917]])
linear1.bias tensor([-0.2089,  0.1105])
linear2.weight tensor([[-0.1090,  0.2564],
        [-0.3957,  0.6632],
        [-0.4036,  0.7066]])
linear2.bias tensor([ 0.1398, -0.0585,  0.4297])
yakhyo
  • 1,540
  • 1
  • 6
  • 22
  • And is there any difference and usage of state_dict and .parameters()? – JAEMTO Nov 26 '21 at 08:09
  • I think the difference of these two only return type, `.state_dict()` return dictionary and `.named_parameters()` returns generator. There is a discussion on : https://stackoverflow.com/a/54747245/14815986 – yakhyo Nov 26 '21 at 08:51