torch.nn
目录
1. torch.nn.Linear(in_features, out_features, bias=True)
2. torch.nn.MSELoss(size_average=None, reduce=None, reduction='mean')
5. torch.nn.ReLU(inplace=False)
1. torch.nn.
Linear
(in_features, out_features, bias=True)
应用一个线性变换
Parameters: |
|
---|
- Input: (N,∗,in_features) where ∗ means any number of additional dimensions。
- Output: (N,∗,out_features) where all but the last dimension are the same shape as the input.除了最后一维,其它维度与in_features相同。
import torch.nn as nn m = nn.Linear(20, 30) input = torch.randn(128, 20) output = m(input) print(output.size()) torch.Size([128, 30])
-
2.
torch.nn.
MSELoss
(size_average=None, reduce=None, reduction='mean')
mean squared error (squared L2 norm):均方误差。
Parameters: |
|
---|
import torch
import torch.nn as nn
loss = nn.MSELoss()
input = torch.randn(3, 5, requires_grad=True)
target = torch.randn(3, 5)
output = loss(input, target)
output.backward()
3. torch.nn.
Sequential
(*args)
顺序容器。模型会按构造器中的顺序添加它。
# Example of using Sequential
model = nn.Sequential(
nn.Conv2d(1,20,5),
nn.ReLU(),
nn.Conv2d(20,64,5),
nn.ReLU()
)
# Example of using Sequential with OrderedDict
model = nn.Sequential(OrderedDict([
('conv1', nn.Conv2d(1,20,5)),
('relu1', nn.ReLU()),
('conv2', nn.Conv2d(20,64,5)),
('relu2', nn.ReLU())
]))
4. torch.nn.Conv2d
(in_channels, out_channels, kernel_size, stride=1, padding=0, dilation=1, groups=1, bias=True)
2维卷积
-
stride
controls the stride for the cross-correlation, a single number or a tuple. 步长,单个数或元组。 -
padding
controls the amount of implicit zero-paddings on both sides forpadding
number of points for each dimension.上下左右两侧0填充的数量 -
dilation
controls the spacing between the kernel points; also known as the à trous algorithm. It is harder to describe, but this link has a nice visualization of whatdilation
does. -
groups
controls the connections between inputs and outputs.in_channels
andout_channels
must both be divisible bygroups
. For example, 控制输入与输出的连接 - At groups=1, all inputs are convolved to all outputs. 所有输入卷积到所有输出
- At groups=2, the operation becomes equivalent to having two conv layers side by side, each seeing half the input channels, and producing half the output channels, and both subsequently concatenated.该操作等价于并排使用两个conv层,每个conv层可以看到一半的输入通道,并生成一半的输出通道,然后将这两个conv层连接起来。
- At groups=
in_channels
, each input channel is convolved with its own set of filters, of size:
kernel_size
, stride
, padding
, dilation可以是一个数,表示长宽一样,也可以是两个元素的元组,分别表示长宽。
Parameters: |
|
---|
>>> # With square kernels and equal stride
>>> m = nn.Conv2d(16, 33, 3, stride=2)
>>> # non-square kernels and unequal stride and with padding
>>> m = nn.Conv2d(16, 33, (3, 5), stride=(2, 1), padding=(4, 2))
>>> # non-square kernels and unequal stride and with padding and dilation
>>> m = nn.Conv2d(16, 33, (3, 5), stride=(2, 1), padding=(4, 2), dilation=(3, 1))
>>> input = torch.randn(20, 16, 50, 100)
>>> output = m(input)
5. torch.nn.
ReLU
(inplace=False)
>>> m = nn.ReLU()
>>> input = torch.randn(2)
>>> output = m(input)
6. torch.nn.
MaxPool2d
(kernel_size, stride=None, padding=0, dilation=1, return_indices=False, ceil_mode=False)
2维最大池化。
If padding
is non-zero, then the input is implicitly zero-padded on both sides for padding
number of points. dilation
controls the spacing between the kernel points. It is harder to describe, but this link has a nice visualization of what dilation
does.
The parameters kernel_size
, stride
, padding
, dilation
can either be:
- a single
int
– in which case the same value is used for the height and width dimension- a
tuple
of two ints – in which case, the first int is used for the height dimension, and the second int for the width dimension
Parameters: |
|
---|
>>> # pool of square window of size=3, stride=2
>>> m = nn.MaxPool2d(3, stride=2)
>>> # pool of non-square window
>>> m = nn.MaxPool2d((3, 2), stride=(2, 1))
>>> input = torch.randn(20, 16, 50, 32)
>>> output = m(input)
7. torch.nn.
CrossEntropyLoss
(weight=None, size_average=None, ignore_index=-100, reduce=None, reduction='mean')
交叉熵损失函数。
This criterion combines nn.LogSoftmax()
and nn.NLLLoss()
in one single class.
It is useful when training a classification problem with C classes. If provided, the optional argument weight
should be a 1D Tensor assigning weight to each of the classes. This is particularly useful when you have an unbalanced training set.
The losses are averaged across observations for each minibatch.
Parameters: |
|
---|
>>> loss = nn.CrossEntropyLoss()
>>> input = torch.randn(3, 5, requires_grad=True)
>>> target = torch.empty(3, dtype=torch.long).random_(5)
>>> output = loss(input, target)
>>> output.backward()
8. torch.nn.
Tanh
tanh**函数。
元素级应用公式:
>>> m = nn.Tanh()
>>> input = torch.randn(2)
>>> output = m(input)
9. torch.nn.
Sigmoid
sigmoid**函数。
>>> m = nn.Sigmoid()
>>> input = torch.randn(2)
>>> output = m(input)
上一篇: 【C#】——注册表技术(二)
下一篇: C#读取windows注册表键值的代码