pytorch学习2

运算


(1)torch.nn.functional.softmax(input, dim)
dim:指明维度,dim=0表示按列计算;dim=1表示按行计算。默认dim的方法已经弃用了,最好声明dim

import torch
x = torch.randn(3, 4)
y=torch.nn.functional.softmax(x, dim=0)
print(x)
print(y)

(2)张量的点乘,即哈达马积,torch.mul()

# 张量的点乘,即哈达马积,torch.mul()
x=torch.tensor([[1,2],
                [3,4]])
y=torch.tensor([[1,2],
                [3,4]])
print(torch.mul(x,y))

(3)矩阵的乘法,torch.mm(),遵循矩阵乘的规则,不能用于3Dtensor和2D的tensor的矩阵乘

x=torch.tensor([[1,2],
                [3,4]])
y=torch.tensor([[1,2],
                [3,4]])
print(torch.mm(x,y))

(4)tensor.matmul(),tensor的矩阵乘,可用用于不同维度的tensot的矩阵乘

import torch
x=torch.tensor([[[1,2,3],
                [2,3,1]],
               [[1,2,3],
                [2,3,1]]])
y=torch.tensor([[2],
                [1],
                [1]])
print(torch.matmul(x,y))
outpuy
tensor([[[7],
         [8]],

        [[7],
         [8]]])

(5)torch.bmm(),3Dtensor的矩阵乘,两个操作的tensor必须是3Dtensor

import torch
x=torch.tensor([[[1,2,3],
                [2,3,1]],
               [[1,2,3],
                [2,3,1]]])
y=torch.tensor([[[1,2,3],
                [2,3,1],
                 [2,3,1]],
                [[1,2,3],
                [2,3,1],
                 [2,3,1]]])
print(torch.bmm(x,y))
output
tensor([[[11, 17,  8],
         [10, 16, 10]],

        [[11, 17,  8],
         [10, 16, 10]]])
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值