pytorch模块应用(一)

记录一下我遇到过的pytorch中的一些函数、模块的应用

TORCH.DIAG

官方文档

torch.diag(input, diagonal=0, *, out=None) → Tensor

参数
  • input:一个Tensor
  • digonal:对角线,控制考虑哪个对角线
    • = 0 主对角线
    • >0 主对角线上方的对角线内容,根据数值而定
    • <0 主对角线下方的对角线内容,根据数值而定
输出

一个Tensor:

  • 如果input是一个1-D的Tensor,返回的是对角线为1-D中数值的2-D矩阵,根据digonal的不同,矩阵的大小也不同。
  • 如果input是一个2-D的矩阵,返回的是其对角线的内容,结果为1-D的Tensor,根据digonal的不同,值也不同。
Example

输入:

	a = torch.Tensor([2,3,4,5])
	print('diagonal = 0')
	print(torch.diag(a))
	print('diagonal > 0')
    print(torch.diag(a,diagonal=1))
    print('diagonal < 0')
    print(torch.diag(a,diagonal=-2))

输出:

diagonal = 0
tensor([[2., 0., 0., 0.],
        [0., 3., 0., 0.],
        [0., 0., 4., 0.],
        [0., 0., 0., 5.]])
diagonal > 0
tensor([[0., 2., 0., 0., 0.],
        [0., 0., 3., 0., 0.],
        [0., 0., 0., 4., 0.],
        [0., 0., 0., 0., 5.],
        [0., 0., 0., 0., 0.]])
diagonal < 0
tensor([[0., 0., 0., 0., 0., 0.],
        [0., 0., 0., 0., 0., 0.],
        [2., 0., 0., 0., 0., 0.],
        [0., 3., 0., 0., 0., 0.],
        [0., 0., 4., 0., 0., 0.],
        [0., 0., 0., 5., 0., 0.]])

三个矩阵因为diagonal大小的不同,矩阵大小也不同。
输入:

a = torch.randn([5,5])
print('diagonal = 0 tensor.shape:{}'.format(list(torch.diag(a).shape)))
print(torch.diag(a))
print('diagonal > 0 tensor.shape:{}'.format(list(torch.diag(a,diagonal=1).shape)))
print(torch.diag(a, diagonal=1))
print('diagonal < 0 tensor.shape:{}'.format(list(torch.diag(a,diagonal=-2).shape)))
print(torch.diag(a, diagonal=-2))

输出 a矩阵

tensor([[ 2.1557,  0.9077,  1.3092,  0.0655, -0.8971],
        [ 0.4594, -0.4521, -0.6643, -0.1641,  1.1304],
        [ 0.3329, -1.6805,  0.7765,  0.7213,  0.2009],
        [ 1.7566, -0.1645,  1.2627,  1.0202, -0.0217],
        [ 2.0029, -0.7275,  1.2391,  1.6995, -1.3719]])

输出:

diagonal = 0 tensor.shape:[5]
tensor([ 2.1557, -0.4521,  0.7765,  1.0202, -1.3719])
diagonal > 0 tensor.shape:[4]
tensor([ 0.9077, -0.6643,  0.7213, -0.0217])
diagonal < 0 tensor.shape:[3]
tensor([ 0.3329, -0.1645,  1.2391])

TORCH.UNSQUEEZE

能够添加Tensor维度的一个函数?

torch.unsqueeze(input, dim) → Tensor

参数
  • input:一个Tensor,任意维度
  • dim:要扩展维度的位置,范围:[-input.dim() - 1, input.dim() + 1) ,-1是指最后一维
输出

一个Tensor,维度经过扩展,dim要比input大1

Example

输入:

    a = torch.randn([5,5]) # 一共25个数
    print(a.dim())
    print(torch.unsqueeze(a,0).shape)
    print(torch.unsqueeze(a,1).shape)
    print(torch.unsqueeze(a,2).shape)
    print(torch.unsqueeze(a,-3).shape)

输出:

2
torch.Size([1, 5, 5])
torch.Size([5, 1, 5])
torch.Size([5, 5, 1])
torch.Size([1, 5, 5])

TORCH.TENSOR.REPEAT

Tensor.repeat(*sizes) → Tensor
沿着指定的维重复这个张量。

参数
  • sizes:torch.Size or int,重复的次数
Example

输入

x = torch.tensor([1, 2, 3])
x.repeat(4, 2)
x.repeat(4, 2, 1).size()

输出

tensor([1, 2, 3])
tensor([[1, 2, 3, 1, 2, 3],
        [1, 2, 3, 1, 2, 3],
        [1, 2, 3, 1, 2, 3],
        [1, 2, 3, 1, 2, 3]])
torch.Size([4, 2, 3])

TORCH.TENSOR.CONTIGUOUS*

Tensor.contiguous(memory_format=torch.contiguous_format) → Tensor
在内存张量中返回包含与自张量相同的数据的连续张量,开辟新内存,contiguous()方法改变了多维数组在内存中的存储顺序,以便配合view方法使用。。如果自张量已经处于指定的内存格式,则此功能返回自张量。

参数
  • memory_format: 所期望的张量的存储格式
输出
  • 指定存储格式的张量

TORCH.BMM

torch.bmm(input, mat2, *, out=None) → Tensor
除去第一维(batch_size),进行的矩阵乘法运算
If input is a (b×n×m) tensor, mat2 is a (b×m×p) tensor, out will be a (b×n×p) tensor.

参数
  • input:参与运算的矩阵1 必须是3-D
  • mat2:参与运算的矩阵2 必须是3-D
输出

经过矩阵运算后的Tensor,3-D

Example

输入:

x = torch.randn([5,3,4])
y = torch.randn([5,4,2])
print(torch.bmm(x,y).shape)

输出:

torch.Size([5, 3, 2])
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值