HUANGYIFEI's picture
Add files using upload-large-folder tool
43c53fb verified
raw
history blame
4.16 kB
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()