def test_kernel_computation(self): """ Testing computation of the lp distance. """ self.report('Testing computed kernel values for specific cnns.') betas = [0.0001] scale = 2.1 struct_coeffs = 1.0 mislabel_coeffs = 1.0 tp_comp = nn_comparators.OTMANNDistanceComputer( self.all_layer_labels, self.label_mismatch_penalty, self.non_assignment_penalty, nn_comparators.CNN_STRUCTURAL_PENALTY_GROUPS, nn_comparators.PATH_LENGTH_TYPES, dflt_mislabel_coeffs=mislabel_coeffs, dflt_struct_coeffs=struct_coeffs) tp_kernel = nn_comparators.generate_otmann_kernel_from_params( 'prod', self.all_layer_labels, self.label_mismatch_penalty, self.non_assignment_penalty, nn_comparators.CNN_STRUCTURAL_PENALTY_GROUPS, nn_comparators.PATH_LENGTH_TYPES, mislabel_coeffs, struct_coeffs, betas, scale, dist_type='lp') cnn_dists = tp_comp(self.cnns, self.cnns) cnn_kernel = tp_kernel(self.cnns) diff = np.linalg.norm(scale * np.exp(-cnn_dists[0] * betas[0]) - cnn_kernel) assert diff < np.linalg.norm(cnn_kernel) * 1e-6
def test_sum_product_equivalence(self): """ Unit-test for testing that both kernels compute the same thing in certain cases. """ dist_type_vals = ['lp', 'emd'] for dist_type in dist_type_vals: if dist_type == 'lp': betas = self.lp_betas scales = [1] elif dist_type == 'emd': betas = self.emd_betas scales = [1] sum_kernel = nn_comparators.generate_otmann_kernel_from_params( 'sum', self.all_layer_labels, self.label_mismatch_penalty, self.non_assignment_penalty, nn_comparators.MLP_STRUCTURAL_PENALTY_GROUPS, nn_comparators.PATH_LENGTH_TYPES, self.mislabel_coeffs, self.struct_coeffs, betas, scales, dist_type=dist_type) prod_kernel = nn_comparators.generate_otmann_kernel_from_params( 'prod', self.all_layer_labels, self.label_mismatch_penalty, self.non_assignment_penalty, nn_comparators.MLP_STRUCTURAL_PENALTY_GROUPS, nn_comparators.PATH_LENGTH_TYPES, self.mislabel_coeffs, self.struct_coeffs, betas, scales, dist_type=dist_type) sum_nn_K = sum_kernel(self.mlps) prod_nn_K = prod_kernel(self.mlps) assert np.linalg.norm(sum_nn_K - prod_nn_K) < _TOL
def test_instantiation_and_computation(self): """ Testing instantiation. """ self.report( 'Testing instantiation of DistSumNNKernelTestCase and computation ' + 'for specific networks.') dist_type_vals = ['lp', 'emd', 'lp-emd'] all_kernels = [] for dist_type in dist_type_vals: if dist_type == 'lp': betas = self.lp_betas scales = [1] elif dist_type == 'emd': betas = self.emd_betas scales = [1] else: betas = [ j for i in zip(self.lp_betas, self.emd_betas) for j in i ] scales = [1, 1] tp_kernel = nn_comparators.generate_otmann_kernel_from_params( 'sum', self.all_layer_labels, self.label_mismatch_penalty, self.non_assignment_penalty, nn_comparators.MLP_STRUCTURAL_PENALTY_GROUPS, nn_comparators.PATH_LENGTH_TYPES, self.mislabel_coeffs, self.struct_coeffs, betas, scales, dist_type=dist_type) nn_K = tp_kernel(self.mlps) nn_eig_vals, _ = np.linalg.eig(nn_K) self.report( 'dist-type: %s, eigvals: %s.' % (dist_type, get_list_of_floats_as_str(sorted(nn_eig_vals)))) assert nn_K.shape == (len(self.mlps), len(self.mlps)) self.report( '%s transport kernel:\n%s' % (dist_type, str(np.round(nn_K, 3))), 'test_result') assert np.all(np.diag(nn_K) == sum(scales)) all_kernels.append(nn_K) # Check if it is in fact the sum if 'lp' in dist_type_vals and 'emd' in dist_type_vals and 'lp-emd' in dist_type_vals: lp_kernel = all_kernels[dist_type_vals.index('lp')] emd_kernel = all_kernels[dist_type_vals.index('emd')] lpemd_kernel = all_kernels[dist_type_vals.index('lp-emd')] assert np.linalg.norm(lpemd_kernel - lp_kernel - emd_kernel) < _TOL