牛骨文教育服务平台(让学习变的简单)
博文笔记

Tenosrflow中基本算术运算函数

创建时间:2017-07-14 投稿人: 浏览次数:1420

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]














声明:该文观点仅代表作者本人,牛骨文系教育信息发布平台,牛骨文仅提供信息存储空间服务。