TensorFlow基本运算
加法:
tf.add(x,y,name=None)
参数说明:
x:一个张量,必须是下列类型之一:bfloat16/half/float32/uint8/int8/int16/int32/int64/complex64/complex128/string
y: 一个张量,类型必须同x
name:操作的名字,可选参数
返回值: x+y
一个张量,类型同x
import tensorflow.compat.v1 as tf 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) sess.close()
减法:
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
import tensorflow.compat.v1 as tf a = tf.constant(2) b = tf.constant(5) subOp = tf.subtract(a,b) sess = tf.Session() init = tf.global_variables_initializer() sess.run(init) print(sess.run(subOp)) print(subOp) sess.close()
乘法:
tf.multiply(x,y,name=None)
参数说明:
x:一个张量,必须是下列类型之一:bfloat16/half/float32/float64/uint8/uint16/int16/int32/int64/complex64/complex128
y:一个张量,类型同x
返回值: x*y
一个张量,类型同x
import tensorflow.compat.v1 as tf a = tf.constant(2) b = tf.constant(5) mulOp = tf.multiply(a,b) sess = tf.Session() init = tf.global_variables_initializer() sess.run(init) print(sess.run(mulOp)) print(mulOp) sess.close()
tf.scalar_mul(scalar,x)
参数说明:
scalar:标量
x:一个张量
返回值: scalar*x
import tensorflow.compat.v1 as tf b = tf.constant(5) scalar_mul_Op = tf.scalar_mul(2,b) sess = tf.Session() init = tf.global_variables_initializer() sess.run(init) print(sess.run(scalar_mul_Op)) print(scalar_mul_Op) sess.close()
除法:
tf.div(x,y,name=None)
参数说明:
x:实数数值类型,分子
y:实数类型,父母
name:操作名字
返回值:
x/y
import tensorflow.compat.v1 as tf a = tf.constant(2) b = tf.constant(5) div_op = tf.div(a,b) sess = tf.Session() init = tf.global_variables_initializer() sess.run(init) print(sess.run(div_op)) print(div_op) sess.close()