例子 二维卷积层 torch.nn.Conv2d
m = torch.nn.Conv2d(16,33,3,stride=2)
#Conv2d(16, 33, kernel_size=(3, 3), stride=(2, 2))
m = torch.nn.Conv2d(16, 33, (3, 5), stride=(2, 1), padding=(4, 2))
#Conv2d(16, 33, kernel_size=(3, 5), stride=(2, 1), padding=(4, 2))
m = torch.nn.Conv2d(16, 33, (3, 5), stride=(2, 1), padding=(4, 2), dilation=(3, 1))
#Conv2d(16, 33, kernel_size=(3, 5), stride=(2, 1), padding=(4, 2), dilation=(3, 1))
input = torch.autograd.Variable(torch.randn(20, 16, 50, 100))
output = m(input)
得出
[[-1.4904e-01, 8.6588e-02, -2.8810e-02, ..., 1.5512e-01,
-2.1110e-02, -7.1621e-02],
[-2.7099e-01, -3.8170e-01, -2.0879e-01, ..., -2.3684e-01,
3.1174e-01, -3.6114e-03],
[-9.1671e-02, 1.1984e+00, -6.8564e-02, ..., 6.3686e-02,
-1.8320e-01, -6.7972e-03],
...,
[-3.3353e-01, 5.1329e-01, 2.9928e-01, ..., -5.1498e-01,
5.3441e-01, -4.0875e-01],
[-3.6444e-01, -5.9975e-02, 1.6595e-02, ..., 4.9555e-02,
6.0619e-01, -7.0390e-01],
[ 2.5910e-02, -3.7171e-01, -5.3760e-01, ..., -8.3047e-04,
8.8043e-02, 1.9482e-01]],
[[-1.4686e-01, 6.2633e-01, 6.6005e-03, ..., -3.1981e-01,
-1.9584e-02, 3.0828e-02],
[ 1.6752e-01, -1.1608e-01, -2.1795e-01, ..., -8.8395e-01,
-1.2678e-01, 2.0240e-02],
[-2.7970e-01, -4.3928e-02, 3.5947e-01, ..., -6.4956e-01,
2.6955e-02, -5.3012e-01],
...,
[ 2.9211e-01, 3.4733e-01, -5.9777e-02, ..., -5.2774e-01,
6.7063e-01, 1.8416e-01],
[-3.7045e-01, -4.6511e-01, 7.5518e-02, ..., 9.0909e-01,
-2.9998e-01, 2.0883e-01],
[-3.0611e-01, 7.1075e-01, 9.5404e-01, ..., -4.2552e-01,
4.8289e-01, -2.4924e-02]]]])
二 torch.nn.Linear 转置
class torch.nn.Linear(in_features, out_features, bias=True)
意思 对输入数据做线性变换:y = Ax + b. 其中
x为(30,20)
A;
b;
y;(30,30)
参数:
in_features - 每个输入样本的大小
out_features - 每个输出样本的大小
bias - 若设置为False,这层不会学习偏置。默认值:True
形状:
输入: (N,in_features)
输出: (N,out_features)
变量:
weight -形状为(out_features x in_features)的模块中可学习的权值
bias -形状为(out_features)的模块中可学习的偏置
torch.mm(x,y)😒 = x*y
torch.equal:如果两个张量有相同的形状和元素值,则返回True ,否则 False。(相当于bool)
x = torch.randn(30, 20) # 输入的维度是(30,20)
m = torch.nn.Linear(20, 30) # 20,30是指维度
print(m)
output = m(x)
print('m.weight.shape:\n ', m.weight.shape)
print('m.bias.shape:\n', m.bias.shape)
print('output.shape:\n', output.shape)
# ans = torch.mm(input,torch.t(m.weight))+m.bias 等价于下面的
ans = torch.mm(x, m.weight.t()) + m.bias
print('ans.shape:\n', ans.shape)
print(torch.equal(ans, output))
输出
(30,20)*(20,30)=(30,30)
Linear(in_features=20, out_features=30, bias=True)
m.weight.shape:
torch.Size([30, 20])
m.bias.shape:
torch.Size([30])
output.shape:
torch.Size([30, 30])
ans.shape:
torch.Size([30, 30])
True
三torch.autograd.Variable 梯度包装
autograd.Variable是torch.autograd中很重要的类。(Variiable是被封装好的类)它用来包装Tensor,将Tensor转换为Variable之后,可以装载梯度信息。
Variable用来构建一个计算图中的节点。将Tensor转换为Variabla类型之后,该Tensor就成了计算图中的一个节点。对于该节点,有两个重要的特性:
**data:**获得该节点的值,即Tensor类型的值
**gred:**获得该节点处的梯度信息
关于Variable的参数
requires_grad:有两个值,True和False,True代表此变量处需要计算梯度,False代表不需要。
grad_fn:可以得知该变量是不是一个计算结果,若是,则grad_fn返回一个与该函数相关的对象,否则是None。
例子1
x = torch.autograd.Variable(torch.randn(2,2))
y = torch.autograd.Variable(torch.randn(2,2))
z = torch.autograd.Variable(torch.randn(2,2),requires_grad = True)
a = x + y
b = a +z
print("--------a-----")
print(a)
print("--------b-----")
print(b)
print("--------c------")
c= a+b
print(c)
输出
--------a-----
tensor([[ 0.1993, -1.0963],
[-0.7935, -1.6006]])
--------b-----
tensor([[-0.5151, -1.2130],
[-1.6000, -2.7531]])
--------c------
tensor([[-0.3158, -2.3092],
[-2.3935, -4.3537]])
四Pooling 函数
torch.nn.functional.avg_pool1d(input, kernel_size, stride=None, padding=0, ceil_mode=False, count_include_pad=True)
定义:对由几个输入平面组成的输入信号进行一维平均池化。
参数:
kernel_size – 窗口的大小 -
stride – 窗口的步长
padding – 在两边添加隐式零填充 -
ceil_mode – 当为True时,将使用ceil代替floor来计算输出形状 - count_include_pad – 当为True时,这将在平均计算时包括补零
例子:
import torch.nn.functional as F
import torch
input =torch.torch.autograd.Variable(torch.Tensor([[[1,2,3,4,5,6,7]]]))
F.avg_pool1d(input, kernel_size=3, stride=2)
输出:
tensor([[[ 2., 4., 6.]]])
定义矩阵:torch.Tensor([[[1,2,3,4,5,6,7]]]) (未完)
torch.nn.functional.avg_pool2d(input, kernel_size, stride=None, padding=0, ceil_mode=False, count_include_pad=True)
定义:在kh x kw区域中应用步长为dh x dw的二维平均池化操作。输出特征的数量等于输入平面的数量。
参数:
input – 输入的张量 (minibatch x in_channels x iH x iW) -
kernel_size – 池化区域的大小,可以是单个数字或者元组 (kh x kw) -
stride – 池化操作的步长,可以是单个数字或者元组 (sh x sw)。默认等于核的大小 -
padding – 在输入上隐式的零填充,可以是单个数字或者一个元组 (padh x padw),默认: 0 -
ceil_mode – 定义空间输出形状的操作 -
count_include_pad – 除以原始非填充图像内的元素数量或kh * kw