PyTorch 对应点相乘、矩阵相乘实例

yipeiwu_com6年前Python基础

一,对应点相乘,x.mul(y) ,即点乘操作,点乘不求和操作,又可以叫作Hadamard product;点乘再求和,即为卷积

data = [[1,2], [3,4], [5, 6]]
tensor = torch.FloatTensor(data)
 
tensor
Out[27]: 
tensor([[ 1., 2.],
    [ 3., 4.],
    [ 5., 6.]])
 
tensor.mul(tensor)
Out[28]: 
tensor([[ 1.,  4.],
    [ 9., 16.],
    [ 25., 36.]])

二,矩阵相乘,x.mm(y) , 矩阵大小需满足: (i, n)x(n, j)

tensor
Out[31]: 
tensor([[ 1., 2.],
    [ 3., 4.],
    [ 5., 6.]])
 
tensor.mm(tensor.t()) # t()是转置
Out[30]: 
tensor([[ 5., 11., 17.],
    [ 11., 25., 39.],
    [ 17., 39., 61.]])

以上这篇(标题)就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持【听图阁-专注于Python设计】。

相关文章

详解Python判定IP地址合法性的三种方法

IP合法性校验是开发中非常常用的,看起来很简单的判断,作用确很大,写起来比较容易出错,今天我们来总结一下,看一下3种常用的IP地址合法性校验的方法。 IPv4的ip地址格式:(1~2...

【python】matplotlib动态显示详解

【python】matplotlib动态显示详解

1.matplotlib动态绘图 python在绘图的时候,需要开启 interactive mode。核心代码如下: plt.ion(); #开启interactive mode...

python 字符串常用函数详解

字符串常用函数: 声明变量 str="Hello World" find() 检测字符串是否包含,返回该字符串位置,如果不包含返回-1 str.find("Hello") # 返回值...

django商品分类及商品数据建模实例详解

基类(商品类及分类类之间共同的字段) class BaseModle(models.Model): name = models.CharField(max_length=32,...

python 解析html之BeautifulSoup

复制代码 代码如下:# coding=utf-8 from BeautifulSoup import BeautifulSoup, Tag, NavigableString from S...