示例#1
0
def test_netmf():
    """
    Testing the NetMF class.
    """
    model = NetMF()

    graph = nx.watts_strogatz_graph(100, 10, 0.5)

    model.fit(graph)

    embedding = model.get_embedding()

    assert embedding.shape[0] == graph.number_of_nodes()
    assert embedding.shape[1] == model.dimensions
    assert type(embedding) == np.ndarray

    model = NetMF(dimensions=32)

    graph = nx.watts_strogatz_graph(150, 10, 0.5)

    model.fit(graph)

    embedding = model.get_embedding()

    assert embedding.shape[0] == graph.number_of_nodes()
    assert embedding.shape[1] == model.dimensions
    assert type(embedding) == np.ndarray
def test_neu():
    """
    Test the NEU meta embedding class.
    """
    graph = nx.newman_watts_strogatz_graph(100, 20, 0.05)

    model = NetMF()
    meta_model = NEU()
    meta_model.fit(graph, model)
    embedding = meta_model.get_embedding()

    assert embedding.shape[0] == graph.number_of_nodes()
示例#3
0
def karate_factory(algo, dim, nwalks, workers):
    if algo == "walklets":
        karate_obj = Walklets(dimensions=int(dim / 4),
                              walk_number=nwalks,
                              workers=workers)
    elif algo == "role2vec":
        karate_obj = Role2Vec(dimensions=dim,
                              walk_number=nwalks,
                              workers=workers)
    elif algo == "diff2vec":
        karate_obj = Diff2Vec(dimensions=dim,
                              diffusion_number=nwalks,
                              workers=workers)
    elif algo == "deepwalk":
        karate_obj = DeepWalk(dimensions=dim,
                              walk_number=nwalks,
                              workers=workers)
    elif algo == "boostne":
        karate_obj = BoostNE(dimensions=int(dim / 17) + 1)
    elif algo == "nodesketch":
        karate_obj = NodeSketch(dimensions=dim)
    elif algo == "netmf":
        karate_obj = NetMF(dimensions=dim)
    elif algo == "hope":
        karate_obj = HOPE(dimensions=dim)
    elif algo == "grarep":
        karate_obj = GraRep(dimensions=int(dim / 5) + 1)
    elif algo == "nmfadmm":
        karate_obj = NMFADMM(dimensions=int(dim / 2))
    elif algo == "graphwave":
        karate_obj = GraphWave()
    elif algo == "laplacian":
        karate_obj = LaplacianEigenmaps(dimensions=dim)
    else:
        raise RuntimeError("Invalid model type: %s" % algo)
    return karate_obj
示例#4
0
文件: MBA.py 项目: agoel00/MBAweb
# Z = np.random.permutation(X)
# X = torch.from_numpy(X)
# Z = torch.from_numpy(Z)

print("Creating Graph Embedddings X and Z...\n")
G = nx.karate_club_graph()
A = nx.adjacency_matrix(G)
P = np.random.permutation(np.eye(A.shape[0], dtype=np.int))
_A = (P @ A) @ (np.linalg.inv(P))
_G = nx.from_numpy_matrix(_A)

correspondences = {}
for val in range(P.shape[0]):
    correspondences[np.argmax(P[val, :])] = val

model = NetMF(dimensions=10)
_model = NetMF(dimensions=10)
model.fit(G)
_model.fit(_G)
X = model.get_embedding()
Z = model.get_embedding()
print("Graph Embeddings Created...\n")
X, Z = torch.from_numpy(X).double(), torch.from_numpy(Z).double()

N = X.shape[0]

BtB = torch.matmul(Z.T, Z)
DtD = torch.matmul(X.T, X)
normBtB = torch.norm(BtB.flatten())
normDtD = torch.norm(DtD.flatten())