pytorch 学习笔记–Getting stared
1.什么是pytorch
Pytorch 是一个基于Python的科学计算包,主要面向以下人群:
- 替代numpy以使用GPU做计算加速
- 一个深度学习平台,有着很好的灵活性和运算速度
2.Getting started
- Tensor
Tensor(张量) 类似于numpy中的ndarrays(n维数组),Tensor可以被用在GPU上做计算加速
from future import print_function
import torch
创建一个5X3的未初始化的矩阵:
x = torch.empty(5,3)
print(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.8946e-42, 0.0000e+00],
[0.0000e+00, 8.4490e-39, 0.0000e+00]])
创建一个随机初始化的矩阵:
x = torch.rand(5,3)
print(x)
输出:
tensor([[0.7069, 0.0923, 0.6649],
[0.2894, 0.3824, 0.4164],
[0.6907, 0.7984, 0.2221],
[0.9965, 0.0228, 0.8455],
[0.9214, 0.0069, 0.3825]])
创建一个全为零的矩阵:
x = torch.zero(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])
基于已有的tensor创建一个新的Tensor,新Tensor将继承已有Tensor的数据结构,比如:dtype,size
x = x.new_ones(5,3,dtype = torch.double) # new_* 就是生产的值为*
print(x)
x = torch.randn_like(x,dtype = torch.float)
print(x)
输出:
tensor([[1., 1., 1.],
[1., 1., 1.],
[1., 1., 1.],
[1., 1., 1.],
[1., 1., 1.]], dtype=torch.float64)
tensor([[0.6666, 0.6183, 0.0078],
[0.6348, 0.8249, 0.7977],
[0.1475, 0.3109, 0.8649],
[0.5252, 0.7206, 0.1772],
[0.3243, 0.4928, 0.0145]])
取Tensor的形状:
print(x.size())
输出:
torch.Size([5, 3])
torch.size是一个元组,支出元组的所有操作
3. 数学运算
加法运算1:
y = torch.rand(5,3)
print(x + y)
输出:
tensor([[1.3393, 0.7669, 0.4820],
[1.5721, 1.1863, 1.5516],
[0.3933, 1.0585, 1.1913],
[0.5856, 0.8414, 0.3255],
[0.5534, 1.2844, 0.1513]])
加法运算2:
print(torch.add(x,y))
输出:
tensor([[1.3393, 0.7669, 0.4820],
[1.5721, 1.1863, 1.5516],
[0.3933, 1.0585, 1.1913],
[0.5856, 0.8414, 0.3255],
[0.5534, 1.2844, 0.1513]])
加法运算:将结果传递给另外一个Tensor
result = torch.empty(5,3)
torch.add(x,y,out = result)
print(result)
输出:
tensor([[ 1.7930, 0.5192, -0.0209],
[ 0.4295, 1.6210, 0.3139],
[ 0.8146, 0.1348, 1.8822],
[-0.0063, 0.9672, 0.9287],
[ 1.9607, 0.0175, 1.1421]])
原地加法运算:
y.add_(x) # y原地加x
print(y)
输出:
tensor([[0.8036, 0.9923, 1.4657],
[0.7780, 0.9869, 1.1723],
[1.5014, 0.9099, 0.9040],
[0.9749, 1.0438, 0.6896],
[1.1903, 1.5337, 1.3718]])
任何Tensor的原地操作只需在方法后加_ 比如 y.copy_()
对于numpy的数组的操作在这里同样适用 :
print(x[:,1]) #打印 第二列的所有值
输出:
tensor([0.1538, 0.3332, 0.7185, 0.5168, 0.9258])
Resizing:如果想改变Tensor的形状,可以使用torch.view()
x = torch.rand(4,4)
y = x.view(16)
z = x.view(-1,8) # -1为省略写法,可以从其他维度自动推导出来
print(x.size(),y.size(),z.size())
输出:
torch.Size([4, 4]) torch.Size([16]) torch.Size([2, 8])
如果存在只有一个值得Tensor,可以用==.item() ==获取他的数值
torch.randn(*size)为Tensor返回一个张量,包含了从正态分布(均值为0,方差为 1,即高斯白噪声)中抽取一组随机数
x = torch.randn(1)
print(x)
print(x.item())
输出:
tensor([-0.5707])
-0.5706535577774048
以后阅读:
这里描述了100+张量操作,包括置换、索引、切片、数学操作、线性代数、随机数等。
Torch和numpy之间的桥梁
将一个Torch张量转换成一个NumPy数组,反之亦然,都是轻而易举的事情。
a = torch.ones(5)
print(a)
b = a.numpy()
print(b)
输出:
tensor([1., 1., 1., 1., 1.])
[1. 1. 1. 1. 1.]
当a的值变化时,b也随之变化,
a.add_(1)
print(a,b)
输出:
tensor([2., 2., 2., 2., 2.]) [2. 2. 2. 2. 2.]
将numpy数组转换成torch.tensor
import numpy as np
a = np.ones(5)
b = torch.from_numpy(a)
print(a)
print(b)
输出:
[1. 1. 1. 1. 1.]
tensor([1., 1., 1., 1., 1.], dtype=torch.float64)
CPU上的所有张量(char张量除外)都支持转换为NumPy并返回。
在GPU上创建Tensor
利用 .to() tensors可以被移动到任何设备上
如果电脑上安装有cuda 可以尝试一下代码:
使用 torch.device将tensors移入或移除GPU
if torch.cuda.is_available():
device = torch.device(“cuda”)
y = torch.ones_like(x,device = device)
x = x.to(device)
z = x+y
print(z)
print(z.to(“cpu”,torch.double))
输出:
tensor([0.8812], device=‘cuda:0’)
tensor([0.8812], dtype=torch.float64)
第一节:getting started 结束