Step02-Function
Before, we implemented the class Variable to store a variable for the deep learning model. In step 2, we will implement a function to make use of the Variable. A function is a definition of a relationship between a variable and another. For example, if we define a function f = x^2 and y=f(x), then this means the relationship between x and y is f. Like this, the class Function of this step will define a relationship between Variable instances.
class Function:
def __call__(self, input):
x=input.data
y=x**2
output=Variable(y)
return output
This class defines a relationship as only "square". But we want a more compatible basis class. So let's modify this to have only common methods.
class Function:
def __call__(self, input):
x=input.data
y=self.forward(x)
output=Variable(y)
return output
def forward(self, x):
raise NotImplementedError()
The class that roles as functions like Square and Exp will inherit this refined Function class.
The error "NotImplementedError" implies that the user must implement a method forward in the child class.
class Square(Function):
def forward(self, x):
return x**2
Because the class Square inherited Function class, __call__ is maintained. The forward method will execute a specific computation.
Until step 10, the Function's input and output are only "one" variable. This will be expanded in step 11.
Now we are done with the basis of the Variable and Function class.
댓글
댓글 쓰기