了解Pytorch|Get Started with PyTorch
作者:互联网
Basics
就像Tensorflow一样,我们也将继续在PyTorch中玩转Tensors。
从数据(列表)中创建张量
data = [[1, 2],[3, 4]]
tensors = torch.tensor(data)
tensors
tensor([[1, 2],
[3, 4]])
从NumPy创建
np_array = np.arange(10)
tensor_np = torch.from_numpy(np_array)
tensor_np
tensor([0, 1, 2, 3, 4, 5, 6, 7, 8, 9], dtype=torch.int32)
形状、ndim和dtype
这与我们在《Numpy教程--第1天》中看到的相同。
tensor_np.shape
torch.Size([10])
tensor_np.ndim
1
tensor_np.dtype
torch.int32
张量操作(Tensor_Operations)
ten1 = torch.tensor([1,2,3])
ten2 = torch.tensor([4,5,6])
ten1+ten2
tensor([5, 7, 9])
你可以使用+
或torch.add
来执行张量添加。
torch.sub(ten2,ten1)
tensor([3, 3, 3])
torch.add(ten1,ten2)
tensor([5, 7, 9])
torch.subtract(ten2,ten1)
tensor([3, 3, 3])
你可以使用-
或torch.sub
来执行张量添加。
ten1*10
tensor([10, 20, 30])
深度学习中非常重要的操作--矩阵乘法
Rules of Matrix Multiplication:
- (3,2) * (3,2) = Error
- (4,3) * (3,2) = (4,2)
- (2,2) * (2,5) = (2,5)
torch.matmul(ten1,ten2)
tensor(32)
matrix4_3 = torch.tensor([[1,2,3],
[4,5,6],
[7,8,9],
[10,11,12]])
matrix4_3.shape
torch.Size([4, 3])
matrix3_2 = torch.tensor([[1,2],
[3,4],
[5,6]])
matrix3_2.shape
torch.Size([3, 2])
result = torch.matmul(matrix4_3,matrix3_2) #=> will result in (4,2)
result
tensor([[ 22, 28],
[ 49, 64],
[ 76, 100],
[103, 136]])
result.shape
torch.Size([4, 2])
你也可以使用torch.mm()
,这是torch.matmul()
的简称。
torch.mm(matrix4_3,matrix3_2)
tensor([[ 22, 28],
[ 49, 64],
[ 76, 100],
[103, 136]])
#张量的转置
matrix4_3
tensor([[ 1, 2, 3],
[ 4, 5, 6],
[ 7, 8, 9],
[10, 11, 12]])
matrix4_3.T
tensor([[ 1, 4, 7, 10],
[ 2, 5, 8, 11],
[ 3, 6, 9, 12]])
torch.t(matrix4_3)
tensor([[ 1, 4, 7, 10],
[ 2, 5, 8, 11],
[ 3, 6, 9, 12]])
更多张量操作
- Zeros
- Ones
- Random
- Full
tensorZeroes = torch.zeros((3,3))
tensorZeroes
tensor([[0., 0., 0.],
[0., 0., 0.],
[0., 0., 0.]])
tensorOnes = torch.ones((3,3))
tensorOnes
tensor([[1., 1., 1.],
[1., 1., 1.],
[1., 1., 1.]])
tensorRandomN = torch.randn((3,3)) #includes negative tensors
tensorRandomN
tensor([[ 1.3255, -0.4937, 1.0488],
[ 1.1797, -0.5422, -0.9703],
[-0.1761, 1.0742, 0.5459]])
tensorRandom = torch.rand((3,3)) #includes only positive tensors
tensorRandom
tensor([[0.2013, 0.9272, 0.7866],
[0.5887, 0.9900, 0.3554],
[0.6128, 0.3316, 0.6635]])
customFill = torch.full((3,3),5)
customFill
tensor([[5, 5, 5],
[5, 5, 5],
[5, 5, 5]])
initialFill = torch.full((3,3),0.01)
initialFill
tensor([[0.0100, 0.0100, 0.0100],
[0.0100, 0.0100, 0.0100],
[0.0100, 0.0100, 0.0100]])
快速入门Torchvision
安装Torchvision,Torchvision软件包,包括流行的数据集、模型架构和计算机视觉的常见图像转换。
!pip install torchvision --no-deps -i https://pypi.tuna.tsinghua.edu.cn/simple
from torch.utils.data import DataLoader
from torchvision import datasets
from torchvision.transforms import ToTensor
from torch import nn
# Download training data from open datasets.
training_data = datasets.FashionMNIST(
root="data",
train=True,
download=True,
transform=ToTensor(),
)
# Download test data from open datasets.
test_data = datasets.FashionMNIST(
root="data",
train=False,
download=True,
transform=ToTensor(),
)
type(training_data)
torchvision.datasets.mnist.FashionMNIST
Dataloader
在我们的数据集上包裹了一个迭代器,并支持自动批处理、采样、洗牌和多进程数据加载。这里我们定义了一个64的批处理量,即dataloader可迭代的每个元素将返回64个特征和标签的批次。