コード例 #1
0
ファイル: main.py プロジェクト: HSIYJND/ROhsi
def test(testloader, model, criterion, epoch, use_cuda):
	model.eval()
	accs   = np.ones((len(testloader))) * -1000.0
	losses = np.ones((len(testloader))) * -1000.0
	for batch_idx, (inputs, targets) in enumerate(testloader):
		if use_cuda:
			inputs, targets = inputs.cuda(), targets.cuda()
		inputs, targets = torch.autograd.Variable(inputs, volatile=True), torch.autograd.Variable(targets)
		outputs = model(inputs)
		losses[batch_idx] = criterion(outputs, targets).item()
		accs[batch_idx] = auxil.accuracy(outputs.data, targets.data, topk=(1,))[0].item()
	return (np.average(losses), np.average(accs))
コード例 #2
0
def train(trainloader, model, criterion, optimizer, epoch, use_cuda):
    model.train()
    accs   = np.ones((len(trainloader))) * -1000.0
    losses = np.ones((len(trainloader))) * -1000.0
    for batch_idx, (inputs, targets) in enumerate(trainloader):
        if use_cuda:
            inputs, targets = inputs.cuda(), targets.cuda()
        inputs, targets = torch.autograd.Variable(inputs), torch.autograd.Variable(targets)
        outputs = model(inputs)
        loss = criterion(outputs, targets)
        losses[batch_idx] = loss.item()
        accs[batch_idx] = auxil.accuracy(outputs.data, targets.data)[0].item()
        optimizer.zero_grad()
        loss.backward()
        optimizer.step()
    return (np.average(losses), np.average(accs))