张量在数学概念中就是一个多维数组,它是标量,向量,矩阵的高纬拓展,比如彩色图像有三个通道,就是平面图像的在第三个维度的扩展
variable是0.4版本之前的一种重要的数据结构,虽然在新版本中已经封装到Tensor中,但是还是重要的基础。 它有如下五个的属性:
Pytorch在0.4.0版本以后, Varibale并入Tensor, 并新增了三个属性:
总结:
功能: 从data创建tensor
import numpy as np
import torch
arr = np.ones((3,3))
print("The datatype of ndarray: ", arr.dtype)
The datatype of ndarray: float64
# 观察到tensor的类型和darray的数据类型一致
t = torch.tensor(arr)
print(t)
tensor([[1., 1., 1.], [1., 1., 1.], [1., 1., 1.]], dtype=torch.float64)
功能: 从numpy创建tensor
注意事项: 从torch.from_numpy创建的tensor与原ndarray共享内存,当修改其中一个的数据,另外一个也将会被改动
arr = np.array([[1,2,3], [4,5,6]])
t = torch.from_numpy(arr)
print("numpy array: \n", arr)
print("tensor: \n", t)
numpy array: [[1 2 3] [4 5 6]] tensor: tensor([[1, 2, 3], [4, 5, 6]])
# 观察到单独修改其中一个都会影响到另一个,它们是共享内存的
print("修改arr后:")
arr[0,0] = 0
print("numpy array: \n", arr)
print("tensor: \n", t)
print("\n 修改tensor后:")
t[0,0] = -1
print("numpy array: \n", arr)
print("tensor: \n", t)
修改arr后: numpy array: [[0 2 3] [4 5 6]] tensor: tensor([[0, 2, 3], [4, 5, 6]]) 修改tensor后: numpy array: [[-1 2 3] [ 4 5 6]] tensor: tensor([[-1, 2, 3], [ 4, 5, 6]])
out_t = torch.tensor([1])
t = torch.zeros((3,3), out=out_t)
# t 和 out_t 的内存地址一样
print(t, '\n', out_t)
print(id(t), id(out_t), id(t)==id(out_t))
tensor([[0, 0, 0], [0, 0, 0], [0, 0, 0]]) tensor([[0, 0, 0], [0, 0, 0], [0, 0, 0]]) 4914215600 4914215600 True
功能: 依input形状创建全0张量
torch.zeros_like(input,
dtype=None,
layout=None,
device=None,
requires_grad=False)
全一张量的创建和之前的全零基本完全一致
torch.ones(*size,
out=None
dtype=None,
layout=torch.strided,
device=None,
requires_grad=False)
torch.ones_like(input,
dtype=None,
layout=None,
device=None,
requires_grad=False)
功能: 依input形状创建全0张量
torch.full(*size,
fill_value,
out=None,
dtype=None,
layout=torch.strided,
device=None,
requires_grad=False)
t = torch.full((3,3), 10)
print(t)
tensor([[10., 10., 10.], [10., 10., 10.], [10., 10., 10.]])
功能: 创建等差的1维张量
注意事项: 数值区间为[start, end)
torch.arange(start=0,
end,
step=1,
out=None,
dtype=None,
layout=torch.strided,
device=None,
requires_grad=False)
# 创建一个偶数数列,注意是左闭右开
t = torch.arange(0,10,2)
print(t)
tensor([0, 2, 4, 6, 8])
功能: 创建均分的1维张量
注意事项: 数值区间为[start, end]
torch.linspace(start=0,
end,
step=100,
out=None,
dtype=None,
layout=torch.strided,
device=None,
requires_grad=False)
功能: 创建均分的1维张量
注意事项: 长度为steps,底为base
# 步长 = (end-start)/(step-1)
t = torch.linspace(2,10,6)
print(t)
tensor([ 2.0000, 3.6000, 5.2000, 6.8000, 8.4000, 10.0000])
# 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:{}\nout_tensor:{}".format(mean, std, t_normal))
# out_tensor:tensor([1.0040, 2.0221, 3.3154, 4.5225]) 分别是四个不同的正态分布得到的
mean:tensor([1., 2., 3., 4.]) std:tensor([1., 2., 3., 4.]) out_tensor:tensor([1.0040, 2.0221, 3.3154, 4.5225])
# mean: 张量, std: 张量
t_normal = torch.normal(0., 1., size=(4,))
print(t_normal)
# out_tensor:tensor([1.0040, 2.0221, 3.3154, 4.5225])
tensor([ 0.2219, -1.1354, 0.2321, 0.5560])
# mean: 张量, std: 张量
mean = torch.arange(1, 5, dtype=torch.float)
std = 1
t_normal = torch.normal(mean, std)
print("mean:{}\nstd:{}\nout_tensor:{}".format(mean, std, t_normal))
#
mean:tensor([1., 2., 3., 4.]) std:1 out_tensor:tensor([0.4883, 2.2987, 2.9339, 3.8829])
功能: 生成标准正态分布
功能: 在区间[0,1)上,生成均匀分布
torch.rand(*size,
out=None,
dtype=None,
layout=torch.strided,
device=None,
requires_grad=False)
功能: 区间[low, high)生成整数均匀分布
torch.randint(low=0,
high,
size,
out=None
layout=torch.strided,
device=None,
requires_grad=False)
功能: 生成丛0到n-1的随机排列,最常见用来生产一个乱序的索引
torch.randprem(n,
out=None
layout=torch.strided,
device=None,
requires_grad=False)
功能: 以input为概率,生成波努力分布(0-1分布,两点分布)
torch.bernoulli(input,
*,
generator=None,
out=None)
功能: 生成丛0到n-1的随机排列,最常见用来生产一个乱序的索引
torch.randprem(n,
out=None
layout=torch.strided,
device=None,
requires_grad=False)
功能: 以input为概率,生成波努力分布(0-1分布,两点分布)
torch.bernoulli(input,
*,
generator=None,
out=None)
pro = torch.tensor([0.9, 0.1, 0.6, 0.7])
torch.bernoulli(pro)
tensor([1., 1., 1., 1.])