def _parse_defences(self, defences): """Apply defences to the classifier :param defences: (string) names of the defences to add, supports "featsqueeze[1-8]" and "labsmooth" """ self.defences = defences if defences: pattern = re.compile("featsqueeze[1-8]?") for d in defences: # Add feature squeezing if pattern.match(d): try: bit_depth = int(d[-1]) self.feature_squeeze = FeatureSqueezing( bit_depth=bit_depth) except: raise ValueError( "You must specify the bit depth for feature squeezing: featsqueeze[1-8]" ) # Add label smoothing if d == "labsmooth": self.label_smooth = LabelSmoothing()
def test_default(self): m, n = 1000, 20 y = np.zeros((m, n)) y[(range(m), np.random.choice(range(n), m))] = 1. ls = LabelSmoothing() _, smooth_y = ls(None, y) self.assertTrue(np.isclose(np.sum(smooth_y, axis=1), np.ones(m)).all()) self.assertTrue((np.max(smooth_y, axis=1) == np.ones(m) * 0.9).all())
def test_customizing(self): m, n = 1000, 20 y = np.zeros((m, n)) y[(range(m), np.random.choice(range(n), m))] = 1. ls = LabelSmoothing(max_value=1.0 / n) _, smooth_y = ls(None, y) self.assertTrue(np.isclose(np.sum(smooth_y, axis=1), np.ones(m)).all()) self.assertTrue((np.max(smooth_y, axis=1) == np.ones(m) / n).all()) self.assertTrue(np.isclose(smooth_y, np.ones((m, n)) / n).all())