def test_loss_gradient(self): (_, _), (x_test, y_test) = self.mnist classifier = ClassifierWrapper(self.model_mnist) # Test gradient grads = classifier.loss_gradient(x_test, y_test) self.assertTrue(np.array(grads.shape == (NB_TEST, 28, 28, 1)).all()) self.assertNotEqual(np.sum(grads), 0)
def test_shapes(self): x_test, y_test = self.mnist[1] classifier = ClassifierWrapper(self.model_mnist) preds = classifier.predict(self.mnist[1][0]) self.assertTrue(preds.shape == y_test.shape) self.assertTrue(classifier.nb_classes == 10) class_grads = classifier.class_gradient(x_test[:11]) self.assertTrue(class_grads.shape == tuple([11, 10] + list(x_test[1].shape))) loss_grads = classifier.loss_gradient(x_test[:11], y_test[:11]) self.assertTrue(loss_grads.shape == x_test[:11].shape)