深度学习Lesson1:张量

深度学习Lesson1:张量

#导包
import torch
import numpy as np
torch.__version__
'1.10.0+cpu'

一、张量的创建与类型

1.张量(Tensor)的创建

# 张量的基本创建方法和numpy数组创建方法类似 torch.tensor(参数:序列 可以是列表、数组、元祖)
t1 = torch.tensor([1,2])
t1
tensor([1, 2])
t2  = torch.tensor((1,2))
t2
tensor([1, 2])
t3 = torch.tensor(np.array((1,2)))
t3
tensor([1, 2], dtype=torch.int32)

2.张量的类型

t1.dtype
torch.int64
t2.dtype
torch.int64
t3.dtype
torch.int32



对比numpy array可以发现,tensor默认创建的整型为int64 而numpy为int32 
np.array([1.1,2.2]).dtype
dtype('float64')
torch.tensor(np.array([1.1,2.2])).dtype
torch.float64
torch.tensor([1.1,2.2]).dtype
torch.float32



对比numpy array可以发现,tensor默认创建的浮点型为float32 而numpy为float64

当然,除了整型和浮点型,常用的tensor类型还有布尔
t2 = torch.tensor([True,False])
t2
tensor([ True, False])
t2.dtype
torch.bool

3.张量类型转化

隐式转换

#浮点型和整数型隐式转换
torch.tensor([1.1,2])
tensor([1.1000, 2.0000])
torch.tensor([1.1,2]).dtype
torch.float32
#布尔型和数值型隐式转换
torch.tensor([True,2,False])
tensor([1, 2, 0])
torch.tensor([True,2,False]).dtype
torch.int64

强制转换

t1
tensor([1, 2])
t1.float()
tensor([1., 2.])
t1.double()
tensor([1., 2.], dtype=torch.float64)
t1.short()
tensor([1, 2], dtype=torch.int16)

二、张量得维度与形变

1.创建高维张量

张量维度的概念与numpy类似

  • 用简单序列创建一维张量
t1 = torch.tensor([1,2])
t1
tensor([1, 2])
# 使用ndim查看张量维度
t1.ndim
1
# 使用shape查看张量形状
t1.shape
torch.Size([2])
# 使用numel()查看张量一共有多少个元素
t1.numel()
2
  • 用序列的序列创建二维张量
t2 = torch.tensor([[1,2],[3,4]])
t2
tensor([[1, 2],
        [3, 4]])
t2.ndim
2
t2.shape
# 可以理解为2行2列的矩阵
torch.Size([2, 2])
t2.numel()
4
len(t2)
2
len(t1)
2

注:len()返回的是张量有几个N-1维张量组成 如:此处len(t2)=2表示t2由两个一维张量组成

  • 零维张量
t = torch.tensor(3)
t.ndim
0
t.shape
torch.Size([])

理解零维张量:
可以将零维张量视为一个单个的数,和python原生的单个数区别不大,唯一的区别是原生的数字无法在GPU上运行

  • 高维张量
a1 = np.array([[1,2,2],[3,4,4]])
a1
array([[1, 2, 2],
       [3, 4, 4]])
a2 = np.array([[5,6,6],[7,8,8]])
a2
array([[5, 6, 6],
       [7, 8, 8]])
# 用两个二维数组创建一个三维张量
t3 = torch.tensor([a1,a2])
t3
c:\users\lenovo\appdata\local\programs\python\python37\lib\site-packages\ipykernel_launcher.py:2: UserWarning: Creating a tensor from a list of numpy.ndarrays is extremely slow. Please consider converting the list to a single numpy.ndarray with numpy.array() before converting to a tensor. (Triggered internally at  ..\torch\csrc\utils\tensor_new.cpp:201.)
  





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

        [[5, 6, 6],
         [7, 8, 8]]], dtype=torch.int32)
t3.ndim
3
t3.shape
#表示有2个矩阵构成,每个矩阵的形状为2*3
torch.Size([2, 2, 3])
len(t3)
2
t3.numel()
12

2.张量的形变

2.1 flatten拉平:将任意维度张量转化为一维张量

t2

t2.flatten()
tensor([1, 2, 3, 4])
t3
tensor([[[1, 2, 2],
         [3, 4, 4]],

        [[5, 6, 6],
         [7, 8, 8]]], dtype=torch.int32)
t3.flatten()
tensor([1, 2, 2, 3, 4, 4, 5, 6, 6, 7, 8, 8], dtype=torch.int32)
t
tensor(3)
t.flatten()
tensor([3])

2.2 reshape方法:任意形变

t1
tensor([1, 2])
# 形变为2行1列
t1.reshape(2,1)
tensor([[1],
        [2]])

注意:reshape后的维度由参数个数决定

t1.reshape(2,1).reshape(2)
tensor([1, 2])

三、特殊张量的创建

#全0张量
torch.zeros([2,3])   #创建2行3列的全0张量
tensor([[0., 0., 0.],
        [0., 0., 0.]])
#全1张量
torch.ones([2,3])   #创建2行3列的全1张量
tensor([[1., 1., 1.],
        [1., 1., 1.]])
#单位矩阵
torch.eye(5)   #创建5阶单位矩阵
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.]])
#对角矩阵
torch.diag([1,2]) #注意:diag方法只支持用tensor创建
---------------------------------------------------------------------------

TypeError                                 Traceback (most recent call last)

<ipython-input-79-a65cf834c4c2> in <module>
      1 #对角矩阵
----> 2 torch.diag([1,2])


TypeError: diag(): argument 'input' (position 1) must be Tensor, not list
torch.diag(torch.tensor([2,3]))
# 可以理解为将tensor转换为对角矩阵
tensor([[2, 0],
        [0, 3]])
# 随机张量(rand方法创建的随机张量服从0-1分布,randn服从正态分布)
torch.rand(2,3)
tensor([[0.1801, 0.3555, 0.2078],
        [0.9565, 0.9357, 0.7806]])
torch.randn(2,3)
tensor([[ 0.7264,  2.2165,  0.5817],
        [ 0.0791, -0.3594,  0.2335]])
torch.normal(2,3,size=(2,2)) #创建一个均值为2,标准差为3满足正态分布的2行2列的随机张量
tensor([[ 4.7136, -2.6452],
        [ 6.2694,  8.4061]])
torch.randint(1,10,[2,4])  #创建一个取值在1-10之间的2行4列的随机张量
tensor([[8, 8, 4, 6],
        [6, 7, 3, 9]])
torch.arange(1,5,0.5) #与python range相同 提示;左闭右开
tensor([1.0000, 1.5000, 2.0000, 2.5000, 3.0000, 3.5000, 4.0000, 4.5000])
torch.linspace(1,5,3) #从1,5等距离取三个数
tensor([1., 3., 5.])
torch.full([2,4],2) #生成2行4列的张量,且取值全为2
tensor([[2, 2, 2, 2],
        [2, 2, 2, 2]])

四、张量与其它类型的转换

t1 = torch.arange(1,11)
t1
tensor([ 1,  2,  3,  4,  5,  6,  7,  8,  9, 10])
# 将tensor转化为numpy array
t1.numpy()
array([ 1,  2,  3,  4,  5,  6,  7,  8,  9, 10], dtype=int64)
np.array(t1)
array([ 1,  2,  3,  4,  5,  6,  7,  8,  9, 10], dtype=int64)
# 将tensor转化为列表
t1.tolist()
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]

五、张量的拷贝

t1
tensor([ 1,  2,  3,  4,  5,  6,  7,  8,  9, 10])
t11 = t1 #浅拷贝
t11
tensor([ 1,  2,  3,  4,  5,  6,  7,  8,  9, 10])
t1[1]=10
t1
tensor([ 1, 10,  3,  4,  5,  6,  7,  8,  9, 10])
t11 #修改t1的值 t11会同步修改
tensor([ 1, 10,  3,  4,  5,  6,  7,  8,  9, 10])
#深拷贝
t11 = t1.clone()
t1
tensor([ 1, 10,  3,  4,  5,  6,  7,  8,  9, 10])
t11
tensor([ 1, 10,  3,  4,  5,  6,  7,  8,  9, 10])
t1[1]=2
t1
tensor([ 1,  2,  3,  4,  5,  6,  7,  8,  9, 10])
t11
tensor([ 1, 10,  3,  4,  5,  6,  7,  8,  9, 10])

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

这个人不主动

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值