コード例 #1
0
ファイル: lle.py プロジェクト: alanyuchenhou/GEM
def main(data_set_name):
    dimensions = 1
    input_file = './graph/' + data_set_name + '.tsv'
    output_file = '../data/' + data_set_name + '.emb'
    # Instatiate the embedding method with hyperparameters
    lle = LocallyLinearEmbedding(dimensions)

    # Load graph
    # graph = graph_util.loadGraphFromEdgeListTxt(input_file)
    graph = networkx.read_weighted_edgelist(input_file)

    # Learn embedding - accepts a networkx graph or file with edge list
    embeddings_array, t = lle.learn_embedding(graph, edge_f=None, is_weighted=True, no_python=True)
    embeddings = pandas.DataFrame(embeddings_array)
    embeddings.to_csv(output_file, sep=' ', na_rep=0.1)
コード例 #2
0
def get_embeddings(graph,
                   embedding_algorithm_enum,
                   dimension_count,
                   hyperparameter,
                   lower=None,
                   higher=None):
    """Generate embeddings. """

    if embedding_algorithm_enum is EmbeddingType.LocallyLinearEmbedding:
        embedding_alg = LocallyLinearEmbedding(d=dimension_count)
    elif embedding_algorithm_enum is EmbeddingType.Hope:
        embedding_alg = HOPE(d=dimension_count, beta=0.01)
    elif embedding_algorithm_enum is EmbeddingType.GF:
        embedding_alg = GraphFactorization(d=dimension_count,
                                           max_iter=100000,
                                           eta=1 * 10**-4,
                                           regu=1.0)
    elif embedding_algorithm_enum is EmbeddingType.LaplacianEigenmaps:
        embedding_alg = LaplacianEigenmaps(d=dimension_count)

    elif embedding_algorithm_enum is EmbeddingType.DegreeNeigDistributionWithout:
        A = np.array([
            np.histogram([graph.degree(neig) for neig in graph.neighbors(i)],
                         bins=dimension_count,
                         density=True,
                         range=(lower, higher))[0] for i in graph.nodes()
        ])
        A = (A - A.mean(axis=0)) / A.std(axis=0)
        return A

    elif embedding_algorithm_enum is EmbeddingType.DegreeNeigDistribution:
        A = np.array([
            np.concatenate([
                np.array([graph.degree(i) / (higher * dimension_count)]),
                np.histogram(
                    [graph.degree(neig) for neig in graph.neighbors(i)],
                    bins=dimension_count - 1,
                    density=True,
                    range=(lower, higher))[0]
            ],
                           axis=0) for i in graph.nodes()
        ])
        A = (A - A.mean(axis=0)) / A.std(axis=0)
        return A

    elif embedding_algorithm_enum is EmbeddingType.DegreeNeigNeigDistribution:
        bin_length = int(dimension_count / 2)

        A = np.array([
            np.concatenate([
                np.array([graph.degree(i) / (higher)]),
                np.histogram(
                    [graph.degree(neig) for neig in graph.neighbors(i)],
                    bins=bin_length,
                    density=True,
                    range=(lower, higher))[0],
                np.histogram([
                    graph.degree(neigneig) for neig in graph.neighbors(i)
                    for neigneig in graph.neighbors(neig)
                ],
                             bins=bin_length,
                             density=True,
                             range=(lower, higher))[0]
            ],
                           axis=0) for i in graph.nodes()
        ])

        A = (A - A.mean(axis=0)) / A.std(axis=0)
        A[:, 0] = A[:, 0]
        A[:, 1:1 + bin_length] = A[:, 1:1 + bin_length]
        A[:, 2 + bin_length:] = A[:, 2 + bin_length:] * hyperparameter
        A = np.nan_to_num(A)
        return A
    else:
        raise NotImplementedError

    A, t = embedding_alg.learn_embedding(graph=graph, no_python=True)

    A = np.dot(A, np.diag(np.sign(np.mean(A, axis=0))))
    A = (A - A.mean(axis=0)) / A.std(axis=0)
    return A