Tensorflow API Math

本文详细介绍了TensorFlow中的各类数学运算符及函数,包括基本算术运算、矩阵运算、张量运算等,适用于初学者快速掌握TensorFlow的数学操作。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Math

Arithmetic Operators
tf.add(x, y, name=None)   # 求和
tf.subtract(x, y, name=None) # 减法
tf.multiply(x, y, name=None)  # 乘法
tf.div(x, y, name=None)  # 除法
tf.mod(x, y, name=None)  # 取模
Basic Math Functions
tf.abs(x, name=None)  # 取绝对值 y=|x|
tf.negative(x, name=None)  # 取反 y = -x
tf.sign(x, name=None)  # 取符号 sign(x)=-1 if x<0;0 if x==0; 1 if x>0
tf.square(x, name=None)  # 取平方 y = x^2
tf.sqrt(x, name=None)  # 取开方 y = x^(1/2)
tf.pow(x, y, name=None) # 幂次方 x^y
tf.exp(x, name=None) #  e的x次方, e^x
tf.log(x, y, name=None)  # log, 一个输入计算lnx, 两个输入计算 log_y(x) 
tf.ceil(x, name=None)  # 取比x大的最小正数
tf.floor(x, name=None)  # 取比x小的最大整数
tf.round(x, name=None) # 取最接近的整数
tf.maximum(x, y, name=None) # 取最大值(x>y ? x:y)
tf.minimum(x, y, name=None) # 取最小值 (x<y ? x:y)
tf.sin(x, name=None) # 三角函数 sin(x)
tf.cos(x, name=None)  # 三角函数 cos(x)
tf.tan(x, name=None)  # 三角函数 tan(x)
tf.atan(x, name=None)  # 三角函数 arctan(x)
Matrix Math Functions
tf.diag(diagonal, name=None)  # 返回以给定值为对角值的对角矩阵 
tf.diag_part(input,name=None)  # 与diag相反
tf.transpose(a, perm=None, name='transpose') # 矩阵的维度调整,perm指定维度(二维转置) 
tf.tf.matmul(a,b,name=None)  # 矩阵乘
tf.matrix_determinant(input,name=None)  # 行列式(方阵), 类型为: float32, float64
tf.matrix_inverse(input,adjoint=None,name=None)  # 矩阵的逆
tf.cholesky(input,name=None)  # 矩阵(方阵)的cholesky分解
qr(input,full_matrices=None,name=None) # 矩阵的 QR 分解
svd(tensor,full_matrices=False,compute_uv=True,name=None)  # 矩阵的 SVD 分解
Tensor Math Function
tf.tensordot(a, b, axes, name=None)  # 张量相乘
Complex Number Functions
tf.complex(real, imag, name=None) # 将两个实数转换为复数形式
tf.conj(x, name=None)  # 计算共轭复数
tf.imag(input,name=None) # 返回虚部
tf.real(input,name=None) # 返回实部
Reduction
# input_tensor 输入的tensor   reduction_indices 指定轴  keep_dims 保持维度
tf.reduce_sum(input_tensor, reduction_indices=None, keep_dims=False, name=None)  # 取和
tf.reduce_prod(input_tensor, reduction_indices=None, keep_dims=False, name=None) # 取乘积
tf.reduce_min(input_tensor, reduction_indices=None, keep_dims=False, name=None) # 取最小值
tf.reduce_max(input_tensor, reduction_indices=None, keep_dims=False, name=None) # 取最大值
tf.reduce_mean(input_tensor, reduction_indices=None, keep_dims=False, name=None) # 取平均值
tf.reduce_all(input_tensor, reduction_indices=None, keep_dims=False, name=None) # 逻辑'与'
tf.reduce_any(input_tensor, reduction_indices=None, keep_dims=False, name=None) # 逻辑'或'
tf.accumulate_n(inputs, shape=None, tensor_dtype=None, name=None) # 计算一系列tensor的和
# tensor ‘a’ is [[1, 2], [3, 4]]; tensor b is [[5, 0], [0, 6]]
tf.accumulate_n([a, b, a]) #  [[7, 4], [6, 14]]
Scan
tf.cumsum(x, axis=0, exclusive=False, reverse=False, name=None)   # 累积求和
tf.cumsum([a, b, c]) ==> [a, a + b, a + b + c]
tf.cumsum([a, b, c], exclusive=True) ==> [0, a, a + b]
tf.cumsum([a, b, c], reverse=True) ==> [a + b + c, b + c, c]
tf.cumsum([a, b, c], exclusive=True, reverse=True) ==> [b + c, c, 0]
tf.cumprod(x, axis=0, exclusive=False, reverse=False, name=None)   # 累积求积
tf.cumprod([a, b, c]) ==> [a, a * b, a * b * c]
tf.cumprod([a, b, c], exclusive=True) ==> [0, a, a * b]
tf.cumprod([a, b, c], reverse=True) ==> [a * b * c, b * c, c]
tf.cumprod([a, b, c], exclusive=True, reverse=True) ==> [b * c, c, 0]
Segmentation
tf.segment_sum(data, segment_ids, name=None) # 沿着segment_ids指定维度分割张量,并累加求和
# c = tf.constant([[1,2,3,4], [-1,-2,-3,-4], [5,6,7,8]])
# tf.segment_sum(c, tf.constant([0, 0, 1]))  ==> [[0 0 0 0] [5 6 7 8]]
# [1, 2, 3, 4] + [-1, -2, -3, -4] = [0, 0, 0, 0]    , [5, 6, 7, 8]=[5, 6, 7, 8]
tf.segment_prod(data, segment_ids, name=None) # 沿着segment_ids指定维度分割张量,并累加求积
tf.segment_min(data, segment_ids, name=None)  # 沿着segment_ids指定维度分割张量,并取最小值
tf.segment_max(data, segment_ids, name=None)  # 沿着segment_ids指定维度分割张量,并取最大值
tf.segment_mean(data, segment_ids, name=None)   # 沿着segment_ids指定维度分割张量,并取平均值
tf.unsorted_segment_sum(data, segment_ids,num_segments, name=None)  # 与tf.segment_sum函数类似,但segment_ids可以无序
tf.sparse_segment_sum(data, indices, segment_ids, name=None)    # 先选取,再分割
Sequence Comparison and Indexing
tf.argmin(input, dimension, name=None)  # 返回input最小值的索引index
tf.argmax(input, dimension, name=None)  # 返回input最大值的索引index
tf.setdiff1d(x, y, index_dtype=tf.int32, name=None) # 返回x,y中不同值及其索引
# x = [1,2,3,4,5], y = [1,3,4,6]
# t = tf.setdiff1d(x,y)    z ==> [2, 5]  ind ==> [1,4]
tf.where(input, name=None)    # 返回bool型tensor中为True的位置
# ‘input’ tensor is [[True, False], [True, False]]
# where(input) ==>[[0, 0], [1, 0]]
tf.unique(x, name=None) # 返回一个元组tuple(y,idx),y为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]
tf.invert_permutation(x, name=None)  # 置换x数据与索引
# tensor x is [3, 4, 0, 2, 1]    invert_permutation(x) ==> [2, 4, 3, 0, 1]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值