Tensorflow: Feeding placeholder from variable

时间:2016-07-16 22:50:19

标签: python machine-learning

I'm feeding to tensorflow computation(train) graph using input queue and tf.train.batch function that prepares huge tensor with data. I have another queue with test data I would like to feed to graph every 50th step.

Question

Given the form of the input (tensors) do I have to define separate test graph for test data computation or I can somehow reuse train grap?

# 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)

I would use placeholders but I can't feed tf.placeholder with tf.Tensors and this is the thing I'm getting from queues. How is it supposed to be done?

I'm really just starting.

1 个答案:

答案 0 :(得分:2)

MNIST example中查看如何完成此操作:您需要使用占位符,其中包含数据的非张量形式的初始化程序(如文件名或CSV),然后在图形内部,使用slice_input_producer - > deocde_jpeg(或其他......) - > tf.train.batch()用于创建批次并将其提供给计算图。

所以你的图表看起来像:

  • 使用大文件名列表/ CSV /范围
  • 初始化占位符
  • tf.slice_input_producer
  • tf.image.decode_jpegtf.py_func - 加载实际数据
  • tf.train.batch - 创建用于培训的小批量
  • 提供给您的模型