算术操作:
操作组 | 操作 |
---|---|
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) | 取模 |
tf.abs(x, name=None) | 求绝对值 |
tf.neg(x, name=None) | 取负 (y = -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) | 取反 |
tf.square(x, name=None) | 计算平方 (y = x * x = x^2) |
tf.round(x, name=None) | 舍入最接近的整数 #‘a’ is [0.9, 2.5, 2.3, -4.4] tf.round(a) ==> [ 1.0, 3.0, 2.0, -4.0 ] |
tf.sqrt(x, name=None) | 开根号 (y = \sqrt{x} = x^{1/2}) |
tf.pow(x, y, name=None) | 幂次方 # tensor ‘x’ is [[2, 2], [3, 3]] # tensor ‘y’ is [[8, 16], [2, 3]] tf.pow(x, y) ==> [[256, 65536], [9, 27]] |
tf.exp(x, name=None) | 计算e的次方 |
tf.log(x, name=None) | 计算log,一个输入计算e的ln,两输入以第二输入为底 |
tf.cos(x, name=None) | 三角函数cosine |
tf.sin(x, name=None) | 三角函数sine |
tf.tan(x, name=None) | 三角函数tan |
tf.atan(x, name=None) | 三角函数ctan |
tf.maximum(x, y, name=None) | 返回最大值 (x > y ? x : y) |
tf.minimum(x, y, name=None) | 返回最小值 (x < y ? x : y) |
张量
数据类型转换
操作组 | 操作 |
---|---|
tf.string_to_number(string_tensor, out_type=None, name=None) | 字符串转为数字 |
tf.to_double(x, name=’ToDouble’) | 转为64位浮点类型–float64 |
tf.to_float(x, name=’ToFloat’) | 转为32位浮点类型–float32 |
tf.to_int32(x, name=’ToInt32’) | 转为32位整型–int32 |
tf.to_int64(x, name=’ToInt64’) | 转为64位整型–int64 |
tf.cast(x, dtype, name=None) | 将x或者x.values转换为dtype # tensor a is [1.8, 2.2], dtype=tf.float tf.cast(a, tf.int32) ==> [1, 2] # dtype=tf.int32 |
Shape 形状操作
操作组 | 操作 |
---|---|
tf.shape(input, name=None) | 返回数据的shape # ‘t’ is [[[1, 1, 1], [2, 2, 2]], [[3, 3, 3], [4, 4, 4]]] shape(t) ==> [2, 2, 3] |
tf.size(input, name=None) | 返回数据的元素数量 # ‘t’ is [[[1, 1, 1], [2, 2, 2]], [[3, 3, 3], [4, 4, 4]]]] size(t) ==> 12 |
tf.rank(input, name=None) | 返回tensor的rank注意:此rank不同于矩阵的rank tensor的rank表示一个tensor需要的索引数目来唯一表示任何一个元素也就是通常所说的 “order”,“degree”或”ndims” #’t’ is [[[1, 1, 1], [2, 2, 2]], [[3, 3, 3], [4, 4, 4]]] # shape of tensor ‘t’ is [2, 2, 3] rank(t) ==> 3 |
tf.reshape(tensor, shape, name=None) | 改变tensor的形状 # tensor ‘t’ is [1, 2, 3, 4, 5, 6, 7, 8, 9] # tensor ‘t’ has shape [9] reshape(t, [3, 3]) ==> [[1, 2, 3],[4, 5, 6],[7, 8, 9]] #如果shape有元素[-1],表示在该维度打平至一维 # -1 将自动推导得为 9:reshape(t, [2, -1]) ==> [[1, 1, 1, 2, 2, 2, 3, 3, 3],[4, 4, 4, 5, 5, 5, 6, 6, 6]] |
tf.expand_dims(input, dim, name=None) | 插入维度1进入一个tensor中 #该操作要求-1-input.dims() # ‘t’ is a tensor of shape [2] shape(expand_dims(t, 0)) ==> [1, 2] shape(expand_dims(t, 1)) ==> [2, 1] shape(expand_dims(t, -1)) ==> [2, 1] <= dim <= input.dims() |
矩阵
操作组 | 操作 |
---|---|
tf.diag(diagonal, name=None) | 返回一个给定对角值的对角tensor # ‘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_par |