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 ...
随机推荐
- jQuery文档处理总结
<!DOCTYPE html> <html lang="cn"> <head> <meta charset="UTF-8&quo ...
- 自学Zabbix之路15.4 Zabbix数据库表结构简单解析-Expressions表、Media表、 Events表
点击返回:自学Zabbix之路 点击返回:自学Zabbix4.0之路 点击返回:自学zabbix集锦 自学Zabbix之路15.4 Zabbix数据库表结构简单解析-Expressions表.Medi ...
- luogu2149 Elaxia的路线 (dijkstra+拓扑dp)
先标记上一个人所有最短路上的边(同时也要标记反向边) 然后拿着另一个人最短路上的边(会构成一个DAG)去做拓扑dp,记从原点到某个点的最大的某个路径的被标记的边的个数 #include<bits ...
- 单片机如何产生PWM信号
用89C52产生控制二相步进电机的程序,用PWM信号控制步进电机 用普通I/O口采用软件定时器中断可以模拟PWM输出 /*采用6MHz晶振,在P1.0脚上输出周期为2.5s,占空比为20%的脉冲信号* ...
- pip install时遇到MemoryError的原因和处理方法
前言:同学们在用pip install的时候,可能会遇到MemoryError的问题 报错如下,看最后一行的memory error关键字: 报错的原因大致如下:(详细细节可以查看此处) This e ...
- windows & gcc & mingw & mysy 编译 openssl
今天有一个项目需要使用到 https, 以前一直用的都是http请求, 用 socket() 实现 https 请求我还真是头一回遇到. 先网上搜索了一下相关资料,明白了 https 相比较 http ...
- 关于移动端及flex
我们知道写pc页面的时候,ui设计图是多少px,我们写网页的时候,就会写多少px,这个其实就是由pc端屏幕的物理像素,和我们设计图的css逻辑像素决定的,由于屏幕的物理像素和css逻辑像素比,刚好是1 ...
- 关闭应用程序(主程序)(WPF)
很多人认为关闭应用程序应该很简单,例如WindowsForm里一个Application.Exit();方法就可以解决问题,但在WPF里面可别滥用,因为WPF里Application类没有该方法,倒是 ...
- call_user_func 和 call_user_func_array用法
说明 call_user_func 和 call_user_func_array 相同:都可以调用函数和类内部的函数,不同:不同的是传递的参数不同,前者是一个参数一个参数传递, 后者是传递array参 ...
- kubernetes Dashboard 使用RBAC 权限认证控制
kubernetes RBAC实战 环境准备 先用kubeadm安装好kubernetes集群,[包地址在此](https://market.aliyun.com/products/56014009/ ...