标签:演示 get iter 数据 tensor 本地 ons code ret
1 import tensorflow as tf 2 import os 3 os.environ[‘TF_CPP_MIN_LOG_LEVEL‘]=‘2‘ 4 def tensorflow_demo(): 5 6 #原生python加法运算 7 a = 2; 8 b=3; 9 c=a+b; 10 print("普通加法运算的结果:\n",c); 11 #tensorflow实现加法运算 12 a_t=tf.constant(2) 13 b_t=tf.constant(3) 14 c_t=a_t+b_t 15 print("tensorflow的加法运算结果:\n",c_t) 16 #开启会话 17 with tf.compat.v1.Session() as sess: 18 c_t_value = sess.run(c_t) 19 print("c_t_value:\n", c_t_value) 20 return None; 21 22 def graph_demo(): 23 """ 24 图的演示 25 :return: 26 """ 27 #TensorFlow实现加法运算 28 a_t = tf.constant(2) 29 b_t = tf.constant(3) 30 c_t = a_t + b_t 31 print("a_t:\n",a_t) 32 print("b_t:\n", b_t) 33 print("TensorFlow加法运算的结果:\n",c_t) 34 #查看默认图 35 #方法1:调用方法 36 default_g = tf.compat.v1.get_default_graph() 37 print("defaut_g:\n", default_g) 38 #方法2:查看属性 39 print("a_t的图属性:\n",a_t.graph) 40 print("c_t的图属性:\n", a_t.graph) 41 42 # 自定义图 43 new_g = tf.Graph() 44 # 在自己的图中定义数据和操作 45 with new_g.as_default(): 46 a_new = tf.constant(20) 47 b_new = tf.constant(30) 48 c_new = a_new + b_new 49 print("c_new:\n", c_new) 50 print("a_new的图属性:\n", a_new.graph) 51 print("c_new的图属性:\n", c_new.graph) 52 53 # 开启会话 54 with tf.compat.v1.Session() as sess: 55 c_t_value = sess.run(c_t) 56 print("c_t_value:\n", c_t_value) 57 print("sess的图属性:\n", sess.graph) 58 # 将图写入本地生成events文件 59 tf.compat.v1.summary.FileWriter("./tmp/summary",graph=sess.graph) 60 61 62 with tf.compat.v1.Session(graph=new_g) as new_sess: 63 c_new_value = new_sess.run(c_new) 64 print("c_new_value:\n", c_new_value) 65 print("new_sess的图属性:\n",new_sess.graph) 66 67 68 69 return None 70 if __name__ == "__main__": 71 #代码1:TensorFlow的基本结构 72 #tensorflow_demo() 73 #代码2:图的演示 74 graph_demo()
代码不同函数里,有图的演示和Tensorflow基本结构
标签:演示 get iter 数据 tensor 本地 ons code ret
原文地址:https://www.cnblogs.com/quxiangjia/p/12275460.html