当前位置:网站首页>详细的np.matmul / np.dot / np.multiply / tf.matmul / tf.multiply / *
详细的np.matmul / np.dot / np.multiply / tf.matmul / tf.multiply / *
2022-08-09 10:43:00 【模糊包】
总结
关于numpy
和tensor
的内部数据乘积,主要是有以下相关函数可以操作。
- 通用函数:
*
numpy
中函数:np.matmul
/np.dot
/np.multiply
tensorflow
中函数:tf.matmul
/tf.multiply
一览表
操作/ 数据 | numpy | tensor |
---|---|---|
* | 按位元素相乘,返回array | 按位元素相乘,返回tensor |
np.matmul(A,B) | 矩阵相乘操作,返回array | <!!无法操作!!> |
np.dot(A,B) | 点积,<内部操作意义不明> | 点积,<内部操作意义不明> |
np.multiply(A,B) | 按位元素相乘,返回array | 按位元素相乘,返回tensor |
tf.matmul(A,B) | 矩阵相乘操作,返回tensor | 矩阵相乘操作,返回tensor |
tf.multiply(A,B) | 按位元素相乘,返回tensor | 按位元素相乘,返回tensor |
- 通用函数可以对两种数据(
tensor
/array
)进行按位元素相乘的操作,返回对应的数据格式(tensor
返回tensor
,array
返回array
) np.matmul
可以对numpy
数据矩阵相乘操作,返回array
,对tensor
数据无法操作; 特殊np.multiply
可以对两种数据(tensor
/array
)均进行按位元素相乘的操作,返回对应的数据格式(tensor
返回tensor
,array
返回array
)tf.matmul(x,y)
可以对两种数据(tensor
/array
)矩阵相乘操作,均生成tensor
;tf.multiply
可以对两种数据(tensor
/array
)均进行按位元素相乘的操作,均生成tensor
。
给定实验数据
# numpy数据
x = np.random.randn(2,3,4)*10 # 2, 3, 4
y = np.random.randn(2,4,3)*10 # 2, 4, 3
z = np.random.randn(2,3,4)*10 # 2, 3, 4
# tensorflow数据
x_ = tf.convert_to_tensor(x) # 2, 3, 4
y_ = tf.convert_to_tensor(y) # 2, 4, 3
z_ = tf.convert_to_tensor(z) # 2, 3, 4
通用函数*
1.在tensor
数据中,只能对shape
相等的数据操作。是等效于tf.mul(x, z)
函数的,也就是按位元素相乘,但是该api
被废弃了,被tf.multiply(x_,z_)
代替。
如下所示的结果
int: x_*z_
out: <tf.Tensor 'mul_4:0' shape=(2, 3, 4) dtype=float64>
如果是x_*y_
会报错,因为矩阵结构不对。
2. 在numpy
的数据,只能对shape
相等的数据操作,就是等效于np.multiply(x, z)
函数的,也就是按位元素相乘。
int: (x*z).shape
out: [2,3,4]
int: np.multiply(x,z).shape
out: [2,3,4]
如果是x*y
会报错,因为矩阵结构不对。
numpy
中的操作函数
1.np.matmul
可以对numpy
数据矩阵相乘操作,返回array
。
对tensor
数据无法操作的原因是,tensor
只有在开启会话后才知道是什么具体内部信息。如下
# 对numpy数据操作
np.matmul(x,y)
array([[[ 135.00353387, 91.52903838, -171.80624465],
[ -48.06257232, 226.70191293, -99.31504134],
[ -13.07481602, -65.88699065, -285.02107715]],
[[-418.64431827, 352.70885364, 386.94553585],
[ 316.96375007, 80.67215049, -130.78989204],
[-207.21033988, 47.25128822, 62.5329744 ]]])
## 对tensor操作,没有开启会话,shape不清晰无法操作。
np.matmul(x_,y_)
ValueError: matmul: Input operand 0 does not have enough dimensions
(has 0, gufunc core with signature (n?,k),(k,m?)->(n?,m?) requires 1)
2.np.multiply
可以对两种数据(tensor
/array
)均进行按位元素相乘的操作,返回对应的数据格式(tensor
返回tensor
,array
返回array
)
# 对numpy数据操作
np.multiply(x,z)
array([[[ 13.2853443 , 17.24155086, -34.87846792, 38.32096247],
[-170.15795371, -32.70534678, -5.48962254, 130.43221194],
[ 38.3870938 , 150.13360364, 44.23867107, -74.42425187]],
[[ -91.30385223, -6.00035432, 9.35122873, 21.70442799],
[ 36.825695 , -104.87642619, -55.69509325, 89.90946649],
[ 52.98619734, 21.6885545 , 2.57517183, -2.36556962]]])
#对tensor操作
np.multiply(x_,z_)
<tf.Tensor 'mul_8:0' shape=(2, 3, 4) dtype=float64>
3.np.dot
这是最神奇的操作,官方描述含义是点积,但是我没咋用过,就不写了,而且生产的值在三维以上后,会非常奇怪,奇怪在违反我们常见的数据变化。。其实这个函数是符合某个公式,大家想详细了解的可以去查。
tensorflow
中的操作函数
1.tf.matmul(x,y)
可以对numpy和tensor
两种数据矩阵相乘操作 ,均生成tensor
。
tf.matmul(x,y)
<tf.Tensor 'MatMul_1:0' shape=(2, 3, 3) dtype=float64>
tf.matmul(x_,y_)
<tf.Tensor 'MatMul_4:0' shape=(2, 3, 3) dtype=float64>
2.tf.multiply(x,y)
可以对numpy和tensor
两种数据 按位元素相乘 操作,,均生成tensor
。
tf.multiply(x,z)
<tf.Tensor 'Mul_6:0' shape=(2, 3, 4) dtype=float64>
tf.multiply(x_,z_)
<tf.Tensor 'Mul_7:0' shape=(2, 3, 4) dtype=float64>
边栏推荐
猜你喜欢
BERT预训练模型(Bidirectional Encoder Representations from Transformers)-原理详解
LM小型可编程控制器软件(基于CoDeSys)笔记二十六:plc的数据存储区(模拟量输入通道部分)
Shell script combat (2nd edition) / People's Posts and Telecommunications Press Script 2 Validate input: letters and numbers only
深度学习--神经网络(基础讲解)
StratoVirt 中的虚拟网卡是如何实现的?
shell脚本实战(第2版)/人民邮电出版社 脚本2 验证输入:仅限字母和数字
2021-01-11-雪碧图做表情管理器
Probably 95% of the people are still making PyTorch mistakes
PoseNet: A Convolutional Network for Real-Time 6-DOF Camera Relocalization论文阅读
ESIM(Enhanced Sequential Inference Model)- 模型详解
随机推荐
虚拟列表key复用问题
可能95%的人还在犯的PyTorch错误
Electron application development best practices
Mysql多表查询
OneNote 教程,如何在 OneNote 中搜索和查找笔记?
好久没上博客了,好长时间没有进展了
我用开天平台做了一个定时发送天气预报系统【开天aPaaS大作战】
VBA实战(11) - 工作表(Sheet) 操作汇总
Transformer+Embedding+Self-Attention原理详解
通过Doc在MySQL数据库中建表
单元测试2之实际结果检查的引用
函数组件和类组件和dva视图更新问题
Dialogue with the DPO of a multinational consumer brand: How to start with data security compliance?See you on 8.11 Live!
实现下拉加载更多
Unix Environment Programming Chapter 15 15.3 Functions popen and pclose
分类预测 | MATLAB实现CNN-LSTM(卷积长短期记忆神经网络)多特征分类预测
判断一段文字的width
2021-01-11-雪碧图做表情管理器
情感分析SowNLP词库
Umi Hooks