当前位置:网站首页>tensorflow einsum函数
tensorflow einsum函数
2022-07-24 05:50:00 【不负韶华ღ】
函数原型
tf.einsum(
equation, *inputs, **kwargs
)
函数说明
用于实现tensorflow中张量的点积、外积、装置、矩阵乘法等操作,einsum是一个表达以上这些运算,包括复杂张量运算在内的优雅方式,基本上,可以把einsum看成一种领域特定语言。一旦你理解并能利用einsum,除了不用记忆和频繁查找特定库函数这个好处以外,你还能够更迅速地编写更加紧凑、高效的代码。
比如,两个张量A、B相乘,得到矩阵C,用公式表示如下:

用相应的einsum表示为:
ij,jk->ik
参数equation表示不同操作对应的einsum记法,是一个字符串;参数*inputs表示输入的多个张量,形状要和equation中的对应。
函数使用
1、矩阵点积,c = sum_i a[i]*b[i]
>>> a = tf.constant([1, 2, 3, 4])
>>> b = tf.constant([2, 2, 2, 2])
>>> tf.einsum("i,i->", a, b)
<tf.Tensor: shape=(), dtype=int32, numpy=20>
2、矩阵乘法,c[i, k] = sum_j a[i, j]*b[j, k]
>>> a = tf.ones([2, 2])
>>> b = tf.ones([2, 2])
>>> tf.einsum("ij,jk->ik", a, b)
<tf.Tensor: shape=(2, 2), dtype=float32, numpy=
array([[2., 2.],
[2., 2.]], dtype=float32)>
3、矩阵转置, b[j, i] = a[i, j]
>>> a = tf.constant([[1, 2], [3, 4]])
>>>> a
<tf.Tensor: shape=(2, 2), dtype=int32, numpy=
array([[1, 2],
[3, 4]])>
>>> tf.einsum("ij->ji", a)
<tf.Tensor: shape=(2, 2), dtype=int32, numpy=
array([[1, 3],
[2, 4]])>
4、获取矩阵的对角元素
>>> a = tf.linalg.band_part(tf.ones((3, 3)), 0, 0)
>>> a
<tf.Tensor: shape=(3, 3), dtype=float32, numpy=
array([[1., 0., 0.],
[0., 1., 0.],
[0., 0., 1.]], dtype=float32)>
>>> tf.einsum("ii->i", a)
<tf.Tensor: shape=(3,), dtype=float32, numpy=array([1., 1., 1.], dtype=float32)>
边栏推荐
- [lvgl (1)] a brief introduction to lvgl
- Special effects - when the mouse moves, there will be a custom expression trail
- kubernetes简介(kubernetes优点)
- sql server 同步数据库 跨网段无公网ip几个常见小问题问题
- HashSet to array
- Special effects - Cherry Blossom falling background effects
- I have seven schemes to realize web real-time message push, seven!
- nodejs开启多进程并实现进程间通信
- [esp8266 spot welder] Based on esp8266 for Arduino
- Speed pointer in JS linked list
猜你喜欢

不运动也能增肌???打一针冬眠黑熊的血清就行

Redis.conf详解

三级分类/菜单的查询——树形结构
![[esp8266 spot welder] Based on esp8266 for Arduino](/img/9a/81b0d16572116445d46ba4e34a6e4e.png)
[esp8266 spot welder] Based on esp8266 for Arduino

GE口:SGMII模式和serdes模式

安装snownlp包过程出现Requirement already satisfied:及Read timed out.问题解决方法

【学习笔记】网页出现白屏可能的原因与优化方法

SparkSQL核心使用,220724,

sql server 同步数据库 跨网段无公网ip几个常见小问题问题

It's not too much to fight a landlord in idea!
随机推荐
神经网络超参数调整(基于ray包)
歹徒逃亡3
Redis.conf details
js和ts学习总结
UE4/5 无法打开文件“xxx.generated.h”(Cannot open file xxx.generated.h)的解决方法总结
nodejs开启多进程并实现进程间通信
[small object velocimeter] only principle, no code
Special effects - bubble tailing occurs when the mouse moves
Jenkins CI CD
[lvgl (5)] label usage
[lvgl (important)] style attribute API function and its parameters
Talk about browser cache again
Ge port: sgmii mode and SerDes mode
【LVGL(2)】LVGL入门,在CodeBlock上进行模拟以及移植STM32
MGR_ mysqlsh_ Keepalive high availability architecture deployment document
【LVGL(5)】标签的(label)用法
[lvgl (2)]
【LVGL(1)】LVGL的简单介绍
类的加载器 和 双亲委派机制详解
MySQL gets the self incrementing line mark (different from MySQL version)