22
33
44class MagnusModel (nn .Module ):
5- def __init__ (self ,
6- imagesize : int ,
7- imagechannels : int ,
8- n_classes :int = 10 ):
9-
5+ def __init__ (self , imagesize : int , imagechannels : int , n_classes : int = 10 ):
106 """
11- Magnus model contains the model for Magnus' part of the homeexam.
7+ Magnus model contains the model for Magnus' part of the homeexam.
128 This class contains a neural network consisting of three linear layers of 133 neurons each,
139 with ReLU activation between each layer.
1410
1511 Args
1612 ----
1713 imagesize (int): Expected size of input image. This is needed to scale first layer input
1814 imagechannels (int): Expected number of image channels. This is needed to scale first layer input
19- n_classes (int): Number of classes we are to provide.
15+ n_classes (int): Number of classes we are to provide.
2016
2117 Returns
2218 -------
2319 MagnusModel (nn.Module): Neural network as described above in this docstring.
2420 """
25-
26-
21+
2722 super ().__init__ ()
28- self .imagesize = imagesize
23+ self .imagesize = imagesize
2924 self .imagechannels = imagechannels
30-
31- self .layer1 = nn .Sequential (* ([
32- nn . Linear ( self . imagechannels * self . imagesize * self . imagesize , 133 ),
33- nn . ReLU ()
34- ]))
35- self .layer2 = nn . Sequential ( * ([
36- nn . Linear ( 133 , 133 ),
37- nn .ReLU ()
38- ]))
39- self . layer3 = nn . Sequential ( * ([
40- nn . Linear ( 133 , n_classes ),
41- nn .ReLU ()
42- ]))
25+
26+ self .layer1 = nn .Sequential (
27+ * (
28+ [
29+ nn . Linear (
30+ self .imagechannels * self . imagesize * self . imagesize , 133
31+ ),
32+ nn .ReLU (),
33+ ]
34+ )
35+ )
36+ self . layer2 = nn . Sequential ( * ([ nn . Linear ( 133 , 133 ), nn .ReLU ()]) )
37+ self . layer3 = nn . Sequential ( * ([ nn . Linear ( 133 , n_classes ), nn . ReLU () ]))
4338
4439 def forward (self , x ):
4540 """
@@ -48,17 +43,17 @@ def forward(self, x):
4843 Args
4944 ----
5045 x (th.Tensor): Four-dimensional tensor in the form (Batch Size x Channels x Image Height x Image Width)
51-
46+
5247 Returns
5348 -------
5449 out (th.Tensor): Class-logits of network given input x
5550 """
5651 assert len (x .size ) == 4
57-
52+
5853 x = x .view (x .size (0 ), - 1 )
59-
54+
6055 x = self .layer1 (x )
6156 x = self .layer2 (x )
6257 out = self .layer3 (x )
63-
58+
6459 return out
0 commit comments