Pytorch Tutorial【Chapter 1. Basic operation of tensor】

Pytorch Tutorial

文章目录

  • [Pytorch Tutorial](#Pytorch Tutorial)
    • [Chapter 1. Basic operation of tensor](#Chapter 1. Basic operation of tensor)
    • Reference

Chapter 1. Basic operation of tensor

本节介绍有关张量Tensor的基本操作

Tensor相当于numpy中的ndarrays

  • 创建空Tensor和全零Tensortorch.zeros(d0,d1)类似于numpy.zeros(size)torch.empty(d0,d1)类似于numpy.empty(size)
python 复制代码
x1 = torch.empty(2,2)
x2 = np.empty((2,2))
x2 = torch.zeros(2,2)
x4 = np.zeros((2,2))
print(x1)
print(x2)
print(x3)
print(x4)
python 复制代码
tensor([[ 1.4013e-45,  0.0000e+00],
        [-4.2944e+22,  6.7683e-43]])
tensor([[0., 0.],
        [0., 0.]])
[[0. 0. 0.]
 [0. 0. 0.]
 [0. 0. 0.]
 [0. 0. 0.]
 [0. 0. 0.]]
[[0. 0.]
 [0. 0.]]
  • 创建随机Tensortorch.rand(d0,d1)相当于numpy.random.rand(d0,d1)rand表示随机变量服从uniform distribution,torch.randn(d0,d1)相当于numpy.random.randn(d0,d1)randn表示随机变量服从 normal distribution
python 复制代码
y1 = torch.rand(2,2)
y2 = np.random.rand(2,2)
y3 = np.random.random((2,2))
print(y1)
print(y2)
print(y3)
python 复制代码
tensor([[0.4407, 0.1455],
        [0.0214, 0.6033]])
[[0.91633254 0.74086994]
 [0.11203967 0.78700098]]
[[0.89562162 0.63706194]
 [0.07474063 0.94183217]]
  • 手动创建Tensortorch.tensor()类似于numpy.array()
python 复制代码
z1 = torch.tensor(np.random.rand(2,2))
z2 = np.array([2,2])
print(z1)
print(z2)
python 复制代码
tensor([[0.8592, 0.2296],
        [0.3234, 0.0014]], dtype=torch.float64)
[2 2]
  • 创建全一Tensortorch.ones(d0,d1)类似于numpy.ones(size)
python 复制代码
c1 = torch.ones(2,2)
c2 = np.ones((2,2))
print(c1)
print(c2)
python 复制代码
tensor([[1., 1.],
        [1., 1.]])
[[1. 1.]
 [1. 1.]]
  • 创建独热码Tensortorch.eye(d0,d1)类似于np.eye(d0,d1)
python 复制代码
c1 = torch.ones(2,2)
c2 = np.ones((2,2))
print(c1)
print(c2)
python 复制代码
tensor([[1., 1.],
        [1., 1.]])
[[1. 1.]
 [1. 1.]]
  • 获得Tensorsizetensor.size()返回tensor的shape
python 复制代码
r = torch.eye(2,2)
print(r.size())
python 复制代码
torch.Size([2, 2])
  • 加法,使用+numpy类似
python 复制代码
a1 = torch.eye(2,2)
a2 = torch.rand(2,2)
print(a1+a2)
b1 = np.eye(2,2)
b2 = np.random.rand(2,2)
print(b1+b2)
python 复制代码
tensor([[1.1682, 0.4159],
        [0.5044, 1.4019]])
[[1.7929664  0.96477472]
 [0.3380899  1.35091993]]
  • 加法,使用torch.add()numpy.add()类似
python 复制代码
a1 = torch.eye(2,2)
a2 = torch.rand(2,2)
print(torch.add(a1,a2))
b1 = np.eye(2,2)
b2 = np.random.rand(2,2)
print(np.add(b1,b2))
python 复制代码
tensor([[1.5244, 0.8070],
        [0.2586, 1.1021]])
[[1.52558655 0.85622143]
 [0.92030175 1.18823413]]
  • 加法,tensor.add(a,b,out=result),将加法的结果保存在预先开辟好的result张量中
python 复制代码
result = torch.empty(3,3)
torch.add(a1,a2, out=result)
print(result)
result = torch.add(a1,a2)
print(result)
python 复制代码
tensor([[1.8654, 0.8892, 0.9849],
        [0.4269, 1.3964, 0.7995],
        [0.2235, 0.3375, 1.3471]])
tensor([[1.8654, 0.8892, 0.9849],
        [0.4269, 1.3964, 0.7995],
        [0.2235, 0.3375, 1.3471]])
  • 加法,in-place原地替换的做法tensor.add_(),注:所有的in-place的做法都有一个_
python 复制代码
a1 = torch.eye(3,3)
a2 = torch.eye(3,3)
a1.add_(a2)
print(a1)
python 复制代码
tensor([[2., 0., 0.],
        [0., 2., 0.],
        [0., 0., 2.]])
  • tensor改变形状,tensor.reshape(size)ndarrays.reshape(size)类似,但tensor.reshape(size)不是in-place的做法,ndarryas.reshape(size)也不是in-place的做法
python 复制代码
a = torch.eye(4,4)
b = a.reshape(2,8)
print(a)
print(b)

c = np.eye(4,4)
d = c.reshape(2,8)
print(c)
print(d)
python 复制代码
tensor([[1., 0., 0., 0.],
        [0., 1., 0., 0.],
        [0., 0., 1., 0.],
        [0., 0., 0., 1.]])
tensor([[1., 0., 0., 0., 0., 1., 0., 0.],
        [0., 0., 1., 0., 0., 0., 0., 1.]])
[[1. 0. 0. 0.]
 [0. 1. 0. 0.]
 [0. 0. 1. 0.]
 [0. 0. 0. 1.]]
[[1. 0. 0. 0. 0. 1. 0. 0.]
 [0. 0. 1. 0. 0. 0. 0. 1.]]
  • 使用tensor.item()来获得 scalar tensor的值
python 复制代码
a = torch.randn(3,3)
print(a)
print(a[0,0])
print(a[0,0].item())
python 复制代码
tensor([[ 0.1261, -0.7185,  0.3167],
        [ 0.7252,  0.9447,  1.6690],
        [ 0.4250, -0.3057,  0.7201]])
tensor(0.1261)
0.126137375831604

Reference

参考教程

相关推荐
小鸡吃米…1 天前
机器学习 - K - 中心聚类
人工智能·机器学习·聚类
好奇龙猫1 天前
【AI学习-comfyUI学习-第三十节-第三十一节-FLUX-SD放大工作流+FLUX图生图工作流-各个部分学习】
人工智能·学习
沈浩(种子思维作者)1 天前
真的能精准医疗吗?癌症能提前发现吗?
人工智能·python·网络安全·健康医疗·量子计算
minhuan1 天前
大模型应用:大模型越大越好?模型参数量与效果的边际效益分析.51
人工智能·大模型参数评估·边际效益分析·大模型参数选择
Cherry的跨界思维1 天前
28、AI测试环境搭建与全栈工具实战:从本地到云平台的完整指南
java·人工智能·vue3·ai测试·ai全栈·测试全栈·ai测试全栈
MM_MS1 天前
Halcon变量控制类型、数据类型转换、字符串格式化、元组操作
开发语言·人工智能·深度学习·算法·目标检测·计算机视觉·视觉检测
ASF1231415sd1 天前
【基于YOLOv10n-CSP-PTB的大豆花朵检测与识别系统详解】
人工智能·yolo·目标跟踪
njsgcs1 天前
ue python二次开发启动教程+ 导入fbx到指定文件夹
开发语言·python·unreal engine·ue
io_T_T1 天前
迭代器 iteration、iter 与 多线程 concurrent 交叉实践(详细)
python
水如烟1 天前
孤能子视角:“意识“的阶段性回顾,“感质“假说
人工智能