(1)+ - * /
(2)** pow square
(3) sqrt
(4) // %
(5) exp log
(6)@ matmul
(7) linear layer

element-wise: + - * /
matrix-wise: @ matmul
dim-wise: reduce_mean/max/min/sum
**

一 + - * / % //运算

**
+:对应矩阵元素相加
-:对应矩阵元素相减
*:对应矩阵元素相除
/:对应矩阵元素相除
%:对应矩阵元素求余
//:对应矩阵元素整除

In [2]: a = tf.ones([2,2])#2*2矩阵,全部填充为1                                  In [3]: b =  tf.fill([2,2],2.)#2*2矩阵,全部填充为2                              In [4]: a
Out[4]:
<tf.Tensor: id=2, shape=(2, 2), dtype=float32, numpy=
array([[1., 1.],[1., 1.]], dtype=float32)>In [5]: b
Out[5]:
<tf.Tensor: id=5, shape=(2, 2), dtype=float32, numpy=
array([[2., 2.],[2., 2.]], dtype=float32)>In [6]: a + b#对应矩阵元素相加
Out[6]:
<tf.Tensor: id=8, shape=(2, 2), dtype=float32, numpy=
array([[3., 3.],[3., 3.]], dtype=float32)>In [7]: a - b#对应矩阵元素相减
Out[7]:
<tf.Tensor: id=10, shape=(2, 2), dtype=float32, numpy=
array([[-1., -1.],[-1., -1.]], dtype=float32)>In [8]: a * b #对应矩阵元素相乘
Out[8]:
<tf.Tensor: id=12, shape=(2, 2), dtype=float32, numpy=
array([[2., 2.],[2., 2.]], dtype=float32)>In [9]: a / b#对应矩阵元素相除
Out[9]:
<tf.Tensor: id=14, shape=(2, 2), dtype=float32, numpy=
array([[0.5, 0.5],[0.5, 0.5]], dtype=float32)>In [10]: b // a #对应矩阵元素整除
Out[10]:
<tf.Tensor: id=16, shape=(2, 2), dtype=float32, numpy=
array([[2., 2.],[2., 2.]], dtype=float32)>In [11]: b % a #对应矩阵元素求余
Out[11]:
<tf.Tensor: id=18, shape=(2, 2), dtype=float32, numpy=
array([[0., 0.],[0., 0.]], dtype=float32)>

**

二 tf.math.log( ) 和tf.math.exp( ) 函数

**

In [12]: a = tf.ones([2,2])                                                     In [13]: a
Out[13]:
<tf.Tensor: id=22, shape=(2, 2), dtype=float32, numpy=
array([[1., 1.],[1., 1.]], dtype=float32)>In [14]: tf.math.log(a)#矩阵对应元素取对数
Out[14]:
<tf.Tensor: id=24, shape=(2, 2), dtype=float32, numpy=
array([[0., 0.],[0., 0.]], dtype=float32)>In [15]: tf.math.exp(a)#矩阵对应元素取指数
Out[15]:
<tf.Tensor: id=26, shape=(2, 2), dtype=float32, numpy=
array([[2.7182817, 2.7182817],[2.7182817, 2.7182817]], dtype=float32)>
In [17]: a = tf.random.normal([2,2])                                            In [18]: a
Out[18]:
<tf.Tensor: id=38, shape=(2, 2), dtype=float32, numpy=
array([[ 0.12121297, -1.6076226 ],[ 1.4407614 ,  0.8430799 ]], dtype=float32)>In [19]: tf.math.log(a)/tf.math.log(2.)#计算矩阵对应元素以2为底的对数
Out[19]:
<tf.Tensor: id=43, shape=(2, 2), dtype=float32, numpy=
array([[-3.044384  ,         nan],[ 0.5268315 , -0.24625869]], dtype=float32)>In [20]: tf.math.log(a)/tf.math.log(10.)#计算矩阵对应元素以10为底的对数
Out[20]:
<tf.Tensor: id=48, shape=(2, 2), dtype=float32, numpy=
array([[-0.91645086,         nan],[ 0.15859208, -0.07413125]], dtype=float32)>

**

三 tf.pow( )和 tf.sqrt( )函数

**

In [2]: a = tf.fill([2,2],2.)                                                   In [3]: a
Out[3]:
<tf.Tensor: id=2, shape=(2, 2), dtype=float32, numpy=
array([[2., 2.],[2., 2.]], dtype=float32)>In [4]: tf.pow(a,3)#矩阵a所有元素取立方
Out[4]:
<tf.Tensor: id=5, shape=(2, 2), dtype=float32, numpy=
array([[8., 8.],[8., 8.]], dtype=float32)>In [5]: a**3#矩阵a所有元素取立方
Out[5]:
<tf.Tensor: id=8, shape=(2, 2), dtype=float32, numpy=
array([[8., 8.],[8., 8.]], dtype=float32)>In [6]: tf.sqrt(a)#矩阵a所有元素开平方
Out[6]:
<tf.Tensor: id=10, shape=(2, 2), dtype=float32, numpy=
array([[1.4142135, 1.4142135],[1.4142135, 1.4142135]], dtype=float32)>

**

四 @ matmul矩阵相乘

**

In [7]: a = tf.fill([2,2],1.)                                                   In [8]: b = tf.fill([2,2],2.)                                                   In [9]: a,b
Out[9]:
(<tf.Tensor: id=14, shape=(2, 2), dtype=float32, numpy=array([[1., 1.],[1., 1.]], dtype=float32)>,<tf.Tensor: id=17, shape=(2, 2), dtype=float32, numpy=array([[2., 2.],[2., 2.]], dtype=float32)>)In [10]: a @ b#矩阵相乘
Out[10]:
<tf.Tensor: id=20, shape=(2, 2), dtype=float32, numpy=
array([[4., 4.],[4., 4.]], dtype=float32)>In [11]: tf.matmul(a,b)#矩阵相乘
Out[11]:
<tf.Tensor: id=22, shape=(2, 2), dtype=float32, numpy=
array([[4., 4.],[4., 4.]], dtype=float32)>
In [12]: a = tf.ones([4,2,3])                                                   In [13]: b = tf.fill([4,3,5],2.)                                                In [14]: a@b    #[2,3]@[3,5] = [2,5]
Out[14]:
<tf.Tensor: id=30, shape=(4, 2, 5), dtype=float32, numpy=
array([[[6., 6., 6., 6., 6.],[6., 6., 6., 6., 6.]],[[6., 6., 6., 6., 6.],[6., 6., 6., 6., 6.]],[[6., 6., 6., 6., 6.],[6., 6., 6., 6., 6.]],[[6., 6., 6., 6., 6.],[6., 6., 6., 6., 6.]]], dtype=float32)>In [15]: tf.matmul(a,b)
Out[15]:
<tf.Tensor: id=32, shape=(4, 2, 5), dtype=float32, numpy=
array([[[6., 6., 6., 6., 6.],[6., 6., 6., 6., 6.]],[[6., 6., 6., 6., 6.],[6., 6., 6., 6., 6.]],[[6., 6., 6., 6., 6.],[6., 6., 6., 6., 6.]],[[6., 6., 6., 6., 6.],[6., 6., 6., 6., 6.]]], dtype=float32)>

With broadcasting

In [2]: a = tf.fill([4,2,3],1.)                                                                                                                                                                             In [3]: b = tf.fill([3,5],2.)                                                                                                                                                                               In [4]: a.shape
Out[4]: TensorShape([4, 2, 3])In [5]: b.shape
Out[5]: TensorShape([3, 5])In [6]: bb = tf.broadcast_to(b,[4,3,5])                                                                                                                                                                     In [7]: bb.shape
Out[7]: TensorShape([4, 3, 5])In [8]: a @ bb
Out[8]:
<tf.Tensor: id=8, shape=(4, 2, 5), dtype=float32, numpy=
array([[[6., 6., 6., 6., 6.],[6., 6., 6., 6., 6.]],[[6., 6., 6., 6., 6.],[6., 6., 6., 6., 6.]],[[6., 6., 6., 6., 6.],[6., 6., 6., 6., 6.]],[[6., 6., 6., 6., 6.],[6., 6., 6., 6., 6.]]], dtype=float32)>In [9]: tf.matmul(a,bb)
Out[9]:
<tf.Tensor: id=10, shape=(4, 2, 5), dtype=float32, numpy=
array([[[6., 6., 6., 6., 6.],[6., 6., 6., 6., 6.]],[[6., 6., 6., 6., 6.],[6., 6., 6., 6., 6.]],[[6., 6., 6., 6., 6.],[6., 6., 6., 6., 6.]],[[6., 6., 6., 6., 6.],[6., 6., 6., 6., 6.]]], dtype=float32)>

TensorFlow2.0:张量的数学运算相关推荐

  1. 【TensorFlow2.0】(5) 数学计算、合并、分割

    各位同学好,今天和大家分享一下TensorFlow2.0中的数学运算方法.合并与分割.内容有: (1)基本运算:(2)矩阵相乘:(3)合并 tf.concat().tf.stack():(4)分割 t ...

  2. 【Pytorch神经网络理论篇】 02 Pytorch快速上手(二)GPU与CPU张量切换+具有随机值的张量+张量的数学运算

    1 在GPU与CPU上定义张量 1.1 GPU与CPU的张量相互转化 import torch # 创建一个张量 a = torch.FloatTensor() # 将CPU上的张量在GPU所管理的内 ...

  3. TensorFlow2.0:张量限幅

    ** 一 tf.clip_by_value( )函数 ** tf.maximum(a,b)返回a和b之间的最大值 tf.minimum(a,b)返回a和b之间的最小值 tf.clip_by_value ...

  4. TensorFlow2.0:张量排序

    ** 一 sort argsort排序 ** tf.sort( )按照升序或者降序对张量进行排序 tf.argsort( )按照升序或者降序对张量进行排序,但返回的是索引 In [4]: a = tf ...

  5. TensorFlow2.0:张量的合并与分割

    ** 一 tf.concat( ) 函数–合并 ** In [2]: a = tf.ones([4,35,8]) In [3]: b = tf.ones([2,35,8]) In [4]: c = t ...

  6. 4-tensorflow中张量的数学运算

    1." + - * / // %(加 减 乘 除 整除 余数) " 2.tf.math.log和tf.exp和tf.pow以及tf.sqrt函数 3.矩阵相乘 1. 满足矩阵相乘的 ...

  7. php点加等于0,php 做数学运算时结果为0的原因

    MSSQL&mdash;字符串分离(Split函数) 前面提到了记录合并,有了合并需求肯定也会有分离需求,说到字符串分离,大家肯定会想到SPLIT函数,这个在.NET,Java和JS中都有函数 ...

  8. 【TensorFlow2.0】(4) 维度变换、广播

    各位同学好,今天我和大家分享一下TensorFlow2.0中有关数学计算的相关操作,主要内容有: (1) 改变维度:reshape():(2) 维度转置:transpose():(3) 增加维度:ex ...

  9. TensorFlow2.0学习

    文章目录 一.TensorFlow的建模流程 1.1 结构化数据建模流程范例 1.1.1 准备数据 1.1.2 定义模型 1.1.3 训练模型 1.1.4 评估模型 1.1.5 使用模型 1.1.6 ...

最新文章

  1. [20180408]那些函数索引适合字段的查询.txt
  2. 吴恩达机器学习 Coursera 笔记(二) - 单变量线性回归
  3. python range()内建函数
  4. qq分享 设备未授权报错解决方案_QQ 注销功能终于上线,我体验了一下!
  5. Flexible Box布局基础知识详解
  6. 【转】XMPP_3920_最靠谱的中文翻译文档
  7. 第二十二期:New一个对象的时候发生了什么?
  8. 在NAS上基础构建云存储系统的两种解决方案
  9. mysql中怎样查看和删除唯一索引
  10. Ansible(自动化运维工具--playbook)
  11. 猎豹网c 语言程序设计,[C/C++基础] 猎豹网校 C++ Primer初级/中级/高级合集发布 猎豹网校Primer视频教程...
  12. 微云存储空间多大_qq微云内存多大
  13. 本科生怎样发表自己的论文
  14. python代码去马赛克,Python黑科技神奇去除马赛克
  15. HTTP缓存 Last-Modified
  16. 小知识·adb安装和使用方法
  17. 用关联规则和聚类探索药物配伍规律
  18. java 混淆_Java 混淆那些事(一):重新认识 ProGuard
  19. 《Microduino实战》——1.1 什么是开源
  20. 如何系统学习一门编程语言? | 黑马程序员

热门文章

  1. 自定义view圆环的改变
  2. 首页 、引导页、版本
  3. CentOS下MySQL主从同步配置
  4. 【品味人生】毕业十年有感,给年轻人的一点忠告
  5. OO4O的session残留问题
  6. 宜昌方言RAP 说唱 《在宜昌》
  7. 多路I/O转接之select模型
  8. [编织消息框架][优化系统]突破连接上限(上)
  9. Android开发——搭建最新版本的Android开发环境
  10. 生命太短暂,我没时间讨厌你