PyTorch常用函数(4)

Torch定义了七种CPU tensor类型和八种GPU tensor类型:
在这里插入图片描述
torch.FloatTensor是默认的tensor类型。

import torch
# 一个张量 tensor 可以从 Python 的 list 或序列构建
print(torch.FloatTensor([[1, 2, 3], [4, 5, 6]]))
# tensor([[1., 2., 3.],
#         [4., 5., 6.]])
# 一个空张量 tensor 可以通过规定其大小来构建
print(torch.IntTensor(2, 4).zero_())
# tensor([[0, 0, 0, 0],
#         [0, 0, 0, 0]], dtype=torch.int32)
# 可以用 python 的索引和切片来获取和修改一个张量 tensor 中的内容
x = torch.FloatTensor([[1, 2, 3], [4, 5, 6]])
print(x[1][2])
# tensor(6.)
x[0][1]=8
print(x)
# tensor([[1., 8., 3.],
#         [4., 5., 6.]])
  • expand(*sizes):返回 tensor 的一个新视图,单个维度扩大为更大的尺寸。该函数只能将size=1的维度扩展到更大的尺寸,如果扩展其他size()的维度会报错。
import torch
x = torch.Tensor([[1], [2], [3]])
print(x.size())
# torch.Size([3, 1])
print(x.expand(3, 4))
# tensor([[1., 1., 1., 1.],
#         [2., 2., 2., 2.],
#         [3., 3., 3., 3.]])
  • index_add_(dim, index, tensor) → Tensor:按参数 index 中的索引数确定的顺序,将参数 tensor 中的元素加到原来的 tensor 中。参数 tensor 的
    尺寸必须严格地与原 tensor 匹配,否则会发生错误。
    参数:
    (1)dim(int):索引index所指向的维度
    (2)index(LongTensor):需要从tensor中选取的索引
    (3)tensor(Tensor):含有相加元素的tensor
import torch
x = torch.Tensor([[1, 1, 1], [1, 1, 1], [1, 1, 1]])
t = torch.Tensor([[1, 2, 3], [4, 5, 6], [7, 8, 9]])
index = torch.LongTensor([0, 2, 1])
x.index_add_(0, index, t)
print(x)
# tensor([[ 2.,  3.,  4.],
#         [ 8.,  9., 10.],
#         [ 5.,  6.,  7.]])
  • index_copy_(dim, index, tensor) → Tensor:按参数 index 中的索引数确定的顺序,将参数 tensor 中的元素复制到原来的 tensor 中。参数 tensor的尺寸必须严格地与原 tensor 匹配,否则会发生错误。
    参数:
    (1)dim(int):索引index所指向的维度
    (2)index(LongTensor):需要从tensor中选取的索引
    (3)tensor(Tensor):含有被复制元素的tensor
import torch
x = torch.Tensor(3, 3)
t = torch.Tensor([[1, 2, 3], [4, 5, 6], [7, 8, 9]])
index = torch.LongTensor([0, 2, 1])
x.index_copy_(0, index, t)
print(x)
# tensor([[1., 2., 3.],
#         [7., 8., 9.],
#         [4., 5., 6.]])

x = torch.Tensor(3, 3)
t = torch.Tensor([[1, 2, 3], [4, 5, 6], [7, 8, 9]])
index = torch.LongTensor([0, 2, 1])
x.index_copy_(1, index, t)
print(x)
# tensor([[1., 3., 2.],
#         [4., 6., 5.],
#         [7., 9., 8.]])
  • index_fill_(dim, index, val) → Tensor:按参数 index 中的索引数确定的顺序,将原 tensor 用参数 val 值填充。
    参数:
    (1)dim(int):索引index所指向的维度
    (2)index(LongTensor):需要从tensor中选取的索引
    (3)val(Tensor):填充的值
import torch
x = torch.Tensor([[1, 2, 3], [4, 5, 6], [7, 8, 9]])
index = torch.LongTensor([0, 2])
x.index_fill_(0, index, -1)
print(x)
# tensor([[-1., -1., -1.],
#         [ 4.,  5.,  6.],
#         [-1., -1., -1.]])
  • map_(tensor, callable):将 callable 作用于本 tensor 和参数 tensor 中的每一个元素,并将结果存放在本 tensor 中
import torch
def f(x,*y): return 4*x+10
x = torch.randint(0,3,(2,3))
print('原来的x是:\n{}'.format(x))
# 原来的x是:
# tensor([[2, 1, 0],
#         [2, 1, 1]])
x.map_(x,f)
print(x)
# tensor([[18, 14, 10],
#         [18, 14, 14]])
  • Tensor.permute(*dims) → Tensor:pemute可以对高维,2阶或以上矩阵进行重排列,或者说转置,而transpose只能对两个维度进行调换
import torch
b = torch.tensor([[[1,4],[2,5]],[[3,7],[4,6]]])
print(b.shape)
# torch.Size([2, 2, 2])
print(b.permute(0,2,1))
# tensor([[[1, 2],
#          [4, 5]],

#         [[3, 4],
#          [7, 6]]])
print(b.transpose(2,1))
# tensor([[[1, 2],
#          [4, 5]],

#         [[3, 4],
#          [7, 6]]])

x = torch.randn(2, 3, 5)
print(x.shape)
# torch.Size([2, 3, 5])
print( x.permute(2, 0, 1).size())
# torch.Size([5, 2, 3])
  • Tensor.scatter_(dim, index, src, reduce=None) → Tensor:将 src 中的所有值按照 index 确定的索引写入本 tensor 中。
self[index[i][j][k]][j][k] = src[i][j][k]  # if dim == 0
self[i][index[i][j][k]][k] = src[i][j][k]  # if dim == 1
self[i][j][index[i][j][k]] = src[i][j][k]  # if dim == 2
# 实现one-hot编码
import torch
label = torch.arange(10).view(-1, 1)
print(label)
# tensor([[0],[1],[2],[3],[4],[5],[6],[7],[8],[9]])
label_onehot = torch.zeros(10, 10).scatter_(1, label, 1)
print(label_onehot)
# tensor([[1., 0., 0., 0., 0., 0., 0., 0., 0., 0.],
#        [0., 1., 0., 0., 0., 0., 0., 0., 0., 0.],
#        [0., 0., 1., 0., 0., 0., 0., 0., 0., 0.],
#        [0., 0., 0., 1., 0., 0., 0., 0., 0., 0.],
#        [0., 0., 0., 0., 1., 0., 0., 0., 0., 0.],
#        [0., 0., 0., 0., 0., 1., 0., 0., 0., 0.],
#        [0., 0., 0., 0., 0., 0., 1., 0., 0., 0.],
#        [0., 0., 0., 0., 0., 0., 0., 1., 0., 0.],
#        [0., 0., 0., 0., 0., 0., 0., 0., 1., 0.],
#        [0., 0., 0., 0., 0., 0., 0., 0., 0., 1.]])

index = torch.tensor([[1], [2], [0], [1]])
onehot = torch.zeros(4, 3)
onehot.scatter_(1, index, 1)
print(onehot)
# tensor([[0., 1., 0.],
#         [0., 0., 1.],
#         [1., 0., 0.],
#         [0., 1., 0.]])

参考目录

https://blog.csdn.net/weixin_40920183/article/details/119814472

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值