numpy和tensorflow中的各种乘法(点乘和矩阵乘)
作者:MHyourh 时间:2022-01-18 05:17:10
点乘和矩阵乘的区别:
1)点乘(即“ * ”) ---- 各个矩阵对应元素做乘法
若 w 为 m*1 的矩阵,x 为 m*n 的矩阵,那么通过点乘结果就会得到一个 m*n 的矩阵。
若 w 为 m*n 的矩阵,x 为 m*n 的矩阵,那么通过点乘结果就会得到一个 m*n 的矩阵。
w的列数只能为 1 或 与x的列数相等(即n),w的行数与x的行数相等 才能进行乘法运算。
2)矩阵乘 ---- 按照矩阵乘法规则做运算
若 w 为 m*p 的矩阵,x 为 p*n 的矩阵,那么通过矩阵相乘结果就会得到一个 m*n 的矩阵。
只有 w 的列数 == x的行数 时,才能进行乘法运算
1. numpy
1)点乘
import numpy as np
w = np.array([[0.4], [1.2]])
x = np.array([range(1,6), range(5,10)])
print w
print x
print w*x
运行结果如下图:
2)矩阵乘
import numpy as np
w = np.array([[0.4, 1.2]])
x = np.array([range(1,6), range(5,10)])
print w
print x
print np.dot(w,x)
运行结果如下:
2. tensorflow
1)点乘
import tensorflow as tf
w = tf.Variable([[0.4], [1.2]], dtype=tf.float32) # w.shape: [2, 1]
x = tf.Variable([range(1,6), range(5,10)], dtype=tf.float32) # x.shape: [2, 5]
y = w * x # 等同于 y = tf.multiply(w, x) y.shape: [2, 5]
sess = tf.Session()
init = tf.global_variables_initializer()
sess.run(init)
print sess.run(w)
print sess.run(x)
print sess.run(y)
运行结果如下:
2)矩阵乘
# coding:utf-8
import tensorflow as tf
w = tf.Variable([[0.4, 1.2]], dtype=tf.float32) # w.shape: [1, 2]
x = tf.Variable([range(1,6), range(5,10)], dtype=tf.float32) # x.shape: [2, 5]
y = tf.matmul(w, x) # y.shape: [1, 5]
sess = tf.Session()
init = tf.global_variables_initializer()
sess.run(init)
print sess.run(w)
print sess.run(x)
print sess.run(y)
运行结果如下:
来源:https://blog.csdn.net/sinat_38682860/article/details/99082542
标签:numpy,tensorflow,乘法
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
Python快速生成随机密码超简单实现
2022-08-07 19:26:09
![](https://img.aspxhome.com/file/2023/3/77923_0s.png)
豆瓣可以做而且值得做的几件事情
2009-04-24 12:07:00
selenium+python自动化测试之页面元素定位
2021-09-30 18:08:55
![](https://img.aspxhome.com/file/2023/0/75900_0s.png)
Python表示矩阵的方法分析
2022-06-24 01:31:59
![](https://img.aspxhome.com/file/2023/2/71342_0s.png)
详解python读写json文件
2022-11-01 16:18:53
更改localhost为其他名字的方法
2023-11-23 08:07:52
SQL Server中导入导出数据的三种方式
2008-11-28 15:53:00
显示你个性的鼠标指针
2011-06-14 09:41:41
让我们走进ASP.NET世界
2007-08-24 08:52:00
SQL Server中ISNULL函数介绍
2009-09-09 21:23:00
SQLServer 镜像功能完全实现
2011-09-30 11:33:07
预防PHPDDOS的发包攻击别人的方法(iis+linux)
2023-10-14 04:09:34
实现一个获取元素样式的函数getStyle
2009-02-10 10:37:00
python禁用键鼠与提权代码实例
2022-12-11 11:54:59
解决Python httpx 运行过程中无限阻塞的问题
2023-03-26 12:17:05
数据库理论:学习基于SQL数据库的算法
2009-01-13 13:38:00
关于python的对象序列化介绍
2023-07-27 05:02:31
Python操作MongoDB数据库PyMongo库使用方法
2023-03-16 00:13:14
asp的日期转换星座函数
2010-06-09 21:05:00
Python中使用gflags实例及原理解析
2023-01-28 20:25:24