tf.session.run()单函数运行和多函数运行区别
觉得有用的话,欢迎一起讨论相互学习~
problem introduction
sess.run([a,b])
sess.run(a)
sess.run(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)
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
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· winform 绘制太阳,地球,月球 运作规律
· AI与.NET技术实操系列(五):向量存储与相似性搜索在 .NET 中的实现
· 超详细:普通电脑也行Windows部署deepseek R1训练数据并当服务器共享给他人
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
· 上周热点回顾(3.3-3.9)