forked from corenel/pytorch-adda
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
implement test() for classifing target data.
- Loading branch information
Showing
1 changed file
with
26 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,31 @@ | ||
"""Test script to classify target data.""" | ||
|
||
import torch | ||
import torch.nn as nn | ||
|
||
from utils import make_variable | ||
|
||
|
||
def eval_tgt(model_src, model_tgt, data_loader): | ||
"""Evaluation for target encoder by source classifier on target dataset.""" | ||
pass | ||
print("=== Evaluating classifier for encoded target domain ===") | ||
model_src.eval() | ||
model_tgt.eval() | ||
loss = 0 | ||
acc = 0 | ||
criterion = nn.NLLLoss() | ||
|
||
for (images, labels) in data_loader: | ||
images = make_variable(images, volatile=True) | ||
labels = make_variable(labels).squeeze_() | ||
|
||
_, preds = model_tgt(images) | ||
loss += criterion(preds, labels).data[0] | ||
|
||
pred_cls = preds.data.max(1)[1] | ||
acc += pred_cls.eq(labels.data).cpu().sum() | ||
|
||
loss /= len(data_loader) | ||
acc /= len(data_loader.dataset) | ||
|
||
print("Avg Loss = {}, Avg Accuracy = {:2%}".format(loss, acc)) |