TensorFlow2.0(2):数学运算

1 基本运算:(+、-、*、/、//、%)

基本运算中所有实例都以下面的张量a、b为例进行:

In [1]: import tensorflow as tf

In [2]: a = tf.random.uniform([2, 3], minval=1, maxval=6,dtype=tf.int32)
   ...: b = tf.random.uniform([2, 3], minval=1, maxval=6,dtype=tf.int32)
2019-10-25 12:41:01.270857: I tensorflow/core/platform/cpu_feature_guard.cc:142] Your CPU supports instructions that this TensorFlow binary was not compiled to use: AVX2

In [3]: a
Out[3]:
<tf.Tensor: id=3, shape=(2, 3), dtype=int32, numpy=
array([[4, 4, 2],
       [3, 2, 3]])>

In [4]: b
Out[4]:
<tf.Tensor: id=7, shape=(2, 3), dtype=int32, numpy=
array([[2, 2, 3],
       [3, 5, 3]])>

(1)加(+)

In [5]: tf.add(a,b)  # 通过add方法执行加操作
Out[5]:
<tf.Tensor: id=10, shape=(2, 3), dtype=int32, numpy=
array([[6, 6, 5],
       [6, 7, 6]])>

In [6]: a + b  # 也可以通过操作符进行
Out[6]:
<tf.Tensor: id=12, shape=(2, 3), dtype=int32, numpy=
array([[6, 6, 5],
       [6, 7, 6]])>

(2)减(-)

In [7]: tf.subtract(a,b)
Out[7]:
<tf.Tensor: id=14, shape=(2, 3), dtype=int32, numpy=
array([[ 2,  2, -1],
       [ 0, -3,  0]])>

In [8]: a - b
Out[8]:
<tf.Tensor: id=16, shape=(2, 3), dtype=int32, numpy=
array([[ 2,  2, -1],
       [ 0, -3,  0]])>

(3)乘法(*)

In [9]: tf.multiply(a,b)
Out[9]:
<tf.Tensor: id=18, shape=(2, 3), dtype=int32, numpy=
array([[ 8,  8,  6],
       [ 9, 10,  9]])>

In [10]: a * b
Out[10]:
<tf.Tensor: id=20, shape=(2, 3), dtype=int32, numpy=
array([[ 8,  8,  6],
       [ 9, 10,  9]])>

(4)除法(/)

In [11]: tf.divide(a,b)
Out[11]:
<tf.Tensor: id=24, shape=(2, 3), dtype=float64, numpy=
array([[2.        , 2.        , 0.66666667],
       [1.        , 0.4       , 1.        ]])>

In [12]: a/b
Out[12]:
<tf.Tensor: id=28, shape=(2, 3), dtype=float64, numpy=
array([[2.        , 2.        
  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值