tensorflow的数学运算符、矩阵运算符

 

元学习论文总结||小样本学习论文总结

2017-2019年计算机视觉顶会文章收录 AAAI2017-2019 CVPR2017-2019 ECCV2018 ICCV2017-2019 ICLR2017-2019 NIPS2017-2019

 

 1.算术运算

# 算术操作符:+ - * / % 
tf.add(x, y, name=None)        # 加法(支持 broadcasting)
tf.add_n(inputs,name=None)     # tf.add_n() 将list中的数值相加 
    tf.add_n([x1,x2,x3],name = 'y')
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(inputs, name=None)  # inputs: A list of Tensor objects, each with same shape and type
tf.squared_difference(x, y, name=None) 

 

2.矩阵运算

# 矩阵乘法(tensors of rank >= 2)
tf.matmul(a, b, 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(a, perm=None, name='transpose')
 
 
# 在张量 a 的最后两个维度上进行转置
tf.matrix_transpose(a, 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(tensor, full_matrices=False, compute_uv=True, name=None)
 
 
# QR 分解
tf.qr(input, full_matrices=None, name=None)
 
 
# 求张量的范数(默认2)
tf.norm(tensor, ord='euclidean', axis=None, keep_dims=False, name=None)
 
 
 
# 构建一个单位矩阵, 或者 batch 个矩阵,batch_shape 以 list 的形式传入
tf.eye(num_rows, num_columns=None, batch_shape=None, dtype=tf.float32, name=None)
# Construct one identity matrix.
tf.eye(2)
==> [[1., 0.],
     [0., 1.]]
 
# 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)
==> [[ 1.,  0.,  0.],
     [ 0.,  1.,  0.]]
 
 
# 构建一个对角矩阵,rank = 2*rank(diagonal)
tf.diag(diagonal, name=None)
# '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.diag_part
tf.matrix_diag
tf.matrix_diag_part
tf.matrix_band_part
tf.matrix_set_diag
tf.cholesky
tf.cholesky_solve
tf.matrix_solve
tf.matrix_triangular_solve
tf.matrix_solve_ls
tf.self_adjoint_eig
tf.self_adjoint_eigvals

 参考

  • 3
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
在C++中,我们可以使用运算符重载(operator overloading)技术来定义矩阵相乘的运算符运算符重载是指对C++中已有的运算符进行重新定义,使其能够适用于自定义的数据类型。 我们可以通过定义一个矩阵类,并在其中重载乘法运算符(*)来实现矩阵相乘操作。在重载运算符时需要注意参数类型和返回值类型的定义。以下是一个简单的矩阵类的示例: ``` class Matrix { private: int row, col; int **data; public: Matrix(int r, int c) : row(r), col(c) { data = new int*[row]; for(int i = 0; i < row; i++) data[i] = new int[col]; } Matrix(const Matrix& m) : row(m.row), col(m.col) { data = new int*[row]; for(int i = 0; i < row; i++) data[i] = new int[col]; for(int i = 0; i < row; i++) for(int j = 0; j < col; j++) data[i][j] = m.data[i][j]; } ~Matrix() { for(int i = 0; i < row; i++) delete[] data[i]; delete[] data; } Matrix operator*(const Matrix& m) const { Matrix result(row, m.col); for(int i = 0; i < row; i++) for(int j = 0; j < m.col; j++) { result.data[i][j] = 0; for(int k = 0; k < col; k++) result.data[i][j] += data[i][k] * m.data[k][j]; } return result; } }; ``` 在上述代码中,我们定义了一个Matrix类,其中包含了矩阵的行数、列数以及数据。在类中,我们重载了乘法运算符(*),使其能够实现矩阵相乘的操作。在重载运算符时,我们需要使用const关键字来确保不会修改原对象,同时返回一个新的矩阵对象。 使用该类进行矩阵相乘操作时,只需使用乘法运算符即可,例如: ``` Matrix A(2, 3); Matrix B(3, 4); // 初始化矩阵A、B Matrix C = A * B; // 矩阵相乘操作 ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值