首页 > 其他 > 详细

03 使用Tensorflow做计算题

时间:2017-02-01 11:37:00      阅读:234      评论:0      收藏:0      [点我收藏+]

    我们使用Tensorflow,计算((a+b)*c)^2/a,然后求平方根。看代码:

 1 import tensorflow as tf
 2 
 3 # 输入储存容器
 4 a = tf.placeholder(tf.float16)
 5 b = tf.placeholder(tf.float16)
 6 c = tf.placeholder(tf.float16)
 7 
 8 # 计算
 9 d = tf.add(a, b) #加法
10 e = tf.multiply(d, c) #乘法
11 f = tf.pow(e, 2) #平方
12 g = tf.divide(f, a) #除法
13 h = tf.sqrt(g) #平方根
14 
15 # 会话
16 sess = tf.Session()
17 
18 # 赋值
19 feed_dict= {a:1, b:2, c:3}
20 
21 # 计算
22 result = sess.run(h, feed_dict= feed_dict)
23 
24 # 关闭会话
25 sess.close()
26 
27 # 输出结果
28 print(result)

    这里让a=1,b=2,c=3,如果输出9.0,证明运行成功。

    Tensorflow做计算的方法是,先把计算的式子构建一个图,然后把这个图和赋值传给C++写的程序一起计算,比较快。

 

03 使用Tensorflow做计算题

原文:http://www.cnblogs.com/tengge/p/6359844.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!