- Deep Learning with PyTorch
- Vishnu Subramanian
- 489字
- 2021-06-24 19:16:27
Layers – fundamental blocks of neural networks
Throughout the rest of the chapter, we will come across different types of layers. To begin, let's try to understand one of the most important layers, the linear layer, which does exactly what our previous network architecture does. The linear layer applies a linear transformation:
What makes it powerful is that fact that the entire function that we wrote in the previous chapter can be written in a single line of code, as follows:
from torch.nn import Linear
myLayer = Linear(in_features=10,out_features=5,bias=True)
The myLayer in the preceding code will accept a tensor of size 10 and outputs a tensor of size 5 after applying linear transformation. Let's look at a simple example of how to do that:
inp = Variable(torch.randn(1,10))
myLayer = Linear(in_features=10,out_features=5,bias=True)
myLayer(inp)
We can access the trainable parameters of the layer using the weights and bias attributes:
myLayer.weight
Output :
Parameter containing:
-0.2386 0.0828 0.2904 0.3133 0.2037 0.1858 -0.2642 0.2862 0.2874 0.1141
0.0512 -0.2286 -0.1717 0.0554 0.1766 -0.0517 0.3112 0.0980 -0.2364 -0.0442
0.0776 -0.2169 0.0183 -0.0384 0.0606 0.2890 -0.0068 0.2344 0.2711 -0.3039
0.1055 0.0224 0.2044 0.0782 0.0790 0.2744 -0.1785 -0.1681 -0.0681 0.3141
0.2715 0.2606 -0.0362 0.0113 0.1299 -0.1112 -0.1652 0.2276 0.3082 -0.2745
[torch.FloatTensor of size 5x10]
myLayer.bias
Output :
Parameter containing:
-0.2646
-0.2232
0.2444
0.2177
0.0897
[torch.FloatTensor of size 5
Linear layers are called by different names, such as dense or fully connected layers across different frameworks. Deep learning architectures used for solving real-world use cases generally contain more than one layer. In PyTorch, we can do it in multiple ways, shown as follows.
One simple approach is passing the output of one layer to another layer:
myLayer1 = Linear(10,5)
myLayer2 = Linear(5,2)
myLayer2(myLayer1(inp))
Each layer will have its own learnable parameters. The idea behind using multiple layers is that each layer will learn some kind of pattern that the later layers will build on. There is a problem in adding just linear layers together, as they fail to learn anything new beyond a simple representation of a linear layer. Let's see through a simple example of why it does not make sense to stack multiple linear layers together.
Let's say we have two linear layers with the following weights:

The preceding architecture with two different layers can be simply represented as a single layer with a different layer. Hence, just stacking multiple linear layers will not help our algorithms to learn anything new. Sometimes, this can be unclear, so we can visualize the architecture with the following mathematical formulas:


To solve this problem, we have different non-linearity functions that help in learning different relationships, rather than only focusing on linear relationships.
There are many different non-linear functions available in deep learning. PyTorch provides these non-linear functionalities as layers and we will be able to use them the same way we used the linear layer.
Some of the popular non-linear functions are as follows:
- Sigmoid
- Tanh
- ReLU
- Leaky ReLU
- 新型電腦主板關(guān)鍵電路維修圖冊(cè)
- 現(xiàn)代辦公設(shè)備使用與維護(hù)
- INSTANT ForgedUI Starter
- The Deep Learning with Keras Workshop
- CC2530單片機(jī)技術(shù)與應(yīng)用
- 微軟互聯(lián)網(wǎng)信息服務(wù)(IIS)最佳實(shí)踐 (微軟技術(shù)開發(fā)者叢書)
- 計(jì)算機(jī)組裝維修與外設(shè)配置(高等職業(yè)院校教改示范教材·計(jì)算機(jī)系列)
- 基于PROTEUS的電路設(shè)計(jì)、仿真與制板
- Java Deep Learning Cookbook
- 電腦組裝與維護(hù)即時(shí)通
- 單片微機(jī)原理及應(yīng)用
- Spring Security 3.x Cookbook
- 微控制器的應(yīng)用
- Instant Website Touch Integration
- 詳解FPGA:人工智能時(shí)代的驅(qū)動(dòng)引擎