queue - 使用队列时如何在 tensorflow 训练期间测试网络

标签 queue neural-network tensorflow deep-learning training-data

我正在使用队列使用下面的代码将我的训练示例提供给我的网络,并且它可以正常工作。

但是,我希望能够每 n 次迭代提供一些测试数据,但我真的不知道应该如何进行。我应该暂时停止队列并手动提供测试数据吗?我应该为测试数据创建另一个队列吗?

编辑:正确的做法是创建一个单独的文件,比如 eval.py ,连续读取最后一个检查点并评估网络?这就是他们在 CIFAR10 示例中的做法。

batch = 128 # size of the batch
x = tf.placeholder("float32", [None, n_steps, n_input])
y = tf.placeholder("float32", [None, n_classes])

queue = tf.RandomShuffleQueue(capacity=4*batch,
                      min_after_dequeue=3*batch,
                      dtypes=[tf.float32, tf.float32],
                      shapes=[[n_steps, n_input], [n_classes]])
enqueue_op = queue.enqueue_many([x, y])
X_batch, Y_batch = queue.dequeue_many(batch)

sess = tf.Session()

def load_and_enqueue(data):
    while True:
        X, Y = data.get_next_batch(batch)
        sess.run(enqueue_op, feed_dict={x: X, y: Y})

train_thread = threading.Thread(target=load_and_enqueue, args=(data))
train_thread.daemon = True
train_thread.start()

for _ in xrange(max_iter):
    sess.run(train_op)

最佳答案

您可以像这样构建另一个测试队列和训练模型的副本作为测试模型:

trainX, trainY = Queue0(batchSize, ...)...
testX, testY= Queue1(batchSize, ...)...
modelTrain = inference(trainX, trainY, ...)
# reuse variables
modelTest = inference(testX, testY, ...)
sess.run(train_op,loss_op,trainX,trainY)
sess.run(test_op,testX,testY)
由于初始化了2个模型,这种方式可能会消耗更多内存,希望看到更好的解决方案

关于queue - 使用队列时如何在 tensorflow 训练期间测试网络,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38297221/

相关文章:

python - 尝试生成简单形式语法的所有句子

c# - 等待手动创建的任务正在卡住 ASP.NET 应用程序

python - pytorch中当输入参数超过两个时如何使用forward()方法

python - 加载保存的模型后得到错误的预测

python - Keras 图像生成器不断给出不同数量的标签

java - 我如何在 Java 中使这些数据结构实现通用?

queue - 用于分布式事件限制的只读队列?

neural-network - 为什么线性函数在多层神经网络中没用?最后一层如何成为第一层输入的线性函数?

python - 是否可以重新训练以前保存的 keras 模型?

tensorflow - feature_columns 的项目必须是 _FeatureColumn 给定 : _VocabularyListCategoricalColumn