zoukankan      html  css  js  c++  java
  • TensorFlow计算图,张量,会话基础知识

     1 import tensorflow as tf
     2 get_default_graph = "tensorflow_get_default_graph.png"
     3 # 当前默认的计算图 tf.get_default_graph
     4 print(tf.get_default_graph())
     5 
     6 # 自定义计算图
     7 # tf.Graph
     8 
     9 # g1中定义名字为v的变量 初始化为0
    10 g1 = tf.Graph()
    11 with g1.as_default():
    12     v = tf.get_variable("v", shape=[1],
    13                         initializer=tf.zeros_initializer())
    14 
    15 # g2中定义名字为v的变量 初始化为1
    16 g2 = tf.Graph()
    17 with g2.as_default():
    18     v = tf.get_variable("v", shape=[1],
    19                         initializer=tf.ones_initializer())
    20 
    21 # initialize_all_variables  Use `tf.global_variables_initializer` instead.
    22 # 在计算图g1中读取变量v的取值  result is[ 0.]
    23 with tf.Session(graph=g1) as sess:
    24     # tf.initialize_all_variables().run()
    25     tf.global_variables_initializer().run()
    26     with tf.variable_scope("", reuse=True):
    27         print(sess.run(tf.get_variable("v")))
    28 
    29 # 在计算图g2中读取变量v的取值  result is [1.]
    30 with tf.Session(graph=g2) as sess:
    31     # tf.initialize_all_variables().run()
    32     tf.global_variables_initializer().run()
    33     with tf.variable_scope("", reuse=True):
    34         print(sess.run(tf.get_variable("v")))
    35 
    36 '''
    37 #计算图可以隔离张量和计算也可以指定计算设备
    38 g=tf.Graph()
    39 #指定GPU
    40 with g.device("/gpu:0"):
    41     result=a+b
    42 
    43 '''
     1 import tensorflow as tf
     2 
     3 #tensor 张量 零阶张量是标量scalar 一阶张量是向量vector n阶张量理解为n维数组
     4 #张量在TensorFlow中不是直接采用数组的形式,只是运算结果的引用。并没有保存数组,保存的是如何得到这些数字的计算过程
     5 
     6 #tf.constan是一个计算,结果为一个张量,保存在变量a中
     7 a=tf.constant([1.0,2.0],name="a")
     8 b=tf.constant([2.0,3.0],name="b")
     9 
    10 result=a+b
    11 print(result)
    12 #Tensor("add:0", shape=(2,), dtype=float32)
    13 
    14 result=tf.add(a,b,name="add")
    15 print(result)
    16 #Tensor("add_1:0", shape=(2,), dtype=float32)
    17 #张量保存三个属性  名字name(唯一标识)  维度shape  类型 dtype
    18 #张量的命名是node:src_output形式给出,node是节点名称,src_output是表示张量来自节点第几个输出
    19 #add_1:0 说明是add节点的第一个输出(编号从0开始)
    20 #shape=(2,) 以为数组,长度为2
    21 
    22 #dtype=float32 每个张量类型唯一,不匹配将报错
    23 '''
    24 a=tf.constant([1,2],name="a")
    25 b=tf.constant([2.0,3.0],name="b")
    26 result=a+b
    27 print(result)
    28 #ValueError: Tensor conversion requested dtype int32 for Tensor with dtype float32: 'Tensor("b_1:0", shape=(2,), dtype=float32)'
    29 '''
    30 
    31 #result.get_shape 获取张量的维度
    32 print(result.get_shape)
    33 # result
    34 # <bound method Tensor.get_shape of <tf.Tensor 'add_1:0' shape=(2,) dtype=float32>>
    35 
    36 #当计算图构造完成后,张量可以获得计算结果 (张量本身没有存储具体的数字)
    37 
    38 
    39 #使用session来执行定义好的运算 (也就是张量存储了运算的过程,使用session执行运算获取结果)
    40 #创建会话
    41 sess=tf.Session()
    42 res=sess.run(result)
    43 print(res)
    44 #result is [ 3.  5.]
    45 #关闭会话是本地运行使用到的资源释放
    46 sess.close()
    47 
    48 #也可以使用python上下文管理器机制,吧所有的计算放在with中,上下文管理器推出是自动释放所有资源,可以避免忘记sess.close()去释放资源
    49 
    50 with tf.Session() as sess:
    51     print(sess.run(result))
    52 #[ 3.  5.]
    53 
    54 
    55 #as_default 通过默认的会话计算张量的取值 会话不会自动生成默认的会话,需要手动指定 指定后可以通过eval来计算张量的取值
    56 sess =tf.Session()
    57 with sess.as_default():
    58     print(result.eval())
    59 #[ 3.  5.]
    60 
    61 #ConfigProto来配置需要生成的会话
    62 #allow_soft_placement GPU设备相关
    63 #log_device_palcement 日志相关
    64 config=tf.ConfigProto(allow_soft_placement=True,
    65                       log_device_placement=True)
    66 sess1=tf.InteractiveSession(config=config)
    67 sess2=tf.Session(config=config)
    68 #Device mapping: no known devices.  tensorflowcorecommon_runtimedirect_session.cc
    69 #Device mapping: no known devices.
    70 
    71 
    72 #PY35	ensorflowcoreplatformcpu_feature_guard.cc:137] Your CPU supports instructions that this TensorFlow binary was not compiled to use: AVX AVX2
  • 相关阅读:
    Oracle ORA-01033: ORACLE initialization or shutdown in progress 错误解决办法
    Oracle用imp导入dmp 提示遇到 ORACLE 错误 12560 TNS: 协议适配器错误 解决方法
    Oracle恢复误删除表操作语句
    DevExpress GridControl使用方法总结
    PL/SQL Developer 中的问题:Initialization error Could not load ".../oci.dll"解决方法
    Oracle中查询当前数据库中的所有表空间和对应的数据文件语句命令
    [PHP]利用XAMPP搭建本地服务器, 然后利用iOS客户端上传数据到本地服务器中(三. PHP端代码实现)
    [iOS]ios archives 出现的是other items而不是iOS Apps的解决方案
    [PHP]利用XAMPP搭建本地服务器, 然后利用iOS客户端上传数据到本地服务器中(二.配置MySQL数据库)
    [软件]XAMPP错误解决
  • 原文地址:https://www.cnblogs.com/vincentqliu/p/7802086.html
Copyright © 2011-2022 走看看