Ejemplo n.º 1
0
        optimizer_init.step()

    l1 = 0.
    for x, y in dataloader_l1:

        optimizer_init.zero_grad()
        pe_x = memory_transfer(node_embedding(x.long()))
        pe_y = memory_transfer(node_embedding(y.long()))
        loss = args.alpha * graph_embedding_criterion(pe_x, pe_y, manifold=manifold).sum()
        l1 += loss.item()
        loss.backward()
        optimizer_init.step()

    print('Loss value for iteration ', i,' is ', l1/len(dataset_l1) + l2/len(dataset_l2))
if(args.plot):
    plot_poincare_disc_embeddings(node_embedding.weight.data,  labels=Y, save_folder="LOG/all_loss", file_name="rcome_embedding_script_gmm_init.png")

optimizer = rsgd.RSGD(list(node_embedding.parameters()) + list(context_embedding.parameters()), args.lr, manifold=manifold)
# main learning loop
for i in range(args.epoch):

    l1 = 0.
    for x, y in dataloader_l1:

        optimizer.zero_grad()
        pe_x = node_embedding(memory_transfer(x.long()))
        pe_y = node_embedding(memory_transfer(y.long()))

        loss = args.alpha * graph_embedding_criterion(pe_x, pe_y, manifold=manifold).sum()
        l1 += loss.item()
        loss.backward()
Ejemplo n.º 2
0
        loss.backward()
        optimizer.step()
    print('Loss value for iteration ', i, ' is ', tloss)

from rcome.function_tools import poincare_alg as pa

weigths = torch.Tensor([[1 if (y in dataset.Y[i]) else 0 for y in range(13)]
                        for i in range(len(X))]).cuda()

barycenters = []
for i in range(13):
    barycenters.append(
        pa.barycenter(model.weight.data, weigths[:, i], verbose=True).cpu())

plot_poincare_disc_embeddings(model.weight.data.cpu().numpy(),
                              labels=dataset.Y,
                              centroids=torch.cat(barycenters),
                              save_folder="LOG/mean",
                              file_name="LFR_hierachical.png")
print(barycenters)
barycenters = []
for i in range(13):
    barycenters.append(
        model.weight.data[weigths[:, i] == 1].mean(0).cpu().unsqueeze(0))

print(barycenters)
plot_poincare_disc_embeddings(model.weight.data.cpu().numpy(),
                              labels=dataset.Y,
                              centroids=torch.cat(barycenters),
                              save_folder="LOG/mean",
                              file_name="LFR_hierarchical_euclidean.png")
                        batch_size=5,
                        shuffle=True,
                        collate_fn=collate_fn_simple)

model = nn.Embedding(len(X), 2, max_norm=0.999)
model.weight.data[:] = model.weight.data * 1e-2

manifold = PoincareBallExact
optimizer = rsgd.RSGD(model.parameters(), 1e-1, manifold=manifold)
default_gt = torch.zeros(20).long()
criterion = nn.CrossEntropyLoss(reduction="sum")

for i in range(50):
    tloss = 0.
    for x, y in dataloader:
        optimizer.zero_grad()
        pe_x = model(x.long())
        pe_y = model(y.long())
        ne = model((torch.rand(len(x), 10) * len(X)).long()).detach()
        loss = graph_embedding_criterion(pe_x, pe_y, z=ne,
                                         manifold=manifold).sum()
        tloss += loss.item()
        loss.backward()
        optimizer.step()
    print('Loss value for iteration ', i, ' is ', tloss)

plot_poincare_disc_embeddings(model.weight.data.numpy(),
                              labels=dataset.Y,
                              save_folder="LOG/second_order",
                              file_name="LFR_second_order_joint.png")
Ejemplo n.º 4
0
from rcome.visualisation_tools.plot_tools import plot_poincare_disc_embeddings
from rcome.function_tools import poincare_alg as pa
from matplotlib import pyplot as plt

data_point = (torch.randn(50, 2) / 5 + 0.6)
print(data_point)
norms = data_point.norm(2, -1)
data_point[norms >= 1.] = torch.einsum('ij, i -> ij', data_point[norms >= 1.],
                                       1 / (norms[norms >= 1.] + 1e-5))
data_point.norm(2, -1)

barycenter_hyperbolic = pa.barycenter(data_point, verbose=True)

print(barycenter_hyperbolic)
plot_poincare_disc_embeddings(data_point.numpy(),
                              close=False,
                              save_folder="LOG/mean",
                              file_name="example_mean_hyperbolic.png")

barycenter_euclidean = data_point.mean(0, keepdim=True)

plt.scatter(barycenter_hyperbolic[:, 0],
            barycenter_hyperbolic[:, 1],
            marker='D',
            s=300.,
            c='red',
            label="Hyperbolic")
plt.scatter(barycenter_euclidean[:, 0],
            barycenter_euclidean[:, 1],
            marker='D',
            s=300.,
            c='green',
# negative sampling distribution
frequency = dataset.getFrequency()
idx = frequency[:,0].sort()[1]
frequency = frequency[idx]**(3/4)
frequency[:,1] /= frequency[:,1].sum()

distribution =  CategoricalDistributionSampler(frequency[:,1])


for i in range(50):
    tloss = 0.
    for x, y in dataloader:
        optimizer.zero_grad()
        pe_x = model(x.long())
        pe_y = model_context(y.long())
        ne = model_context(distribution.sample(sample_shape=(len(x), 10))).detach()
        loss = graph_embedding_criterion(pe_x, pe_y, z=ne, manifold=manifold).sum()
        tloss += loss.item()
        loss.backward()
        optimizer.step()
    print('Loss value for iteration ', i,' is ', tloss)


plot_poincare_disc_embeddings(model.weight.data.numpy(),
                              labels=dataset.Y,
                              draw_circle=False,
                              save_folder="LOG/second_order",
                              file_name="LFR_second_order_negative_euclidean.png")

torch.save(model.weight.data, "LOG/second_order/euclidean_representation.pth")
Ejemplo n.º 6
0
dataset = corpora.NeigbhorFlatCorpus(X, Y)
dataloader = DataLoader(dataset, batch_size=5, shuffle=True)

model = nn.Embedding(len(X), 2, max_norm=0.999)
model.weight.data[:] = model.weight.data * 1e-2
model.cuda()

manifold = PoincareBallExact
optimizer = rsgd.RSGD(model.parameters(), 1e-1, manifold=manifold)
default_gt = torch.zeros(20).long()
criterion = nn.CrossEntropyLoss(reduction="sum")


for i in range(50):
    tloss = 0.
    for x, y in dataloader:

        optimizer.zero_grad()
        pe_x = model(x.long().cuda())
        pe_y = model(y.long().cuda())
        ne = model((torch.rand(len(x), 10) * len(X)).long().cuda())
        loss = tree_embedding_criterion(pe_x, pe_y, z=ne, manifold=manifold).sum()
        tloss += loss.item()
        loss.backward()
        optimizer.step()
    print('Loss value for iteration ', i,' is ', tloss)


plot_poincare_disc_embeddings(model.weight.data.cpu().numpy(), labels=dataset.Y, save_folder="LOG/first_order", file_name="DBLP_hierarchical.png")
Ejemplo n.º 7
0
                                         manifold=manifold).sum()
        tloss += loss.item()
        loss.backward()
        optimizer.step()
    print('Loss value for iteration ', i, ' is ', tloss)

em_alg = poincare_em.PoincareEM(13)
em_alg.fit(model.weight.data)

NF = em_alg.get_normalisation_coef()
pi, mu, sigma = em_alg.get_parameters()
pik = em_alg.get_pik(model.weight.data)

plot_poincare_disc_embeddings(model.weight.data.numpy(),
                              labels=dataset.Y,
                              save_folder="LOG/community_loss",
                              file_name="LFR_before_community_loss.png",
                              centroids=mu)

optimizer = rsgd.RSGD(model.parameters(), 2e-2, manifold=manifold)

dataset_o3 = corpora_tools.from_indexable(
    torch.arange(0, len(X), 1).unsqueeze(-1))
dataloader_o3 = DataLoader(dataset_o3,
                           batch_size=10,
                           shuffle=True,
                           drop_last=False)

dataset_o1 = corpora.NeigbhorFlatCorpus(X, Y)
dataloader_o1 = DataLoader(dataset_o1,
                           batch_size=10,