TensorFlow基础(5)张量运算

第一篇(常用数学运算)

基本数学运算

加减乘除运算:
tf.add(x, y)  # 逐个元素 加
tf.subtract(x, y)  # 减
tf.multiply(x, y)  # 乘
tf.divide(x ,y)  # 除
tf.math.mod(x, y)  # 取模

a = tf.constant([0, 1, 2])
b = tf.constant([3, 4, 5])
tf.add(a, b)
幂指对数运算:
tf.pow(x, y)  # 对x求y的幂次方
tf.square(x)  # 对x逐个元素计算平方
tf.sqrt(x)  # 对x逐个元素开平方根
tf.exp(x)  # 计算e的x次方
tf.math.log(x)  # 计算自然对数,底数为e

tf.pow()

>>> x = tf.range(4)
>>> x
<tf.Tensor: shape=(4,), dtype=int32, numpy=array([0, 1, 2, 3])>
>>> tf.pow(x, 2)
<tf.Tensor: shape=(4,), dtype=int32, numpy=array([0, 1, 4, 9])>

# 逐元素幂运算
>>> x = tf.constant([[2, 2], [3, 3]])
>>> y = tf.constant([[8, 16], [2, 3]])
>>> tf.pow(x, y)
<tf.Tensor: shape=(2, 2), dtype=int32, numpy=
array([[  256, 65536],
       [    9,    27]])>

# 注意:当指数小于1时,x必须为浮点数。如:
>>> x = tf.constant([1, 4, 9, 16], dtype=tf.float32)
>>> tf.pow(x, 0.5)
<tf.Tensor: shape=(4,), dtype=float32, numpy=array([1., 2., 3., 4.], dtype=float32)>

tf.square(x)

>>> x = tf.constant([1, 2, 3, 4])
>>> tf.square(x)
<tf.Tensor: shape=(4,), dtype=int32, numpy=array([ 1,  4,  9, 16])>

tf.sqrt(x)

>>> x = tf.constant([1, 4, 9, 16], dtype=tf.float32)
>>> tf.sqrt(x)
<tf.Tensor: shape=(4,), dtype=float32, 
numpy=array([0.99999994, 1.9999999 , 2.9999998 , 3.9999998 ], dtype=float32)>

tf.exp(1.)

>>> tf.exp(1.)
<tf.Tensor: shape=(), dtype=float32, numpy=2.7182817>

>>> x = tf.exp(3.)
>>> tf.math.log(x)
<tf.Tensor: shape=(), dtype=float32, numpy=3.0>


换底公式

	 		log   b
	 		    c
log   b = ——————————————
    a       log   a	
    	        c
# 利用换底公式求以2为底256的对数
>>> x = tf.constant(256.)
>>> y = tf.constant(2.)
>>> tf.math.log(x) / tf.math.log(y)
<tf.Tensor: shape=(), dtype=float32, numpy=8.0>

>>> x = tf.constant([[1, 9], [16, 100]], dtype=tf.float32)
>>> y = tf.constant([[2, 3], [2, 10]], dtype=tf.float32)
>>> tf.math.log(x) / tf.math.log(y)
<tf.Tensor: shape=(2, 2), dtype=float32, numpy=
array([[0., 2.],
       [4., 2.]], dtype=float32)>

其他运算

tf.sign(x)  # 返回x的符号
tf.abs(x)  # 对x逐元素求绝对值
tf.negative(x)  # 对x逐元素求相反数,y = -x
tf.reciprocal(x)  # 取x的倒数
tf.logical_not(x)  # 对x逐元素求逻辑非
tf.ceil(x)  # 向上取整
tf.floor(x)  # 向下取整
tf.rint(x)  # 取最接近的整数
tf.round(x)  # 对x逐元素求舍入最接近的整数
tf.maximum(x)  # 返回两tensor中的最大值
tf.minimum(x)  # 返回两tensor中的最小值 

三角函数和反三角函数运算

tf.cos(x)  # 三角函数cos
tf.sin(x)  # 三角函数sin
tf.tan(x)  # 三角函数tan
tf.acos(x)  # 反三角函数arccos
tf.asin(x)  # 反三角函数arcsin
tf.atan(x)  # 反三角函数arctan

重载运算符

x + y  # tf.add()
x - y  # tf.subtract()
x * y  # tf.multiply()
x / y  # tf.truediv()
x // y  # tf.floordiv()
x % y  # tf.math.mod()
x ** y  # tf.math.mod()
-x  # tf.neg()
abs(x)  # tf.abs()
x & y  # tf.logical_and()
x | y  # tf.logical_or()
x ^ y  # tf.logical_xor()
~x  # tf.logical_not()
x < y  # tf.less()
x <= y  # tf.less_equal()
x > y  # tf.greater()
x >= y  # tf.greater_equal()
# 加减乘除
>>> a = tf.constant([0, 1, 2, 3])
>>> b = tf.constant([4, 5, 6, 7])
>>> a + b
<tf.Tensor: shape=(4,), dtype=int32, numpy=array([ 4,  6,  8, 10])>
>>> a - b
<tf.Tensor: shape=(4,), dtype=int32, numpy=array([-4, -4, -4, -4])>
>>> a * b
<tf.Tensor: shape=(4,), dtype=int32, numpy=array([ 0,  5, 12, 21])>
>>> a / b
<tf.Tensor: shape=(4,), dtype=float64, numpy=array([0.        , 0.2       , 0.33333333, 0.42857143])>

# 取余
>>> a = tf.constant([0, 1, 2, 3])
>>> b = 2
>>> a % b
<tf.Tensor: shape=(4,), dtype=int32, numpy=array([0, 1, 0, 1])>

# 整除
>>> a = tf.constant([[0 ,1, 2, 3], [0, -1, -2, -3]])
>>> b = 2
>>> a // b  # 地板除法,结果总是取比精确值小的整数
<tf.Tensor: shape=(2, 4), dtype=int32, numpy=
array([[ 0,  0,  1,  1],
       [ 0, -1, -1, -2]])>

# 指数运算
>>> a = tf.constant([0, 1, 2, 3])
>>> b = 2
>>> a ** b
<tf.Tensor: shape=(4,), dtype=int32, numpy=array([0, 1, 4, 9])>

广播机制

# 定义张量
>>> a = tf.constant([1, 2, 3])
>>> a
<tf.Tensor: shape=(3,), dtype=int32, numpy=array([1, 2, 3])>
>>> b = tf.constant(np.arange(12).reshape(4, 3))
>>> b
<tf.Tensor: shape=(4, 3), dtype=int32, numpy=
array([[ 0,  1,  2],
       [ 3,  4,  5],
       [ 6,  7,  8],
       [ 9, 10, 11]])>

# 加法广播
>>> a + b  # 两个张量的最后一个维度长度相同
<tf.Tensor: shape=(4, 3), dtype=int32, numpy=
array([[ 1,  3,  5],
       [ 4,  6,  8],
       [ 7,  9, 11],
       [10, 12, 14]])>

# 乘法广播
>>> a * b  # 两个张量的最后一个维度长度相同
<tf.Tensor: shape=(4, 3), dtype=int32, numpy=
array([[ 0,  2,  6],
       [ 3,  8, 15],
       [ 6, 14, 24],
       [ 9, 20, 33]])>

当张量和NumPy数组共同参与运算时

# 执行TensorFlow操作,那么TensorFlow将自动的把NumPy数组转换为张量
>>> nd = np.ones([2, 2])
>>> nd
array([[1., 1.],
       [1., 1.]])
>>> t = tf.multiply(nd, 36)
>>> t
<tf.Tensor: shape=(2, 2), dtype=float64, numpy=
array([[36., 36.],
       [36., 36.]])>

# 执行NumPy操作,那么NumPy将自动的把张量转换为NumPy数组
>>> np.add(nd, t)
array([[37., 37.],
       [37., 37.]])

# 使用运算符操作,只要操作数中有一个是Tensor对象,就把所有的操作数都转化为张量
>>> a = tf.constant([1, 2, 3])
>>> b = np.array((4, 5, 6))
>>> a + b
<tf.Tensor: shape=(3,), dtype=int32, numpy=array([5, 7, 9])>

第二篇(张量的向量乘法(点乘)运算,常用的数据统计函数)

向量乘法(点乘)

# 点乘
tf.matmul()  # @运算符

>>> a = tf.constant(np.arange(6), shape=(2, 3))
>>> b = tf.constant(np.arange(6).reshape(3, 2))
>>> a
<tf.Tensor: shape=(2, 3), dtype=int32, numpy=
array([[0, 1, 2],
       [3, 4, 5]])>
>>> b
<tf.Tensor: shape=(3, 2), dtype=int32, numpy=
array([[0, 1],
       [2, 3],
       [4, 5]])>
>>> tf.matmul(a, b)
<tf.Tensor: shape=(2, 2), dtype=int32, numpy=
array([[10, 13],
       [28, 40]])>
>>> a @ b
<tf.Tensor: shape=(2, 2), dtype=int32, numpy=
array([[10, 13],
       [28, 40]])>

多维向量点乘

# 会采用广播机制

# 三维 @ 二维
>>> a = tf.random.normal([2, 3, 5])
>>> b = tf.random.normal([5, 4])
>>> tf.matmul(a, b)
<tf.Tensor: shape=(2, 3, 4), dtype=float32, numpy=
array([[[-0.46224403,  0.5559157 ,  0.91833496,  0.77977157],
        [ 0.9138464 ,  0.3697196 , -2.7319918 , -0.15955633],
        [ 1.1351829 , -0.07287735, -1.5838656 , -3.3237963 ]],
       [[-3.1731474 , -2.3501658 , -0.4614391 , -3.4226882 ],
        [ 1.6954948 , -1.8960452 , -0.89765346, -1.3077705 ],
        [ 0.99747443, -0.674185  ,  1.675419  ,  3.811974  ]]],
      dtype=float32)>

# 三维 @ 三维
# 后面两个维度点乘
>>> a = tf.constant(np.arange(12), shape=(2, 2, 3))
>>> b = tf.constant(np.arange(12), shape=(2, 3, 2))
>>> a
<tf.Tensor: shape=(2, 2, 3), dtype=int32, numpy=
array([[[ 0,  1,  2],
        [ 3,  4,  5]],
       [[ 6,  7,  8],
        [ 9, 10, 11]]])>
>>> b
<tf.Tensor: shape=(2, 3, 2), dtype=int32, numpy=
array([[[ 0,  1],
        [ 2,  3],
        [ 4,  5]],
       [[ 6,  7],
        [ 8,  9],
        [10, 11]]])>
>>> a @ b
<tf.Tensor: shape=(2, 2, 2), dtype=int32, numpy=
array([[[ 10,  13],
        [ 28,  40]],
       [[172, 193],
        [244, 274]]])>

# 四维 @ 四维
# 最后两个维度点乘
>>> a = tf.constant(np.arange(24), shape=(2, 2, 2, 3))
>>> b = tf.constant(np.arange(24), shape=(2, 2, 3, 2))
>>> a
<tf.Tensor: shape=(2, 2, 2, 3), dtype=int32, numpy=
array([[[[ 0,  1,  2],
         [ 3,  4,  5]],
        [[ 6,  7,  8],
         [ 9, 10, 11]]],
       [[[12, 13, 14],
         [15, 16, 17]],
        [[18, 19, 20],
         [21, 22, 23]]]])>
>>> b
<tf.Tensor: shape=(2, 2, 3, 2), dtype=int32, numpy=
array([[[[ 0,  1],
         [ 2,  3],
         [ 4,  5]],
        [[ 6,  7],
         [ 8,  9],
         [10, 11]]],
       [[[12, 13],
         [14, 15],
         [16, 17]],
        [[18, 19],
         [20, 21],
         [22, 23]]]])>
>>> a @ b
<tf.Tensor: shape=(2, 2, 2, 2), dtype=int32, numpy=
array([[[[  10,   13],
         [  28,   40]],
        [[ 172,  193],
         [ 244,  274]]],
       [[[ 550,  589],
         [ 676,  724]],
        [[1144, 1201],
         [1324, 1390]]]])>

数据统计:求张量在某个维度上、或者全局的统计值

# 返回的张量的维度均会降低(指定维度会消失)
tf.reduce_sum(input_tensor, axis)  # 求和
tf.reduce_mean(input_tensor, axis)  # 求平均值
tf.reduce_max(input_tensor, axis)  # 求最大值
tf.reduce_min(input_tensor, axis)  # 求最小值

# 求和
>>> a = tf.constant([[1, 5, 3], [4, 2, 6]])
>>> a
<tf.Tensor: shape=(2, 3), dtype=int32, numpy=
array([[1, 5, 3],
       [4, 2, 6]])>
>>> tf.reduce_sum(a, axis=0)
<tf.Tensor: shape=(3,), dtype=int32, numpy=array([5, 7, 9])>
>>> tf.reduce_sum(a, axis=1)
<tf.Tensor: shape=(2,), dtype=int32, numpy=array([ 9, 12])>
>>> tf.reduce_sum(a)  # 如果不指定axis
<tf.Tensor: shape=(), dtype=int32, numpy=21>

注意:int32求均值后仍然为int32,可用tf.cast(a, tf.float32)转为浮点型后再求均值

# 获取最值的索引
tf.argmax()
tf.argmin()
>>> a = tf.constant([[1, 5, 3], [4, 2, 6]])
>>> a
<tf.Tensor: shape=(2, 3), dtype=int32, numpy=
array([[1, 5, 3],
       [4, 2, 6]])>
>>> tf.argmax(a, axis=0)
<tf.Tensor: shape=(3,), dtype=int64, numpy=array([1, 0, 1], dtype=int64)>
>>> tf.argmax(a, axis=1)
<tf.Tensor: shape=(2,), dtype=int64, numpy=array([1, 2], dtype=int64)>
>>> tf.argmax(a)  #  与reduce_max不同,默认axis等于0
<tf.Tensor: shape=(3,), dtype=int64, numpy=array([1, 0, 1], dtype=int64)>
  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值