目录
- import tensorflow as tf
-
- def tensorflow_demo():
- # tensorflow基本结构
- # 原生python加法计算
- a = 3
- b = 4
- c = a +b
- print("c:\n",c)
-
- # tensorflow实现加法计算
- a_t = tf.constant(2)
- b_t = tf.constant(3)
- c_t = a_t + b_t
- print("tensorflow:\n",c_t)
-
- # 开启会话
- with tf.Session() as sess:
- c_t_value = sess.run(c_t)
- print("c_t_value:\n",c_t_value)
- return None
-
- if __name__ == "__main__":
- # 代码1 :tensorflow基本结构
- tensorflow_demo()
- import tensorflow as tf
-
- def graph_demo():
- # 图的演示
- # Tensorflow实现加法运算
- a_t = tf.constant(2)
- b_t = tf.constant(3)
- c_t = a_t + b_t
- print("tensorflow:\n", c_t)
- # 查看默认图
- # 方法1:调用方法
- default_g = tf.get_default_graph()
- print("default:\n",default_g)
- # 方法2:查看属性
- print("a_t的图属性:\n",a_t.graph)
- print("c_t的图属性:\n",c_t.graph)
- # 开启会话
- with tf.Session() as sess:
- c_t_value = sess.run(c_t)
- print("c_t_value:\n", c_t_value)
- print("sess的图属性:\n", sess.graph)
- return None
-
-
- if __name__ == "__main__":
- # 代码2:图的演示
- graph_demo()
- import tensorflow as tf
-
- def graph_demo():
- # 图的演示
- # Tensorflow实现加法运算
- a_t = tf.constant(2)
- b_t = tf.constant(3)
- c_t = a_t + b_t
- print("tensorflow:\n", c_t)
-
- # 查看默认图
- # 方法1:调用方法
- default_g = tf.get_default_graph()
- print("default:\n",default_g)
- # 方法2:查看属性
- print("a_t的图属性:\n",a_t.graph)
- print("c_t的图属性:\n",c_t.graph)
- # 开启会话
- with tf.Session() as sess:
- c_t_value = sess.run(c_t)
- print("c_t_value:\n", c_t_value)
- print("sess的图属性:\n", sess.graph)
-
- # 自定义图
- new_g = tf.Graph()
- # 在自己的图中定义数据和操作
- with new_g.as_default():
- a_new = tf.constant(20)
- b_new = tf.constant(30)
- c_new = a_new + b_new
- print("c_new:\n",c_new)
- return None
-
-
- if __name__ == "__main__":
- # 代码2:图的演示
- graph_demo()
- import tensorflow as tf
-
- def graph_demo():
- # 图的演示
- # Tensorflow实现加法运算
- a_t = tf.constant(2)
- b_t = tf.constant(3)
- c_t = a_t + b_t
- print("tensorflow:\n", c_t)
-
- # 查看默认图
- # 方法1:调用方法
- default_g = tf.get_default_graph()
- print("default:\n",default_g)
- # 方法2:查看属性
- print("a_t的图属性:\n",a_t.graph)
- print("c_t的图属性:\n",c_t.graph)
-
- # 自定义图
- new_g = tf.Graph()
- # 在自己的图中定义数据和操作
- with new_g.as_default():
- a_new = tf.constant(20)
- b_new = tf.constant(30)
- c_new = a_new + b_new
- print("c_new:\n",c_new)
- print("a_new的图属性:\n", a_new.graph)
- print("c_new的图属性:\n", c_new.graph)
-
- # 开启会话
- with tf.Session() as sess:
- c_t_value = sess.run(c_t)
- print("c_t_value:\n", c_t_value)
- print("sess的图属性:\n", sess.graph)
-
- # 开启new_g的会话
- with tf.Session(graph = new_g) as new_sess:
- c_new_value = new_sess.run((c_new))
- print("c_new_value:\n",c_new_value)
- print("new_sess的图属性:\n",new_sess.graph)
-
- return None
-
-
- if __name__ == "__main__":
- # 代码2:图的演示
- graph_demo()