官术网_书友最值得收藏!

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.

主站蜘蛛池模板: 天津市| 卓尼县| 融水| 崇州市| 博乐市| 广灵县| 潜山县| 南平市| 灌南县| 宜兴市| 大余县| 松滋市| 鹤山市| 东莞市| 东明县| 阿尔山市| 鄱阳县| 林州市| 句容市| 大同县| 高州市| 朝阳县| 汾西县| 册亨县| 双流县| 七台河市| 丰宁| 盐源县| 广宗县| 罗定市| 安乡县| 苏尼特左旗| 彰化县| 南安市| 瓮安县| 东台市| 茶陵县| 湾仔区| 白山市| 常州市| 南漳县|