1.tf.add
tf.add或tf.math.add:加法操作
tf.add( x, y, name=None ) 参数说明: x:一个张量。必须是下列类型之一:bfloat16, half, float32, float64, uint8, int8, int16, int32, int64, complex64, complex128, string. y:一个张量。类型必须同x。 name:操作的名字,可选参数 返回值:x+y 一个张量,类型同x |
a=tf.constant(2) b=tf.constant(5) addOp=tf.add(a,b) sess=tf.Session() init=tf.global_variables_initializer() sess.run(init) print(sess.run(addOp)) print(addOp) |
输出:
7 Tensor("Add_4:0", shape=(), dtype=int32) |
2.tf.subtract
减法操作
tf.subtract( x, y, name=None ) 参数说明: X:一个张量。必须是下列类型之一:bfloat16, half, float32, float64, uint8, int8, uint16, int16, int32, int64, complex64, complex128 y:一个张量。类型同x. name:张量操作的名字,可选参数。 返回值:x-y 一个张量,类型同x |
Subtract支持广播操作
subtractOp=tf.subtract(a,b) print(sess.run(subtractOp)) print(subtractOp) |
输出:
-3 Tensor("Sub:0", shape=(), dtype=int32) |
3.tf.multiply
乘法操作
tf.multiply( x, y, name=None ) 参数说明: X:一个张量。必须是下列类型之一:bfloat16, half, float32, float64, uint8, int8, uint16, int16, int32, int64, complex64, complex128 y:一个张量。类型同x. name:张量操作的名字,可选参数。 返回值:x*y 一个张量,类型同x |
multiply=tf.multiply(a,b,name='mul') print(sess.run(multiply)) print(multiply) |
输出:
10 Tensor("mul:0", shape=(), dtype=int32) |
4.tf.scalar_mul
标量和张量相乘
tf.scalar_mul( scalar, x ) 参数说明: scalar:标量 x:一个张量 返回值:sclar*x |
scalar_mul_op=tf.scalar_mul(4,a) print(sess.run(scalar_mul_op)) print(scalar_mul_op) |
输出:
8 Tensor("mul_4:0", shape=(), dtype=int32) |
5.tf.div
除法操作,如果其中一个是浮点数,则结果是浮点类型,否则是整数类型
tf.div( x, y, name=None )
参数说明: x:实数数值类型,分子 y:实数类型,分母 name:操作名字。 返回值: x/y |
divOp=tf.div(1.0,2) print(sess.run(divOp)) print(divOp) |
输出:
0.5 Tensor("div:0", shape=(), dtype=float32) |