예제 #1
0
 def testAccuracy(self, x_test, y_test, q_pred, headId):
     acc = 0
     num_pred_samples = 100
     for x_test_batch, y_test_batch in self.getBatch(x_test, y_test):
         monteCarlo = MonteCarlo(q_pred, num_pred_samples)
         y_pred_batch = monteCarlo.computeMonteCarlo(x_test_batch, headId)
         _, y_pred_batch = torch.max(y_pred_batch.data, 1)
         y_pred_batch = torch.eye(
             self.dataGen.get_dims()[1])[y_pred_batch].type(FloatTensor)
         acc += torch.sum(torch.mul(y_pred_batch, y_test_batch)).item()
     return acc / y_test.shape[0]
예제 #2
0
parameters = vanillaNN.getParameters()
qPrior = ParametersDistribution(sharedDim, headDim, headCount)
qPrior.setParameters(parameters, 1)
parameters = qPrior.getFlattenedParameters(1)

for i, (images, labels) in enumerate(trainLoader):
    images = images.reshape(-1, 28 * 28).to(Device)
    yOnehot = _onehot(labels)
    qPosterior = maximizeVariationalLowerBound(images,
                                               yOnehot,
                                               qPrior,
                                               taskId=1)

print("Prediction Time :-) ")

with torch.no_grad():
    correct = 0
    total = 0

    for images, labels in testLoader:
        images = images.reshape(-1, 28 * 28).to(Device)
        labels = labels.to(Device)
        monteCarlo = MonteCarlo(qPrior, numSamples)
        predicted = monteCarlo.computeMonteCarlo(images, 1)
        _, predicted = torch.max(predicted.data, 1)
        total += labels.size(0)
        correct += (predicted == labels).sum().item()
    print('Accuracy of the network on the 10000 test images: {} %'.format(
        100 * correct / total))