- Deep Learning with PyTorch
- Vishnu Subramanian
- 212字
- 2021-06-24 19:16:28
The PyTorch way of building deep learning algorithms
All the networks in PyTorch are implemented as classes, subclassing a PyTorch class called nn.Module, and should implement __init__ and forward methods. Inside the init function, we initialize any layers, such as the linear layer, which we covered in the previous section. In the forward method, we pass our input data into the layers that we initialized in our init method and return our final output. The non-linear functions are often directly used in the forward function and some use it in the init method too. The following code snippet shows how a deep learning architecture is implemented in PyTorch:
class MyFirstNetwork(nn.Module):
def __init__(self,input_size,hidden_size,output_size):
super(MyFirstNetwork,self).__init__()
self.layer1 = nn.Linear(input_size,hidden_size)
self.layer2 = nn.Linear(hidden_size,output_size)
def __forward__(self,input):
out = self.layer1(input)
out = nn.ReLU(out)
out = self.layer2(out)
return out
If you are new to Python, some of the preceding code could be difficult to understand, but all it is doing is inheriting a parent class and implementing two methods in it. In Python, we subclass by passing the parent class as an argument to the class name. The init method acts as a constructor in Python and super is used to pass on arguments of the child class to the parent class, which in our case is nn.Module.
- 圖解西門子S7-200系列PLC入門
- Learning AngularJS Animations
- Python GUI Programming:A Complete Reference Guide
- 電腦軟硬件維修大全(實例精華版)
- SDL Game Development
- 計算機組裝·維護與故障排除
- BeagleBone By Example
- 嵌入式系統設計教程
- 從零開始學51單片機C語言
- Mastering Manga Studio 5
- 電腦軟硬件維修從入門到精通
- Creating Flat Design Websites
- OpenGL Game Development By Example
- Hands-On Artificial Intelligence for Banking
- BeagleBone Robotic Projects