Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

PyTorch torch.nn.Module Function

PyTorch torch.nn.Module Function

PyTorch torch.nn Reference Manual PyTorch torch.nn Reference Manual


torch.nn.Module is the base class for all neural network modules in PyTorch.

All custom network models should inherit from this class, which provides features such as parameter management, device migration, and model saving.

Class definition



torch.nn.Module

Main attributes


Usage examples

Example 1: Creating a custom module

Inherit from nn.Module to create a custom network:

Example

import

torch

import

torch.

nn

as

nn

class

SimpleNet

(

nn.

Module

)

:

   

def

init

(

self

,

input_dim

,

hidden_dim

,

output_dim

)

:

       

super

(

SimpleNet

,

self

)

.

init

(

)

       

Define network layers

       

self

.

fc1

=

nn.

Linear

(

input_dim

,

hidden_dim

)

       

self

.

relu

=

nn.

ReLU

(

)

       

self

.

fc2

=

nn.

Linear

(

hidden_dim

,

output_dim

)

   

def

forward

(

self

,

x

)

:

       

Define the forward pass

        x

=

self

.

fc1

(

x

)

        x

=

self

.

relu

(

x

)

        x

=

self

.

fc2

(

x

)

       

return

x

Instantiate the model

model

=

SimpleNet

(

input_dim

=

784

,

hidden_dim

=

256

,

output_dim

=

10

)

Testing

x

=

torch.

randn

(

32

,

784

)

output

=

model

(

x

)

print

(

"Model structure:"

)

print

(

model

)

print

(

"nInput shape:"

,

x.

shape

)

print

(

"Output shape:"

,

output.

shape

)

Example 2: Managing parameters

Access and manage model parameters:

Example

import

torch

import

torch.

nn

as

nn

class

Net

(

nn.

Module

)

:

   

def

init

(

self

)

:

       

super

(

Net

,

self

)

.

init

(

)

       

self

.

conv1

=

nn.

Conv2d

(

3

,

16

,

3

)

       

self

.

bn1

=

nn.

BatchNorm2d

(

16

)

       

self

.

fc

=

nn.

Linear

(

16

14

14

,

10

)

   

def

forward

(

self

,

x

)

:

        x

=

self

.

conv1

(

x

)

        x

=

self

.

bn1

(

x

)

        x

=

x.

view

(

x.

size

(

0

)

,

1

)

        x

=

self

.

fc

(

x

)

       

return

x

model

=

Net

(

)

Count the number of parameters

total_params

=

sum

(

p.

numel

(

)

for

p

in

model.

parameters

(

)

)

trainable_params

=

sum

(

p.

numel

(

)

for

p

in

model.

parameters

(

)

if

p.

requires_grad

)

print

(

"Total number of parameters:"

,

total_params

)

print

(

"Number of trainable parameters:"

,

trainable_params

)

Access specific parameters

print

(

"nconv1 weight shape:"

,

model.

conv1

.

weight

.

shape

)

print

(

"fc bias shape:"

,

model.

fc

.

bias

.

shape

)

Example 3: Model saving and loading

Save and load a model:

Example

import

torch

import

torch.

nn

as

nn

import

tempfile

import

os

class

Net

(

nn.

Module

)

:

   

def

init

(

self

)

:

       

super

(

Net

,

self

)

.

init

(

)

       

self

.

fc

=

nn.

Linear

(

10

,

5

)

   

def

forward

(

self

,

x

)

:

       

return

self

.

fc

(

x

)

model

=

Net

(

)

Save the Entire Model

with

tempfile

.

NamedTemporaryFile

(

delete

=

False

,

suffix

=

'.pth'

)

as

f:

    torch.

save

(

model

,

f.

name

)

    path_full

=

f.

name

Save state_dict (recommended)

with

tempfile

.

NamedTemporaryFile

(

delete

=

False

,

suffix

=

'.pth'

)

as

f:

    torch.

save

(

model.

state_dict

(

)

,

f.

name

)

    path_state

=

f.

name

Load model

loaded_model

=

Net

(

)

loaded_model.

load_state_dict

(

torch.

load

(

path_state

)

)

loaded_model.

eval

(

)

Test the loaded model

x

=

torch.

randn

(

2

,

10

)

output1

=

model

(

x

)

output2

=

loaded_model

(

x

)

print

(

"Original output:"

,

output1

[

0

]

.

tolist

(

)

)

print

(

"Output after loading:"

,

output2

[

0

]

.

tolist

(

)

)

Clean up

os

.

remove

(

path_full

)

os

.

remove

(

path_state

)

Example 4: Device migration

Move the model between different devices:

Example

import

torch

import

torch.

nn

as

nn

class

Net

(

nn.

Module

)

:

   

def

init

(

self

)

:

       

super

(

Net

,

self

)

.

init

(

)

       

self

.

fc

=

nn.

Linear

(

10

,

5

)

   

def

forward

(

self

,

x

)

:

       

return

self

.

fc

(

x

)

model

=

Net

(

)

Check the current device

print

(

"Parameter device:"

,

model.

fc

.

weight

.

device

)

Move to GPU (if available)

if

torch.

cuda

.

is_available

(

)

:

    model

=

model.

cuda

(

)

   

print

(

"After moving to GPU:"

,

model.

fc

.

weight

.

device

)

Move back to CPU

model

=

model.

cpu

(

)

print

(

"After moving back to CPU:"

,

model.

fc

.

weight

.

device

)

Example 5: Initialization using apply

Recursively initialize using apply:

Example

import

torch

import

torch.

nn

as

nn

def

init_weights

(

module

)

:

   

if

isinstance

(

module

,

nn.

Linear

)

:

        nn.

init

.

xavier_uniform_

(

module.

weight

)

       

if

module.

bias

is

not

None

:

            nn.

init

.

zeros_

(

module.

bias

)

   

elif

isinstance

(

module

,

nn.

Conv2d

)

:

        nn.

init

.

kaiming_normal_

(

module.

weight

)

class

Net

(

nn.

Module

)

:

   

def

init

(

self

)

:

       

super

(

Net

,

self

)

.

init

(

)

       

self

.

fc1

=

nn.

Linear

(

10

,

20

)

       

self

.

fc2

=

nn.

Linear

(

20

,

5

)

   

def

forward

(

self

,

x

)

:

       

return

self

.

fc2

(

self

.

fc1

(

x

)

)

model

=

Net

(

)

model.

apply

(

init_weights

)

print

(

"fc1 weights:"

,

model.

fc1

.

weight

[

0

,

:

3

]

.

tolist

(

)

)

print

(

"fc2 weights:"

,

model.

fc2

.

weight

[

0

,

:

3

]

.

tolist

(

)

)

Example 6: Complex network architecture

Build a network with branches:

Example

import

torch

import

torch.

nn

as

nn

class

BranchNet

(

nn.

Module

)

:

   

def

init

(

self

)

:

       

super

(

BranchNet

,

self

)

.

init

(

)

       

Main trunk

       

self

.

shared

=

nn.

Linear

(

10

,

20

)

       

Branch

       

self

.

branch_a

=

nn.

Linear

(

20

,

5

)

       

self

.

branch_b

=

nn.

Linear

(

20

,

3

)

   

def

forward

(

self

,

x

)

:

        feat

=

self

.

shared

(

x

)

        out_a

=

self

.

branch_a

(

feat

)

        out_b

=

self

.

branch_b

(

feat

)

       

return

out_a

,

out_b

model

=

BranchNet

(

)

x

=

torch.

randn

(

4

,

10

)

out_a

,

out_b

=

model

(

x

)

print

(

"Branch A output:"

,

out_a.

shape

)

print

(

"Branch B output:"

,

out_b.

shape

)


Common questions

Q1: Why must super().init() be called?

It calls the parent class's initialization method, ensuring the module registers parameters correctly.

Simply use print(model) or torchvision's summary function.

parameters() returns learnable parameters, while modules() returns all modules.


Use Cases

nn.Module is the foundation for building all custom neural networks. Its main application scenarios include:

Tip: All custom Modules need to implement the forward method.


PyTorch torch.nn Reference Manual PyTorch torch.nn Reference Manual