记录pytorch中一些常用函数

1. torch.stacktorch.cat

torch.stack(tensors, dim=0, *, out=None) → Tensor

  • Concatenates a sequence of tensors along a new dimension.
  • All tensors need to be of the same size.

torch.cat(tensors, dim=0, *, out=None) → Tensor

  • Concatenates the given sequence of seq tensors in the given dimension. All tensors must either have the same shape (except in the concatenating dimension) or be empty.
  • torch.stack会新加增加一个维度,可以理解为叠加
  • torch.cat会增加现有维度的值,可以理解为续接
>>> a = torch.Tensor([1, 2, 3])
>>> a.size()
torch.Size([3])
>>> torch.stack((a, a)).size()
torch.Size([2, 3])
>>> torch.cat((a, a)).size()
torch.Size([6])

2. torch.index_select

torch.index_select(input, dim, index, *, out=None) → Tensor

  • Returns a new tensor which indexes the input tensor along dimension dim using the entries in index which is a LongTensor.
  • The returned tensor has the same number of dimensions as the original tensor (input). The dimth dimension has the same size as the length of index; other dimensions have the same size as in the original tensor.
>>> a = torch.arange(1, 13).view(3, 4)
>>> a
tensor([[ 1,  2,  3,  4],
        [ 5,  6,  7,  8],
        [ 9, 10, 11, 12]])
>>> b = torch.index_select(a, 0, torch.tensor([0, 2]))
>>> b
tensor([[ 1,  2,  3,  4],
        [ 9, 10, 11, 12]])
>>> a.index_select(0, torch.tensor([0, 2]))  # 另一种用法
tensor([[ 1,  2,  3,  4],
        [ 9, 10, 11, 12]])
>>> c = torch.index_select(a, 1, torch.tensor([1, 3]))
>>> c
tensor([[ 2,  4],
        [ 6,  8],
        [10, 12]])

3. detachdetach_

detach()

  • Returns a new Tensor, detached from the current graph.
  • The result will never require gradient.
  • Returned Tensor shares the same storage with the original one. In-place modifications on either of them will be seen, and may trigger errors in correctness checks.

detach_()

  • Detaches the Tensor from the graph that created it, making it a leaf. Views cannot be detached in-place.

4. torch.unsqueeze

torch.unsqueeze(input, dim) → Tensor

  • Returns a new tensor with a dimension of size one inserted at the specified position.
  • The returned tensor shares the same underlying data with this tensor.
>>> x = torch.tensor([1, 2, 3, 4])
>>> torch.unsqueeze(x, 0)
tensor([[ 1, 2, 3, 4]])
>>> x.unsqueeze(1)  # 另一种用法
tensor([[1],
        [2],
        [3],
        [4]])
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值