src.model
1import torch 2from torch import nn 3 4 5class BasicCNN(nn.Module): 6 """Example Convolutional Neural Network (CNN) for MNIST identification.""" 7 8 def __init__(self): 9 super().__init__() 10 11 self.conv1 = nn.Conv2d(in_channels=1, out_channels=10, kernel_size=3, padding="valid") 12 self.conv2 = nn.Conv2d(in_channels=10, out_channels=7, kernel_size=3, padding="valid") 13 self.conv3 = nn.Conv2d(in_channels=7, out_channels=5, kernel_size=3, padding="valid") 14 self.conv4 = nn.Conv2d(in_channels=5, out_channels=1, kernel_size=3, padding="valid") 15 16 self.act1 = nn.ReLU() 17 self.act2 = nn.ReLU() 18 self.act3 = nn.ReLU() 19 self.act4 = nn.ReLU() 20 21 self.flatten = nn.Flatten(start_dim=1, end_dim=-1) 22 self.linear1 = nn.Linear(in_features=400, out_features=10) 23 24 def forward(self, x: torch.Tensor) -> tuple[torch.Tensor, torch.Tensor]: 25 """Returns prediction as well as the embedding (one layer before final prediction). The embedding is used for visualizing the projections. 26 27 Args: 28 x (torch.Tensor): The input image. 29 30 Returns: 31 tuple[torch.Tensor, torch.Tensor]: Prediction and embedding tensors. 32 """ 33 x = self.conv1(x) 34 x = self.act1(x) 35 x = self.conv2(x) 36 x = self.act2(x) 37 x = self.conv3(x) 38 x = self.act3(x) 39 x = self.conv4(x) 40 x = self.act4(x) 41 e = self.flatten(x) 42 x = self.linear1(e) 43 44 return x, e
class
BasicCNN(torch.nn.modules.module.Module):
6class BasicCNN(nn.Module): 7 """Example Convolutional Neural Network (CNN) for MNIST identification.""" 8 9 def __init__(self): 10 super().__init__() 11 12 self.conv1 = nn.Conv2d(in_channels=1, out_channels=10, kernel_size=3, padding="valid") 13 self.conv2 = nn.Conv2d(in_channels=10, out_channels=7, kernel_size=3, padding="valid") 14 self.conv3 = nn.Conv2d(in_channels=7, out_channels=5, kernel_size=3, padding="valid") 15 self.conv4 = nn.Conv2d(in_channels=5, out_channels=1, kernel_size=3, padding="valid") 16 17 self.act1 = nn.ReLU() 18 self.act2 = nn.ReLU() 19 self.act3 = nn.ReLU() 20 self.act4 = nn.ReLU() 21 22 self.flatten = nn.Flatten(start_dim=1, end_dim=-1) 23 self.linear1 = nn.Linear(in_features=400, out_features=10) 24 25 def forward(self, x: torch.Tensor) -> tuple[torch.Tensor, torch.Tensor]: 26 """Returns prediction as well as the embedding (one layer before final prediction). The embedding is used for visualizing the projections. 27 28 Args: 29 x (torch.Tensor): The input image. 30 31 Returns: 32 tuple[torch.Tensor, torch.Tensor]: Prediction and embedding tensors. 33 """ 34 x = self.conv1(x) 35 x = self.act1(x) 36 x = self.conv2(x) 37 x = self.act2(x) 38 x = self.conv3(x) 39 x = self.act3(x) 40 x = self.conv4(x) 41 x = self.act4(x) 42 e = self.flatten(x) 43 x = self.linear1(e) 44 45 return x, e
Example Convolutional Neural Network (CNN) for MNIST identification.
BasicCNN()
9 def __init__(self): 10 super().__init__() 11 12 self.conv1 = nn.Conv2d(in_channels=1, out_channels=10, kernel_size=3, padding="valid") 13 self.conv2 = nn.Conv2d(in_channels=10, out_channels=7, kernel_size=3, padding="valid") 14 self.conv3 = nn.Conv2d(in_channels=7, out_channels=5, kernel_size=3, padding="valid") 15 self.conv4 = nn.Conv2d(in_channels=5, out_channels=1, kernel_size=3, padding="valid") 16 17 self.act1 = nn.ReLU() 18 self.act2 = nn.ReLU() 19 self.act3 = nn.ReLU() 20 self.act4 = nn.ReLU() 21 22 self.flatten = nn.Flatten(start_dim=1, end_dim=-1) 23 self.linear1 = nn.Linear(in_features=400, out_features=10)
Initializes internal Module state, shared by both nn.Module and ScriptModule.
def
forward(self, x: torch.Tensor) -> tuple[torch.Tensor, torch.Tensor]:
25 def forward(self, x: torch.Tensor) -> tuple[torch.Tensor, torch.Tensor]: 26 """Returns prediction as well as the embedding (one layer before final prediction). The embedding is used for visualizing the projections. 27 28 Args: 29 x (torch.Tensor): The input image. 30 31 Returns: 32 tuple[torch.Tensor, torch.Tensor]: Prediction and embedding tensors. 33 """ 34 x = self.conv1(x) 35 x = self.act1(x) 36 x = self.conv2(x) 37 x = self.act2(x) 38 x = self.conv3(x) 39 x = self.act3(x) 40 x = self.conv4(x) 41 x = self.act4(x) 42 e = self.flatten(x) 43 x = self.linear1(e) 44 45 return x, e
Returns prediction as well as the embedding (one layer before final prediction). The embedding is used for visualizing the projections.
Arguments:
- x (torch.Tensor): The input image.
Returns:
tuple[torch.Tensor, torch.Tensor]: Prediction and embedding tensors.
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