Skip to main content
 首页 » 编程设计

python之tensorflow : Feeding placeholder from variable

2024年10月17日30zhoujg

我正在使用输入队列向 tensorflow computation(train) 图提供数据 tf.train.batch 函数,用数据准备巨大的张量。 我有另一个队列,其中包含我想每 50 步提供一次图表的测试数据。

问题

鉴于输入的形式(张量),我是否必须为测试数据计算定义单独的测试图,或者我可以以某种方式重用训练图?

# Prepare data 
batch = tf.train.batch([train_image, train_label], batch_size=200) 
batchT = tf.train.batch([test_image, test_label], batch_size=200) 
 
x = tf.reshape(batch[0], [-1, IMG_SIZE, IMG_SIZE, 3]) 
y_ = batch[1] 
xT = tf.reshape(batchT[0], [-1, IMG_SIZE, IMG_SIZE, 3]) 
y_T = batchT[1] 
 
# Graph definition 
train_step = ... # train_step = g(x) 
 
# Session 
sess = tf.Session() 
sess.run(tf.initialize_all_variables()) 
 
for i in range(1000): 
  if i%50 == 0:  
  # here i would like reuse train graph but with tensor x replaced by x_t 
  # train_accuracy = ? 
  # print("step %d, training accuracy %g"%(i, train_accuracy)) 
 
train_step.run(session=sess) 

我会使用 placeholders 但我不能将 tf.placeholder 与 tf.Tensors 一起使用,这就是我得到的东西来自队列。 应该怎么做?

我真的才刚刚开始。

请您参考如下方法:

看看 MNIST example 中这是如何完成的:你需要使用一个占位符和你的数据的非张量形式的初始值设定项(如文件名或 CSV),然后在图中,使用 slice_input_producer -> deocde_jpeg(或其他...) -> tf.train .batch() 创建批处理并将其提供给计算图。

所以你的图表看起来像这样:

  • 用大文件名列表/CSV/范围初始化的占位符
  • tf.slice_input_producer
  • tf.image.decode_jpeg 或 tf.py_func - 加载实际数据
  • tf.train.batch - 创建用于训练的小批量
  • 喂给你的模型