質問編集履歴
1
PyTorch公式チュートリアルの具体例に差し替えました
    
        title	
    CHANGED
    
    | 
         
            File without changes
         
     | 
    
        body	
    CHANGED
    
    | 
         @@ -1,22 +1,53 @@ 
     | 
|
| 
       1 
     | 
    
         
            -
             
     | 
| 
       2 
     | 
    
         
            -
            Pytorch 
     | 
| 
       3 
     | 
    
         
            -
            以下のようなソースコードでデータxが与えられて時、model = Model(x)のようにインスタンスが作られると思うのですが、なぜxがforward関数に渡されるのでしょうか?クラスのインスタンスの引数は_init_に渡されると思うのですが、nn.Moduleのソースではinitは引数がselfだけでよくわからなかったです。
         
     | 
| 
      
 1 
     | 
    
         
            +
            PyTorchを使って機械学習の勉強をしています。pythonの学習も最近始めたばかりです。
         
     | 
| 
      
 2 
     | 
    
         
            +
            以下のPytorchのtutorialのコードでNet()のインスタンスnetを作成し、引数にinputを渡していると思いますが、なぜこれがforward関数に渡されるのでしょうか?インスタンスの引数は_init_に渡されると思いますが、Netもnn.Moduleも_init_の引数にselfしか持っておらず、どういう仕組みなのか全くわかりません。Python素人なので基礎的な質問かもしれませんがよろしくお願いします。
         
     | 
| 
       4 
3 
     | 
    
         | 
| 
      
 4 
     | 
    
         
            +
            [PyTorchのtutorial](https://pytorch.org/tutorials/beginner/blitz/neural_networks_tutorial.html)
         
     | 
| 
       5 
5 
     | 
    
         | 
| 
       6 
6 
     | 
    
         | 
| 
       7 
7 
     | 
    
         
             
            ### 該当のソースコード
         
     | 
| 
       8 
8 
     | 
    
         | 
| 
       9 
9 
     | 
    
         
             
            ```python
         
     | 
| 
      
 10 
     | 
    
         
            +
            import torch
         
     | 
| 
       10 
11 
     | 
    
         
             
            import torch.nn as nn
         
     | 
| 
       11 
12 
     | 
    
         
             
            import torch.nn.functional as F
         
     | 
| 
       12 
13 
     | 
    
         | 
| 
      
 14 
     | 
    
         
            +
             
     | 
| 
       13 
     | 
    
         
            -
            class  
     | 
| 
      
 15 
     | 
    
         
            +
            class Net(nn.Module):
         
     | 
| 
      
 16 
     | 
    
         
            +
             
     | 
| 
       14 
17 
     | 
    
         
             
                def __init__(self):
         
     | 
| 
       15 
     | 
    
         
            -
                    super( 
     | 
| 
      
 18 
     | 
    
         
            +
                    super(Net, self).__init__()
         
     | 
| 
      
 19 
     | 
    
         
            +
                    # 1 input image channel, 6 output channels, 3x3 square convolution
         
     | 
| 
      
 20 
     | 
    
         
            +
                    # kernel
         
     | 
| 
       16 
     | 
    
         
            -
                    self.conv1 = nn.Conv2d(1,  
     | 
| 
      
 21 
     | 
    
         
            +
                    self.conv1 = nn.Conv2d(1, 6, 3)
         
     | 
| 
       17 
     | 
    
         
            -
                    self.conv2 = nn.Conv2d( 
     | 
| 
      
 22 
     | 
    
         
            +
                    self.conv2 = nn.Conv2d(6, 16, 3)
         
     | 
| 
      
 23 
     | 
    
         
            +
                    # an affine operation: y = Wx + b
         
     | 
| 
      
 24 
     | 
    
         
            +
                    self.fc1 = nn.Linear(16 * 6 * 6, 120)  # 6*6 from image dimension
         
     | 
| 
      
 25 
     | 
    
         
            +
                    self.fc2 = nn.Linear(120, 84)
         
     | 
| 
      
 26 
     | 
    
         
            +
                    self.fc3 = nn.Linear(84, 10)
         
     | 
| 
       18 
27 
     | 
    
         | 
| 
       19 
28 
     | 
    
         
             
                def forward(self, x):
         
     | 
| 
      
 29 
     | 
    
         
            +
                    # Max pooling over a (2, 2) window
         
     | 
| 
      
 30 
     | 
    
         
            +
                    x = F.max_pool2d(F.relu(self.conv1(x)), (2, 2))
         
     | 
| 
      
 31 
     | 
    
         
            +
                    # If the size is a square you can only specify a single number
         
     | 
| 
      
 32 
     | 
    
         
            +
                    x = F.max_pool2d(F.relu(self.conv2(x)), 2)
         
     | 
| 
      
 33 
     | 
    
         
            +
                    x = x.view(-1, self.num_flat_features(x))
         
     | 
| 
       20 
     | 
    
         
            -
                    x = F.relu(self. 
     | 
| 
      
 34 
     | 
    
         
            +
                    x = F.relu(self.fc1(x))
         
     | 
| 
       21 
     | 
    
         
            -
                     
     | 
| 
      
 35 
     | 
    
         
            +
                    x = F.relu(self.fc2(x))
         
     | 
| 
      
 36 
     | 
    
         
            +
                    x = self.fc3(x)
         
     | 
| 
      
 37 
     | 
    
         
            +
                    return x
         
     | 
| 
      
 38 
     | 
    
         
            +
             
     | 
| 
      
 39 
     | 
    
         
            +
                def num_flat_features(self, x):
         
     | 
| 
      
 40 
     | 
    
         
            +
                    size = x.size()[1:]  # all dimensions except the batch dimension
         
     | 
| 
      
 41 
     | 
    
         
            +
                    num_features = 1
         
     | 
| 
      
 42 
     | 
    
         
            +
                    for s in size:
         
     | 
| 
      
 43 
     | 
    
         
            +
                        num_features *= s
         
     | 
| 
      
 44 
     | 
    
         
            +
                    return num_features
         
     | 
| 
      
 45 
     | 
    
         
            +
             
     | 
| 
      
 46 
     | 
    
         
            +
             
     | 
| 
      
 47 
     | 
    
         
            +
            net = Net()
         
     | 
| 
      
 48 
     | 
    
         
            +
            print(net)
         
     | 
| 
      
 49 
     | 
    
         
            +
             
     | 
| 
      
 50 
     | 
    
         
            +
            input = torch.randn(1, 1, 32, 32)
         
     | 
| 
      
 51 
     | 
    
         
            +
            out = net(input)
         
     | 
| 
      
 52 
     | 
    
         
            +
            print(out)
         
     | 
| 
       22 
53 
     | 
    
         
             
            ```
         
     |