Compute accuracy with the given dataset iterator.
(model, dataset)
| 216 | |
| 217 | |
| 218 | def evaluate(model, dataset): |
| 219 | """Compute accuracy with the given dataset iterator.""" |
| 220 | mean_loss = tfe.metrics.Mean() |
| 221 | accuracy = tfe.metrics.Accuracy() |
| 222 | for x, y in dataset: |
| 223 | logits, _ = model(x, training=False) |
| 224 | loss = model.compute_loss(logits=logits, labels=y) |
| 225 | accuracy( |
| 226 | labels=tf.cast(y, tf.int64), |
| 227 | predictions=tf.argmax(logits, axis=1, output_type=tf.int64)) |
| 228 | mean_loss(loss) |
| 229 | |
| 230 | return accuracy.result().numpy(), mean_loss.result().numpy() |
| 231 | |
| 232 | |
| 233 | if __name__ == "__main__": |