-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
96 lines (73 loc) · 2.6 KB
/
main.py
File metadata and controls
96 lines (73 loc) · 2.6 KB
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
96
# from torch_geometric.data import Data
from torch_geometric.loader import DataLoader
# from torch_geometric.datasets import Planetoid
import torch_geometric.datasets.citation_full as citation_full
import torch
# import dgl
import torch.nn as nn
from gat import GAT
def main() -> None:
"""Main function for training GAT on the CORA dataset.
"""
# Get CORA dataset from DGL
# Create DataLoader
# open ./data/cora/cora.content
# open ./data/cora/cora.cites
# Create DataLoader
dataset = citation_full.CitationFull(root='./data/CoraML', name='cora_ML')
dataloader = DataLoader(dataset, batch_size=32, shuffle=True)
# Create model, try setting to cuda, cpu if not available
device = torch.device('cpu')
# device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
model = GAT(dataset.num_features, dataset.num_classes).to(device)
# Define loss function and optimizer
criterion = nn.CrossEntropyLoss()
optimizer = torch.optim.Adam(model.parameters(), lr=0.005, weight_decay=0.0005)
# Check if there is a save in the saves/ directory, if so load it
try:
model.load_state_dict(torch.load("saves/gat.pth"))
print("Loaded model from saves/gat.pth")
except FileNotFoundError:
print("No save found, training from scratch")
print("Training")
# Training loop
initial = 0
best = 100000000000
# Look at data
for data in dataloader:
freq = {}
for i in data.y:
if i.item() in freq:
freq[i.item()] += 1
else:
freq[i.item()] = 1
print(freq)
for epoch in range(100):
total_loss = 0
for data in dataloader:
# data = data.to(device)
# Convert targets to float
optimizer.zero_grad()
output = model.forward(data)
loss = criterion(output, data.y)
loss.backward()
optimizer.step()
total_loss += loss.item()
if total_loss < best:
best = total_loss
if initial == 0:
initial = total_loss
print(f"Epoch {epoch+1}: Loss = {total_loss} (Initial = {initial}, Best = {best})")
# Open saves directory and save GAT model
torch.save(model.state_dict(), "saves/gat.pth")
model.eval()
for data in dataloader:
output = model.forward(data)
output = torch.argmax(output, 1)
correct = 0
for i in range(len(output)):
if output[i] == data.y[i]:
correct += 1
print(f"Accuracy: {100*correct/len(output)}%")
return
main()