目录
2.1.2通过torch.from_numpy(ndarray)创建
2.2.3通过torch.ones()和torch.ones_like()创建
2.2.4通过torch.full()和torch.full_like()创建
2.3.2通过torch.randn()和torch.randn_like()创建
本节简介
介绍pytorch中的数据结构——Tensor,Tensor是PyTorch中最基础的概念,其参与了整个运算过程,因此本节将介绍张量的概念和属性,如data, device, dtype等,并介绍tensor的基本创建方法,如直接创建、依数值创建和依概率分布创建等。
1.张量(Tensor)的概念
1.1Pytorch中的张量
在数学概念中,张量表示多维数组,在Pytorch中,张量还是Pytorch自动求导的关键。
1.2Tensor与Variable
在torch0.4.0之前,Variable是特别重要的数据类型,在torch0.4.0之后已经并入到Tensor中。
2.张量的创建
2.1直接创建
2.1.1通过torch.tensor()创建
测试代码
import torch
import numpy as np
# =============================== example 1 ===============================
# 通过torch.tensor创建张量
#
flag = True
# flag = False
if flag:
arr = np.ones((3, 3))
print("ndarray的数据类型:", arr.dtype)
# t = torch.tensor(arr, device='cuda')
t = torch.tensor(arr)
print(t)
输出
ndarray的数据类型: float64
tensor([[1., 1., 1.],
[1., 1., 1.],
[1., 1., 1.]], dtype=torch.float64)
如果将张量放到GPU上,可以将device(所在设备)设置为cuda
t = torch.tensor(arr, device='cuda')
输出
ndarray的数据类型: float64
tensor([[1., 1., 1.],
[1., 1., 1.],
[1., 1., 1.]], device='cuda:0', dtype=torch.float64)# 只有一个GPU所以显示为0
2.1.2通过torch.from_numpy(ndarray)创建
测试代码
# =============================== example 2 ===============================
# 通过torch.from_numpy创建张量
flag = True
# flag = False
if flag:
arr = np.array([[1, 2, 3], [4, 5, 6]])
t = torch.from_numpy(arr)
# print("numpy array: ", arr)
# print("tensor : ", t)
# print("\n修改arr")
# arr[0, 0] = 0
# print("numpy array: ", arr)
# print("tensor : ", t)
print("\n修改tensor")
t[0, 0] = -1
print("numpy array: ", arr)
print("tensor : ", t)
输出
修改tensor
numpy array: [[-1 2 3]
[ 4 5 6]]
tensor : tensor([[-1, 2, 3],
[ 4, 5, 6]], dtype=torch.int32)
2.2依据数值创建
2.2.1通过torch.zeros()创建
测试代码
# =============================== example 3 ===============================
# 通过torch.zeros创建张量
flag = True
# flag = False
if flag:
out_t = torch.tensor([1])
t = torch.zeros((3, 3), out=out_t) # out的作用:将torch.zeros(3, 3)的值赋值给out_t
print(t, '\n', out_t)
print(id(t), id(out_t), id(t) == id(out_t)) # 输出t和out_t的内存地址并判断是否一致
输出
tensor([[0, 0, 0],
[0, 0, 0],
[0, 0, 0]])
tensor([[0, 0, 0],
[0, 0, 0],
[0, 0, 0]])
1564816705240 1564816705240 True
2.2.2通过torch.zeros_like()创建
2.2.3通过torch.ones()和torch.ones_like()创建
方法和创建全0张量一样,将zeros改为ones即可
2.2.4通过torch.full()和torch.full_like()创建
测试代码
# =============================== exmaple 4 ===============================
# 通过torch.full创建全1张量
flag = True
#flag = False
if flag:
t = torch.full((3, 3), 1) # 创建3*3的全1张量
print(t)
输出
tensor([[1., 1., 1.],
[1., 1., 1.],
[1., 1., 1.]])
2.2.5通过torch.arange()创建
这里创建的是前闭后开的数值区间。
测试代码
# =============================== exmaple 5 ===============================
# 通过torch.arange创建等差数列张量
flag = True
#flag = False
if flag:
t = torch.arange(2, 10, 2) # 前闭后开,取不到10
print(t)
输出
tensor([2, 4, 6, 8])
2.2.6通过torch.linspace()创建
测试代码
# =============================== exmaple 6 ===============================
# 通过torch.linspace创建均分数列张量
flag = True
#flag = False
if flag:
# t = torch.linspace(2, 10, 5)
t = torch.linspace(2, 10, 6)
print(t)
输出
tensor([ 2.0000, 3.6000, 5.2000, 6.8000, 8.4000, 10.0000])
2.2.7通过torch.logspace()创建
2.2.8通过torch.eye()创建
对角矩阵:对角线上值均为1的矩阵,对角矩阵为方阵。
由于对角矩阵为方阵,故m的数值一般不用设置。
2.3依据概率分布创建
2.3.1通过torch.normal()创建
测试代码
# =============================== exmaple 7 ===============================
# 通过torch.normal创建正态分布张量
flag = True
# flag = False
if flag:
# mean:张量 std: 张量
print("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:标量 std: 标量
print("2. mean:标量 std: 标量")
t_normal = torch.normal(0., 1., size=(4,))
print(t_normal)
# mean:张量 std: 标量
print("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:标量 std: 张量
print("4. mean:标量 std: 张量")
mean = 0
std = torch.arange(1, 5, dtype=torch.float)
t_normal = torch.normal(mean, std)
print("mean:{}\nstd:{}".format(mean, std))
print(t_normal)
输出
1. mean:张量 std: 张量
mean:tensor([1., 2., 3., 4.])
std:tensor([1., 2., 3., 4.])
tensor([1.6614, 2.5338, 3.1850, 6.4853])
2. mean:标量 std: 标量
tensor([-0.4519, -0.1661, -1.5228, 0.3817])
3. mean:张量 std: 标量
mean:tensor([1., 2., 3., 4.])
std:1
tensor([-0.0276, 1.4369, 2.1077, 3.9417])
4. mean:标量 std: 张量
mean:0
std:tensor([1., 2., 3., 4.])
tensor([-0.1955, -1.9313, 1.2672, 1.0693])
2.3.2通过torch.randn()和torch.randn_like()创建
n表示normal(标准),标准正态分布:mean为0,std为1。