tf.session.run()单函数运行和多函数运行区别

觉得有用的话,欢迎一起讨论相互学习~

problem introduction

sess.run([a,b]) # (1)同时运行a,b两个函数
sess.run(a)
sess.run(b) # (2)运行完a函数后再运行b函数
  • 这两个语句初看时没有任何区别,但是如果a,b函数恰好是读取example_batch和label_batch这种需要使用到 数据批次输入输出函数时 例如(tf.train.shuffle_batch.tf.reader.read).
  • (1)式只会调用一次输入数据函数,则得到的example_batch和label_batch来自同一批次。 (2)式会单独调用两次输入数据函数,则得到的example_batch来自上一批次而label_batch来自下一批次。
  • 这个需要十分注意,因为如果我们想要实时打印出label_batch和inference(example_batch)时,即将输入数据的标签和经过模型预测推断的结果进行比较时.如果我们使用(2)中的写法,则label_batch和inference(example_batch)并不是来自与同一批次数据。

example code

这里我们分别使用两种不同的代码,读取csv文件中的数据。我们观察这两种方式读取的数据有什么不同。

源程序文件下载

test_tf_train_batch.csv

import tensorflow as tf

BATCH_SIZE = 400
NUM_THREADS = 2
MAX_NUM = 5 def read_data(file_queue):
reader = tf.TextLineReader(skip_header_lines=1)
key, value = reader.read(file_queue)
defaults = [[0], [0.], [0.]]
NUM, C, Tensile = tf.decode_csv(value, defaults)
vertor_example = tf.stack([C])
vertor_label = tf.stack([Tensile])
vertor_num = tf.stack([NUM]) return vertor_example, vertor_label, vertor_num def create_pipeline(filename, batch_size, num_threads):
file_queue = tf.train.string_input_producer([filename]) # 设置文件名队列
example, label, no = read_data(file_queue) # 读取数据和标签 example_batch, label_batch, no_batch = tf.train.batch(
[example, label, no], batch_size=batch_size, num_threads=num_threads, capacity=MAX_NUM) return example_batch, label_batch, no_batch x_train_batch, y_train_batch, no_train_batch = create_pipeline('test_tf_train_batch.csv', batch_size=BATCH_SIZE,
num_threads=NUM_THREADS) init_op = tf.global_variables_initializer()
local_init_op = tf.local_variables_initializer()
with tf.Session() as sess:
sess.run(local_init_op)
sess.run(init_op)
coord = tf.train.Coordinator()
threads = tf.train.start_queue_runners(coord=coord)
# 同时运行的方式
example, label, num = sess.run([x_train_batch, y_train_batch, no_train_batch])
print('The first mode to load data')
print('example', example)
print('label', label)
print('num', num) # 分别运行的方式
# example = sess.run(x_train_batch)
# label = sess.run(y_train_batch)
# num = sess.run(no_train_batch)
# print('The second mode to load data')
# print('example', example)
# print('label', label)
# print('num', num) coord.request_stop()
coord.join(threads)

Result

Run at the same time

example, label, num = sess.run([x_train_batch, y_train_batch, no_train_batch])
print('The first mode to load data')
print('example', example)
print('label', label)
print('num', num)
example label num
[ 0.294 ] [ 0.59821427] [1]
[ 0.31 ] [ 0.51785713] [2]
[ 0.2 ] [ 0.79464287] [3]
[ 0.30000001] [ 0.4732143 ] [4]
[ 0.36000001] [ 0.6964286 ] [5]

Run respectively

   example = sess.run(x_train_batch)
label = sess.run(y_train_batch)
num = sess.run(no_train_batch)
print('The second mode to load data')
print('example\n', example)
print('label\n', label)
print('num\n', num)

经过对比原始数据,我们发现采用单独运行的方式读取的example来自第一个batch,label来自下一个batch,而num来自第三个batch.也就是说其实我们单独运行了三次文件输入的程序。虽然是个小事,但是有些方面不注意,我们会酿成大错

example label num
[ 0.294 ] [ 0.5625 ] [11]
[ 0.31 ] [ 0.3482143 ] [13]
[ 0.2 ] [ 0.5535714 ] [12]
[ 0.30000001] [ 0.5714286 ] [14]
[ 0.36000001] [ 0.48214287] [15]
  • 原始数据
C tensile NUM
0.294 0.598214286 1
0.31 0.517857143 2
0.2 0.794642857 3
0.3 0.473214286 4
0.36 0.696428571 5
0.28 0.5625 6
0.2 0.348214286 7
0.284 0.553571429 8
0.38 0.482142857 9
0.44 0.571428571 10
0.214 0.660714286 11
0.72 0.589285714 12
0.38 0.616071429 13
0.266 0.5 14
0.46 0.642857143 15

tf.session.run()单函数运行和多函数运行区别的更多相关文章

  1. Tensorflow中的图(tf.Graph)和会话(tf.Session)详解

    Tensorflow中的图(tf.Graph)和会话(tf.Session) Tensorflow编程系统 Tensorflow工具或者说深度学习本身就是一个连贯紧密的系统.一般的系统是一个自治独立的 ...

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

    图(tf.Graph):计算图,主要用于构建网络,本身不进行任何实际的计算. 会话(tf.session):会话,主要用于执行网络.所有关于神经网络的计算都在这里进行,它执行的依据是计算图或者计算图的 ...

  3. tf.Session()函数的参数应用(tensorflow中使用tf.ConfigProto()配置Session运行参数&&GPU设备指定)

    版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明.本文链接:https://blog.csdn.net/dcrmg/article/details ...

  4. tensorflow函数解析:Session.run和Tensor.eval的区别

    tensorflow函数解析:Session.run和Tensor.eval 翻译 2017年04月20日 15:05:50 标签: tensorflow / 机器学习 / 深度学习 / python ...

  5. 【转载】 TensorFlow函数:tf.Session()和tf.Session().as_default()的区别

    原文地址: https://blog.csdn.net/Enchanted_ZhouH/article/details/77571939 ------------------------------- ...

  6. tf.app.run() 运行结束时,报错:SystemExit exception: no description

    环境:Python3.6.6 + tensorflow-gpu 源码如下: import tensorflow as tf def main(): print("hello tf.app.r ...

  7. tensorflow函数解析:Session.run和Tensor.eval

    原问题链接: http://stackoverflow.com/questions/33610685/in-tensorflow-what-is-the-difference-between-sess ...

  8. tensorflow函数解析: tf.Session() 和tf.InteractiveSession()

    链接如下: http://stackoverflow.com/questions/41791469/difference-between-tf-session-and-tf-interactivese ...

  9. tf.Session()和tf.InteractiveSession()的区别

    官方tutorial是这么说的: The only difference with a regular Session is that an InteractiveSession installs i ...

随机推荐

  1. 手动配置网卡配置文件ifcfg-eth0

    linux 其他知识目录 原文链接:https://www.cnblogs.com/arvintang/p/5990599.html 网络接口配置文件[root@localhost ~]# cat / ...

  2. android开发问题 Failed to pull selection 菜鸟记录

    在eclipse中开发创建了一个sqlite数据库文件,为了查看数据库文件的内容,决定复制到PC上一看究竟,位置在data……里 当我点击ddms文件浏览里的pull a file from the ...

  3. Python3 迭代器和生成器

    想要搞明白什么是迭代器,首先要了解几个名词:容器(container).迭代(iteration).可迭代对象(iterable).迭代器(iterator).生成器(generator). 看图是不 ...

  4. CS小分队第一阶段冲刺站立会议(5月13日)

    昨日成果:昨日由于课程满课,未进行项目的制作 遇到困难:/ 今天计划:为2048和扫雷添加游戏音效,和组员一起合作对扫雷进行外观美化,学习程序生成时渐隐等特效

  5. 用python脚本计算某一个文件的行数

    python可以统计文件的行数,你相信吗?不管你信不信反正我信了.下面我们来看一下python怎样统计文件的行数,代码很简单,我也做了注释,很简单的实现... 1 2 3 4 5 6 7 8 9 10 ...

  6. HDU 5434 Peace small elephant 状压dp+矩阵快速幂

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=5434 Peace small elephant  Accepts: 38  Submissions: ...

  7. Spring的初始化:org.springframework.web.context.ContextLoaderListener

    在web.xml中配置 <listener>    <listener-class>org.springframework.web.context.ContextLoaderL ...

  8. Java 静态代码块&构造代码块&局部代码块

    /* 静态代码块. 随着类的加载而执行.而且只执行一次. 作用: 用于给类进行初始化. */ class StaticCode { static int num ; static { num = 10 ...

  9. ngx_http_rewrite_module(重定向)

    1:指定rewrite规则 rewrite regex replacement [flag];   什么是rewrite规则:If the specified regular expression m ...

  10. PAT 甲级 1081 Rational Sum (数据不严谨 点名批评)

    https://pintia.cn/problem-sets/994805342720868352/problems/994805386161274880 Given N rational numbe ...