def test_mlp_distance_computation(self): """ Tests the computation of the distance for CNNs. """ self.report('Testing distance computation for specific mlps.') # Create the transport computation object mlp_layer_labels, label_mismatch_penalty = \ nn_comparators.get_mlp_layer_label_mismatch_penalties(self.non_assignment_penalty, 'reg') mlp_tp_comp = nn_comparators.OTMANNDistanceComputer( mlp_layer_labels, label_mismatch_penalty, self.non_assignment_penalty, nn_comparators.MLP_STRUCTURAL_PENALTY_GROUPS, nn_comparators.PATH_LENGTH_TYPES, dflt_mislabel_coeffs=1.0, dflt_struct_coeffs=1.0) # Create the mlp architectures mlps = generate_mlp_architectures() struct_coeffs = [0.1, 0.2] dist_types = ['lp-emd', 'lp', 'emd'] mislabel_coeffs = [1.0] * len(struct_coeffs) self.report( 'Testing distance computation for specific mlps with default coeffs.' ) self._test_dist_comp_for_single_conn_coeff(mlps, dist_types, mlp_tp_comp) self.report( 'Testing distance computation for specific mlps with conn_coeffs=%s.' % (struct_coeffs)) self._test_dist_comp_for_multiple_coeffs(mlps, dist_types, mislabel_coeffs, struct_coeffs, mlp_tp_comp)
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 __init__(self, *args, **kwargs): """ Constructor. """ super(TransportNNDistanceComputerTestCase, self).__init__(*args, **kwargs) self.non_assignment_penalty = 1 cnn_layer_labels, label_mismatch_penalty = \ nn_comparators.get_cnn_layer_label_mismatch_penalties(self.non_assignment_penalty) self.tp_comp = nn_comparators.OTMANNDistanceComputer( cnn_layer_labels, label_mismatch_penalty, self.non_assignment_penalty, nn_comparators.CNN_STRUCTURAL_PENALTY_GROUPS, nn_comparators.PATH_LENGTH_TYPES, dflt_mislabel_coeffs=1.0, dflt_struct_coeffs=1.0) self.cnns = generate_cnn_architectures()
def __init__(self, *args, **kwargs): """ Constructor. """ super(DistSumNNKernelTestCase, self).__init__(*args, **kwargs) self.non_assignment_penalty = 1 mlp_layer_labels, label_mismatch_penalty = \ nn_comparators.get_mlp_layer_label_mismatch_penalties(self.non_assignment_penalty, 'reg') self.all_layer_labels = mlp_layer_labels self.label_mismatch_penalty = label_mismatch_penalty self.tp_comp = nn_comparators.OTMANNDistanceComputer( mlp_layer_labels, label_mismatch_penalty, self.non_assignment_penalty, nn_comparators.MLP_STRUCTURAL_PENALTY_GROUPS, nn_comparators.PATH_LENGTH_TYPES) self.mislabel_coeffs = [2.0, 2.0, 1.0, 1.0, 1.0] self.struct_coeffs = [0.25, 0.5, 1.0, 2.0, 4.0] self.lp_betas = [1e-6] * len(self.struct_coeffs) self.emd_betas = [1] * len(self.struct_coeffs) self.mlps = generate_mlp_architectures()