简体   繁体   English

在Tensorflow中同时进行培训和测试

[英]Simultaneous training and testing in Tensorflow

I am trying to perform training and testing of a neural network in Tensorflow in the same script, same graph, same session. 我试图在相同的脚本,相同的图形,相同的会话中在Tensorflow中执行神经网络的训练和测试。 I read that it is possible, however when I look at the accuracy/loss results from the training and testing ops, it seems as if both ops are just a continuation of the training process somehow. 我读到这是可能的,但是当我查看训练和测试操作的准确性/丢失结果时,似乎两个操作只是某种方式的训练过程的延续。 Eg train.acc. 例如train.acc。 will end the epoch at .84, then testing will start at 0.84, end at 0.87, and then training will resume with an acc. 将在0.84结束时代,然后测试将从0.84开始,结束于0.87,然后训练将以acc恢复。 of 0.87... 0.87 ...

My code is constructed like this: 我的代码构造如下:

1) defining calculations for acc and loss 1)定义acc和loss的计算

calc_loss = tf.nn.sparse_softmax_cross_entropy_with_logits(labels=labels, logits=logits)
    acc, acc_op = tf.metrics.accuracy(labels=labels, predictions=predictions)
loss = tf.metrics.mean(calc_loss)

2) running the above in one and the same tf.session, eg for acc: 2)在同一个tf.session中运行上述内容,例如:

acc_value, acc_op_value = sess.run([acc, acc_op], feed_dict=feed_dict_train)
test_acc_value, test_acc_op_value = sess.run([acc, acc_op], feed_dict=feed_dict_test) 

the two dictionaries contain different data. 这两个词典包含不同的数据。 My question is, do I need to define different ops for training and testing - is this where the training and testing get mixed up? 我的问题是,我是否需要为培训和测试定义不同的操作 - 这是培训和测试混淆的地方吗? Or is it impossible to test and train in one session? 或者在一次会议中测试和训练是不可能的? What would be a clean and simple way to go about this? 什么是干净而简单的方法呢? A link to a code example that illustrates this would also be of help (as I am not managing to find anything that directly answers my question). 指向此示例的代码示例的链接也会有所帮助(因为我没有找到任何直接回答我问题的内容)。

The metrics in tf.metrics are stateful; tf.metrics中的指标是有状态的; they create variables to accumulate partial results in, so you shouldn't expect them to auto-reset. 它们创建变量以累积部分结果,因此您不应期望它们自动重置。 Instead use the metrics in tf.contrib.metrics or tf.keras.metrics and session.run the ops to reset them accordingly. 而是使用tf.contrib.metrics的指标或tf.keras.metrics和session.run操作来相应地重置它们。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM