Example #1
0
def main():
    # Parameters for Stochastic block model graph
    # Todal of 1000 nodes
    node_num = 1000
    # Test with two communities
    community_num = 2
    # At each iteration migrate 10 nodes from one community to the another
    node_change_num = 10
    # Length of total time steps the graph will dynamically change
    length = 7
    # output directory for result
    outdir = './output'
    intr = './intermediate'
    if not os.path.exists(outdir):
        os.mkdir(outdir)
    if not os.path.exists(intr):
        os.mkdir(intr)
    testDataType = 'sbm_cd'
    # Generate the dynamic graph
    dynamic_sbm_series = list(sbm.get_community_diminish_series_v2(node_num,
                                                                   community_num,
                                                                   length,
                                                                   1,  # comminity ID to perturb
                                                                   node_change_num))
    graphs = [g[0] for g in dynamic_sbm_series]
    # parameters for the dynamic embedding
    # dimension of the embedding
    dim_emb = 8
    lookback = 2

    # dynAERNN
    embedding = DynAERNN(d=dim_emb,
                         beta=5,
                         n_prev_graphs=lookback,
                         nu1=1e-6,
                         nu2=1e-6,
                         n_aeunits=[500, 300],
                         n_lstmunits=[500, dim_emb],
                         rho=0.3,
                         n_iter=2,
                         xeta=1e-3,
                         n_batch=100,
                         modelfile=['./intermediate/enc_model_dynAERNN.json',
                                    './intermediate/dec_model_dynAERNN.json'],
                         weightfile=['./intermediate/enc_weights_dynAERNN.hdf5',
                                     './intermediate/dec_weights_dynAERNN.hdf5'],
                         savefilesuffix="testing")

    embs = []
    t1 = time()
    for temp_var in range(lookback + 1, length + 1):
        emb, _ = embedding.learn_embeddings(graphs[:temp_var])
        embs.append(emb)
    print(embedding._method_name + ':\n\tTraining time: %f' % (time() - t1))
    plt.figure()
    plt.clf()
    plot_dynamic_sbm_embedding.plot_dynamic_sbm_embedding_v2(embs[-5:-1], dynamic_sbm_series[-5:])
    plt.show()
Example #2
0
def main():
    # data_list = ['cellphone', 'enron', 'fbmessages', 'HS11', 'HS12', 'primary', 'workplace']
    data_list = ['bitcoin_alpha', 'bitcoin_otc', 'college_msg', 'enron_all', 'enron_all_shuffle']
    funcs = ['AE', 'AERNN']
    for data in data_list:
        graphs = process('data/' + data)
        length = len(graphs)
        dim_emb = 128
        lookback = 3

        for func in funcs:
            MAP_list = []
            for i in range(length - lookback - 1):
                if func == 'AERNN':
                    embedding = DynAERNN(d=dim_emb,
                                         beta=5,
                                         n_prev_graphs=lookback,
                                         nu1=1e-6,
                                         nu2=1e-6,
                                         n_aeunits=[500, 300],
                                         n_lstmunits=[500, dim_emb],
                                         rho=0.3,
                                         n_iter=250,
                                         xeta=1e-3,
                                         n_batch=100,
                                         modelfile=None,
                                         weightfile=None,
                                         savefilesuffix=None)
                elif func == 'RNN':
                    embedding = DynRNN(d=dim_emb,
                                       beta=5,
                                       n_prev_graphs=lookback,
                                       nu1=1e-6,
                                       nu2=1e-6,
                                       n_enc_units=[500, 300],
                                       n_dec_units=[500, 300],
                                       rho=0.3,
                                       n_iter=250,
                                       xeta=1e-3,
                                       n_batch=100,
                                       modelfile=None,
                                       weightfile=None,
                                       savefilesuffix=None)
                else:
                    embedding = DynAE(d=dim_emb,
                                      beta=5,
                                      n_prev_graphs=lookback,
                                      nu1=1e-6,
                                      nu2=1e-6,
                                      n_units=[500, 300, ],
                                      rho=0.3,
                                      n_iter=250,
                                      xeta=1e-4,
                                      n_batch=100,
                                      modelfile=None,
                                      weightfile=None,
                                      savefilesuffix=None)

                embs = []
                t1 = time()
                # for temp_var in range(lookback + 1, length + 1):
                emb, _ = embedding.learn_embeddings(graphs[i: i + lookback + 1])
                embs.append(emb)
                print(embedding._method_name + ':\n\tTraining time: %f' % (time() - t1))
                pred_adj = graphify(embedding.predict_next_adj())
                edge_index_pre = evaluation_util.getEdgeListFromAdjMtx(adj=pred_adj)
                MAP = metrics.computeMAP(edge_index_pre, graphs[i + lookback + 1])
                MAP_list.append(MAP)
                print('第' + str(i) + '-' + str(i + lookback) + '个时间片的MAP值为' + str(MAP))
            MAP_list.append(np.mean(MAP_list))
            result = {'MAP值': MAP_list}
            label = []
            for i in range(len(MAP_list) - 1):
                row = '第' + str(i) + '-' + str(i + lookback) + '个时间片'
                label.append(row)
            label.append('mean_MAP')
            if not os.path.exists('result/' + data):
                os.mkdir('result/' + data)
            csv_path = 'result/' + data + '/' + str(func) + '.csv'
            df = pd.DataFrame(result, index=label)
            df.to_csv(csv_path)
Example #3
0
                                                                           node_change_num)
     dynamic_embedding = DynAERNN(
         d=100,
         beta=100,
         n_prev_graphs=lookback,
         nu1=1e-6,
         nu2=1e-6,
         n_units=[50, 30, ],
         rho=0.3,
         n_iter=30,
         xeta=0.005,
         n_batch=50,
         modelfile=['./intermediate/enc_model.json', './intermediate/dec_model.json'],
         weightfile=['./intermediate/enc_weights.hdf5', './intermediate/dec_weights.hdf5'],
     )
     dynamic_embedding.learn_embeddings([g[0] for g in dynamic_sbm_series])
     plot_dynamic_sbm_embedding.plot_dynamic_sbm_embedding(dynamic_embedding.get_embeddings(), dynamic_sbm_series)
     plt.savefig('result/visualization_DynRNN_rp.png')
     plt.show()
 elif args.testDataType == 'sbm_cd':
     node_num = 100
     community_num = 2
     node_change_num = args.nodemigration
     dynamic_sbm_series = dynamic_SBM_graph.get_community_diminish_series_v2(node_num,
                                                                             community_num, length, 1,
                                                                             node_change_num)
     dynamic_embedding = DynAERNN(
         d=dim_emb,
         beta=5,
         n_prev_graphs=lookback,
         nu1=1e-6,
def plot_dynam_graph(title, graph_list):
    #dynAERNN
    graphs = []
    for graph in graph_list:
        if not graph == None:
            graphs.append(graph)
    dynamic_sbm_series = graphs
    outdir = os.path.exists(os.path.dirname(__file__)+"/out")
    testDataType = 'sbm_cd'
    length = len(graph_list)
    dim_emb  = 128
    lookback = 2
    embedding = DynAERNN(d   = dim_emb,
                beta           = 5,
                n_prev_graphs  = lookback,
                nu1            = 1e-6,
                nu2            = 1e-6,
                n_aeunits      = [500, 300],
                n_lstmunits    = [500,dim_emb],
                rho            = 0.3,
                n_iter         = 250,
                xeta           = 1e-3,
                n_batch        = 100,
                modelfile      = None, #['./intermediate/enc_model_dynAERNN.json', 
                                #'./intermediate/dec_model_dynAERNN.json'],
                weightfile     = None, #['./intermediate/enc_weights_dynAERNN.hdf5', 
                                #'./intermediate/dec_weights_dynAERNN.hdf5'],
                savefilesuffix = None) #"testing")

    embs = []
    t1 = time()
    for temp_var in range(lookback+1, length+1):
                    emb, _ = embedding.learn_embeddings(graphs[:temp_var])
                    embs.append(emb)
    print (embedding._method_name+':\n\tTraining time: %f' % (time() - t1))
    plt.figure()
    plt.clf()    
    plot_dynamic_sbm_embedding.plot_dynamic_sbm_embedding_v2(embs[-5:-1], dynamic_sbm_series[-5:])    
    plt.show()

    # #dynamicTriad
    datafile  = dataprep_util.prep_input_dynTriad(graphs, length, testDataType)
    embedding= dynamicTriad(niters     = 20,
                    starttime  = 0,
                    datafile   = datafile,
                    batchsize  = 1000,
                    nsteps     = length,
                    embdim     = dim_emb,
                    stepsize   = 1,
                    stepstride = 1,
                    outdir     = outdir,
                    cachefn    = '/tmp/'+ testDataType,
                    lr         = 0.1,
                    beta       = [0.1,0.1],
                    negdup     = 1,
                    datasetmod = 'core.dataset.adjlist',
                    trainmod   = 'dynamicgem.dynamictriad.core.algorithm.dynamic_triad',
                    pretrain_size = length,
                    sampling_args = {},
                    validation = 'link_reconstruction',
                    datatype   = testDataType,
                    scale      = 1,
                    classifier = 'lr',
                    debug      = False,
                    test       = 'link_predict',
                    repeat     = 1,
                    resultdir  = outdir,
                    testDataType = testDataType,
                    clname       = 'lr')
                    #node_num     = node_num )
    t1 = time()
    embedding.learn_embedding()
    print (embedding._method_name+':\n\tTraining time: %f' % (time() - t1))
    embedding.get_embedding()
    embedding.plotresults(dynamic_sbm_series)