def _loss_fn(self, image, label):
# lazy import
import torch
import torch.nn as nn
from torch.autograd import Variable
image = self._process_input(image)
target = np.array([label])
target = torch.from_numpy(target)
if self.cuda: # pragma: no cover
target = target.cuda()
target = Variable(target)
images = torch.from_numpy(image[None])
if self.cuda: # pragma: no cover
images = images.cuda()
images = Variable(images, volatile=True)
predictions = self._model(images)
ce = nn.CrossEntropyLoss()
loss = ce(predictions, target)
loss = loss.data
if self.cuda: # pragma: no cover
loss = loss.cpu()
loss = loss.numpy()
return loss
评论列表
文章目录