Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Clarification for self.forward function in Python

I am not able to understand this sample_losses = self.forward(output, y) defined under the class Loss.

From which "forward function" it is taking input as forward function is previously defined for all three classes i.e. Dense_layer, Activation_ReLU and Activation_Softmax?

class Layer_Dense:
    def __init__(self, n_inputs, n_neurons):
        self.weights = 0.01 * np.random.randn(n_inputs, n_neurons)
        self.biases = np.zeros((1, n_neurons))
        print(self.weights)
    def forward(self, inputs):
        self.output = np.dot(inputs, self.weights) + self.biases
class Activation_ReLU:
    def forward(self, inputs):
        self.output= np.maximum(0, inputs)
class Activation_Softmax:
    def forward (self, inputs):
        exp_values = np.exp(inputs - np.max(inputs, axis = 1, keepdims= True ))
        probabilities= exp_values/np.sum(exp_values, axis = 1, keepdims= True )
        self.output = probabilities
class Loss:
    def calculate(self, output, y):
        sample_losses = self.forward(output, y)
        data_loss = np.mean(sample_losses)
        return data_loss
like image 774
Himanshu Gaur Avatar asked Nov 29 '25 18:11

Himanshu Gaur


1 Answers

self.forward() is similar to call method but with registered hooks. This is used to directly call a method in the class when an instance name is called. These methods are inherited from nn.Module.

https://gist.github.com/nathanhubens/5a9fc090dcfbf03759068ae0fc3df1c9

Or refer to the source code:

https://github.com/pytorch/pytorch/blob/master/torch/nn/modules/module.py#L485

like image 132
Nivesh Gadipudi Avatar answered Dec 02 '25 06:12

Nivesh Gadipudi



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!