-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGATNet.py
More file actions
87 lines (84 loc) · 4.02 KB
/
GATNet.py
File metadata and controls
87 lines (84 loc) · 4.02 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
import pickle
import torch
import torch.nn.functional as F
import torch_geometric.transforms as T
from torch_scatter import scatter_mean
from torch_geometric.datasets import Planetoid
from torch_geometric.nn import GCNConv, GATConv
from torch_geometric.loader import DataLoader
from GAT import GraphAttentionLayer
class GATNet(torch.nn.Module):
def __init__(self, model_name, dataset_name, num_features):
super().__init__()
self.dataset_name = dataset_name
self.model_name = model_name
if model_name == 'GAT':
if dataset_name == "CIFAR10":
self.conv1 = GraphAttentionLayer(num_features, 8, num_heads=8, concat=True, dropout=0.0)
self.conv2 = GraphAttentionLayer(64,8,num_heads=8,concat=True,dropout=0.0)
self.lin1 = torch.nn.Linear(64,64)
self.lin2 = torch.nn.Linear(64,10)
elif dataset_name == "Cora":
self.conv1 = GraphAttentionLayer(num_features, 8, num_heads=8, concat=True, dropout=0.6)
self.conv2 = GraphAttentionLayer(64, 7, num_heads=1, concat=False, dropout=0.6)
elif dataset_name == "Citeseer":
self.conv1 = GraphAttentionLayer(num_features, 8, num_heads=8, concat=True, dropout=0.6)
self.conv2 = GraphAttentionLayer(64, 6, num_heads=1, concat=False, dropout=0.6)
elif dataset_name == "Pubmed":
self.conv1 = GraphAttentionLayer(num_features, 8, num_heads=8, concat=True, dropout=0.6)
self.conv2 = GraphAttentionLayer(64, 3, num_heads=8, concat=False, dropout=0.6)
elif dataset_name == "AmazonComp":
self.conv1 = GraphAttentionLayer(num_features, 8, num_heads=8, concat=True, dropout=0.6)
self.conv2 = GraphAttentionLayer(64, 10, num_heads=8, concat=False, dropout=0.6)
elif dataset_name == "AmazonPhotos":
self.conv1 = GraphAttentionLayer(num_features, 8, num_heads=8, concat=True, dropout=0.6)
self.conv2 = GraphAttentionLayer(64, 8, num_heads=8, concat=False, dropout=0.6)
elif model_name == 'GCN':
if self.dataset_name == "CIFAR10":
self.conv1 = GCNConv(num_features,64)
self.conv2 = GCNConv(64,64)
self.lin1 = torch.nn.Linear(64,64)
self.lin2 = torch.nn.Linear(64,10)
elif dataset_name == "Cora":
self.conv1 = GCNConv(num_features, 64)
self.conv2 = GCNConv(64, 7)
elif dataset_name == "Citeseer":
self.conv1 = GCNConv(num_features, 64)
self.conv2 = GCNConv(64, 6)
elif dataset_name == "Pubmed":
self.conv1 = GCNConv(num_features, 64)
self.conv2 = GCNConv(64, 3)
elif dataset_name == "AmazonComp":
self.conv1 = GCNConv(num_features, 64)
self.conv2 = GCNConv(64, 10)
elif dataset_name == "AmazonPhotos":
self.conv1 = GCNConv(num_features, 64)
self.conv2 = GCNConv(64, 8)
def forward(self, data):
x, edge_index = data.x, data.edge_index
if self.dataset_name == "CIFAR10":
x = self.conv1(x, edge_index)
if self.model_name == "GCN":
x = F.relu(x)
else:
x = F.elu(x)
x = self.conv2(x, edge_index)
if self.model_name == "GCN":
x = F.relu(x)
else:
x = F.elu(x)
x = scatter_mean(x, data.batch, dim=0)
x = F.relu(self.lin1(x))
x = F.log_softmax(self.lin2(x), dim=1)
return x
else:
x = F.dropout(x, p=0.6, training=self.training)
x = self.conv1(x, edge_index)
if self.model_name == "GCN":
x = F.relu(x)
else:
x = F.elu(x)
x = F.dropout(x, p=0.6, training=self.training)
x = self.conv2(x, edge_index)
x = F.log_softmax(x, dim=1)
return x