Tensorflow中基本算术运算函数如下:
tf.add(x,y,name=None) # 求和运算;
import tensorflow as tf;
A = 5
B = 2
with tf.Session() as sess:
print(sess.run(tf.add(A,B))) #输出结果为7;
tf.subtract(x,y,name=None) # 减法运算 ,tensorflow更新到1.0后,函数名发生了变化,之前函数名为tf.sub(x,y,name=None)
import tensorflow as tf;
A = 5
B = 2
with tf.Session() as sess:
print(sess.run(tf.subtract(A,B))) #输出结果为3
tf.multiply(x,y,name=None) #乘法运算 ,之前函数名为tf.mul(x,y,name=None)
import tensorflow as tf;
A=5
B=2
with tf.Session() as sess:
print(sess.run(tf.multiply(A,B))) #输出结果为10
tf.div(x,y,name=None) #除法运算
import tensorflow as tf;
A=5
B=2
with tf.Session() as sess:
print(sess.run(tf.div(A,B))) #输出结果为2
tf.mod(x,y,name=None) # 取模运算
import tensorflow as tf;
A=5
B=2
with tf.Session() as sess:
print(sess.run(tf.mod(A,B))) #输出结果为1
tf.abs(x,name=None) #求绝对值
import tensorflow as tf;
A=-5
B=2
with tf.Session() as sess:
print(sess.run(tf.abs(A))) #输出结果为5
tf.
negative(x,name=None) #取负运算(y=-x)
import tensorflow as tf;
A=-5
B=2
with tf.Session() as sess:
print(sess.run(tf.negative(A))) #输出结果为5
print(sess.run(tf.negative(B))) #输出结果为-2
tf.sign(x,name=None) #返回符合 x大于0,则返回1,小于0,则返回-1;
import tensorflow as tf;
A=-5
B=2
with tf.Session() as sess:
print(sess.run(tf.sign(A))) #输出结果为-1
print(sess.run(tf.sign(B))) #输出结果为1
tf.reciprocal(x,name=None) #取反运算
import tensorflow as tf;
A=-5
B=1/2
with tf.Session() as sess:
print(sess.run(tf.reciprocal(B))) #输出结果为2
tf.square(x,name=None) #计算平方
import tensorflow as tf;
A=-5
B=1/2
with tf.Session() as sess:
print(sess.run(tf.square(B)))#输出结果为0.25
tf.round(x,name=None) #舍入最接近的整数
import tensorflow as tf;
A=-5.1
B=2.6
with tf.Session() as sess:
print(sess.run(tf.round(A))) #输出结果为-5
print(sess.run(tf.round(B))) #输出结果为3
tf.pow(x,y,name=None) #幂次方
import tensorflow as tf;
A=[2,3]
B=2
with tf.Session() as sess:
print(sess.run(tf.pow(A,B))) #输出结果为[4,9]