src.network
1import torch 2import torch.nn as nn 3 4 5class AudioNetwork(nn.Module): 6 def __init__(self): 7 super().__init__() 8 9 self.conv1 = nn.Conv2d(2, 16, kernel_size=5) 10 self.conv2 = nn.Conv2d(16, 32, kernel_size=5) 11 12 self.max_pool2d_1 = nn.MaxPool2d(kernel_size=2) 13 self.max_pool2d_2 = nn.MaxPool2d(kernel_size=2) 14 15 self.flatten = nn.Flatten() 16 17 self.fc1 = nn.Linear(267488, 50) 18 self.fc2 = nn.Linear(50, 25) 19 20 self.relu1 = nn.ReLU() 21 self.relu2 = nn.ReLU() 22 self.relu3 = nn.ReLU() 23 self.relu4 = nn.ReLU() 24 25 def forward(self, x): 26 x = self.conv1(x) 27 x = self.max_pool2d_1(x) 28 x = self.relu1(x) 29 30 x = self.conv2(x) 31 x = self.max_pool2d_2(x) 32 x = self.relu2(x) 33 34 x = self.flatten(x) 35 36 x = self.fc1(x) 37 x = self.relu3(x) 38 x = self.fc2(x) 39 x = self.relu4(x) 40 41 return x 42 43 44if __name__ == "__main__": 45 network = AudioNetwork() 46 input = torch.ones(16, 2, 64, 2584, requires_grad=True) 47 output = network(input) 48 49 print("done")
6class AudioNetwork(nn.Module): 7 def __init__(self): 8 super().__init__() 9 10 self.conv1 = nn.Conv2d(2, 16, kernel_size=5) 11 self.conv2 = nn.Conv2d(16, 32, kernel_size=5) 12 13 self.max_pool2d_1 = nn.MaxPool2d(kernel_size=2) 14 self.max_pool2d_2 = nn.MaxPool2d(kernel_size=2) 15 16 self.flatten = nn.Flatten() 17 18 self.fc1 = nn.Linear(267488, 50) 19 self.fc2 = nn.Linear(50, 25) 20 21 self.relu1 = nn.ReLU() 22 self.relu2 = nn.ReLU() 23 self.relu3 = nn.ReLU() 24 self.relu4 = nn.ReLU() 25 26 def forward(self, x): 27 x = self.conv1(x) 28 x = self.max_pool2d_1(x) 29 x = self.relu1(x) 30 31 x = self.conv2(x) 32 x = self.max_pool2d_2(x) 33 x = self.relu2(x) 34 35 x = self.flatten(x) 36 37 x = self.fc1(x) 38 x = self.relu3(x) 39 x = self.fc2(x) 40 x = self.relu4(x) 41 42 return x
Base class for all neural network modules.
Your models should also subclass this class.
Modules can also contain other Modules, allowing to nest them in a tree structure. You can assign the submodules as regular attributes::
import torch.nn as nn
import torch.nn.functional as F
class Model(nn.Module):
def __init__(self):
super().__init__()
self.conv1 = nn.Conv2d(1, 20, 5)
self.conv2 = nn.Conv2d(20, 20, 5)
def forward(self, x):
x = F.relu(self.conv1(x))
return F.relu(self.conv2(x))
Submodules assigned in this way will be registered, and will have their
parameters converted too when you call to()
, etc.
As per the example above, an __init__()
call to the parent class
must be made before assignment on the child.
:ivar training: Boolean represents whether this module is in training or evaluation mode. :vartype training: bool
7 def __init__(self): 8 super().__init__() 9 10 self.conv1 = nn.Conv2d(2, 16, kernel_size=5) 11 self.conv2 = nn.Conv2d(16, 32, kernel_size=5) 12 13 self.max_pool2d_1 = nn.MaxPool2d(kernel_size=2) 14 self.max_pool2d_2 = nn.MaxPool2d(kernel_size=2) 15 16 self.flatten = nn.Flatten() 17 18 self.fc1 = nn.Linear(267488, 50) 19 self.fc2 = nn.Linear(50, 25) 20 21 self.relu1 = nn.ReLU() 22 self.relu2 = nn.ReLU() 23 self.relu3 = nn.ReLU() 24 self.relu4 = nn.ReLU()
Initialize internal Module state, shared by both nn.Module and ScriptModule.
26 def forward(self, x): 27 x = self.conv1(x) 28 x = self.max_pool2d_1(x) 29 x = self.relu1(x) 30 31 x = self.conv2(x) 32 x = self.max_pool2d_2(x) 33 x = self.relu2(x) 34 35 x = self.flatten(x) 36 37 x = self.fc1(x) 38 x = self.relu3(x) 39 x = self.fc2(x) 40 x = self.relu4(x) 41 42 return x
Define the computation performed at every call.
Should be overridden by all subclasses.
Although the recipe for forward pass needs to be defined within
this function, one should call the Module
instance afterwards
instead of this since the former takes care of running the
registered hooks while the latter silently ignores them.
Inherited Members
- torch.nn.modules.module.Module
- dump_patches
- training
- call_super_init
- register_buffer
- register_parameter
- add_module
- register_module
- get_submodule
- get_parameter
- get_buffer
- get_extra_state
- set_extra_state
- apply
- cuda
- ipu
- xpu
- cpu
- type
- float
- double
- half
- bfloat16
- to_empty
- to
- register_full_backward_pre_hook
- register_backward_hook
- register_full_backward_hook
- register_forward_pre_hook
- register_forward_hook
- register_state_dict_pre_hook
- state_dict
- register_load_state_dict_post_hook
- load_state_dict
- parameters
- named_parameters
- buffers
- named_buffers
- children
- named_children
- modules
- named_modules
- train
- eval
- requires_grad_
- zero_grad
- extra_repr
- compile