Build Deep Learning Framework:Step2 Function

Post at — Feb 10, 2025
#Deep_Learning_Framework

How to Build Deep Learning Framework Step By Step Using 60 steps:Step2

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
import numpy as np

class Variable:
    def __init__(self, data):
        self.data = data

class Function:
    def __call__(self, input):
        x = input.data
        y = self.forward(x)
        output = Variable(y)
        return output

    def forward(self, in_data):
        raise NotImplementedError()

class Square(Function):
    def forward(self, x):
        return x ** 2

x = Variable(np.array(10))
f = Square()
y = f(x)
print(type(y))
print(y.data)

result:

1
2
3
$ python step02.py 
<class '__main__.Variable'>
100

Explanation: The _call_ method in Python is a special method that allows objects to be called like functions. When you create an object and call it with parentheses (), Python automatically calls the _call_ method of that object.