slp-ntua / slp-lab-support-19-20

1 stars 0 forks source link

Lab3 :2.2 Output Layer(s) (models.py:EX5) #16

Open foivospar opened 4 years ago

foivospar commented 4 years ago

Για ένα layer με μία μη γραμμική συνάρτηση ενεργοποίησης (π.χ. Relu), πώς μπορώ να επιλέξω εγώ τη διάσταση για την έξοδο του layer όπως λέει η εκφώνηση ; Απ'ότι έχω καταλάβει, η μη γραμμική συνάρτηση εφαρμόζεται element-wise στην είσοδο, οπότε η διάσταση εξόδου καθορίζεται από τη διάσταση εισόδου.

georgepar commented 4 years ago

Οκ ας πάρουμε την απλή περίπτωση ενός fully connected (torch.nn.Linear) layer με ReLU activation. Η πράξη που θες να κάνεις εδώ είναι

y = relu(x * W^T + b)

Εδώ όταν δημιουργείς το layer ορίζεις input και output size που είναι οι διαστάσεις του πίνακα W.

Οπότε το activation εφαρμόζεται elementwise πάνω στο x*W^T + b και το μέγεθος αυτού καθορίζεται από το W

Σου στέλνω και ένα toy example με κώδικα

In [21]: import torch                                                                                                                                                                                              

In [22]: layer = torch.nn.Linear(10, 5)  # fully connected layer with input_size=10, output_size=5                                                                                                                 

In [23]: x = torch.rand(32, 10)  # random batch of 32 samples of input_size=10 for simulation                                                                                                                      

In [24]: print(x.shape)                                                                                                                                                                                            
torch.Size([32, 10])

In [25]: y1 = torch.nn.functional.relu(layer(x))  #  y1 = relu(x * W^T + b)                                                                                                                                        

In [26]: print(y1.shape)  # output -> batch_size x output_size = 32x5                                                                                                                                              
torch.Size([32, 5])

In [27]:  # The operations in detail                                                                                                                                                                               

In [28]: W = layer.weight                                                                                                                                                                                          

In [29]: print(W.shape)                                                                                                                                                                                            
torch.Size([5, 10])

In [30]: b = layer.bias                                                                                                                                                                                            

In [31]: print(b.shape)                                                                                                                                                                                            
torch.Size([5])

In [32]: y2 = torch.nn.functional.relu(torch.matmul(x, W.t()) + b)  # Let's do it by hand                                                                                                                          

In [33]: print(y2.shape)                                                                                                                                                                                           
torch.Size([32, 5])

In [34]: assert torch.all(torch.isclose(y1, y2)).item(), "These should be equal"    
foivospar commented 4 years ago

Αα οπότε βάζουμε Relu μετά από Linear, γιατί όπως το γράφει η εκφώνηση φαίνεται σα να εννοεί απλώς ένα μη γραμμικό (π.χ. Relu) module