pytorch_lesson1 张量的创建和常用方法

文章目录


神经网络兴起的三驾马车:算法、算例和数据

一、关于GPU

什么是GPU?什么是CPU?

GPU并非CPU的替代品,GPU也不是”更高层次“的CPU。这两种处理器都执行计算机运行所需的相同的“计算过程”,但不同的是,CPU擅长处理复杂、连续的计算问题,例如操作系统、程序、键盘操作、鼠标操作等,而GPU擅长处理简单、大量、重复、并行的计算问题,比如游戏中的3D图形渲染,他们之间不能互相代替。CPU是几个博士生,GPU是成千上万个小学生,我把它比喻成CPU是王兴,GPU是美团骑手。对于复杂问题,GPU不如CPU解决得好,甚至不能解决,而对于简单大量问题,CPU再牛,也敌不过GPU并行处理的效率和规模。

任何GPU都适合深度学习吗?

不是。市面上大部分深度学习框架都只支持N卡,也就是NVIDIA(英伟达)显卡,如果你电脑中的显卡不是NVIDIA(比如,可能是Intel或者AMD的显卡,也可能有GPU功能),那你要使用PyTorch的GPU版本就几乎不可能。苹果计算机大部分都搭载AMD显卡,许多高性能游戏本也是搭载A卡,所以苹果计算机很多时候都无法使用PyTorch的GPU版本。当然,如果你的Mac搭载的是NVIDIA的显卡,那就没有问题了。需要注意的是,新硬件发行后,PyTorch等深度学习框架需要一段时间进行迭代,之后才能与新硬件匹配使用。

白嫖GPU

在这里插入图片描述

二、 tensor张量

import torch
#查看版本号
torch.__version__

(一)张量(Tensor)的基本创建及其类型

1、张量(Tensor)函数创建方法

(1)通过列表创建张量
t = torch.tensor([1, 2])
print(t)
print(type(t))
(2)通过元组创造张量
#通过元组创建张量
t0 = torch.tensor((1, 2))
print(t0)
(3)通过数组重建张量
import numpy as np
a = np.array((1, 2))
print(a)

#通过数组在重建张量
t1 = torch.tensor(a)
print(t1)

2、张量的类型

张量和数组类似,都拥有dtype方法,可返回张量类型

(1)整数型
t = torch.tensor([1, 2])
print(t.dtype)
#torch.int64

t0 = torch.tensor((1, 2))
print(t0.dtype)
#torch.int64

a = np.array((1, 2))
t1 = torch.tensor(a)
print(a.dtype)
#int32
print(t1.dtype)
#torch.int32

我们可以发现,整数型的数组模型创建int32(整型)类型,而张量则默认创建int64(长整型)类型

(2)浮点型
b = np.array([1.1, 2.2])
print(b.dtype)
#float64 双精度浮点型数值

c = torch.tensor(b)
print(c.dtype)
#torch.float64

d = torch.tensor([1.1, 2.2]).dtype
print(d)
#torch.float32

创建浮点型数组的时候,张量默认float32(单精度浮点型),而array则是模型float64(双精度浮点)

(3)布尔类型
t3 = torch.tensor([True, False])
print(t3.dtype)
#torch.bool

pytorch中的tensor类型
在这里插入图片描述

(4)通过dtype参数,在创建张量过程中设置类型
#创建int16张量类型
t4 = torch.tensor([1.1, 2.7], dtype=torch.int16)
print(t4)
#torch.int16
#tensor([1, 2], dtype=torch.int16)
(5)复数类型对象创建
t5 = torch.tensor(1 + 2j)  #1是实部。2是虚部
print(t5)
#tensor(1.+2.j)

3、张量类型的转化

(1)张量类型的隐式转化

隐式转化,tensor和numpy一样,当张量各元素属于不同类型时,系统会自动进行隐式转化

#浮点型和整型的隐式转换
e = torch.tensor([1.1, 2])
print(e)  #tensor([1.1000, 2.0000])
print(e.dtype)  #torch.float32

#布尔型和数值型的隐式转换
f = torch.tensor([False, 2.0])
print(f)
#tensor([0., 2.])
g = torch.tensor([True, 2.0])
print(g)
#tensor([1., 2.])
(2)张量类型的转化方法

我们还可以使用.float()、.int()等方法对张量类型进行转化

t = torch.tensor([1, 2])
print(t)

#转化为浮点型
q = t.float()
print(q)
#tensor([1., 2.], dtype=torch.float64)

q1 = t.double()#转化为双精度
print(q1)
print(q1.dtype)
#torch.float64

q2 = t.short()#转化为16位整数
print(q2.dtype)
#torch.int16

(二)张量的维度与形变

1、创建高维张量

(1)用简单序列创建一维数组,包含“简单”元素的序列创建一维数组
t4 = torch.tensor([1, 2])
print(t4)

#使用ndim属性查看张量的维度
print(t4.ndim)
#1
#使用shape查看形状
print(t4.shape)
#torch.Size([2]) #意味着包括两个元素的一维数组

#或者利用size
print(t4.size())
#torch.Size([2])

print(len(t4))
#2
#返回总共拥有几个数
print(t1.numel())
#2
#虽然在一维张量len和numel返回的结果都一样,但是高维张量则不然

(2)用“序列”的序列创建二维组

可以用形状相同的序列组成一个新的序列,进而将其转化为二维张量


# 用list的list创建二维数组
t5 = torch.tensor([[1, 2], [3, 4]])
print(t5)

tensor([[1, 2],
        [3, 4]])
print(t5.ndim)
#2 2维数组
print(t5.shape)
#torch.Size([2, 2])两行两列的矩阵

print(len(t5))
#2 分别是由两个元素构成的,分别是[1, 2], [3, 4]
print(t5.numel())
#4
(3)零维张量——标量
t0 = torch.tensor(1)
#标量只有一个具体的数,不能进行拓展
print(t0)
#tensor(1)
print(t0.ndim)
#0
print(t0.shape)
#torch.Size([])

目前,我们可将零维张量视为拥有张量属性的单独一个数。(例如,张量可以存在GPU上,但Python原生的数值型对象不行,但零维张量可以,尽管是零维。)从学术名称来说,Python中单独一个数是scalars(标量),而零维的张量则是tensor。

(4)高维张量

三维及三维以上的张量,我们就将其称为高维张量。当然,在高维张量中,最常见的还是三维张量。我们可以将其理解为二维数组或者矩阵的集合。

a1 = np.array([[1, 2, 2], [3, 4, 4]])
a2 = np.array([[5, 6, 6], [7, 8, 8]])
a3 = torch.tensor([a1, a2])
print(a3)

tensor([[[1, 2, 2],
         [3, 4, 4]],

        [[5, 6, 6],
         [7, 8, 8]]], dtype=torch.int32)
print(a3.ndim)
#3,3维向量
print(a3.shape)
#torch.Size([2, 2, 3]) 由两个矩阵构成,每个矩阵都是2行3列的矩阵,从外到里的元素个数
print(len(a3))
#2 这个长度仅仅只指最外层的结构由几个元素构成
print(a3.numel())
#12

2、张量的形变

(1)flatten
tensor([[[1, 2, 2],
         [3, 4, 4]],

        [[5, 6, 6],
         [7, 8, 8]]], dtype=torch.int32)
         
a3.flatten()
#tensor([1, 2, 2, 3, 4, 4, 5, 6, 6, 7, 8, 8], dtype=torch.int32)
#以行为单位进行拉平
(2)reshape
print(a3)
'''
tensor([[[1, 2, 2],
         [3, 4, 4]],

        [[5, 6, 6],
         [7, 8, 8]]], dtype=torch.int32)
'''
a4 = a3.reshape(2, 3, 2)
print(a4)

tensor([[[1, 2],
         [2, 3],
         [4, 4]],

        [[5, 6],
         [6, 7],
         [8, 8]]], dtype=torch.int32)
         
print(t)
#tensor([1, 2])
print(t.shape)
#torch.Size([2])
tp = t.reshape(2, 1)
print(tp)
print(tp.shape)
#torch.Size([2, 1])

#转化后生成一维张量
tq = tp.reshape(2)  #效果等同于reshape(2,)
#tensor([1, 2]) 转化为一维向量
print(tq)
print(tq.shape)
#torch.Size([2])

#转化后生成二维张量
t1 = t.reshape(1, 2)  #生成一个二维度向量,一行两列
print(t1)
#tensor([[1, 2]])
print(t1.ndim)
#2
t2 = t1.reshape(1, 1, 2)
print(t2)
#tensor([[[1, 2]]]) 是由一个矩阵组成的,这个矩阵包括1行2列
t3 = t1.reshape(1, 2, 1)
print(t3)
'''
tensor([[[1],
         [2]]])
'''

如何通过reshape把a3拉平

print(a3)
print(a3.flatten())
a5 = a3.reshape(1, 1, 12)
print(a5)
#tensor([[[1, 2, 2, 3, 4, 4, 5, 6, 6, 7, 8, 8]]], dtype=torch.int32)
a6 = a5.reshape(12)
print(a6)
#tensor([1, 2, 2, 3, 4, 4, 5, 6, 6, 7, 8, 8], dtype=torch.int32)

(三)特殊张量的创建方法

1、特殊取值的张量创建方法

(1)全0向量
c = torch.zeros([2, 3])
print(c)
'''
tensor([[0., 0., 0.],
        [0., 0., 0.]])
'''
(2)全1向量
c1 = torch.ones([2, 3])
print(c1)

tensor([[1., 1., 1.],
        [1., 1., 1.]])

(3)单位矩阵
c2 = torch.eye(5)
print(c2)

tensor([[1., 0., 0., 0., 0.],
        [0., 1., 0., 0., 0.],
        [0., 0., 1., 0., 0.],
        [0., 0., 0., 1., 0.],
        [0., 0., 0., 0., 1.]])
(4)对角矩阵
c3 = torch.diag(torch.tensor([2, 4, 6, 8]))
print(c3)

tensor([[2, 0, 0, 0],
        [0, 4, 0, 0],
        [0, 0, 6, 0],
        [0, 0, 0, 8]])

diag不能直接通过list创建对角矩阵,会报错

(5)rand:各元素服从0-1的均匀分布
c4 = torch.rand(2, 3)
print(c4)
'''
tensor([[0.3774, 0.5339, 0.3454],
        [0.8226, 0.1460, 0.3585]])
'''

(6)randn:各元素服从标准正态分布——期望值为0,标准差为1的分布

c5 = torch.randn(2, 3)
print(c5)
'''
tensor([[-0.4191,  0.2243,  0.4377],
        [-0.4996,  0.4183,  0.4053]])
(7)normal:服从指定正态分布的张量
c6 = torch.normal(2, 3, size=(2, 3))
#服从均值为2,标准差为3的正态分布
print(c6)
'''
tensor([[-6.8164e+00, -2.4348e-03, -3.4560e+00],
        [ 8.2434e-01,  3.6669e-01,  1.7867e+00]])
'''
(8)radint:整数随机采样结构
c7 = torch.randint(1, 10, [2, 4]) #在1-10之间随机取整数,组成两行四列的矩阵
print(c7)
'''
tensor([[1, 6, 7, 3],
        [9, 5, 6, 3]])
'''
(9)arange/linspace:生成数列
print(torch.arange(5))  #生成的一维数组效果跟range一样
#tensor([0, 1, 2, 3, 4])
print(torch.arange(1, 5, 0.5))  #生成的一维数组左闭右开,每隔0.5取一个数
#tensor([1.0000, 1.5000, 2.0000, 2.5000, 3.0000, 3.5000, 4.0000, 4.5000])
print(torch.linspace(1, 5, 3))  #从1到5左右都包含,等距离取3个数
#tensor([1., 3., 5.])
(10)empty生成未初始化的指定形状矩阵
print(torch.empty(2, 3)) #数值上没有初始化,但是形状上已经确定了

tensor([[0., 0., 0.],
        [0., 0., 0.]])
(11)生成指定形状,填充指定数值
print(torch.empty(2, 3))
'''
tensor([[2, 2, 2, 2],
        [2, 2, 2, 2]])
'''

2、创建指定形状的数组

print(t1)
print(torch.full_like(t1, 3))
#tensor([[3, 3]])
print(torch.randint_like(a3, 1, 10))

tensor([[[1, 2, 7],
         [4, 3, 4]],

        [[1, 6, 8],
         [9, 8, 8]]], dtype=torch.int32)


print(torch.zeros_like(t4))
#tensor([0, 0])

(四)张量和其他相关类型之间的转化方法

1、numpy方法:张量转化为数组

print(a6)
#tensor([1, 2, 2, 3, 4, 4, 5, 6, 6, 7, 8, 8], dtype=torch.int32)

a7 = a6.numpy()
print(a7)
#[1 2 2 3 4 4 5 6 6 7 8 8]

## 当然,也可以通过np.array函数直接转化为array
print(np.array(a6))
#[1 2 2 3 4 4 5 6 6 7 8 8]

2、tolist方法:张量转化为列表

print(a6.tolist())
#[1, 2, 2, 3, 4, 4, 5, 6, 6, 7, 8, 8]
print(list(a6))

[tensor(1, dtype=torch.int32), tensor(2, dtype=torch.int32), tensor(2, dtype=torch.int32), tensor(3, dtype=torch.int32), tensor(4, dtype=torch.int32), tensor(4, 

3、item()方法:转化为数据

n = torch.tensor(1)
print(n.item()) 
#1

(五)张量的深浅拷贝

1、浅拷贝

t = torch.arange(10)
print(t)
#tensor([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])

t1 = t  #t1是t的浅拷贝
print(t1)
print(t[1])
t[1] = 10 #将张量t1的值修改
print(t)  #tensor(1)
#tensor([ 0, 10,  2,  3,  4,  5,  6,  7,  8,  9])
print(t1)
#tensor([ 0, 10,  2,  3,  4,  5,  6,  7,  8,  9]) #t修改之后,t1同步修改

2、深拷贝

此处t1和t11二者指向相同的对象。而要使得t11不随t1对象改变而改变,则需要对t11进行深拷贝,从而使得t11单独拥有一份对象。
t2 = t.clone()
print(t2)

t[1] = 20
print(t)
#tensor([ 0, 20,  2,  3,  4,  5,  6,  7,  8,  9])
print(t2)
#tensor([ 0, 10,  2,  3,  4,  5,  6,  7,  8,  9])
  • 3
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值