File size: 4,163 Bytes
43c53fb
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
import argparse
import os

import dgl
import torch.utils.data
from dgl.dataloading import GraphDataLoader
from torch import optim
from tqdm import tqdm
from QM9_dataset_class import PreprocessedQM9Dataset
from model import SimpleGnn, GMae
import torch.nn as nn

def train_epoch(epoch, graphLoader: torch.utils.data.DataLoader,

                model: nn.Module,device, optimizer:torch.optim.Optimizer,

                save_dir:str

                ):
    print(f"epoch {epoch} started!")
    model.train()
    model.encoder.train()
    model.decoder.train()
    model.to(device)
    loss_epoch = 0
    for batch in tqdm(graphLoader):
        optimizer.zero_grad()
        batch_g, _ = batch
        R = batch_g.ndata["R"].to(device)
        # Z_index = batch_g.ndata["Z_index"].to(device)
        Z_index = batch_g.ndata["Z_index"].to(device)
        Z_emb = model.encode_atom_index(Z_index)
        feat = torch.cat([R, Z_emb], dim=1)
        batch_g = batch_g.to(device)
        loss = model.mask_attr_prediction(batch_g, feat)
        loss.backward()
        optimizer.step()
        loss_epoch += loss.item()
    return loss_epoch


def train_loop(dataset_path, epochs, batch_size,device,save_dir):
    device = torch.device(device)
    dataset = PreprocessedQM9Dataset(None)
    dataset.load_dataset(dataset_path)
    print("Dataset loaded:", dataset_path, "Total samples:", len(dataset))
    print("Initializing dataloader")
    myGLoader = GraphDataLoader(dataset, batch_size=batch_size, pin_memory=True,shuffle=False)
    sage_enc = SimpleGnn(in_feats=7, hid_feats=4, out_feats=4) # 7 = R_dim(3)+Z_embedding_dim(4)
    sage_dec = SimpleGnn(in_feats=4, hid_feats=4, out_feats=7)
    gmae = GMae(sage_enc, sage_dec, 7, 4, 7, replace_rate=0)
    optimizer = optim.Adam(gmae.parameters(), lr=1e-3)
    print("Start training", "epochs:", epochs, "batch_size:", batch_size)
    for epoch in range(epochs):
        loss_epoch = train_epoch(epoch, myGLoader,gmae,device,optimizer,save_dir)
        formatted_loss_epoch = f"{loss_epoch:.3f}"
        save_path = os.path.join(save_dir,f"epoch_{epoch}",f"gmae_{formatted_loss_epoch}.pt")
        save_subdir = os.path.dirname(save_path)
        if not os.path.exists(save_subdir):
            os.makedirs(save_subdir, exist_ok=True)
        torch.save(gmae.state_dict(), save_path)
        print(f"Epoch:{epoch},loss:{loss_epoch},Model saved:{save_path}")
        with torch.no_grad():
            embedded_graphs = []
            print(f"Epoch:{epoch},start embedding")
            gmae.eval()
            gmae.encoder.eval()
            for batch in tqdm(myGLoader):
                batch_g, _ = batch
                R = batch_g.ndata["R"].to(device)
                Z_index = batch_g.ndata["Z_index"].to(device)
                Z_emb = gmae.encode_atom_index(Z_index)
                feat = torch.cat([R, Z_emb], dim=1)
                batch_g = batch_g.to(device)
                batch_g.ndata["embedding"] = gmae.embed(batch_g,feat)
                unbatched_graphs = dgl.unbatch(batch_g)
                embedded_graphs.extend(unbatched_graphs)
            for idx,embedded_graph in enumerate(embedded_graphs):
                embeddings_save_path = os.path.join(save_dir, f"epoch_{epoch}", f"embedding_{idx}.dgl")
                dgl.save_graphs(embeddings_save_path, [embedded_graph])
            print(f"epoch:{epoch},embedding saved:{embeddings_save_path},total_graphs:{len(embedded_graphs)}")



def main():
    parser = argparse.ArgumentParser(description="Prepare QM9 dataset")
    parser.add_argument('--dataset_path', type=str, default='dataset/QM9_dataset_processed.pt')
    parser.add_argument('--batch_size', type=int, default=4)
    parser.add_argument('--epochs', type=int, default=10, help='number of epochs')
    parser.add_argument("--device", type=str, default='cuda:0')
    parser.add_argument("--save_dir", type=str, default='./model')
    args = parser.parse_args()
    train_loop(args.dataset_path, args.epochs, args.batch_size,args.device,args.save_dir)


if __name__ == '__main__':
    main()