22.02.2024 Views

Daniel Voigt Godoy - Deep Learning with PyTorch Step-by-Step A Beginner’s Guide-leanpub

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

Output

OrderedDict([

('0.weight',

tensor([[ 0.4414, 0.4792, -0.1353],

[ 0.5304, -0.1265, 0.1165],

[-0.2811, 0.3391, 0.5090],

[-0.4236, 0.5018, 0.1081],

[ 0.4266, 0.0782, 0.2784]],

device='cuda:0')),

('0.bias',

tensor([-0.0815, 0.4451, 0.0853, -0.2695, 0.1472],

device='cuda:0')),

('1.weight',

tensor([[-0.2060, -0.0524, -0.1816, 0.2967, -0.3530]],

device='cuda:0')),

('1.bias',

tensor([-0.2062], device='cuda:0'))])

Since this sequential model does not have attribute names, state_dict() uses

numeric prefixes.

You can also use a model’s add_module() method to name the layers:

torch.manual_seed(42)

# Building the model from the figure above

model = nn.Sequential()

model.add_module('layer1', nn.Linear(3, 5))

model.add_module('layer2', nn.Linear(5, 1))

model.to(device)

Output

Sequential(

(layer1): Linear(in_features=3, out_features=5, bias=True)

(layer2): Linear(in_features=5, out_features=1, bias=True)

)

114 | Chapter 1: A Simple Regression Problem

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!