tf.matmul()和tf.multipy()的区别
首先我们分析一下下面的代码:
import tensorflow as tf
import numpy as np a=tf.constant([[1., 2., 3.],[4., 5., 6.]])
b=np.float32(np.random.randn(3,2))
#c=tf.matmul(a,b)
c=tf.multiply(a,b)
init=tf.global_variables_initializer()
with tf.Session() as sess:
print(c.eval())
问题是上面的代码编译正确吗?编译一下就知道,错误信息如下:
ValueError: Dimensions must be equal, but are 2 and 3 for 'Mul' (op: 'Mul') with input shapes: [2,3], [3,2].
显然,tf.multiply()表示点积,因此维度要一样。而tf.matmul()表示普通的矩阵乘法。
而且tf.multiply(a,b)和tf.matmul(a,b)都要求a和b的类型必须一致。但是之间存在着细微的区别。
在tf中所有返回的tensor,不管传进去是什么类型,传出来的都是numpy ndarray对象。
看看官网API介绍:
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
)
tf.multiply(
x,
y,
name=None
)
但是tf.matmul(a,b)函数不仅要求a和b的类型必须完全一致,同时返回的tensor类型同a和b一致;而tf.multiply(a,b)函数仅要求a和b的类型显式一致,同时返回的tensor类型与a一致,即在不声明类型的情况下,编译不报错。
例如:
#类型一致,可以运行
import tensorflow as tf
import numpy as np a=tf.constant([[1, 2, 3],[4, 5, 6]],dtype=np.float32)
b=np.float32(np.random.randn(3,2))
c=tf.matmul(a,b)
#c=tf.multiply(a,b)
init=tf.global_variables_initializer()
with tf.Session() as sess:
print (type(c.eval()),type(a.eval()),type(b))
#类型不一致,不可以运行
import tensorflow as tf
import numpy as np a=tf.constant([[1, 2, 3],[4, 5, 6]])
b=np.float32(np.random.randn(3,2))
c=tf.matmul(a,b)
#c=tf.multiply(a,b)
init=tf.global_variables_initializer()
with tf.Session() as sess:
print (type(c.eval()),type(a.eval()),type(b))
#类型不一致,可以运行,结果的类型和a一致
import tensorflow as tf
import numpy as np a=tf.constant([[1, 2, 3],[4, 5, 6]])
b=np.float32(np.random.randn(2,3))
#c=tf.matmul(a,b)
c=tf.multiply(a,b)
init=tf.global_variables_initializer()
with tf.Session() as sess:
print (c.eval())
print (type(c.eval()),type(a.eval()),type(b))
#类型不一致,不可以运行
import tensorflow as tf
import numpy as np a=tf.constant([[1, 2, 3],[4, 5, 6]], dtype=np.float32)
b=tf.constant([[1, 2, 3],[4, 5, 6]], dtype=np.int32)
#c=tf.matmul(a,b)
c=tf.multiply(a,b)
init=tf.global_variables_initializer()
with tf.Session() as sess:
print (c.eval())
print (type(c.eval()),type(a.eval()),type(b))
tf.matmul()和tf.multipy()的区别的更多相关文章
- tf.matmul() 和tf.multiply() 的区别
1.tf.multiply()两个矩阵中对应元素各自相乘 格式: tf.multiply(x, y, name=None) 参数: x: 一个类型为:half, float32, float64, u ...
- deep_learning_Function_tf.add()、tf.subtract()、tf.multiply()、tf.div()
tf.add().tf.subtract().tf.multiply().tf.div()函数介绍和示例 1. tf.add() 释义:加法操作 示例: x = tf.constant(2, dtyp ...
- tf.multiply()和tf.matmul()区别
(1)tf.multiply是点乘,即Returns x * y element-wise. (2)tf.matmul是矩阵乘法,即Multiplies matrix a by matrix b, p ...
- 图文:TF卡和SD卡的区别及什么是TF卡?什么是SD卡
小型存储设备凭借低廉的价格.多样化的品种.实用等特性大量充斥在大家身边,比如智能手机手机上.数码照相机上.游戏机上(一般是掌机)等都小型电子设备都频繁的使用到这种统称为SD的产品,比如TF卡和SD卡( ...
- tf.variable和tf.get_Variable以及tf.name_scope和tf.variable_scope的区别
在训练深度网络时,为了减少需要训练参数的个数(比如具有simase结构的LSTM模型).或是多机多卡并行化训练大数据大模型(比如数据并行化)等情况时,往往需要共享变量.另外一方面是当一个深度学习模型变 ...
- 【TensorFlow基础】tf.add 和 tf.nn.bias_add 的区别
1. tf.add(x, y, name) Args: x: A `Tensor`. Must be one of the following types: `bfloat16`, `half`, ...
- 深度学习原理与框架-Tensorflow基本操作-mnist数据集的逻辑回归 1.tf.matmul(点乘操作) 2.tf.equal(对应位置是否相等) 3.tf.cast(将布尔类型转换为数值类型) 4.tf.argmax(返回最大值的索引) 5.tf.nn.softmax(计算softmax概率值) 6.tf.train.GradientDescentOptimizer(损失值梯度下降器)
1. tf.matmul(X, w) # 进行点乘操作 参数说明:X,w都表示输入的数据, 2.tf.equal(x, y) # 比较两个数据对应位置的数是否相等,返回值为True,或者False 参 ...
- tf.Session()和tf.InteractiveSession()的区别
官方tutorial是这么说的: The only difference with a regular Session is that an InteractiveSession installs i ...
- tf.matmul函数和tf.multiply函数
tf.matmul(a,b,transpose_a=False,transpose_b=False, adjoint_a=False, adjoint_b=False, a_is_sparse=Fal ...
随机推荐
- USDT(omniCore)测试环境搭建
一.测试环境搭建. 注:由于window版本的omni出现同步不了的问题,推荐使用linux系统进行usdt测试链的搭建. 1.下载omnicore: wget https://bintray.com ...
- BZOJ1895Pku3580 supermemo——非旋转treap
题目描述 给出一个初始序列fA1;A2;:::Ang,要求你编写程序支持如下操作: 1. ADDxyD:给子序列fAx:::Ayg的每个元素都加上D.例如对f1,2, 3,4,5g执行"AD ...
- POJ - 3159(Candies)差分约束
题意: 就是分糖果 然后A觉得B比他优秀 所以分的糖果可以比他多 但最多不能超过c1个, B又觉得A比他优秀.... 符合差分约束的条件 设A分了x个 B分了y个 则x-y <= c1 , ...
- java web项目406错误的解决
返回的消息头浏览器不能解释 这里我们使用了@ResponseBody,返回数据后缀是,.json,但是我们的映射器后缀又是.html.最后浏览器收到数据不知该以哪种类型数据来进行解析,所以就会报406 ...
- 10 Zabbix Item类型之Zabbix IPMI类型
点击返回:自学Zabbix之路 Zabbix Item类型之Zabbix IPMI类型 一般使用zabbix IPMI 监控硬件信息,比如说温度. 在编译安装zabbix server的时候,一定要加 ...
- CPP相关的常见错误(更新ing)
01.只允许在C99模式下使用 for 循环初始化申明 解决:指定下即可 gcc -o xxx -std=c99 02.
- JAVA多线程之中断机制(如何处理中断?)
一,介绍 这篇文章主要记录使用 interrupt() 方法中断线程,以及如何对InterruptedException进行处理.感觉对InterruptedException异常进行处理是一件谨慎且 ...
- Java NIO -- 直接缓冲区与非直接缓冲区
直接缓冲区与非直接缓冲区: 非直接缓冲区:通过 allocate() 方法分配缓冲区,将缓冲区建立在 JVM 的内存中直接缓冲区:通过 allocateDirect() 方法分配直接缓冲区,将缓冲区建 ...
- Spring的FactoryBean使用
Spring中有两种类型的Bean,一种是普通Bean,另一种是工厂Bean,即FactoryBean.工厂Bean跟普通Bean不同,其返回的对象不是指定类的一个实例,其返回的是该工厂Bean的g ...
- (转)IBM mq基本使用
一.下载7.5 Trial版本 http://www.ibm.com/developerworks/downloads/ws/wmq/ 这是下载网址,下载前先必须注册IBM ID,下载完成后一路Nex ...