Build Deep Learning Framework:Step3 Continuous calling of functions

Post at — Feb 12, 2025
#Deep_Learning_Framework

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

 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
26
27
28
29
30
31
32
33
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, x):
        raise NotImplementedError()

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

class Exp(Function):
    def forward(self, x):
        return np.exp(x)

A = Square()
B = Exp()
C = Square()

x = Variable(np.array(0.5))
a = A(x)
b = B(a)
y = C(b)
print(y.data)

Continuous calling prepares for efficient calculation of the derivative of each variable, and the algorithm behind it is called backpropagation algorithm