def _test_gradient_w(self): """Test for backard passing of w in kernel gradients""" if not self._has_gradient(): self.logger.info( "Gradient is not implemented for %s. " "Skipping multiple-point tests.", self.kernel.class_type) return # check if the gradient computed when passing w is the same as the # gradient computed with w=None and pre-multiplied with w # test on single point w = CArray.rand(shape=(1, ), random_state=0) self.kernel.rv = self.p2_dense grad_1 = self.kernel.gradient(self.p1_dense, w=w) grad_2 = w * (self.kernel.gradient(self.p1_dense)) grad_2 = grad_2.ravel() self.assertTrue(grad_1.is_vector_like) self.assertTrue(grad_2.is_vector_like) self.assert_array_almost_equal(grad_1, grad_2, decimal=10) # test on multiple points w = CArray.rand(shape=(5, ), random_state=0) self.kernel.rv = self.d_dense[:5, :].X grad_1 = self.kernel.gradient(self.p1_dense, w=w) grad_2 = w.dot(self.kernel.gradient(self.p1_dense)).ravel() self.assertTrue(grad_1.is_vector_like) self.assertTrue(grad_2.is_vector_like) self.assert_array_almost_equal(grad_1, grad_2, decimal=10)
def test_multiclass_gradient(self): """Test if gradient is correct when requesting for all classes with w""" multiclass = CClassifierMulticlassOVA(classifier=CClassifierSVM, class_weight='balanced') multiclass.fit(self.dataset.X, self.dataset.Y) div = CArray.rand(shape=multiclass.n_classes, random_state=0) def f_x(x): x = multiclass.predict(x, return_decision_function=True)[1] return CArray((x / div).mean()) def grad_f_x(x): w = CArray.ones(shape=multiclass.n_classes) / \ (div * multiclass.n_classes) return multiclass.gradient(x, w=w) i = 5 # Sample to test x = self.dataset.X[i, :] from secml.optim.function import CFunction check_grad_val = CFunction(f_x, grad_f_x).check_grad(x, epsilon=1e-1) self.logger.info( "norm(grad - num_grad): %s", str(check_grad_val)) self.assertLess(check_grad_val, 1e-3)
def _test_similarity_shape_sparse(self): """Test shape of kernel.""" self.logger.info("Testing shape of " + self.kernel.class_type + " kernel output.") x_vect = CArray.rand(shape=(1, 10)).ravel().tosparse() x_mat = CArray.rand(shape=(10, 10)).tosparse() x_col = CArray.rand(shape=(10, 1)).tosparse() x_single = CArray.rand(shape=(1, 1)).tosparse() self._cmp_kernel(self.kernel.k, x_vect, x_vect) self._cmp_kernel(self.kernel.k, x_mat, x_vect) self._cmp_kernel(self.kernel.k, x_vect, x_mat) self._cmp_kernel(self.kernel.k, x_mat, x_mat) self._cmp_kernel(self.kernel.k, x_col, x_col) self._cmp_kernel(self.kernel.k, x_col, x_single) self._cmp_kernel(self.kernel.k, x_single, x_col) self._cmp_kernel(self.kernel.k, x_single, x_single)
def _rnd_init_poisoning_points(self, n_points=None, init_from_val=False, val=None): """Returns a random set of poisoning points randomly with flipped labels.""" if init_from_val: if val: init_dataset = val else: init_dataset = self.val else: init_dataset = self.surrogate_data if init_dataset is None: raise ValueError("Surrogate data not set!") if (self._n_points is None or self._n_points == 0) and (n_points is None or n_points == 0): raise ValueError("Number of poisoning points (n_points) not set!") if n_points is None: n_points = self.n_points idx = CArray.randsample(init_dataset.num_samples, n_points, random_state=self.random_seed) xc = init_dataset.X[idx, :].deepcopy() if not self.discrete: # if the attack is in a continuous space we add a # little perturbation to the initial poisoning point random_noise = CArray.rand(shape=xc.shape, random_state=self.random_seed) xc += 1e-3 * (2 * random_noise - 1) else: xc = self.add_discrete_perturbation(xc) yc = CArray(init_dataset.Y[idx]).deepcopy() # true labels # randomly pick yc from a different class for i in range(yc.size): labels = CArray.randsample(init_dataset.num_classes, 2, random_state=self.random_seed) if yc[i] == labels[0]: yc[i] = labels[1] else: yc[i] = labels[0] return xc, yc
def test_rand(self): """Test for CArray.rand() classmethod.""" self.logger.info("Test for CArray.rand() classmethod.") for shape in [(1, ), (2, ), (1, 2), (2, 1), (2, 2)]: for sparse in [False, True]: res = CArray.rand(shape=shape, sparse=sparse) self.logger.info("CArray.rand(shape={:}, sparse={:}):" "\n{:}".format(shape, sparse, res)) self.assertIsInstance(res, CArray) self.assertEqual(res.isdense, not sparse) self.assertEqual(res.issparse, sparse) if len(shape) == 1 and sparse is True: # Sparse "vectors" have len(shape) == 2 self.assertEqual(res.shape, (1, shape[0])) else: self.assertEqual(res.shape, shape) self.assertIsSubDtype(res.dtype, float) # Interval of random values is [0.0, 1.0) self.assertFalse((res < 0).any()) self.assertFalse((res >= 1).any())
from secml.array import CArray from secml.figure import CFigure fig = CFigure(fontsize=12) n = 12 X = CArray.arange(n) Y1 = (1 - X / float(n)) * (1.0 - 0.5) * CArray.rand((n, )) + 0.5 Y2 = (1 - X / float(n)) * (1.0 - 0.5) * CArray.rand((n, )) + 0.5 fig.sp.xticks([0.025, 0.025, 0.95, 0.95]) fig.sp.bar(X, Y1, facecolor='#9999ff', edgecolor='white') fig.sp.bar(X, -Y2, facecolor='#ff9999', edgecolor='white') for x, y in zip(X, Y1): fig.sp.text(x, y, '%.2f' % y, ha='center', va='bottom') for x, y in zip(X, Y2): fig.sp.text(x, -y - 0.02, '%.2f' % y, ha='center', va='top') fig.sp.xlim(-.5, n - .5) fig.sp.xticks(()) fig.sp.ylim(-1.25, 1.25) fig.sp.yticks(()) fig.sp.grid() fig.show()