Creating Data Model using Custom Module
There is another way to find the prediction. In the previous section, we find the prediction using forward () and by implementing a linear model. This method is very efficient and reliable. It is easy to understand and implement.
In the Custom Module, we create a customize module with class, and it's init() and forward() method and model. The init() method is used to initialize the new instances of the class. In this init() method the first argument is self, which indicates the instance of the class the object that's yet to be initialized and after itself, we can add additional arguments.
Next parameter is to initialize the instance of the linear model. In our previous section initializing a linear model requires input size, as well as output size, equals to 1 but in the custom module, we pass input size and output size variable without passing its default value.
In this, it is required to import the nn package of the torch. In this, we use inheritance such that this subclass will leverage code from our base class and in the module.
The module itself will typically act as a base class for all neural network modules. After that, we create a model through which we make a prediction.
Let see an example how prediction is done by creating custom module:
For single data
<torch._C.Generator object at 0x000001B9B6C4E2B0> tensor([0.0739], grad_fn=<AddBackward0>)
For multiple data
<torch._C.Generator object at 0x000001B9B6C4E2B0> tensor([[0.0739], [0.5891], [1.1044]], grad_fn=<AddmmBackward>)