tf中 multiply 和 matmul 的区别
发布日期:2022-02-25 00:55:16 浏览次数:53 分类:技术文章

本文共 3560 字,大约阅读时间需要 11 分钟。

multiply: element-wise,x,y维数必须相同,否则报错,Returns x * y element-wise.

matmul: Multiplies matrix a by matrix b, producing a * b. 个人理解和numpy库里的np.dot用法相同

tf库解释:

1.tf.multiply

 

tf.multiply(

    x,
    y,
    name=None
)

Defined in .

See the guide: 

Returns x * y element-wise.

NOTE<a href="../tf/multiply"><code>tf.multiply</code></a> supports broadcasting. More about broadcasting 

Args:

  • x: A Tensor. Must be one of the following types: bfloat16halffloat32float64uint8int8uint16int16int32int64complex64complex128.
  • y: A Tensor. Must have the same type as x.
  • name: A name for the operation (optional).

Returns:

Tensor. Has the same type as x.

2.tf.matmul

 

tf.matmul(

    a,
    b,
    transpose_a=False,
    transpose_b=False,
    adjoint_a=False,
    adjoint_b=False,
    a_is_sparse=False,
    b_is_sparse=False,
    name=None
)

Defined in .

See the guide: 

Multiplies matrix a by matrix b, producing a * b.

The inputs must, following any transpositions, be tensors of rank >= 2 where the inner 2 dimensions specify valid matrix multiplication arguments, and any further outer dimensions match.

Both matrices must be of the same type. The supported types are: float16float32float64int32complex64complex128.

Either matrix can be transposed or adjointed (conjugated and transposed) on the fly by setting one of the corresponding flag to True. These are False by default.

If one or both of the matrices contain a lot of zeros, a more efficient multiplication algorithm can be used by setting the corresponding a_is_sparse or b_is_sparse flag to True. These are False by default. This optimization is only available for plain matrices (rank-2 tensors) with datatypes bfloat16 or float32.

For example:

 

# 2-D tensor `a`

# [[1, 2, 3],
#  [4, 5, 6]]
a = tf.constant([1, 2, 3, 4, 5, 6], shape=[2, 3])
# 2-D tensor `b`
# [[ 7,  8],
#  [ 9, 10],
#  [11, 12]]
b = tf.constant([7, 8, 9, 10, 11, 12], shape=[3, 2])
# `a` * `b`
# [[ 58,  64],
#  [139, 154]]
c = tf.matmul(a, b)
# 3-D tensor `a`
# [[[ 1,  2,  3],
#   [ 4,  5,  6]],
#  [[ 7,  8,  9],
#   [10, 11, 12]]]
a = tf.constant(np.arange(1, 13, dtype=np.int32),
                shape=[2, 2, 3])
# 3-D tensor `b`
# [[[13, 14],
#   [15, 16],
#   [17, 18]],
#  [[19, 20],
#   [21, 22],
#   [23, 24]]]
b = tf.constant(np.arange(13, 25, dtype=np.int32),
                shape=[2, 3, 2])
# `a` * `b`
# [[[ 94, 100],
#   [229, 244]],
#  [[508, 532],
#   [697, 730]]]
c = tf.matmul(a, b)
# Since python >= 3.5 the @ operator is supported (see PEP 465).
# In TensorFlow, it simply calls the `tf.matmul()` function, so the
# following lines are equivalent:
d = a @ b @ [[10.], [11.]]
d = tf.matmul(tf.matmul(a, b), [[10.], [11.]])

Args:

  • aTensor of type float16float32float64int32complex64complex128 and rank > 1.
  • bTensor with same type and rank as a.
  • transpose_a: If Truea is transposed before multiplication.
  • transpose_b: If Trueb is transposed before multiplication.
  • adjoint_a: If Truea is conjugated and transposed before multiplication.
  • adjoint_b: If Trueb is conjugated and transposed before multiplication.
  • a_is_sparse: If Truea is treated as a sparse matrix.
  • b_is_sparse: If Trueb is treated as a sparse matrix.
  • name: Name for the operation (optional).

Returns:

Tensor of the same type as a and b where each inner-most matrix is the product of the corresponding matrices in a and b, e.g. if all transpose or adjoint attributes are False:

output[..., i, j] = sum_k (a[..., i, k] * b[..., k, j]), for all indices i, j.

  • Note: This is matrix product, not element-wise product.

Raises:

  • ValueError: If transpose_a and adjoint_a, or transpose_b and adjoint_b are both set to True.

转载地址:https://blog.csdn.net/qq_30285471/article/details/81265953 如侵犯您的版权,请留言回复原文章的地址,我们会给您删除此文章,给您带来不便请您谅解!

上一篇:适配器模式
下一篇:tf.assign

发表评论

最新留言

网站不错 人气很旺了 加油
[***.192.178.218]2024年03月02日 01时27分06秒

关于作者

    喝酒易醉,品茶养心,人生如梦,品茶悟道,何以解忧?唯有杜康!
-- 愿君每日到此一游!

推荐文章

web开发 mysql安装_mysqlinstallerwebcommunity5.7.21.0.msi安装图文教程 2019-04-21
mysql concat 整数型_MySQL 数字类型转换函数(concat/cast) 2019-04-21
mysql单元格函数是_MySQL常用内置函数 2019-04-21
mysql 怎么字段分裂_你可以分裂/爆炸MySQL查询中的字段吗? 2019-04-21
mysql server卸载出错_Mysql卸载问题Start Server卡住报错解决方法 2019-04-21
全国省市区 mysql_2017全国省市区数据库【含三款数据库】 2019-04-21
druid加载MySQL驱动原理_你好,想知道mybatis+druid+jdbc 原理介绍? 2019-04-21
mysql 怎样链接jdbc_jdbc怎么链接mysql数据库 2019-04-21
mysql学生课程表试题_Mysql练习之 学生表、课程表 、教师表、成绩表 50道练习题... 2019-04-21
java exec封装_Java 执行系统命令工具类(commons-exec) 2019-04-21
php sha512解密,PHP加密函数 sha256 sha512 sha256_file() sha512_file() 2019-04-21
php将文件夹打包zip文件,PHP把文件夹打包成ZIP文件 2019-04-21
mysql in 逗号分隔,使用MySQL IN()从逗号分隔的值中获取记录? 2019-04-21
php静态页面示例,php生成静态页面的简单示例_php实例 2019-04-21
迭代函数系统matlab实现,线性随机IFS迭代Matlab示例程序.doc 2019-04-21
w3wp ash oracle,[20200409]使用ash_wait_chains注意的一个细节.txt 2019-04-21
c语言程序设计教程谭浩强张基温,C语言程序设计教程(第2版) 2019-04-21
idea xml文件引入类提示_IDEA中导入多module的Maven项目后子项目不正常的情况 2019-04-21
太赫兹芯片是什么原理_什么是太赫兹技术?艾舒朗太赫兹理疗鞋——脑梗后遗症+便秘+久站腿疼腿肿案例... 2019-04-21
exfat默认配置大小_mac开发配置技巧 2019-04-21