Ejemplo n.º 1
0
    def test_save_load(self):
        """
        Test if saving and loading the model in a new object gives the same results
        """
        filename = os.getcwd() + "/data/test_save_load"
        graph = gb.create_directed_barbell(4, 4)
        gae = GraphAutoEncoder(graph,
                               learning_rate=0.01,
                               support_size=[5, 5],
                               dims=[3, 5, 7, 6, 2],
                               batch_size=12,
                               max_total_steps=50,
                               verbose=True)
        gae.fit(graph)
        embed = gae.calculate_embeddings()
        gae.save_model(filename)

        gae2 = GraphAutoEncoder(graph,
                                learning_rate=0.01,
                                support_size=[5, 5],
                                dims=[3, 5, 7, 6, 2],
                                batch_size=12,
                                max_total_steps=50,
                                verbose=True)
        gae2.load_model(filename, graph)
        embed2 = gae2.calculate_embeddings()

        embed3 = np.subtract(embed, embed2)
        self.assertAlmostEqual(
            np.sum(embed3), 0, 4,
            "loaded model gives different result then original")
Ejemplo n.º 2
0
        d['weight'] = ndic[u] * ndic[v]

#%% create and train model
gae = GraphAutoEncoder(graph, learning_rate=0.01, support_size=[5, 5], dims=[3, 5, 7, 6, 2],
                       batch_size=30, max_total_steps=1000, verbose=True, act=tf.nn.tanh)
if TRAIN:
    train_res = {}
    for i in range(len(gae.dims)):
        if i in [1, 2]:
            train_res["l"+str(i+1)] = gae.train_layer(i+1, dropout=0.1)
        else:
            train_res["l"+str(i+1)] = gae.train_layer(i+1)

    train_res['all'] = gae.train_layer(len(gae.dims), all_layers=True)
    pickle.dump(train_res, open(RESULTS_FILE, "wb"))
    gae.save_model(MODEL_FILENAME)
else:
    gae.load_model(MODEL_FILENAME, graph)

embed = gae.calculate_embeddings()


# %% get tabel with node details
indeg = graph.in_degree()
outdeg = graph.out_degree()
tbl = np.array([[y, x['label1'], x['label2'], indeg[y], outdeg[y], embed[y, 1], embed[y, 2]]
                for y, x in graph.nodes(data=True)])
pd_tbl = pd.DataFrame(tbl[:, 1:], tbl[:, 0],
                      ['label1', 'label2', 'in_degree', 'out_degree', 'embed1', 'embed2'])
print(pd_tbl)