22
33
44class MagnusModel (nn .Module ):
5- def __init__ (self , image_shape : int , num_classes : int , imagechannels : int ):
5+ def __init__ (self , image_shape , num_classes : int , nr_channels : int ):
66 """
7- Magnus model contains the model for Magnus' part of the homeexam.
8- This class contains a neural network consisting of three linear layers of 133 neurons each,
9- with ReLU activation between each layer.
10-
11- Args
12- ----
13- image_shape (int): Expected size of input image. This is needed to scale first layer input
14- imagechannels (int): Expected number of image channels. This is needed to scale first layer input
15- num_classes (int): Number of classes we are to provide.
16-
17- Returns
18- -------
19- MagnusModel (nn.Module): Neural network as described above in this docstring.
7+ Initializes the MagnusModel, a neural network designed for image classification tasks.
8+
9+ The model consists of three linear layers, each with 133 neurons, and uses ReLU activation
10+ functions between the layers. The first layer's input size is determined by the image shape
11+ and number of channels, while the output layer's size is determined by the number of classes.
12+ Args:
13+ image_shape (tuple): A tuple representing the dimensions of the input image (Channels, Height, Width).
14+ num_classes (int): The number of output classes for classification.
15+ nr_channels (int): The number of channels in the input image.
16+ Returns:
17+ MagnusModel (nn.Module): An instance of the MagnusModel neural network.
2018 """
2119 super ().__init__ ()
22- self .image_shape = image_shape
23- self .imagechannels = imagechannels
24-
20+ _ , H , W = image_shape
21+
2522 self .layer1 = nn .Sequential (* ([
26- nn .Linear (self . imagechannels * self . imagesize * self . imagesize , 133 ),
23+ nn .Linear (nr_channels * H * W , 133 ),
2724 nn .ReLU (),
2825 ]))
2926 self .layer2 = nn .Sequential (* ([
@@ -32,27 +29,32 @@ def __init__(self, image_shape: int, num_classes: int, imagechannels: int):
3229 ]))
3330 self .layer3 = nn .Sequential (* ([
3431 nn .Linear (133 , num_classes ),
35- nn .ReLU ()
3632 ]))
37-
3833 def forward (self , x ):
3934 """
40- Forward pass of MagnusModel
41-
42- Args
43- ----
44- x (th.Tensor): Four-dimensional tensor in the form (Batch Size x Channels x Image Height x Image Width)
45-
46- Returns
47- -------
48- out (th.Tensor): Class-logits of network given input x
35+ Defines the forward pass of the MagnusModel.
36+ Args:
37+ x (torch.Tensor): A four-dimensional tensor with shape (Batch Size, Channels, Image Height, Image Width).
38+ Returns:
39+ torch.Tensor: The output tensor containing class logits for each input sample.
4940 """
50- assert len (x .size ) == 4
51-
41+ assert len (x .size ()) == 4
5242 x = x .view (x .size (0 ), - 1 )
53-
5443 x = self .layer1 (x )
5544 x = self .layer2 (x )
5645 out = self .layer3 (x )
57-
5846 return out
47+
48+
49+ if __name__ == '__main__' :
50+ import torch as th
51+
52+ data_shape = [28 ,28 ]
53+
54+ data_shape = (3 , * data_shape )
55+ model = MagnusModel (data_shape , 10 )
56+
57+ dummy_img = th .rand ((5 ,* data_shape ))
58+ print (dummy_img .size ())
59+ with th .no_grad ():
60+ print (model (dummy_img ).size ())
0 commit comments