pytorch 学习笔记1 —— 变量操作

为什么学习pytorch

  • 替代numpy,使得可以更好使用GPUs
  • 为深度学习提供最大的灵活性和速度

学习前提

确保安装了torch的包,以下句子可以顺利执行

import torch

变量操作

来源:https://pytorch.org/tutorials/beginner/blitz/tensor_tutorial.html#sphx-glr-beginner-blitz-tensor-tutorial-py

声明

  • 没有初始化(垃圾值)
x = torch.empty(5, 3)
print(x)

out:
tensor([[8.4490e-39, 9.2755e-39, 1.0653e-38],
        [8.4489e-39, 9.6429e-39, 8.4490e-39],
        [9.6429e-39, 9.2755e-39, 1.0286e-38],
        [9.0919e-39, 8.9082e-39, 9.2755e-39],
        [8.4490e-39, 9.2755e-39, 9.6429e-39]])
  • 使用随机值初始化
x = torch.rand(5, 3)
print(x)

out:
tensor([[0.8901, 0.3888, 0.8734],
        [0.8738, 0.0639, 0.2489],
        [0.8157, 0.1134, 0.3066],
        [0.7617, 0.9803, 0.4271],
        [0.1756, 0.1482, 0.2855]])
  • 全部赋0(或者1)
x = torch.zeros(5, 3, dtype=torch.long)
print(x)
# 同理:ones

out:
tensor([[0, 0, 0],
        [0, 0, 0],
        [0, 0, 0],
        [0, 0, 0],
        [0, 0, 0]])
  • 使用值进行初始化
x = torch.tensor([1, 2, 5.5])
print(x)

out:
tensor([1.0000, 2.0000, 5.5000])

变量属性

  • 获取变量的格式大小:x.size()
x = torch.zeros(5, 3, dtype=torch.long)
print(x.size())

out:
torch.Size([5, 3]) # 返回的是一个tuple

变量操作

  • 相加减
x = torch.ones(5, 3)
y = torch.ones(5, 3)
res = x + y
  • 直接操作原数据
y.add_(x)
# 把x加到y上
# 任何加上‘_’都会改变原来的值
  • 取部分:x[:,1]
print(y)
print(y[:,1])

out:
tensor([[2., 2., 2.],
        [2., 2., 2.],
        [2., 2., 2.],
        [2., 2., 2.],
        [2., 2., 2.]])
tensor([2., 2., 2., 2., 2.])
  • 改变形状reshape:使用torch.view
x = torch.randn(4, 4)
y = x.view(16)
print(y.size())
print(y)
z = x.view(-1, 8) # -1将自动计算维度
print(z.size())
print(z)

out:
torch.Size([16])
tensor([-0.9323,  0.0361,  0.6241, -0.4999, -1.1119, -2.3558,  1.6778,  0.5107,
         0.0895,  1.2031,  0.2117,  1.2669, -0.5290,  0.5751, -0.2783,  0.3618])
torch.Size([2, 8])
tensor([[-0.9323,  0.0361,  0.6241, -0.4999, -1.1119, -2.3558,  1.6778,  0.5107],
        [ 0.0895,  1.2031,  0.2117,  1.2669, -0.5290,  0.5751, -0.2783,  0.3618]])

和numpy的转换

  • 将一个torch tensor转化为numpy array
a = torch.ones(5)
print(a)
b = a.numpy()
print(b)

out:
tensor([1., 1., 1., 1., 1.])
[1. 1. 1. 1. 1.]
  • 将一个numpy array 转化为torch tensor
import numpy as np
a = np.ones(5)
b = torch.from_numpy(a)
print(b)

out:
tensor([1., 1., 1., 1., 1.], dtype=torch.float64)
  • 关于和numpy的关系,可以看官网教程的说法
Numpy is a great framework, but it cannot utilize GPUs to accelerate its numerical computations. For modern deep neural networks, GPUs often provide speedups of 50x or greater, so unfortunately numpy won’t be enough for modern deep learning.

Here we introduce the most fundamental PyTorch concept: the Tensor. A PyTorch Tensor is conceptually identical to a numpy array: a Tensor is an n-dimensional array, and PyTorch provides many functions for operating on these Tensors. Behind the scenes, Tensors can keep track of a computational graph and gradients, but they’re also useful as a generic tool for scientific computing.

Also unlike numpy, PyTorch Tensors can utilize GPUs to accelerate their numeric computations. To run a PyTorch Tensor on GPU, you simply need to cast it to a new datatype.

大致意思有以下几点:

  • numpy无法使用GPUs加速计算。而GPUs加速的话可以快50倍以上。pytorch是可以使用GPUs加速的。
  • pytorch tensor概念上和numpy完全相同。同样提供了很多相似的操作。
  • tensor可以追踪计算图和梯度(这在后面介绍梯度的部分会用到)

CUDA tensor

  • 将tensor移动到任何设备:使用.to方法
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([0.3882], device='cuda:0')
tensor([0.3882], dtype=torch.float64)

(由于本机不支持CUDA,所以该示例没办法测试…)

你可能感兴趣的:(pytorch,pytorch,变量操作)