1. PyTorch简介
2. 张量的概念及创建
2.1 直接创建Tensor
2.2 依据数值创建Tensor
2.3 依据概率分布创建Tensor
2017年,FAIR(Facebook AI Research)发布PyTorch,是在Torch框架基础上打造的,(Torch使用Lua语言,受众小未普及)。
英文文档:https://pytorch.org/docs/stable/index.html
中文文档:https://pytorch-cn.readthedocs.io/zh/latest/
本地安装: python, Anaconda, PyCharm, CUDA, cuDNN, torch, torchvision,
安装指南:https://www.cnblogs.com/yanqiang/p/12749030.html
张量就是一个多维数组,是标量(0维)、向量(1维)、矩阵(2维)的拓展。
Variable: 是torch.autograd中的数据类型。有data(被封装的Tensor),grad(data的梯度),grad_fn(创建Tensor的Function),requires_grad(是否需要梯度),is_leaf(是否为叶子节点)几个属性。
PyTorch 0.4.0 之后,Variable并入Tensor,还包括有dtype(张量的数据类型,如torch.FloatTensor,torch.cuda.FloatTensor),shape(张量形状),device(张量所在设备,cpu/gpu)。
torch.tensor 功能:从data创建tensor
import torch
import numpy as np
torch.manual_seed(1)
# torch.tensor(data, # 数据,可以是list, numpy
# dtype=None, # 数据类型,与data一致
# device=None, # 所在设备,cuda/cpu
# requires_grad=False, # 是否需要梯度
# pin_memory=False) # 是否需要锁页内存
arr = np.ones((3, 3))
print("ndarray的数据类型:", arr.dtype) # float64
t = torch.tensor(arr, device=‘cuda‘)
print(t)
# tensor([[1., 1., 1.],
# [1., 1., 1.],
# [1., 1., 1.]], device=‘cuda:0‘, dtype=torch.float64)
t = torch.tensor(arr)
print(t)
# tensor([[1., 1., 1.],
# [1., 1., 1.],
# [1., 1., 1.]], dtype=torch.float64)
除了这种方式,还有一种是torch.from_numpy(ndarray)方法,从numpy创建tensor,区别在于:这种方式创建的tensor是和原ndarry共享内存的,当其中一个改变,另一个也会改变
# 测试代码
arr = np.array([[1, 2, 3], [4, 5, 6]])
t = torch.from_numpy(arr)
print("numpy array: ", arr)
print("tensor : ", t)
print("修改tensor")
t[0, 0] = -1
print("numpy array: ", arr)
print("tensor : ", t)
torch.zeros 创建全0张量
torch.zeros_like() 依据input创建全0张量
# 1.
# torch.zeros(*size, # 张量的形状
# out=None, # 输出的张量
# dtype=None,
# layout=torch.strided, # 内存中布局形式,有strided, sparse_coo等
# device=None,
# requires_grad=False)
out_t = torch.tensor([1])
t = torch.zeros((3, 3), out=out_t)
print(t, ‘\n‘, out_t)
# tensor([[0, 0, 0],
# [0, 0, 0],
# [0, 0, 0]])
print(id(t), id(out_t), id(t) == id(out_t)) # True 同一个数据,仅命名不同
# 2.
# torch.zeros_like(input, # 创建与input形状相同的全0张量
# dtype=None,
# layout=torch.strided,
# device=None,
# requires_grad=False)
torch.ones(), torch.ones_like()类似
torch.full(), torch.full_like()
# torch.full(size, # 张量的形状
# fill_value, # 张量的值
# out=None,
# dtype=None,
# layout=torch.strided,
# device=None,
# requires_grad=False)
t = torch.full((3, 3), 1)
print(t)
# tensor([[1., 1., 1.],
# [1., 1., 1.],
# [1., 1., 1.]])
torch.arange()
# torch.arange(start=0, # 数值区间 [start, end)
# end,
# step=1, # 数列公差
# out=None,
# dtype=None,
# layout=torch.strided,
# device=None,
# requires_grad=False)
t = torch.arange(2, 10, 2)
print(t) # tensor([2, 4, 6, 8])
torch.linspace() 创建均分的一维张量
# torch.linespace(start, # 数值区间 [start, end],闭区间
# end,
# step=100, # 数列长度
# out=None,
# dtype=None,
# layout=torch.strided,
# device=None,
# requires_grad=False)
t = torch.linspace(2, 10, 6) # (10-2)/(6-1)=1.6
print(t) # tensor([ 2.0000, 3.6000, 5.2000, 6.8000, 8.4000, 10.0000])
torch.logspace() 属性多一个base=10.0,对数底
torch.eye() 创建对角矩阵
# torch.eye(n, # 矩阵行数,默认为方阵
# m=None,
# out=None,
# dtype=None,
# layout=torch.strided,
# device=None,
# requires_grad=False)
比如,正态分布(高斯分布),均值mean, 标准差std, 分别为标量,张量,共四种模式。
# torch.normal(mean,
# std,
# out=None)
#
# torch.normal(mean,
# std,
# size,
# out=None)
#
# 1. mean:张量 std: 张量
mean = torch.arange(1, 5, dtype=torch.float)
std = torch.arange(1, 5, dtype=torch.float)
t_normal = torch.normal(mean, std)
print("mean:{}\nstd:{}".format(mean, std))
print(t_normal)
# mean:tensor([1., 2., 3., 4.])
# std:tensor([1., 2., 3., 4.])
# tensor([1.6614, 2.5338, 3.1850, 6.4853])
# mean和std各位置一一对应
# 2. mean:标量 std: 标量
t_normal = torch.normal(0., 1., size=(4,))
print(t_normal)
# tensor([-0.4519, -0.1661, -1.5228, 0.3817])
# 3. mean:张量 std: 标量
mean = torch.arange(1, 5, dtype=torch.float)
std = 1
t_normal = torch.normal(mean, std)
print("mean:{}\nstd:{}".format(mean, std))
print(t_normal)
# mean:tensor([1., 2., 3., 4.])
# std:1
# tensor([-0.0276, 1.4369, 2.1077, 3.9417])
# 4. mean: 标量 std: 张量
mean = 1
std = torch.arange(1, 5, dtype=torch.float)
t_normal = torch.normal(mean, std)
print("mean:{}\nstd:{}".format(mean, std))
print(t_normal)
# mean:1
# std:tensor([1., 2., 3., 4.])
# tensor([ 0.8045, -0.9313, 2.2672, 2.0693])
原文:https://www.cnblogs.com/yanqiang/p/12748578.html