http://www.tensorfly.cn/tfdoc/api_docs/python/math_ops.html#AUTOGENERATED-arithmetic-operators
维度:
tf.constant(2) #()
tf.constant([2]) #(1, )
tf.constant([2, 1])#(2, )
tf.constant([[2, 1]]) # (1,2)
tf.constant([[2, 1], [3, 4]]) # (2, 2)
tf.constant([[[2, 1], [3, 4]]]) #(1, 2, 2)
标量维度为0,有几个[]就是多少维,shape确定方法:先去掉最外层括号,此时有N个元素,则第0维度为N;逐层向里去括号,确定第几维度的数目。
一、Tensor 之间的运算规则
1) 相同大小 Tensor 之间的任何算术运算都会将运算应用到元素级
2) 不同大小 Tensor(要求dimension 0 必须相同) 之间的运算叫做广播(broadcasting)
3) Tensor 与 Scalar(0维 tensor) 间的算术运算会将那个标量值传播到各个元素
4) Note:TensorFLow 在进行数学运算时,一定要求各个 Tensor 数据类型一致
#若以下命令不存在,则通过tf.math.x调用即可
Arithmetic Operators
#element-wise 逐元素运算
tf.add(x, y, name=None)
tf.sub(x, y, name=None)
tf.mul(x, y, name=None) #乘法
tf.div(x, y, name=None) #除法
tf.mod(x, y, name=None) #求余数
Basic Math Functions
#element-wise 逐元素运算
tf.add_n(inputs, name=None)
#Add all input tensors element wise. 对输入tensor所有元素进行求和
tf.abs(x, name=None)
#逐元素转为绝对值
tf.neg(x, name=None)
#逐元素取反 -x
tf.sign(x, name=None)
#y = sign(x) = -1 if x < 0; 0 if x == 0; 1 if x > 0.
tf.inv(x, name=None)
#逐元素 取倒数1/x
tf.square(x, name=None)
#逐元素 x**2
tf.round(x, name=None)
#逐元素 四舍五入
tf.sqrt(x, name=None)
#逐元素 求平方根
tf.rsqrt(x, name=None)
#逐元素 求1/平方根
tf.pow(x, y, name=None)
#逐元素 求x的y次方
tf.exp(x, name=None)
#逐元素 求e的x次
tf.log(x, name=None)
#逐元素 求loge
tf.ceil(x, name=None)
#逐元素 向上取整
tf.floor(x, name=None)
#逐元素 向下取整
tf.maximum(x, y, name=None)
#Returns the max of x and y (i.e. x > y ? x : y) 返回值维度与x一致
tf.minimum(x, y, name=None)
#Returns the min of x and y (i.e. x < y ? x : y) 返回值维度与x一致
tf.cos(x, name=None)
tf.sin(x, name=None)
Matrix Math Functions
tf.diag(diagonal, name=None)
#创建对角矩阵 diagonal为list,每个值代表对应维度对角上的值
# 'diagonal' is [1, 2, 3, 4]
tf.diag(diagonal) ==> [[1, 0, 0, 0]
[0, 2, 0, 0]
[0, 0, 3, 0]
[0, 0, 0, 4]]
tf.transpose(a, perm=None, name='transpose')
#转换维度 perm用于指定维度顺序
#转置
# 'x' is [[1 2 3]
# [4 5 6]]
tf.transpose(x) ==> [[1 4]
[2 5]
[3 6]]
#第1,2维度互换
tf.transpose(b, perm=[0, 2, 1]) ==> [[[1 4]
[2 5]
[3 6]]
[[7 10]
[8 11]
[9 12]]]
tf.matmul(a, b, transpose_a=False, transpose_b=False, a_is_sparse=False, b_is_sparse=False, name=None)
#矩阵乘法
#输入必须为2维矩阵
# 2-D tensor `a`
a = tf.constant([1, 2, 3, 4, 5, 6], shape=[2, 3]) => [[1. 2. 3.]
[4. 5. 6.]]
# 2-D tensor `b`
b = tf.constant([7, 8, 9, 10, 11, 12], shape=[3, 2]) => [[7. 8.]
[9. 10.]
[11. 12.]]
c = tf.matmul(a, b) => [[58 64]
[139 154]]
tf.batch_matmul(x, y, adj_x=None, adj_y=None, name=None)
#batch 矩阵乘法
tf.matrix_determinant(input, name=None)
#计算矩阵行列式
tf.batch_matrix_determinant(input, name=None)
#batch 计算矩阵行列式
tf.matrix_inverse(input, name=None)
#求矩阵的逆
tf.batch_matrix_inverse(input, name=None)
#batch 矩阵的逆
tf.cholesky(input, name=None)
# Calculates the Cholesky decomposition of a square matrix.
Complex Number Functions
复数操作
Reduction
TensorFlow provides several operations that you can use to perform common math computations that reduce various dimensions of a tensor.
tf.reduce_sum(input_tensor, reduction_indices=None, keep_dims=False, name=None)
#计算输入 tensor 所有元素的和,或者计算指定的轴所有元素的和
# 'x' is [[1, 1, 1]
# [1, 1, 1]] #(2,3)
tf.reduce_sum(x) ==> 6
tf.reduce_sum(x, 0) ==> [2, 2, 2]
tf.reduce_sum(x, 1) ==> [3, 3]
tf.reduce_sum(x, 1, keep_dims=True) ==> [[3], [3]] # 维度不缩减
tf.reduce_sum(x, [0, 1]) ==> 6
# 计算输入 tensor 所有元素的均值/最大值/最小值/积/逻辑与/或
# 或者计算指定的轴所有元素的均值/最大值/最小值/积/逻辑与/或(just like reduce_sum)
tf.reduce_mean(input_tensor, axis=None, keep_dims=False, name=None)
tf.reduce_max(input_tensor, axis=None, keep_dims=False, name=None)
tf.reduce_min(input_tensor, axis=None, keep_dims=False, name=None)
tf.reduce_prod(input_tensor, axis=None, keep_dims=False, name=None)
tf.reduce_all(input_tensor, axis=None, keep_dims=False, name=None) # 全部满足条件
tf.reduce_any(input_tensor, axis=None, keep_dims=False, name=None) #至少有一个满足条件
tf.accumulate_n(inputs, shape=None, tensor_dtype=None, name=None)
#针对inputs进行逐元素相加 inputs指定多少tensor相加
# tensor 'a' is [[1, 2], [3, 4]
# tensor `b` is [[5, 0], [0, 6]]
tf.math.accumulate_n([a, b, a]) ==> [[7, 4], [6, 14]]
# Explicitly pass shape and type
tf.math.accumulate_n([a, b, a], shape=[2, 2], tensor_dtype=tf.int32)
==> [[7, 4], [6, 14]]
Segmentation
沿着第一维(x 轴)根据 segment_ids(list)分割好相应的数据后再进行操作
data为待分割数据
segment_ids为分割下标。segment_ids的大小应与data的第一个维度长度(k)相同。且segment_ids的值应小于k。
segment_ids的值相同的进行同一操作
比如上图:
segment_ids = [0, 0, 0, 1, 2, 2, 3, 3]
data = [5, 1, 7, 2, 3, 4, 1, 3]
加入执行tf.math.segment_sum(data, segment_ids, name=None)
则segment_id=0的 [5, 1, 7]进行求和
segment_id=1的 [2]进行求和
segment_id=2的 [3, 4]进行求和
segment_id=3的 [1, 3]进行求和
最后返回[13, 2, 7, 4]
tf.math.segment_sum(data, segment_ids, name=None)
tf.math.segment_prod(data, segment_ids, name=None)
tf.math.segment_min(data, segment_ids, name=None)
tf.math.segment_max(data, segment_ids, name=None)
tf.math.segment_mean(data, segment_ids, name=None)
tf.math.unsorted_segment_sum(data, segment_ids, num_segments, name=None)
Sequence Comparison and Indexing
tf.argmin(input, dimension, name=None)
#根据指定dimension 求最小值对应索引
tf.argmax(input, dimension, name=None)
tf.listdiff(x, y, name=None)
#找出在x中有,y中没有的元素。返回该元素列表,和对应元素在x的索引
x = [1, 2, 3, 4, 5, 6]
y = [1, 3, 5]
out ==> [2, 4, 6]
idx ==> [1, 3, 5]
tf.where(input, name=None)
#返回 True 元素在input tensor中的位置
# 'input' tensor is [[True, False]
# [True, False]]
# 'input' has two true values, so output has two coordinates.
# 'input' has rank of 2, so coordinates have two indices.
tf.where(input) ==> [[0, 0],
[1, 0]]
# x if condition else y, condition 为 bool 类型的,可用tf.equal()等来表示
# x 和 y 的形状和数据类型必须一致
tf.where(condition, x=None, y=None, name=None)
tf.unique(x, name=None)
#找到x中独立元素和其索引
# tensor 'x' is [1, 1, 2, 4, 4, 4, 7, 8, 8]
y, idx = unique(x)
y ==> [1, 2, 4, 7, 8]
idx ==> [0, 0, 1, 2, 2, 2, 3, 4, 4]