什么是pytorch(1开始)(翻译)

it2024-07-27  12

Deep Learning with PyTorch: A 60 Minute Blitz

作者: Soumith Chintala

部分翻译:me

本内容包含:

在高级层面理解pytorch的tensor库以及神经网络。训练一个用于图像分类的小的神经网络。

This tutorial assumes that you have a basic familiarity of numpy

阅读本文前,你需要具有numpy的知识。

当然需要安装好pytorch和torchvision库。

开始

张量

张量类似于 NumPy的N维数组, 添加了可以在GPU上进行加速计算。

from __future__ import print_functionimport torch

构建一个5*3的矩阵,没有初始化:

x = torch.empty(5, 3)    #全部都是0.0print(x)

print(x.dtype) #数据类型  float32print(type(x)) 

tensor([[0.0000e+00, 0.0000e+00, 0.0000e+00], [0.0000e+00, 0.0000e+00, 0.0000e+00], [0.0000e+00, 0.0000e+00, 0.0000e+00], [0.0000e+00, 1.5190e-42, 0.0000e+00], [0.0000e+00, 1.1628e+27, 0.0000e+00]]) torch.float32 <class 'torch.Tensor'>

 构建一个随机的矩阵

x = torch.rand(5, 3)print(x)

tensor([[0.5689, 0.6057, 0.5855], [0.4125, 0.2739, 0.7563], [0.8674, 0.7034, 0.5811], [0.9939, 0.5941, 0.6916], [0.9194, 0.8064, 0.3800]])

 

构建一个填充为零的矩阵和类型为长整型(long):

x = torch.zeros(5, 3, dtype=torch.long)print(x)

tensor([[0, 0, 0], [0, 0, 0], [0, 0, 0], [0, 0, 0], [0, 0, 0]])

直接从数据构建一个Tensor:

x = torch.tensor([5.5, 3])print(x)

tensor([5.5000, 3.0000])

或者基于现有的张量创建一个新的。这些方法会复用输入张量的性质,例如:dtype,除非一个新的值提供给用户。

print(x)x = x.new_ones(5, 3, dtype=torch.double)      # new_* methods take in sizes    #这个搞不懂不建议print(x)x = torch.randn_like(x, dtype=torch.float)    # override dtype!    #产生同样类型的建议使用torch.randn_like, torch.ones_like(tensor)print(x)   

tensor([5.5000, 3.0000]) tensor([[1., 1., 1.], [1., 1., 1.], [1., 1., 1.], [1., 1., 1.], [1., 1., 1.]], dtype=torch.float64) tensor([[-0.9820, -0.4020, 0.6092], [-0.1853, 0.6631, -0.9670], [-0.1934, 1.3743, -0.5245], [ 1.0157, -0.0022, -0.1337], [-0.7105, 0.4798, 2.2316]])

获取张量的大小:

print(x.size())

h,w=x.size()print(h,w)

torch.Size([5, 3]) 5 3

注意:torch.Size实际上是一个元组, 所以支持所有元组的操作。.

运算

运算有多种语法格式。在下面的例子里,我们看加法运算。

加法运算的语法1:

x = torch.rand(5, 3)

y = torch.rand(5, 3)print(x + y)

tensor([[-0.3402, 0.4303, 0.7074], [ 0.4024, 1.4834, -0.7325], [ 0.4572, 1.8391, -0.0452], [ 1.2108, 0.9043, 0.6351], [-0.6921, 0.9278, 2.4968]])

加法运算的语法2:

print(torch.add(x, y))

tensor([[-0.3402, 0.4303, 0.7074], [ 0.4024, 1.4834, -0.7325], [ 0.4572, 1.8391, -0.0452], [ 1.2108, 0.9043, 0.6351], [-0.6921, 0.9278, 2.4968]])

把结果作为参数:

result = torch.empty(5, 3)torch.add(x, y, out=result)print(result)

tensor([[-0.3402, 0.4303, 0.7074], [ 0.4024, 1.4834, -0.7325], [ 0.4572, 1.8391, -0.0452], [ 1.2108, 0.9043, 0.6351], [-0.6921, 0.9278, 2.4968]])

加法:直接加到某参数:

# adds x to yy.add_(x)print(y)

注意:任何以“_”结尾的运算,都会改变张量自身。例如: x.copy_(y), x.t_(), 将会改变 x.

 

可以使用NUmpy里的切片方法对Tensor切片!

print(x[:, 1])

缩放:如果你想对张量缩放/改变维度,可以使用torch.view:

x = torch.randn(4, 4)y = x.view(16)z = x.view(-1, 8)  # the size -1 is inferred from other dimensionsprint(x.size(), y.size(), z.size())v=x.view(-1)  #-1直接把他拉直了。print(v.size())

torch.Size([4, 4]) torch.Size([16]) torch.Size([2, 8]) torch.Size([16])

如果你有一个元素的张量,使用.item() 方法来得到其Python自身的数值类型。

x = torch.randn(1)print(x)print(x.item())print(x[0])print(x[0].item())

tensor([0.4783]) 0.4782998859882355 tensor(0.4783) 0.4782998859882355

后续阅读:

torch有100+ 个张量运算符, 包括转置,切片,数学运算,线性代数,随机数,等,参见: https://pytorch.org/docs/stable/torch.html

 

连接NumPy

在numpy的数组和torch的tensor 间的转换非常容易。

torch的tensor和numpy的数组共享内部的存储单元,改变一个,另一个也改变。例子:

将Torch 张量转为一个Numpy数组

a = torch.ones(5) print(a)

Out:

tensor([1., 1., 1., 1., 1.]) b = a.numpy() print(b)

Out:

[1. 1. 1. 1. 1.]

可以看到数组里的值发生变换了,也就说一个张量和与之对应的numpy数组是共享内存的:

a.add_(1) print(a) print(b)

Out:

tensor([2., 2., 2., 2., 2.]) [2. 2. 2. 2. 2.]

将 NumPy 数组to Torch 张量

例子:

import numpy as np a = np.ones(5) b = torch.from_numpy(a) np.add(a, 1, out=a) print(a) print(b)

Out:

[2. 2. 2. 2. 2.] tensor([2., 2., 2., 2., 2.], dtype=torch.float64)

除CharTensor外,所有CPU上的张量支持与numpy 数组间的转换。

CUDA 张量

张量可以被移动到任何设备上,通过.to方法。

# let us run this cell only if CUDA is available 检测CUDA是否可用 # We will use ``torch.device`` objects to move tensors in and out of GPU 可以使用torch.device对象来移动对象 if torch.cuda.is_available(): device = torch.device("cuda") # a CUDA device object y = torch.ones_like(x, device=device) # directly create a tensor on GPU x = x.to(device) # or just use strings ``.to("cuda")`` z = x + y print(z) print(z.to("cpu", torch.double)) # ``.to`` can also change dtype together!

Out:

tensor([2.9218], device='cuda:0') tensor([2.9218], dtype=torch.float64)

这里注意的是torch.cuda.is_available()  torch.device('cuda')   device参数,.to(device)

转载于:https://www.cnblogs.com/yjphhw/p/9777028.html

最新回复(0)