def cnn2mlp(model_path, model_params=CNN_MODEL_PARAMS, verbose=False):
    cnn_model = load_model2(model_path)
    cnn_weights, cnn_biases = extract_weights(cnn_model,
                                              with_bias=True)

    mlp_weights, _, mlp_biases = extract_cnn_weights(cnn_weights,
                                                     biases=cnn_biases,
                                                     verbose=verbose,
                                                     as_sparse=True)

    return SimpleMLP(mlp_weights, mlp_biases, model_params)
Example #2
0
def cnn_tensors_to_flat_weights_and_graph(weights_array, max_weight_convention, as_sparse):
    flat_weights_array, _ = extract_cnn_weights(weights_array,
                                                with_avg=(max_weight_convention=='one_on_n'),
                                                as_sparse=as_sparse)

    for w_tens in flat_weights_array:
        assert len(w_tens.shape) == 2, f"weight tensor should have been flattened but has shape {w_tens.shape}"

    adj_mat = weights_to_graph(flat_weights_array)

    return (flat_weights_array, adj_mat)
Example #3
0
def save_weights(model, model_path, network_type, _log):

    weight_path = str(model_path) + '-weights.pckl'

    weights = extract_weights(model)

    picklify(weight_path, weights)
    ex.add_artifact(weight_path)

    if network_type == 'cnn':
        _log.info('Expanding CNN layers...')

        expanded_weights, constraints = extract_cnn_weights(weights,
                                                            as_sparse=True,
                                                            verbose=True)

        expanded_weight_path = str(model_path) + '-weights-expanded.pckl'
        constraintst_path = str(model_path) + '-constraints-expanded.pckl'

        picklify(expanded_weight_path, expanded_weights)
        ex.add_artifact(expanded_weight_path)

        picklify(constraintst_path, constraints)
        ex.add_artifact(constraintst_path)
def save_weights(model, model_path, network_type, unroll_cnns, _log):

    weight_path = str(model_path) + '-weights.pckl'

    weights = extract_weights(model)

    picklify(weight_path, weights)
    ex.add_artifact(weight_path)

    if network_type == 'cnn':

        _log.info('Expanding CNN layers...')

        if unroll_cnns:  # if extracting cnn weights via unrolling into mlp

            expanded_weights, constraints = extract_cnn_weights(weights,
                                                                as_sparse=True,
                                                                verbose=True)

            expanded_weight_path = str(model_path) + '-weights-expanded.pckl'
            constraintst_path = str(model_path) + '-constraints-expanded.pckl'

            picklify(expanded_weight_path, expanded_weights)
            ex.add_artifact(expanded_weight_path)

            picklify(constraintst_path, constraints)
            ex.add_artifact(constraintst_path)

        else:  # if extracting cn weights via treating each filter as a unit

            _log.info(
                f'Raw CNN model weight shapes: {[wgt.shape for wgt in weights]}'
            )

            extracted_weights_l1 = extract_cnn_weights_filters_as_units(
                weights, norm=1)

            extracted_weights_l2 = extract_cnn_weights_filters_as_units(
                weights, norm=2)

            _log.info(
                f'Extracted CNN model weight shapes: {[ew.shape for ew in extracted_weights_l1]}'
            )

            extracted_weight_path_l1 = str(
                model_path) + '-weights-filter-units_l1.pckl'
            extracted_weight_path_l2 = str(
                model_path) + '-weights-filter-units_l2.pckl'

            picklify(extracted_weight_path_l1, extracted_weights_l1)
            ex.add_artifact(extracted_weight_path_l1)
            picklify(extracted_weight_path_l2, extracted_weights_l2)
            ex.add_artifact(extracted_weight_path_l2)

    if network_type == 'cnn_vgg':

        _log.info('Expanding CNN_VGG layers...')

        weights = extract_weights(model, with_batch_norm=True)
        extracted_weights_l1 = extract_cnn_weights_filters_as_units(
            weights, norm=1, with_batch_norm=True)

        _log.info(
            f'Extracted CNN model weight shapes: {[ew.shape for ew in extracted_weights_l1]}'
        )

        extracted_weight_path_l1 = str(
            model_path) + '-weights-filter-units_l1.pckl'

        picklify(extracted_weight_path_l1, extracted_weights_l1)
        ex.add_artifact(extracted_weight_path_l1)
Example #5
0
def plot_eigenvalues(weights_path, n_eigenvalues=None, ax=None, **kwargs):

    weights = load_weights(weights_path)

    if 'cnn' in str(weights_path):
        weights, _ = extract_cnn_weights(
            weights, with_avg=True)  #(max_weight_convention=='one_on_n'))
        print([w.shape for w in weights])

    # TODO: take simpler solution from delete_isolated_ccs_refactored
    adj_mat = weights_to_graph(weights)

    _, components = sparse.csgraph.connected_components(adj_mat)

    most_common_component_counts = Counter(components).most_common(2)
    main_component_id = most_common_component_counts[0][0]
    assert (len(most_common_component_counts) == 1
            or most_common_component_counts[1][1] == 1)

    main_component_mask = (components == main_component_id)

    selected_adj_mat = adj_mat[main_component_mask, :][:, main_component_mask]

    nrom_laplacian_matrix = sparse.csgraph.laplacian(selected_adj_mat,
                                                     normed=True)

    if n_eigenvalues == None:
        start, end = 0, selected_adj_mat.shape[0] - 2
    elif isinstance(n_eigenvalues, int):
        start, end = 0, n_eigenvalues
    elif isinstance(n_eigenvalues, tuple):
        start, end = n_eigenvalues
    else:
        raise TypeError(
            'n_eigenvalues should be either None or int or tuple or slice.')
    """
    eigen_values, _ = sparse.linalg.eigs(nrom_laplacian_matrix, k=end,
                                         which='SM')
    """

    sigma = 1

    OP = nrom_laplacian_matrix - sigma * sparse.eye(
        nrom_laplacian_matrix.shape[0])
    OPinv = sparse.linalg.LinearOperator(
        matvec=lambda v: sparse.linalg.minres(OP, v, tol=1e-5)[0],
        shape=nrom_laplacian_matrix.shape,
        dtype=nrom_laplacian_matrix.dtype)
    eigen_values, _ = sparse.linalg.eigsh(nrom_laplacian_matrix,
                                          sigma=sigma,
                                          k=end,
                                          which='LM',
                                          tol=1e-5,
                                          OPinv=OPinv)

    eigen_values = np.sort(eigen_values)

    eigen_values = eigen_values[start:end]

    if ax is None:
        _, ax = plt.subplots(1)

    ax.xaxis.set_major_locator(MaxNLocator(integer=True))

    if 'linestyle' not in kwargs:
        kwargs['linestyle'] = 'none'
        kwargs['marker'] = '*'
        kwargs['markersize'] = 5

    return ax.plot(range(start + 1, end + 1), eigen_values, **kwargs)