JobPlus知识库 IT 工业智能4.0 文章
tensorflow-基础函数-数学函数使用总结

对于新手使用tensorflow进行深度学习编程的时候,往往是比较别扭的,因为它里面有很多的函数,在使用中需要记住,这个框架的优点是灵活性很高,相反它的缺点就是很多东西都需要自己去写,如果嫌麻烦可以考虑后端结合使用Keras来进行编程,那样很多模型会简化很多,Ok 这篇文章主要是把常用的基础函数做了个汇总,方便日后使用可以查询到:


import tensorflow as tf

# 算术操作符:+ - * / %

x =""

y = ""

tf.add(x, y, name=None)        # 加法(支持 broadcasting)

tf.subtract(x, y, name=None)   # 减法

tf.multiply(x, y, name=None)   # 乘法

tf.divide(x, y, name=None)     # 浮点除法, 返回浮点数(python3 除法)

tf.mod(x, y, name=None)        # 取余


# 幂指对数操作符:^ ^2 ^0.5 e^ ln

tf.pow(x, y, name=None)        # 幂次方

tf.square(x, name=None)       # 平方

tf.sqrt(x, name=None)          # 开根号,必须传入浮点数或复数

tf.exp(x, name=None)           # 计算 e 的次方

tf.log(x, name=None)           # 以 e 为底,必须传入浮点数或复数


# 取符号、负、倒数、绝对值、近似、两数中较大/小的

tf.negative(x, name=None)      # 取负(y = -x).

tf.sign(x, name=None)          # 返回 x 的符号

tf.reciprocal(x, name=None)    # 取倒数

tf.abs(x, name=None)           # 求绝对值

tf.round(x, name=None)         # 四舍五入

tf.ceil(x, name=None)          # 向上取整

tf.floor(x, name=None)         # 向下取整

tf.rint(x, name=None)          # 取最接近的整数

tf.maximum(x, y, name=None)    # 返回两tensor中的最大值 (x > y ? x : y)

tf.minimum(x, y, name=None)    # 返回两tensor中的最小值 (x < y ? x : y)


# 三角函数和反三角函数

tf.cos(x, name=None) 

tf.sin(x, name=None) 

tf.tan(x, name=None)

 tf.acos(x, name=None) 

tf.asin(x, name=None) 

tf.atan(x, name=None)


# 其它

tf.div(x, y, name=None)  # python 2.7 除法, x/y-->int or x/float(y)-->float

tf.truediv(x, y, name=None) # python 3 除法, x/y-->float

tf.floordiv(x, y, name=None)  # python 3 除法, x//y-->int

tf.realdiv(x, y, name=None) 

tf.truncatediv(x, y, name=None) 

tf.floor_div(x, y, name=None) 

tf.truncatemod(x, y, name=None) 

tf.floormod(x, y, name=None) 

tf.cross(x, y, name=None) 

tf.add_n(x, name=None)  # inputs: A list of Tensor objects, each with same shape and type

tf.squared_difference(x, y, name=None)


#矩阵数学函数

# 矩阵乘法(tensors of rank >= 2)

tf.matmul(x, y, transpose_a=False, transpose_b=False,    adjoint_a=False, adjoint_b=False, a_is_sparse=False, b_is_sparse=False, name=None)


# 转置,可以通过指定 perm=[1, 0] 来进行轴变换

tf.transpose(x, perm=None, name='transpose')


# 在张量 a 的最后两个维度上进行转置

tf.matrix_transpose(x, name='matrix_transpose')

# Matrix with two batch dimensions, x.shape is [1, 2, 3, 4]

# tf.matrix_transpose(x) is shape [1, 2, 4, 3]


# 求矩阵的迹

tf.trace(x, name=None)


# 计算方阵行列式的值

tf.matrix_determinant(input, name=None)


# 求解可逆方阵的逆,input 必须为浮点型或复数

tf.matrix_inverse(input, adjoint=None, name=None)


# 奇异值分解

tf.svd(x, full_matrices=False, compute_uv=True, name=None)


# QR 分解

tf.qr(input, full_matrices=None, name=None)


# 求张量的范数(默认2)

tf.norm(x, ord='euclidean', axis=None, keep_dims=False, name=None)


# 构建一个单位矩阵, 或者 batch 个矩阵,batch_shape 以 list 的形式传入

tf.eye(x, num_columns=None, batch_shape=None, dtype=tf.float32, name=None)

# Construct one identity matrix.

tf.eye(2)


# Construct a batch of 3 identity matricies, each 2 x 2.

# batch_identity[i, :, :] is a 2 x 2 identity matrix, i = 0, 1, 2.

batch_identity = tf.eye(2, batch_shape=[3])


# Construct one 2 x 3 "identity" matrix

tf.eye(2, num_columns=3)


#聚合操作

# 计算输入 tensor 所有元素的和,或者计算指定的轴所有元素的和

tf.reduce_sum(x, axis=None, keep_dims=False, name=None)

# 'x' is [[1, 1, 1]

#         [1, 1, 1]]

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


# 计算指定的轴所有元素的均值/最大值/最小值/积

# reduction_indices 和axis是一个意思

# axis = 0 代表是按列

# axis = 1 代表是按行

# axis 没有这个参数的话 就代表操作所有元素

tf.reduce_mean(x, axis=None, keep_dims=False, name=None) 

tf.reduce_max(x, axis=None, keep_dims=False, name=None) 

tf.reduce_min(x, axis=None, keep_dims=False, name=None) 

tf.reduce_prod(x, axis=None, keep_dims=False, name=None)


#序列比较与索引提取

# 比较两个 list 或者 string 的不同,并返回不同的值和索引

tf.setdiff1d(x, y, index_dtype=tf.int32, name=None)


# 返回 x 中的唯一值所组成的tensor 和原 tensor 中元素在现 tensor 中的索引

tf.unique(x, out_idx=None, name=None)


# x if condition else y, condition 为 bool 类型的,可用tf.equal()等来表示

# x 和 y 的形状和数据类型必须一致

condition = ""

tf.where(condition, x=None, y=None, name=None)


# 返回沿着坐标轴方向的最大/最小值的索引

tf.argmax(input, axis=None, name=None, output_type=tf.int64) 

tf.argmin(input, axis=None, name=None, output_type=tf.int64)


# x 的值当作 y 的索引,range(len(x)) 索引当作 y 的值

# y[x[i]] = i for i in [0, 1, ..., len(x) - 1]

tf.invert_permutation(x, name=None)

上面把常用数学函数给总结了一下,希望方便大家学习汇总!


如果觉得我的文章对您有用,请随意打赏。您的支持将鼓励我继续创作!

¥ 打赏支持
90人赞 举报
分享到
用户评价(0)

暂无评价,你也可以发布评价哦:)

扫码APP

扫描使用APP

扫码使用

扫描使用小程序