tensorflow中的tf.session.run()

转自:huahuanzhu

1

函数参数

run(    fetches,   feed_dict=None,    options=None,    run_metadata=None)

tf.Session.run() 执行 fetches 中的操作,计算 fetches 中的张量值。

这个函数执行一步 TensorFlow 运算,通过运行必要的图块来执行每一个操作,并且计算每一个 fetches 中的张量的值,用相关的输入变量替换 feed_dict 中的值。

fetches 参数可能是一个单一图元素,或者任意嵌套列表,元组,namedtuple,字典,或者有序字典在叶子中包含图元素。

tf.Session.run()函数返回值为fetches的执行结果。如果fetches是一个元素就返回一个值;若fetches是一个list,则返回list的值,若fetches是一个字典类型,则返回和fetches同keys的字典。

(Either a single valueif `fetches` is a single graph element, or
a list of values if `fetches` is a list, or a dictionary with the
same keys as `fetches` if that is a dictionary (described above).)


2

session.run([fetch1, fetch2])

关于 session.run([fetch1, fetch2]),请看http://stackoverflow.com/questions/42407611/how-tensorflow-handle-the-computional-graph-when-executing-sess-run/42408368?noredirect=1#comment71994086_42408368

执行sess.run()时,tensorflow是否计算了整个图

我们在编写代码的时候,总是要先定义好整个图,然后才调用sess.run()。那么调用sess.run()的时候,程序是否执行了整个图

import tensorflow as tf
state = tf.Variable(0.0,dtype=tf.float32)
one = tf.constant(1.0,dtype=tf.float32)
new_val = tf.add(state, one)
update = tf.assign(state, new_val) #返回tensor, 值为new_val
update2 = tf.assign(state, 10000)  #没有fetch,便没有执行
init = tf.initialize_all_variables()
with tf.Session() as sess:
    sess.run(init)
    for _ in range(3):
        print sess.run(update)
  
  
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11

和上个程序差不多,但我们这次仅仅是fetch “update”,输出是1.0 , 2.0, 3.0,可以看出,tensorflow并没有计算整个图,只是计算了与想要fetch 的值相关的部分

sess.run() 中的feed_dict

我们都知道feed_dict的作用是给使用placeholder创建出来的tensor赋值。其实,他的作用更加广泛:feed 使用一个 值临时替换一个 op 的输出结果. 你可以提供 feed 数据作为 run() 调用的参数. feed 只在调用它的方法内有效, 方法结束, feed 就会消失.

import tensorflow as tf
y = tf.Variable(1)
b = tf.identity(y)
with tf.Session() as sess:
    tf.global_variables_initializer().run()
    print(sess.run(b,feed_dict={y:3})) #使用3 替换掉
    #tf.Variable(1)的输出结果,所以打印出来3 
    #feed_dict{y.name:3} 和上面写法等价

    print(sess.run(b))  #由于feed只在调用他的方法范围内有效,所以这个打印的结果是 1
  
  
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10

输出是3 1
参考文献:
[1] tensorflow whitepaper

<think>好的,我现在需要解决用户的问题:如何在TensorFlow 2.x中同时使用TensorBoard Projector插件并禁用Eager Execution。首先,我得回顾一下TensorFlow 2.x的变化,尤其是关于tf.contrib的移除和Eager Execution的默认启用。 用户提到旧版代码使用了tensorflow.contrib.tensorboard.plugins.projector,而TensorFlow 2.x已经移除了tf.contrib,所以需要找到替代方案。根据之前的引用,比如引用[3]和引用[5],可能Projector插件现在集成到了TensorBoard中,或者需要其他导入方式。可能需要使用tensorboard.plugins.projector或者通过兼容性模块来引入。 然后,禁用Eager Execution在TensorFlow 2.x中可以通过tf.compat.v1.disable_eager_execution()来实现,但需要注意这个函数必须在任何操作之前调用,否则会报错。所以代码中需要先禁用Eager,再导入其他模块或定义计算图。 接下来,需要处理Projector的API。查看引用[4],用户提到了从tensorflow.contrib.tensorboard.plugins导入projector,但2.x中这个路径不存在了。可能需要改用其他的包,比如从tensorboard.plugins.projector导入,或者使用tf.compat.v1中的兼容性函数。 另外,引用[3]提到了使用Embedding Projector的在线工具,但代码中可能需要将嵌入向量和元数据保存到日志目录,然后启动TensorBoard来查看。因此,代码示例需要包含保存嵌入向量的逻辑,配置Projector的参数,并生成相应的文件如metadata.tsv。 可能还需要使用tf.summary.create_file_writer来创建日志文件,然后使用Projector的API配置嵌入层。同时,需要确保在禁用Eager Execution的情况下,使用Session来运行初始化操作,比如保存检查点,因为Projector需要从检查点读取嵌入向量。 现在,综合这些信息,编写代码的大致步骤应该是: 1. 禁用Eager Execution。 2. 导入必要的模块,包括TensorBoard的Projector插件。 3. 定义模型和嵌入层。 4. 配置Projector参数,指定嵌入变量和元数据路径。 5. 创建日志目录和文件写入器。 6. 使用Session运行初始化,保存检查点。 需要注意的坑:在禁用Eager后,必须使用tf.compat.v1.Session来运行初始化操作,并且检查点的保存可能需要手动处理。同时,metadata.tsv文件需要正确生成并放在指定的路径,供Projector读取。 最后,验证代码是否能在TensorFlow 2.x中运行,并启动TensorBoard查看投影结果。如果有错误,可能需要检查导入路径是否正确,以及禁用Eager后的计算图模式是否兼容旧代码。</think>要在TensorFlow 2.x中兼容旧版`tensorflow.contrib`中的Projector插件并禁用Eager Execution,需结合`tf.compat.v1`接口和TensorBoard的独立包实现。以下是分步示例: --- ### **1. 兼容性代码实现** ```python import tensorflow as tf from tensorboard.plugins import projector # 新版Projector插件路径 import numpy as np # 禁用Eager Execution(必须放在最前面) tf.compat.v1.disable_eager_execution() # 定义嵌入变量(示例数据) embedding_var = tf.compat.v1.get_variable("embedding", shape=[1000, 128], initializer=tf.random_normal_initializer()) # 创建元数据文件(如标签) metadata_path = "logs/projector/metadata.tsv" with open(metadata_path, 'w') as f: for i in range(1000): f.write(f'label_{i}\n') # 配置Projector参数 config = projector.ProjectorConfig() embedding = config.embeddings.add() embedding.tensor_name = embedding_var.name embedding.metadata_path = metadata_path # 保存配置到日志目录 summary_writer = tf.compat.v1.summary.FileWriter('logs/projector') projector.visualize_embeddings(summary_writer, config) # 初始化变量并保存检查点 with tf.compat.v1.Session() as sess: sess.run(tf.compat.v1.global_variables_initializer()) saver = tf.compat.v1.train.Saver() saver.save(sess, 'logs/projector/model.ckpt', global_step=0) ``` --- ### **2. 关键点说明** 1. **禁用Eager Execution** `tf.compat.v1.disable_eager_execution()`需在代码开头调用,确保后续操作处于计算图模式[^1]。 2. **Projector插件迁移** TensorFlow 2.x将Projector从`tf.contrib`移至独立包`tensorboard.plugins.projector`,需通过`from tensorboard.plugins import projector`导入[^3][^5]。 3. **检查点保存** 在计算图模式下,需手动创建`tf.compat.v1.Session`和`Saver`对象保存嵌入变量,Projector依赖检查点文件解析嵌入数据[^4]。 4. **元数据文件格式** `metadata.tsv`需与嵌入数据维度匹配,并通过`embedding.metadata_path`指定路径。 --- ### **3. 启动TensorBoard** ```bash tensorboard --logdir=logs/projector ``` 访问`http://localhost:6006`后,在**Projector**标签页中可查看高维向量投影结果[^2][^3]。 ---
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值