• session:

    • with tf.Session() as sess:/ tf.InteractiveSession()

    • 初始化:

      • tf.global_variables_initializer()
      with tf.Session() as sess:sess.run(tf.global_variables_initializer())
      

0. tf.Graph

  • 命名空间与 operation name(oper.name 获取操作名):

    c_0 = tf.constant(0, name="c")  # => operation named "c"
    print(c_0.name)
    # Already-used names will be "uniquified".
    c_1 = tf.constant(2, name="c")  # => operation named "c_1"# Name scopes add a prefix to all operations created in the same context.
    with tf.name_scope("outer"):c_2 = tf.constant(2, name="c")  # => operation named "outer/c"# Name scopes nest like paths in a hierarchical file system.with tf.name_scope("inner"):c_3 = tf.constant(3, name="c")  # => operation named "outer/inner/c"# Exiting a name scope context will return to the previous prefix.c_4 = tf.constant(4, name="c")  # => operation named "outer/c_1"# Already-used name scopes will be "uniquified".with tf.name_scope("inner"):c_5 = tf.constant(5, name="c")  # => operation named "outer/inner_1/c"
    

1. 使用 tf.Session().run() 读取变量的值十分耗时

#CODING: UTF-8
import time
import tensorflow as tfN = 100000
x = tf.constant([1.])
b = 1.with tf.Session() as sess:sess.run(tf.initialize_all_variables()) t1 = time.time()for _ in range(N):y = sess.run(x)print('使用sess.run() 读取变量数据耗时', time.time()-t1)t2 = time.time()for _ in range(N):a = bprint('直接赋值耗时', time.time()-t2)

2. tf.Session().run() 与 Tensor.eval()

假设 x 为 tf 下的一个 Tensor 对象,t.eval() 执行的动作就是 tf.Session().run(t) 。

import tensorflow as tf
x = tf.constant([5.])print(tf.Session().run(x))with tf.Session():print(x.eval())

在第二个例子中,session的作用就象context manager,context manager在with块的生存期,将session作为默认的 session。对简单应用的情形(如单元测试),context manager的方法可以得到更简洁的代码;如果你的代码要处理多个graph和 session,更直白的方式可能是显式调用Session.run()。

  • session.run()是非常耗时的,千万不要用session.run的方式去取数据
  • Session.run()和Tensor.eval()区别是什么?

TensorFlow 学习(二)—— tf.Graph、tf.Session() 与 tf.Session().run()相关推荐

  1. Tensorflow 学习二 tf.Session().run

    以下为tf.Session().run 说明,其接受的fetches参数可以有多种类型. def run(self, fetches, feed_dict=None, options=None, ru ...

  2. TensorFlow学习笔记(一)---graph,op,tensor,Session,Variables,Tensorboard

    1.   机器学习                         深度学习 算法 分类:神经网络(简单)          神经网络(深度)     回归                       ...

  3. tensorflow学习(二)——finetune预训练网络--以mobileNetV1为例

    文章目录 一. 流程 二. mobileNetV1预训练 一. 流程 微调预训练的流程为 准备数据 定义网络结构,除了网络外,还可以根据需要定义梯度下降算法,准确度计算等. 找到网络的定义,设置需要导 ...

  4. tensorflow学习笔记(四十五):sess.run(tf.global_variables_initializer()) 做了什么?

    当我们训练自己的神经网络的时候,无一例外的就是都会加上一句 sess.run(tf.global_variables_initializer()) ,这行代码的官方解释是 初始化模型的参数.那么,它到 ...

  5. tensorflow学习笔记——使用TensorFlow操作MNIST数据(1)

    续集请点击我:tensorflow学习笔记--使用TensorFlow操作MNIST数据(2) 本节开始学习使用tensorflow教程,当然从最简单的MNIST开始.这怎么说呢,就好比编程入门有He ...

  6. tensorflow学习笔记——AlexNet

    2012年,Alex Krizhevsky.Ilya Sutskever在多伦多大学Geoff Hinton的实验室设计出了一个深层的卷积神经网络AlexNet,夺得了2012年ImageNet LS ...

  7. tensorflow学习笔记(二十六):构建TF代码

    如何构建TF代码 batch_size: batch的大小 mini_batch: 将训练样本以batch_size分组 epoch_size: 样本分为几个min_batch num_epoch : ...

  8. TensorFlow学习——tf.GPUOptions和tf.ConfigProto用法解析

    在服务器上用多GPU做训练时,由于想只用其中的一个GPU设备做训练,可使用深度学习代码运行时往往出现多个GPU显存被占满清理.出现该现象主要是tensorflow训练时默认占用所有GPU的显存. 查看 ...

  9. Tensorflow学习笔记2:About Session, Graph, Operation and Tensor

    简介 上一篇笔记:Tensorflow学习笔记1:Get Started 我们谈到Tensorflow是基于图(Graph)的计算系统.而图的节点则是由操作(Operation)来构成的,而图的各个节 ...

最新文章

  1. wamp安装多版本php,WampServer安装多个php版本
  2. python函数模块概念_python中模块和包的概念
  3. 第一个java程序释义_Java 学习之路(1)第一个Java程序
  4. oracle执行先决条件检查失败的解决方法
  5. OSU!(bzoj 4318)
  6. php dlt645,DLMS - DLT645规约转换模块
  7. Citrix桌面及应用虚拟化系列之三:活动目录架构准备
  8. linux删除文件夹和文件
  9. 论文翻译——FingerSound:Recognizing unistroke thumb gestures using a ring
  10. 视图函数的基本理解 django
  11. JQuery全选反选 随其他checkbox自动勾选全选反选
  12. selenium 控制ie_Python爬虫---Selenium的简单介绍
  13. repo同步代码_一次协作多端同步,打通看云、github互相同步(serverless实践)
  14. 超级外链工具_慈溪SEO优化_豆瓣外链:豆瓣小组做外链的注意事项?
  15. 22.docker wait
  16. NodeMCU实现远程控制LED灯
  17. python代码_Python发送邮件基础知识与代码讲解!
  18. http接口开发与调用案例
  19. MATLAB零基础入门教程视频课程
  20. Word 2010如何对论文中参考文献进行标注

热门文章

  1. C# WinForm开发系列
  2. python要求将字符串循环右移n次_python 基础知识
  3. std::kill_dependency
  4. java split 坑_Java坑锦集一 - split函数
  5. Doris之故障恢复(重点)
  6. Scala文件内容生成本地文件
  7. 机器学习之监督学习(三)——支持向量机(SVM)
  8. tensorflow精进之路(二十二)——使用slim模型对图像识别与检测(下)(VGG19模型)
  9. anaconda tensorflow import PIL 报错的解决方法
  10. php stripcslashes 转义,stripcslashes()