zoukankan      html  css  js  c++  java
  • 深度学习之tensorflow框架(上)

     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("普通加法运算的结果:
    ",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的加法运算结果:
    ",c_t)
    16     #开启会话
    17     with tf.compat.v1.Session() as sess:
    18         c_t_value = sess.run(c_t)
    19         print("c_t_value:
    ", 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:
    ",a_t)
    32     print("b_t:
    ", b_t)
    33     print("TensorFlow加法运算的结果:
    ",c_t)
    34     #查看默认图
    35     #方法1:调用方法
    36     default_g = tf.compat.v1.get_default_graph()
    37     print("defaut_g:
    ", default_g)
    38     #方法2:查看属性
    39     print("a_t的图属性:
    ",a_t.graph)
    40     print("c_t的图属性:
    ", 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:
    ", c_new)
    50         print("a_new的图属性:
    ", a_new.graph)
    51         print("c_new的图属性:
    ", 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:
    ", c_t_value)
    57         print("sess的图属性:
    ", 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:
    ", c_new_value)
    65         print("new_sess的图属性:
    ",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基本结构

  • 相关阅读:
    指针数组与数组指针
    209. 长度最小的子数组
    面试题 05.08. 绘制直线(位运算)
    1160. 拼写单词
    88. 合并两个有序数组
    80. 删除排序数组中的重复项 II(On)
    python自定义异常和主动抛出异常
    类的构造方法1(类中的特殊方法)
    python之判断一个值是不是可以被调用
    主动调用其他类的成员(普通调用和super方法调用)
  • 原文地址:https://www.cnblogs.com/quxiangjia/p/12275460.html
Copyright © 2011-2022 走看看