Path: blob/master/3 - Natural Language Processing with Sequence Models/Week 1/C3W1_L2_Classes and Subclasses.ipynb
65 views
Classes and subclasses
In this notebook, I will show you the basics of classes and subclasses in Python. As you've seen in the lectures from this week, Trax uses layer classes as building blocks for deep learning models, so it is important to understand how classes and subclasses behave in order to be able to build custom layers when needed.
By completing this notebook, you will:
Be able to define classes and subclasses in Python
Understand how inheritance works in subclasses
Be able to work with instances
Part 1: Parameters, methods and instances
First, let's define a class My_Class.
My_Class has one parameter x without any value. You can think of parameters as the variables that every object assigned to a class will have. So, at this point, any object of class My_Class would have a variable x equal to None. To check this, I'll create two instances of that class and get the value of x for both of them.
For an existing instance you can assign new values for any of its parameters. In the next cell, assign a value of 5 to the parameter x of instance_a.
1.1 The __init__ method
When you want to assign values to the parameters of your class when an instance is created, it is necessary to define a special method: __init__. The __init__ method is called when you create an instance of a class. It can have multiple arguments to initialize the paramenters of your instance. In the next cell I will define My_Class with an __init__ method that takes the instance (self) and an argument y as inputs.
In this case, the parameter x of an instance from My_Class would take the value of an argument y. The argument self is used to pass information from the instance being created to the method __init__. In the next cell, create an instance instance_c, with x equal to 10.
Note that in this case, you had to pass the argument y from the __init__ method to create an instance of My_Class.
1.2 The __call__ method
Another important method is the __call__ method. It is performed whenever you call an initialized instance of a class. It can have multiple arguments and you can define it to do whatever you want like
Change a parameter,
Print a message,
Create new variables, etc.
In the next cell, I'll define My_Class with the same __init__ method as before and with a __call__ method that adds z to parameter x and prints the result.
Let’s create instance_d with x equal to 5.
And now, see what happens when instance_d is called with argument 10.
Now, you are ready to complete the following cell so any instance from My_Class:
Is initialized taking two arguments
yandzand assigns them tox_1andx_2, respectively. And,When called, takes the values of the parameters
x_1andx_2, sums them, prints and returns the result.
Run the next cell to check your implementation. If everything is correct, you shouldn't get any errors.
Addition of 10 and 15 is 25
All tests passed!
1.3 Custom methods
In addition to the __init__ and __call__ methods, your classes can have custom-built methods to do whatever you want when called. To define a custom method, you have to indicate its input arguments, the instructions that you want it to perform and the values to return (if any). In the next cell, My_Class is defined with my_method that multiplies the values of x_1 and x_2, sums that product with an input w, and returns the result.
Create an instance instance_f of My_Class with any integer values that you want for x_1 and x_2. For that instance, see the result of calling My_method, with an argument w equal to 16.
As you can corroborate in the previous cell, to call a custom method m, with arguments args, for an instance i you must write i.m(args). With that in mind, methods can call others within a class. In the following cell, try to define new_method which calls my_method with v as input argument. Try to do this on your own in the cell given below.
SPOILER ALERT Solution:
Part 2: Subclasses and Inheritance
Trax uses classes and subclasses to define layers. The base class in Trax is layer, which means that every layer from a deep learning model is defined as a subclass of the layer class. In this part of the notebook, you are going to see how subclasses work. To define a subclass sub from class super, you have to write class sub(super): and define any method and parameter that you want for your subclass. In the next cell, I define sub_c as a subclass of My_Class with only one method (additional_method).
2.1 Inheritance
When you define a subclass sub, every method and parameter is inherited from super class, including the __init__ and __call__ methods. This means that any instance from sub can use the methods defined in super. Run the following cell and see for yourself.
As you can see, sub_c does not have an initialization method __init__, it is inherited from My_class. However, you can overwrite any method you want by defining it again in the subclass. For instance, in the next cell define a class sub_c with a redefined my_Method that multiplies x_1 and x_2 but does not add any additional argument.
To check your implementation run the following cell.
In the next cell, two instances are created, one of My_Class and another one of sub_c. The instances are initialized with equal x_1 and x_2 parameters.
As you can see, even though sub_c is a subclass from My_Class and both instances are initialized with the same values, My_method returns different results for each instance because you overwrote My_method for sub_c.
Congratulations! You just reviewed the basics behind classes and subclasses. Now you can define your own classes and subclasses, work with instances and overwrite inherited methods. The concepts within this notebook are more than enough to understand how layers in Trax work.