import tensorflow as tf
get_default_graph = "tensorflow_get_default_graph.png"
# 当前默认的计算图 tf.get_default_graph
print(tf.get_default_graph()) # 自定义计算图
# tf.Graph # g1中定义名字为v的变量 初始化为0
g1 = tf.Graph()
with g1.as_default():
v = tf.get_variable("v", shape=[1],
initializer=tf.zeros_initializer()) # g2中定义名字为v的变量 初始化为1
g2 = tf.Graph()
with g2.as_default():
v = tf.get_variable("v", shape=[1],
initializer=tf.ones_initializer()) # initialize_all_variables Use `tf.global_variables_initializer` instead.
# 在计算图g1中读取变量v的取值 result is[ 0.]
with tf.Session(graph=g1) as sess:
# tf.initialize_all_variables().run()
tf.global_variables_initializer().run()
with tf.variable_scope("", reuse=True):
print(sess.run(tf.get_variable("v"))) # 在计算图g2中读取变量v的取值 result is [1.]
with tf.Session(graph=g2) as sess:
# tf.initialize_all_variables().run()
tf.global_variables_initializer().run()
with tf.variable_scope("", reuse=True):
print(sess.run(tf.get_variable("v"))) '''
#计算图可以隔离张量和计算也可以指定计算设备
g=tf.Graph()
#指定GPU
with g.device("/gpu:0"):
result=a+b '''
 import tensorflow as tf

 #tensor 张量 零阶张量是标量scalar 一阶张量是向量vector n阶张量理解为n维数组
#张量在TensorFlow中不是直接采用数组的形式,只是运算结果的引用。并没有保存数组,保存的是如何得到这些数字的计算过程 #tf.constan是一个计算,结果为一个张量,保存在变量a中
a=tf.constant([1.0,2.0],name="a")
b=tf.constant([2.0,3.0],name="b") result=a+b
print(result)
#Tensor("add:0", shape=(2,), dtype=float32) result=tf.add(a,b,name="add")
print(result)
#Tensor("add_1:0", shape=(2,), dtype=float32)
#张量保存三个属性 名字name(唯一标识) 维度shape 类型 dtype
#张量的命名是node:src_output形式给出,node是节点名称,src_output是表示张量来自节点第几个输出
#add_1:0 说明是add节点的第一个输出(编号从0开始)
#shape=(2,) 以为数组,长度为2 #dtype=float32 每个张量类型唯一,不匹配将报错
'''
a=tf.constant([1,2],name="a")
b=tf.constant([2.0,3.0],name="b")
result=a+b
print(result)
#ValueError: Tensor conversion requested dtype int32 for Tensor with dtype float32: 'Tensor("b_1:0", shape=(2,), dtype=float32)'
''' #result.get_shape 获取张量的维度
print(result.get_shape)
# result
# <bound method Tensor.get_shape of <tf.Tensor 'add_1:0' shape=(2,) dtype=float32>> #当计算图构造完成后,张量可以获得计算结果 (张量本身没有存储具体的数字) #使用session来执行定义好的运算 (也就是张量存储了运算的过程,使用session执行运算获取结果)
#创建会话
sess=tf.Session()
res=sess.run(result)
print(res)
#result is [ 3. 5.]
#关闭会话是本地运行使用到的资源释放
sess.close() #也可以使用python上下文管理器机制,吧所有的计算放在with中,上下文管理器推出是自动释放所有资源,可以避免忘记sess.close()去释放资源 with tf.Session() as sess:
print(sess.run(result))
#[ 3. 5.] #as_default 通过默认的会话计算张量的取值 会话不会自动生成默认的会话,需要手动指定 指定后可以通过eval来计算张量的取值
sess =tf.Session()
with sess.as_default():
print(result.eval())
#[ 3. 5.] #ConfigProto来配置需要生成的会话
#allow_soft_placement GPU设备相关
#log_device_palcement 日志相关
config=tf.ConfigProto(allow_soft_placement=True,
log_device_placement=True)
sess1=tf.InteractiveSession(config=config)
sess2=tf.Session(config=config)
#Device mapping: no known devices. tensorflow\core\common_runtime\direct_session.cc
#Device mapping: no known devices. #PY\35\tensorflow\core\platform\cpu_feature_guard.cc:137] Your CPU supports instructions that this TensorFlow binary was not compiled to use: AVX AVX2

TensorFlow计算图,张量,会话基础知识的更多相关文章

  1. TensorFlow应用实战 | TensorFlow基础知识

    挺长的~超出估计值了~预计阅读时间20分钟. 从helloworld开始 mkdir 1.helloworld cd 1.helloworldvim helloworld.py 代码: # -*- c ...

  2. tensorflow笔记(一)之基础知识

    tensorflow笔记(一)之基础知识 版权声明:本文为博主原创文章,转载请指明转载地址 http://www.cnblogs.com/fydeblog/p/7399701.html 前言 这篇no ...

  3. Ternsorflow 学习:002-Tensorflow 基础知识

    前言: 使用 TensorFlow 之前你需要了解关于 TensorFlow 的以下基础知识: 使用图(graphs) 来表示计算 在会话(session) 中执行图 使用张量(tensors) 来代 ...

  4. TFLite基础知识

    此基础知识仅为个人学习记录,如有错误或遗漏之处,还请各位同行给个提示. 概述 TFLite主要含有如下内容: (1)TFLite提供一系列针对移动平台的核心算子,包括量化和浮点运算.另外,TFLite ...

  5. [源码解析] 深度学习分布式训练框架 Horovod (1) --- 基础知识

    [源码解析] 深度学习分布式训练框架 Horovod --- (1) 基础知识 目录 [源码解析] 深度学习分布式训练框架 Horovod --- (1) 基础知识 0x00 摘要 0x01 分布式并 ...

  6. [源码解析] PyTorch 流水线并行实现 (1)--基础知识

    [源码解析] PyTorch 流水线并行实现 (1)--基础知识 目录 [源码解析] PyTorch 流水线并行实现 (1)--基础知识 0x00 摘要 0x01 历史 1.1 GPipe 1.2 t ...

  7. [源码解析] TensorFlow 分布式 DistributedStrategy 之基础篇

    [源码解析] TensorFlow 分布式 DistributedStrategy 之基础篇 目录 [源码解析] TensorFlow 分布式 DistributedStrategy 之基础篇 1. ...

  8. Oracle数据库基础知识

    oracle数据库plsql developer   目录(?)[-] 一     SQL基础知识 创建删除数据库 创建删除修改表 添加修改删除列 oracle cascade用法 添加删除约束主键外 ...

  9. Spring基础知识

    Spring基础知识 利用spring完成松耦合 接口 public interface IOutputGenerator { public void generateOutput(); } 实现类 ...

随机推荐

  1. 指定html转pdf文档

    1.资源 <script type="text/javascript" src="./js/canvg2.js"></script> & ...

  2. Tensorflow一些常用基本概念与函数(二)

    1.tensorflow的基本运作 为了快速的熟悉TensorFlow编程,下面从一段简单的代码开始: import tensorflow as tf #定义‘符号’变量,也称为占位符 a = tf. ...

  3. C# Word转PDF/HTML/XML/XPS/SVG/EMF/EPUB/TIFF

    一款有着强大的文档转换功能的工具,无论何时何地都会是现代办公环境极为需要的.在本篇文章中,将介绍关于Word文档的转换功能(Word转XPS/SVG/EMF/EPUB/TIFF).希望方法中的代码能为 ...

  4. OAF调用JavaScript新开窗口

    在OAF框架中,ORACLE标准本身并不推荐使用JS,但是仍然提供了相应的方法. String oaUrl="https://www.baidu.com/"; pageContex ...

  5. 探究JS中对象的深拷贝和浅拷贝

    深拷贝和浅拷贝的区别 在讲深拷贝和浅拷贝的区别之前,回想一下我们平时拷贝一个对象时是怎么操作的?是不是像这样? var testObj1 = {a: 1, b:2}, testObj2=testObj ...

  6. PHP:第六章——正则表达式的基本概念

    <?php header("Content-Type:text/html;charset=utf-8"); //正则表达式的基本概念: //宽松匹配和严格匹配: //常见的匹 ...

  7. POJ 3087 Shuffle'm Up bfs

    题目链接:Shuffle'm Up 除了英文题有点恶心.发现模拟 + bfs 就可以过的时候,就是水了. 一个bug 就是filp函数得到string s12失败了.恩.据大腿告知,string 并不 ...

  8. npm设置仓库

    如果国外官方的npm仓库下载速度很慢的话,可以考虑更换npm仓库,加快下载包的速度. 1.通过config命令 npm config set registry https://registry.npm ...

  9. [置顶] Android 打包apk无敌报错

    前言: 这个问题从昨天上午一直到现在,请教了很多大佬,都没有给出确定的解决方案,可能他们也没碰到过可能,不过还是挺感谢他们的建议,一直到今天中午午休,我一直都在想这个问题,就是下面的这个,看了国内很多 ...

  10. ASP.NET CORE网站部署到 windows server 的IIS 上去

    章基于我自己经验的一个总结,在windows服务器上部署asp.net core网站.环境是 windows server 2012数据中心版本 第一步先安装 IIS 服务器 接下来就是一路下一步,然 ...