深度学习笔记之N维数组

数据操作

数据结构-N维数组

零维:就是一个数(类别)

一维:就是一个向量(特征向量)

二维:就是由多个向量组成的矩阵(特征矩阵)宽*高

三维:就是RGB图片 宽*高*通道(RGB三个通道)

四维:就是RGB图片的批量处理 宽*高*通道*批量大小 

五维:就是视频的批量处理 宽*高*通道*批量大小*时间

数据的创建

数组的创建需要

①形状(几维,大小)

# 生成0-11
x = torch.arange(12)
print(x)   # tensor([ 0,  1,  2,  3,  4,  5,  6,  7,  8,  9, 10, 11])

#重构数组的维度
x = x.reshape(3, 4)
print(x)  # tensor([[ 0,  1,  2,  3], [ 4,  5,  6,  7], [ 8,  9, 10, 11]])

# 创造数组eg.三维
p = torch.tensor([[[1, 2, 3], [2, 3, 4], [3, 4, 5]]])
print(p.shape)  # torch.Size([1, 3, 3])


X = torch.arange(12, dtype=torch.float32).reshape((3, 4))
Y = torch.tensor([[2.0, 1, 4, 3], [1, 2, 3, 4], [4, 3, 2, 1]])
# 通过逻辑运算符构建二元表
print(X == Y)

②每个元素的类型

# dtype=torch.float32指定生成为浮点数
X = torch.arange(12, dtype=torch.float32).reshape((3, 4))

③每个元素的值

# 全赋值为0
y = torch.zeros(2, 3, 4)
print(y)  #tensor([[[0., 0., 0., 0.],
          #         [0., 0., 0., 0.],
          #         [0., 0., 0., 0.]],

          #        [[0., 0., 0., 0.],
          #         [0., 0., 0., 0.],
          #         [0., 0., 0., 0.]]])

# 全赋值为1
z = torch.ones(2, 3, 4)
print(z)  

访问元素

# 访问张量的形状和张量中的元素总数
print(x.shape) # torch.Size([12])
# 元素总数
print(x.numel())  # 12

m = torch.tensor([1.0, 2, 4, 8])
n = torch.tensor([2, 2, 2, 2])
# **求幂
print(m+n, m-n, m*n, m/n, m**n)  #tensor([ 3.,  4.,  6., 10.]) 
                                 #tensor([-1.,  0.,  2.,  6.]) 
                                 #tensor([ 2.,  4.,  8., 16.]) 
                                 #tensor([0.5000, 1.0000, 2.0000, 4.0000]) 
                                 #tensor([ 1.,  4., 16., 64.])

# 把多个张量连接在一起,同一个形状时
# dtype=torch.float32指定生成为浮点数
X = torch.arange(12, dtype=torch.float32).reshape((3, 4))
Y = torch.tensor([[2.0, 1, 4, 3], [1, 2, 3, 4], [4, 3, 2, 1]])
print(X, Y)                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                  
print(torch.cat((X, Y), dim=0))
print(torch.cat((X, Y), dim=1))
# tensor([[ 0.,  1.,  2.,  3.],
#         [ 4.,  5.,  6.,  7.],
#         [ 8.,  9., 10., 11.],
#         [ 2.,  1.,  4.,  3.],
#         [ 1.,  2.,  3.,  4.],
#         [ 4.,  3.,  2.,  1.]])
# tensor([[ 0.,  1.,  2.,  3.,  2.,  1.,  4.,  3.],
#         [ 4.,  5.,  6.,  7.,  1.,  2.,  3.,  4.],
#         [ 8.,  9., 10., 11.,  4.,  3.,  2.,  1.]])

# 对张量中每一个元素进行求和 会产生只有一个元素的张量
print(X.sum())  #tensor(66.)

# 可以用[-1]选择最后一个元素,用[1:3](左闭右开)选择第二个和第三个元素
print(X[-1], X[1:3])   #tensor([ 8.,  9., 10., 11.]) 
                       #tensor([[ 4.,  5.,  6.,  7.],
                       #        [ 8.,  9., 10., 11.]])

# 除了读取还可以通过指定索引来将元素写入矩阵
X[1, 2] = 9
print(X)  # tensor([[ 0.,  1.,  2.,  3.],
          #         [ 4.,  5.,  9.,  7.],
          #         [ 8.,  9., 10., 11.]])

# 多个元素赋相同值
X[0: 2, :] = 12
print(X)   #tensor([[12., 12., 12., 12.],
           #        [12., 12., 12., 12.],
           #        [8.,  9., 10., 11.]])

# 形状不同可以通过广播机制来执行按元素操作,维度不一样就不行了
# 采用的就是将小的扩大为大的一致
a = torch.arange(3).reshape((3, 1))
b = torch.arange(2).reshape((1, 2))
print(a, b)
print(a+b)   # tensor([[0], [1], [2]]) 
             # tensor([[0, 1]])
             # tensor([[0, 1], [1, 2], [2, 3]])

# 为新结果分配内存
before = id(Y)
# 此时的Y的内存已经重新分配
Y = Y + X
# 内存没有改变
Y += X
print(before == id(Y))

# 执行原地操作
Z = torch.zeros_like(Y)
print("id(Z)", id(Z))
Z[:] = Y+X
print("id(Z)", id(Z))

转换成NumPy张量

A = X.numpy()
B = torch.tensor(A)
print(type(A), type(B))   # <class 'numpy.ndarray'> <class 'torch.Tensor'>

a = torch.tensor([3.5])
# a.item()将a转换成numpy的浮点数
print(a, a.item(), float(a), int(a))
# tensor([3.5000]) 3.5 3.5 3

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值