现在我们必须知道可以加到TensorFlow计算图上的其他计算工具。 除了标准的算式运算外,TensorFlow提供给我们更多需要注意的运算符,我们应当在继续之前知道如何使用它们。同样,我们需要运行一下下面的命令来创建一个 graph session:

>>> import numpy as np
>>> import tensorflow as tf
>>> from tensorflow.python.framework import ops
>>> ops.reset_default_graph()
>>> tf.compat.v1.disable_eager_execution()
>>> sess = tf.compat.v1.Session()

TensorFlow对张量有标准的运算符:add() , sub() , mul() , 和 div() . 需要指出的是,这部分所有的运算除了特别说明外,都会输出element-wise式输出结果。

div() 函数及其相关的函数

div() 返回与输出结果类型相同的结果。这意味着如果输入的是整数的话,它返回 the floor of the division (是 Python 2 的近亲)。为了产生 Python 3 版本的结果,TensorFlow提供了 truediv() 函数,如下:

>>> print(sess.run(tf.compat.v1.div(3,4)))
0
>>> print(sess.run(tf.compat.v1.truediv(3,4)))
0.75
>>> print(sess.run(tf.compat.v1.div(3.0,4)))
0.75

如果我们浮点数然后希望做一个整数除法,我们可以用 floordiv() 函数。 需要注意的是,我们仍然返回一个浮点数,但是已经被近似成最近邻的整数。如下:

>>> print(sess.run(tf.compat.v1.floordiv(3.0,4.0)))
0.0

mod() 函数

另外一个重要的函数就是 mod() . 这个函数返回除法的余数。如下:

>>> print(sess.run(tf.compat.v1.mod(22,5)))
2
>>> print(sess.run(tf.compat.v1.mod(22.0,5)))
2.0

cross() 函数

两个张量的叉乘可以通过调用 tensorflow.compat.v1.cross() 函数来实现。记住,这里的叉乘只定义到俩个三维向量,所以它仅支持俩个三维向量。如下:

>>> print(sess.run(tf.compat.v1.cross([1.,2.,3.],[4.,5.,6.])))
[-3.  6. -3.]
>>> help(tf.compat.v1.cross)

以下是 help() 函数返回的结果:

cross(a, b, name=None)

Compute the pairwise cross product.

a and b must be the same shape; they can either be simple 3-element vectors, or any shape where the innermost dimension is 3. In the latter case, each pair of corresponding 3-element vectors is cross-multiplied independently.

参数:
  • a (Tensor) – Must be one of the following types: float32, float64, int32, uint8, int16, int8, int64, bfloat16, uint16, half, uint32, uint64. A tensor containing 3-element vectors.
  • b (Tensor) – Must have the same type as a.
  • name – A name for the operation (optional).
返回类型:

Tensor

返回:

Has the same type as a.

常用的数学函数列表

注意

所有这些函数都是element-wise式运行。

常用数学函数 描述
tensorflow.compat.v1.abs() 输入张量的绝对值
tensorflow.compat.v1.ceil() 输入张量的向上舍入函数
tensorflow.compat.v1.cos() 输入张量的Cosine函数
tensorflow.compat.v1.exp() 输入张量的exp函数
tensorflow.compat.v1.floor() 输入张量的向下舍入函数
tensorflow.compat.v1.inv() 输入张量的倒数(用不了)
tensorflow.compat.v1.log() 输入张量的自然对数
tensorflow.compat.v1.maximum() 输入张量的最大值
tensorflow.compat.v1.minimum 输入张量的最小值
tensorflow.compat.v1.negative() 输入张量的负值
tensorflow.compat.v1.pow() 第一张量上升到第二张量元素
tensorflow.compat.v1.round() 输入张量的近似
tensorflow.compat.v1.rsqrt() 输入张量平方根的倒数
tensorflow.compat.v1.sign() 输出 -1, 0, 或 1 取决输入张量的符号
tensorflow.compat.v1.sin() 输入张量的Sine函数
tensorflow.compat.v1.sqrt() 输入张量的平方根
tensorflow.compat.v1.square() 输入张量的平方

以下是这些常用数学函数的实例:

>>> A = tf.fill([3,3],1.0)
>>> B = tf.constant([[-1.,-2.,-3.],[-4.,-5.,-6.],[-7.,-8.,-9.]])

>>> sess.run(tf.compat.v1.abs(B))
array([[1., 2., 3.],
       [4., 5., 6.],
       [7., 8., 9.]], dtype=float32)

>>> sess.run(tf.compat.v1.ceil(B))
array([[-1., -2., -3.],
       [-4., -5., -6.],
       [-7., -8., -9.]], dtype=float32)

>>> sess.run(tf.compat.v1.cos(B))
array([[ 0.5403023 , -0.4161468 , -0.9899925 ],
       [-0.6536436 ,  0.28366217,  0.96017027],
       [ 0.75390226, -0.14550003, -0.91113025]], dtype=float32)

>>> C = sess.run(tf.compat.v1.ceil(sess.run(tf.compat.v1.cos(B))))
>>> C
array([[ 1., -0., -0.],
       [-0.,  1.,  1.],
       [ 1., -0., -0.]], dtype=float32)

>>> sess.run(tf.compat.v1.exp(B))
array([[3.6787945e-01, 1.3533528e-01, 4.9787067e-02],
       [1.8315639e-02, 6.7379470e-03, 2.4787523e-03],
       [9.1188197e-04, 3.3546262e-04, 1.2340980e-04]], dtype=float32)

>>> D = sess.run(tf.compat.v1.floor(sess.run(tf.compat.v1.cos(B))))
>>> D
array([[ 0., -1., -1.],
       [-1.,  0.,  0.],
       [ 0., -1., -1.]], dtype=float32)

>>> sess.run(tf.compat.v1.log(A))
array([[0., 0., 0.],
       [0., 0., 0.],
       [0., 0., 0.]], dtype=float32)

>>> sess.run(tf.compat.v1.maximum(A,C))
array([[1., 1., 1.],
       [1., 1., 1.],
       [1., 1., 1.]], dtype=float32)

>>> sess.run(tf.compat.v1.minimum(A,C))
array([[ 1., -0., -0.],
       [-0.,  1.,  1.],
       [ 1., -0., -0.]], dtype=float32)

>>> sess.run(tf.compat.v1.negative(B))
array([[1., 2., 3.],
       [4., 5., 6.],
       [7., 8., 9.]], dtype=float32)

# 平方
>>> sess.run(tf.compat.v1.pow(B,2))
array([[ 1.,  4.,  9.],
       [16., 25., 36.],
       [49., 64., 81.]], dtype=float32)

>>> sess.run(tf.compat.v1.round(C))
array([[ 1., -0., -0.],
       [-0.,  1.,  1.],
       [ 1., -0., -0.]], dtype=float32)

# rsqrt是指reverse + square root, 即求平方根之后再求倒数
>>> E = sess.run(tf.compat.v1.rsqrt(tf.compat.v1.exp(B)))
>>> E
array([[ 1.6487212,  2.7182817,  4.481689 ],
       [ 7.3890557, 12.182494 , 20.085537 ],
       [33.11545  , 54.598145 , 90.017136 ]], dtype=float32)

>>> F = sess.run(tf.compat.v1.sqrt(tf.compat.v1.exp(B)))
>>> F
array([[0.60653067, 0.36787942, 0.22313015],
       [0.13533528, 0.082085  , 0.04978707],
       [0.03019738, 0.01831564, 0.011109  ]], dtype=float32)
>>> sess.run(tf.compat.v1.multiply(E,F))
array([[1.        , 0.99999994, 0.99999994],
       [0.99999994, 1.        , 1.        ],
       [1.        , 0.9999998 , 1.        ]], dtype=float32)

>>> sess.run(tf.compat.v1.sign(A))
array([[1., 1., 1.],
       [1., 1., 1.],
       [1., 1., 1.]], dtype=float32)

>>> sess.run(tf.compat.v1.sign(A))
array([[1., 1., 1.],
       [1., 1., 1.],
       [1., 1., 1.]], dtype=float32)
>>> sess.run(tf.compat.v1.sign(B))
array([[-1., -1., -1.],
       [-1., -1., -1.],
       [-1., -1., -1.]], dtype=float32)

>>> sess.run(tf.compat.v1.sign(D))
array([[ 0., -1., -1.],
       [-1.,  0.,  0.],
       [ 0., -1., -1.]], dtype=float32)

以下是 tensorflow.compat.v1.sign 的详细介绍:

sign(x, name=None)

返回矩阵元素的符号。如果 x < 0 , 返回 -1; 如果 x==0 , 返回 0; 如果 x>0 , 返回 1. 对于复数而言, 如果 x!=0, 返回 y=sign(x)=x/|x|, 否则返回 0

参数:x (Tensor) – 必须下面中个一种类型,bfloat16`, half, float32, float64, int32, int64, complex64, complex128.
关键字参数:name – 操作符的名称(可选)
返回:x (张量)形状
返回类型:Tensor
>>> sess.run(tf.compat.v1.sin(B))
array([[-0.84147096, -0.9092974 , -0.14112   ],
       [ 0.7568025 ,  0.9589243 ,  0.2794155 ],
       [-0.6569866 , -0.98935825, -0.4121185 ]], dtype=float32)

>>> sess.run(tf.compat.v1.sqrt(A))
array([[0.99999994, 0.99999994, 0.99999994],
       [0.99999994, 0.99999994, 0.99999994],
       [0.99999994, 0.99999994, 1.        ]], dtype=float32)

>>> sess.run(tf.compat.v1.square(B))
array([[ 1.,  4.,  9.],
       [16., 25., 36.],
       [49., 64., 81.]], dtype=float32)

特殊数学函数列表

这里还有一些值得注意的特殊数学函数,这些函数可能会在机器学习中出现,幸运的是,TensorFlow有一些内置函数可以调用。值得注意的是,这些函数除了特殊说明,都是元素式运行的。

特殊数学函数 描述
tensorflow.compat.v1.digamma() Psi 函数,是 lgamma() 函数的导数
tensorflow.compat.v1.erf() 输入张量的高斯误差函数(元素式运行)
tensorflow.compat.v1.erfc() 输入张量的高斯误差补余函数
tensorflow.compat.v1.igamma() 下正则不完全伽玛函数
tensorflow.compat.v1.igammac() 上正则不完全伽玛函数
tensorflow.compat.v1.lbeta() beta 函数绝对值的自然对数
tensorflow.compat.v1.lgamma() gamma 函数绝对值的自然对数
tensorflow.compat.v1.squared_difference() 两个张量差值的平方

下面给出这些函数的实例,详情请看本节学习模块:

>>> sess.run(tf.compat.v1.digamma(E))
array([[0.16705728, 0.8049263 , 1.384306  ],
       [1.9308087 , 2.4583962 , 2.9749    ],
       [3.4848251 , 3.9908142 , 4.494435  ]], dtype=float32)

>>> sess.run(tf.compat.v1.erf(B))
array([[-0.8427007, -0.9953223, -0.999978 ],
       [-1.       , -1.       , -1.       ],
       [-1.       , -1.       , -1.       ]], dtype=float32)

>>> sess.run(tf.compat.v1.erfc(B))
array([[1.8427007, 1.9953222, 1.999978 ],
       [2.       , 2.       , 2.       ],
       [2.       , 2.       , 2.       ]], dtype=float32)

>>> sess.run(tf.compat.v1.igamma(A,E))
array([[0.8077043 , 0.93401194, 0.9886857 ],
       [0.999382  , 0.9999949 , 1.        ],
       [1.        , 1.        , 1.        ]], dtype=float32)

>>> sess.run(tf.compat.v1.igammac(A,E))
array([[1.9229566e-01, 6.5988049e-02, 1.1314288e-02],
       [6.1797921e-04, 5.1192928e-06, 1.8921789e-09],
       [4.1508981e-15, 1.9423487e-24, 0.0000000e+00]], dtype=float32)

>>> sess.run(tf.compat.v1.lbeta(E))
array([  -7.5096974,  -40.50966  , -182.8869   ], dtype=float32)

>>> sess.run(tf.compat.v1.lgamma(E))
array([[-1.0544503e-01,  4.4946167e-01,  2.4283466e+00],
       [ 7.3192654e+00,  1.7949518e+01,  3.9594162e+01],
       [ 8.1960083e+01,  1.6271490e+02,  3.1372983e+02]], dtype=float32)

# 最简单理解的函数
>>> sess.run(tf.compat.v1.squared_difference(A,B))
array([[  4.,   9.,  16.],
       [ 25.,  36.,  49.],
       [ 64.,  81., 100.]], dtype=float32)

知道哪些函数可以用,可以加到计算图上,对我们来说很重要。大多数情况下,我们只需要关注前面提到函数。我们也可以自己定义函数或者自己根据数学表达式利用前面提到的函数,如下:

# tan()函数 tan(pi/4) = 1
>>> from numpy import pi
>>> print(sess.run(tf.compat.v1.div(tf.sin(pi/4.),tf.cos(pi/4.))))
1.0

自定义函数

如果我们想在计算图中加一些没在表格中出现的函数,我们可以通过前面的函数来创建自己想要的函数。这里一个函数例子,我们可以加到我们的计算图中:

>>> def custom_polynomial(value):
...   return (tf.compat.v1.subtract(3*tf.compat.v1.square(value),value)+10)
>>> print(sess.run(custom_polynomial(11)))
362

这里我们创建了一个多项式函数: \(f(x) = 3 \ast x^2-x+10\)

本节学习模块

注意

tensorflow.compat.v1.div函数介绍

Divides x / y elementwise (using Python 2 division operator semantics). (deprecated)

Warning: THIS FUNCTION IS DEPRECATED. It will be removed in a future version. Instructions for updating: Deprecated in favor of operator or tf.math.divide.

NOTE: Prefer using the Tensor division operator or tf.divide which obey Python 3 division operator semantics.

This function divides x and y, forcing Python 2 semantics. That is, if x and y are both integers then the result will be an integer. This is in contrast to Python 3, where division with / is always a float while division with // is always an integer.

param x:Tensor numerator of real numeric type.
param y:Tensor denominator of real numeric type.
param name:A name for the operation (optional).
returns:x / y returns the quotient of x and y.

注意

tensorflow.compat.v1.truediv函数介绍

Divides x / y elementwise (using Python 3 division operator semantics).

NOTE: Prefer using the Tensor operator or tf.divide which obey Python division operator semantics.

This function forces Python 3 division operator semantics where all integer arguments are cast to floating types first. This op is generated by normal x / y division in Python 3 and in Python 2.7 with from __future__ import division. If you want integer division that rounds down, use x // y or tf.math.floordiv.

x and y must have the same numeric type. If the inputs are floating point, the output will have the same type. If the inputs are integral, the inputs are cast to float32 for int8 and int16 and float64 for int32 and int64 (matching the behavior of Numpy).

param x:Tensor numerator of numeric type.
param y:Tensor denominator of numeric type.
param name:A name for the operation (optional).
returns:x / y evaluated in floating point.
raises:TypeError – If x and y have different dtypes.

注意

tensorflow.compat.v1.floordiv函数介绍

Divides x / y elementwise, rounding toward the most negative integer.

The same as tf.compat.v1.div(x,y) for integers, but uses tf.floor(tf.compat.v1.div(x,y)) for floating point arguments so that the result is always an integer (though possibly an integer represented as floating point). This op is generated by x // y floor division in Python 3 and in Python 2.7 with from __future__ import division.

x and y must have the same type, and the result will have the same type as well.

param x:Tensor numerator of real numeric type.
param y:Tensor denominator of real numeric type.
param name:A name for the operation (optional).
returns:x / y rounded down.
raises:TypeError – If the inputs are complex.

注意

tensorflow.compat.v1.mod函数介绍

Returns element-wise remainder of division. When x < 0 xor y < 0 is

true, this follows Python semantics in that the result here is consistent with a flooring divide. E.g. floor(x / y) * y + mod(x, y) = x.

NOTE: math.floormod supports broadcasting. More about broadcasting [here](http://docs.scipy.org/doc/numpy/user/basics.broadcasting.html)

param x:A Tensor. Must be one of the following types: int32, int64, bfloat16, half, float32, float64.
param y:A Tensor. Must have the same type as x.
param name:A name for the operation (optional).
returns:A Tensor. Has the same type as x.

注意

tensorflow.compat.v1.cross函数介绍

Compute the pairwise cross product.

a and b must be the same shape; they can either be simple 3-element vectors, or any shape where the innermost dimension is 3. In the latter case, each pair of corresponding 3-element vectors is cross-multiplied independently.

param a:A Tensor. Must be one of the following types: float32, float64, int32, uint8, int16, int8, int64, bfloat16, uint16, half, uint32, uint64. A tensor containing 3-element vectors.
param b:A Tensor. Must have the same type as a. Another tensor, of same type and shape as a.
param name:A name for the operation (optional).
returns:A Tensor. Has the same type as a.

注意

tensorflow.compat.v1.pow函数介绍

Computes the power of one value to another.

Given a tensor x and a tensor y, this operation computes \(x^y\) for corresponding elements in x and y. For example:

`python x = tf.constant([[2, 2], [3, 3]]) y = tf.constant([[8, 16], [2, 3]]) tf.pow(x, y)  # [[256, 65536], [9, 27]] `

param x:A Tensor of type float16, float32, float64, int32, int64, complex64, or complex128.
param y:A Tensor of type float16, float32, float64, int32, int64, complex64, or complex128.
param name:A name for the operation (optional).
returns:A Tensor.

注意

tensorflow.compat.v1.rsqrt函数介绍

Computes reciprocal of square root of x element-wise.

For example:

>>> x = tf.constant([2., 0., -2.])
>>> tf.math.rsqrt(x)
<tf.Tensor: shape=(3,), dtype=float32,
numpy=array([0.707, inf, nan], dtype=float32)>
param x:A tf.Tensor. Must be one of the following types: bfloat16, half, float32, float64. int32
param name:A name for the operation (optional).
returns:A tf.Tensor. Has the same type as x.

注意

tensorflow.compat.v1.digamma函数介绍

Computes Psi, the derivative of Lgamma (the log of the absolute value of

Gamma(x)), element-wise.

param x:A Tensor. Must be one of the following types: bfloat16, half, float32, float64.
param name:A name for the operation (optional).
returns:A Tensor. Has the same type as x.

注意

tensorflow.compat.v1.erf函数介绍

Computes the Gauss error function of x element-wise.

param x:

A Tensor. Must be one of the following types: bfloat16, half, float32, float64.

param name:

A name for the operation (optional).

returns:

A Tensor. Has the same type as x.

If x is a SparseTensor, returns SparseTensor(x.indices, tf.math.erf(x.values, …), x.dense_shape)

注意

tensorflow.compat.v1.erfc函数介绍

Computes the complementary error function of x element-wise.

param x:A Tensor. Must be one of the following types: bfloat16, half, float32, float64.
param name:A name for the operation (optional).
returns:A Tensor. Has the same type as x.

注意

tensorflow.compat.v1.igamma函数介绍

Compute the lower regularized incomplete Gamma function P(a, x).

The lower regularized incomplete Gamma function is defined as:

\(P(a, x) = gamma(a, x) / Gamma(a) = 1 - Q(a, x)\)

where

\(gamma(a, x) = \int_{0}^{x} t^{a-1} exp(-t) dt\)

is the lower incomplete Gamma function.

Note, above Q(a, x) (Igammac) is the upper regularized complete Gamma function.

param a:A Tensor. Must be one of the following types: float32, float64.
param x:A Tensor. Must have the same type as a.
param name:A name for the operation (optional).
returns:A Tensor. Has the same type as a.

注意

tensorflow.compat.v1.igammac函数介绍

Compute the upper regularized incomplete Gamma function Q(a, x).

The upper regularized incomplete Gamma function is defined as:

\(Q(a, x) = Gamma(a, x) / Gamma(a) = 1 - P(a, x)\)

where

\(Gamma(a, x) = int_{x}^{infty} t^{a-1} exp(-t) dt\)

is the upper incomplete Gama function.

Note, above P(a, x) (Igamma) is the lower regularized complete Gamma function.

param a:A Tensor. Must be one of the following types: float32, float64.
param x:A Tensor. Must have the same type as a.
param name:A name for the operation (optional).
returns:A Tensor. Has the same type as a.

注意

tensorflow.compat.v1.lbeta函数介绍

Computes \(ln(|Beta(x)|)\), reducing along the last dimension.

Given one-dimensional $z = [z_1,…,z_K]$, we define

$$Beta(z) = frac{prod_j Gamma(z_j)}{Gamma(sum_j z_j)},$$

where $Gamma$ is the gamma function.

And for $n + 1$ dimensional $x$ with shape $[N_1, …, N_n, K]$, we define

$$lbeta(x)[i_1, …, i_n] = log{|Beta(x[i_1, ..., i_n, :])|}.$$

In other words, the last dimension is treated as the $z$ vector.

Note that if $z = [u, v]$, then

$$Beta(z) = frac{Gamma(u)Gamma(v)}{Gamma(u + v)}
= int_0^1 t^{u-1} (1 - t)^{v-1} mathrm{d}t,$$

which defines the traditional bivariate beta function.

If the last dimension is empty, we follow the convention that the sum over the empty set is zero, and the product is one.

param x:A rank n + 1 Tensor, n >= 0 with type float, or double.
param name:A name for the operation (optional).
returns:The logarithm of \(|Beta(x)|\) reducing along the last dimension.

注意

tensorflow.compat.v1.lgamma函数介绍

Computes the log of the absolute value of Gamma(x) element-wise.

For positive numbers, this function computes log((input - 1)!) for every element in the tensor. lgamma(5) = log((5-1)!) = log(4!) = log(24) = 3.1780539

Example:

`python x = tf.constant([0, 0.5, 1, 4.5, -4, -5.6]) tf.math.lgamma(x) ==> [inf, 0.5723649, 0., 2.4537368, inf, -4.6477685] `

param x:A Tensor. Must be one of the following types: bfloat16, half, float32, float64.
param name:A name for the operation (optional).
returns:A Tensor. Has the same type as x.

注意

tensorflow.compat.v1.squared_difference函数介绍

Returns (x - y)(x - y) element-wise.

NOTE: math.squared_difference supports broadcasting. More about broadcasting [here](http://docs.scipy.org/doc/numpy/user/basics.broadcasting.html)

param x:A Tensor. Must be one of the following types: bfloat16, half, float32, float64, int32, int64, complex64, complex128.
param y:A Tensor. Must have the same type as x.
param name:A name for the operation (optional).
returns:A Tensor. Has the same type as x.