TensorFlow 学习(二)—— tf Graph tf Session 与 tf Session run

  • session:

    • with tf.Session() as sess:/ tf.InteractiveSession()
    • 初始化:
      • tf.global_variables_initializer()
      with tf.Session() as sess:
      	sess.run(tf.global_variables_initializer())
      
      • 1
      • 2

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
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18

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

#CODING: UTF-8
import time
import tensorflow as tf

N = 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 = b
	print(‘直接赋值耗时‘, time.time()-t2)
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20

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())
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7

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

再分享一下我老师大神的人工智能教程吧。零基础!通俗易懂!风趣幽默!还带黄段子!希望你也加入到我们人工智能的队伍中来!https://www.cnblogs.com/captainbed

原文地址:https://www.cnblogs.com/siwnhwxh/p/10185295.html

时间: 2024-07-29 18:01:23

TensorFlow 学习(二)—— tf Graph tf Session 与 tf Session run的相关文章

【Tensorflow】(tf.Graph)和(tf.session)

图(tf.Graph):计算图,主要用于构建网络,本身不进行任何实际的计算. 会话(tf.session):会话,主要用于执行网络.所有关于神经网络的计算都在这里进行,它执行的依据是计算图或者计算图的一部分,同时,会话也会负责分配计算资源和变量存放,以及维护执行过程中的变量. Tensorflow的几种基本数据类型: tf.constant(value, dtype=None, shape=None, name='Const', verify_shape=False) tf.Variable(i

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

简介 上一篇笔记:Tensorflow学习笔记1:Get Started 我们谈到Tensorflow是基于图(Graph)的计算系统.而图的节点则是由操作(Operation)来构成的,而图的各个节点之间则是由张量(Tensor)作为边来连接在一起的.所以Tensorflow的计算过程就是一个Tensor流图.Tensorflow的图则是必须在一个Session中来计算.这篇笔记来大致介绍一下Session.Graph.Operation和Tensor. Session Session提供了O

tf.variance_scaling_initializer() tensorflow学习:参数初始化

CNN中最重要的就是参数了,包括W,b. 我们训练CNN的最终目的就是得到最好的参数,使得目标函数取得最小值.参数的初始化也同样重要,因此微调受到很多人的重视,那么tf提供了哪些初始化参数的方法呢,我们能不能自己进行初始化呢? 所有的初始化方法都定义在tensorflow/python/ops/init_ops.py 1.tf.constant_initializer() 也可以简写为tf.Constant() 初始化为常数,这个非常有用,通常偏置项就是用它初始化的. 由它衍生出的两个初始化方法

tensorflow 基础学习二:实现一个神经网络

tensorflow变量 在tensorflow中,变量(tf.Variable)的作用就是用来保存和更新神经网络中的参数,在声明变量的同时需要指定其初始值. tensorflow中支持的随机数生成器: 函数名称 随机数分布 主要参数 tf.random_normal 正态分布 平均值.标准差.取值类型 tf.truncated_normal 正态分布,但如果随机出来的值偏离平均值超过2个标准差,那么这个数将会被重新随机 平均值.标准差.取值类型 tf.random_uniform 平均分布 最

tf.Session()、tf.InteractiveSession()

官方tutorial是这么说的: The only difference with a regular Session is that an InteractiveSession installs itself as the default session on construction. The methods Tensor.eval() and Operation.run() will use that session to run ops. 翻译一下就是:tf.InteractiveSes

tensorflow学习笔记(二)

import tensorflow as tfimport numpy as npimport mathimport tensorflow.examples.tutorials.mnist as mn sess = tf.InteractiveSession()mnist = mn.input_data.read_data_sets("E:\\Python35\\Lib\\site-packages\\tensorflow\\examples\\tutorials\\mnist\\MNIST_d

tensorflow学习4-过拟合-over-fitting

过拟合: 真实的应用中,并不是让模型尽量模拟训练数据的行为,而是希望训练数据对未知做出判断. 模型过于复杂后,模型会积极每一个噪声的部分,而不是学习数据中的通用 趋势.当一个模型的参数比训练数据还要多的时候,这个模型就可以记忆这个所以训练数据的结果,而使损失函数为0. 避免过拟合的常用方法:正则化.在损失函数中加入刻画模型复杂程度的指标.损失函数: J(θ) 引入正则化损失:J(θ)+λR(ω) λ代表模型复杂损失在总损失的比列,R(ω)刻画的是模型的复杂程度. 模型的复杂程度由权重决定,一般.

Tensorflow学习笔记(一):MNIST机器学习入门

学习深度学习,首先从深度学习的入门MNIST入手.通过这个例子,了解Tensorflow的工作流程和机器学习的基本概念. 一  MNIST数据集 MNIST是入门级的计算机视觉数据集,包含了各种手写数字的图片.在这个例子中就是通过机器学习训练一个模型,以识别图片中的数字. MNIST数据集来自 http://yann.lecun.com/exdb/mnist/ Tensorflow提供了一份python代码用于自动下载安装数据集.Tensorflow官方文档中的url打不开,在CSDN上找到了一

Tensorflow学习笔记(2):变量,常用类和一些操作

变量是用来存储和更新参数的,也就是网络中的W或b.变量会被放在内存中.当模型训练结束后,他们需要被存在硬盘上,以便将来使用或分析模型. 一.变量 创建和初始化 当创建一个变量的时候,需要将一个Tensor作为初始值传入构造函数Variable().这个初始值可以是随机值也可以是常量.Tensor的初始值需要指定shape,这个shape通常都是固定的,但是也可以通过一些高级方法重新调整. 只是创建了变量还是不够的,需要在定义一个初始化的操作,并且在使用任何变量之前,运行初始化的操作.例: 1 i