张量、矩阵乘法 与 concatenate

1. 张量乘法

1.1 dot product

有张量a和张量b

a = torch.tensor([1,2,3])

b = torch.tensor([2,3,1])

c = torch.dot(a,b) = 11

torch.dot(a,b)对参数的限制:a和b均为1维张量。

1.2 element-wise product

c = torch.multiply(a,b) = \begin{bmatrix} 2 &6 &3 \end{bmatrix}

2. 矩阵乘法

2.1 inner product

a = torch.tensor([[1,2,3],[2,3,1]])

b = a.T

c = torch.matmul(a,b) = \begin{bmatrix} 14 & 11 \\ 11 & 14 \end{bmatrix}

2.2 element-wise product

a = torch.tensor([[1,2,3],[2,3,1]])

b = torch.tensor([[1,2,2],[3,2,1]])

c = torch.multiply(a,b) 或者 c= torch.mul(a,b) 

综上,我们可以发现,张量的点积和矩阵的内积其实是一个道理,同理,张量和矩阵的对应位置元素乘积也是一个道理,我们称之为Hadamard product。

3. torch.concat()

对张量或矩阵沿同一维度进行拼接

a = torch.tensor([1,2,3])

b = torch.tensor([2,3,1])

c= torch.concat([a,b],dim=0) = \begin{bmatrix} 1 &2 &3 \\2 &3 &1 \end{bmatrix}

d = torch.concat([a,b],dim=1) = \begin{bmatrix} 1 &2 &3 &2 &3 &1 \end{bmatrix}

a = torch.tensor([[1,2,3],[2,1,3]])

b = torch.tensor([[2,3,1],[3,2,1]])

c = torch.concat([a,b],dim=0) = \begin{bmatrix} 1 &2 &3 \\2 &1 &3 \\2 & 3 & 1\\ 3 & 2 &1 \end{bmatrix}

c = torch.concat([a,b],dim=1) = \begin{bmatrix} 1 &2 &3 &2 &3 &1 \\2 &1 &3 &3 &2 &1 \end{bmatrix}

4.torch.stack()

在新的维度上进行拼接

c = torch.stack([a,b],0),得到2*2*3的矩阵。

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值