Pytorch开发者手册
pytorch维度解释
input: seq_lenbatch_sizeinput_size
self.embedding=torch.nn.Embedding(input_size,emb_size)
output:seq_lenbatch_sizehidden_size
self.gru=torch.nn.GRU(emb_size,hidden_size,num_layers, bidirectional=True,dropout=0.8)
output,hidden=self.gru(embedd)
output: seq_lenbatch_size (hidden_sizenum_directions)
hidden:(n_layersnum_directions) * batch_size *hidden_size
一 、函数
(1)nn.MaxPool1d
class torch.nn.MaxPool1d(kernel_size, stride=None, padding=0, dilation=1, return_indices=False, ceil_mode=False)
kernel_size(int or tuple) - max pooling的窗口大小
stride(int or tuple, optional) - max pooling的窗口移动的步长。默认值是kernel_size
padding(int or tuple, optional) - 输入的每一条边补充0的层数
dilation(int or tuple, optional) – 一个控制窗口中元素步幅的参数
return_indices - 如果等于True,会返回输出最大值的序号,对于上采样操作会有帮助
ceil_mode - 如果等于True,计算输出信号大小的时候,会使用向上取整,代替默认的向下取整的操作
eg:
import torch
import torch.nn as nun
m = nn.MaxPool1d(3, stride=2)
input = torch.randn(2, 4, 5)
output = m(input)
print(input)
print(output)
tensor([[[ 1.0000, -0.2401, -1.1694, 0.3162, 0.5495],
[-0.0668, -0.1455, -0.6538, 0.7396, -1.2040],
[ 1.9750, 0.9449, 0.6117, 0.9990, -0.8750],
[ 0.5644, -0.2105, 0.1103, 1.2369, 0.3041]],
[[-0.6587, 0.5686, -0.4883, 1.4393, 2.0761],
[ 0.7883, 0.9219, 3.0924, 0.0957, 0.3198],
[ 1.3996, 0.7220, 0.8044, 1.4365, -0.9084],
[ 0.8209, 0.6308, 1.7780, -0.2451, -0.7157]]])
tensor([[[ 1.0000, 0.5495],
[-0.0668, 0.7396],
[ 1.9750, 0.9990],
[ 0.5644, 1.2369]],
[[ 0.5686, 2.0761],
[ 3.0924, 3.0924],
[ 1.3996, 1.4365],
[ 1.7780, 1.7780]]])
二、Tensor维度变换
维度变换的的主要函数有
torch.reshape() 或者torch.view()
torch.reshape() 可以改变张量的形状
torch.squeeze() 改变维度
torch.unsqueeze() 增加维度
torch.transpose() 可以交换维度
a=torch.Tensor(1,2,3,3)
print(a.size())
b=a.view([3,-1])
print(b.size())
c=torch.squeeze(a)
print(c.size())
e=torch.transpose(a,0,1)
print(e.size())
'''f=a.t() 只有小于等于2个维度才可以转置
print(f.size())'''
d=torch.unsqueeze(a,3)
print(d.size())
torch.Size([1, 2, 3, 3])
torch.Size([3, 6])
torch.Size([2, 3, 3])
torch.Size([2, 1, 3, 3])
torch.Size([1, 2, 3, 1, 3])
三、pytorch进行多GPU的计算
device=torch.device("cuda" if torch.cuda.is_available() else "cpu")
model=model.to(device)