Skip to content

Commit a5952fb

Browse files
committed
modified code, added draft of computational experiment
1 parent 5bb5abe commit a5952fb

11 files changed

+843
-756
lines changed

Udeneev2025Surrogate.pdf

194 KB
Binary file not shown.

code/GCN.py

+27-28
Original file line numberDiff line numberDiff line change
@@ -37,17 +37,20 @@ def __init__(
3737
self.gc1 = GCNConv(input_dim, hidden_dim)
3838
self.gc2 = GCNConv(hidden_dim, hidden_dim)
3939
self.graph_norm = GraphNorm(hidden_dim)
40+
self.layer_norm = nn.LayerNorm(hidden_dim)
4041
self.dropout = nn.Dropout(dropout)
4142
self.pooling = pooling
4243
self.fc = nn.Linear(hidden_dim, embedding_dim)
4344

4445
def forward(self, x, edge_index):
4546
x = F.relu(self.gc1(x, edge_index))
4647
x = self.graph_norm(x)
48+
x = self.layer_norm(x)
4749
x = self.dropout(x)
4850

4951
x = F.relu(self.gc2(x, edge_index))
5052
x = self.graph_norm(x)
53+
x = self.layer_norm(x)
5154
x = self.dropout(x)
5255

5356
# Глобальное агрегирование узловых признаков для получения представления всего графа
@@ -74,22 +77,25 @@ def __init__(self, input_dim, output_dim=16, dropout=0.5, pooling="max"):
7477

7578
self.input_dim = input_dim
7679
self.output_dim = output_dim
80+
self.hidden_dim = 64 # базовая размерность скрытого слоя
7781

78-
self.gc1 = GCNConv(input_dim, 128)
79-
self.gc2 = GCNConv(128, 256)
80-
self.gc3 = GCNConv(256, 64)
81-
self.layer_norm = nn.LayerNorm(64)
82-
self.fc = nn.Linear(64, output_dim)
83-
self.dropout = nn.Dropout(dropout)
84-
self.pooling = pooling
85-
86-
self.key = nn.Linear(256, 256)
87-
self.query = nn.Linear(256, 256)
82+
self.gc1 = GCNConv(input_dim, self.hidden_dim)
83+
self.gc2 = GCNConv(self.hidden_dim, 256)
84+
self.gc3 = GCNConv(256, 512)
85+
self.gc4 = GCNConv(512, self.hidden_dim)
8886

8987
self.residual_proj = (
90-
nn.Linear(input_dim, 64) if input_dim != 64 else nn.Identity()
88+
nn.Linear(input_dim, self.hidden_dim) if input_dim != self.hidden_dim else nn.Identity()
9189
)
9290

91+
self.layer_norm = nn.LayerNorm(self.hidden_dim)
92+
self.dropout = nn.Dropout(dropout)
93+
self.pooling = pooling
94+
95+
self.fc1 = nn.Linear(self.hidden_dim, self.hidden_dim)
96+
self.fc_norm = nn.LayerNorm(self.hidden_dim)
97+
self.fc2 = nn.Linear(self.hidden_dim, output_dim)
98+
9399
def forward(self, x, edge_index):
94100
residual = self.residual_proj(x)
95101

@@ -99,22 +105,13 @@ def forward(self, x, edge_index):
99105
x = F.leaky_relu(self.gc2(x, edge_index))
100106
x = self.dropout(x)
101107

102-
keys = self.key(x) # [N, 128]
103-
queries = self.query(x) # [N, 128]
104-
attn_scores = torch.mm(queries, keys.T) # [N, N]
105-
106-
row, col = edge_index
107-
mask = torch.zeros_like(attn_scores)
108-
mask[row, col] = 1
109-
attn_scores = attn_scores * mask
110-
attn_scores = F.softmax(attn_scores, dim=-1)
111-
112-
x = torch.mm(attn_scores, x) # [N, N] * [N, 128] → [N, 128]
113-
114108
x = F.leaky_relu(self.gc3(x, edge_index))
115109
x = self.dropout(x)
116110

117-
x = self.layer_norm(x + residual) # LayerNorm
111+
x = F.leaky_relu(self.gc4(x, edge_index))
112+
x = self.dropout(x)
113+
114+
x = self.layer_norm(x + residual)
118115

119116
if self.pooling == "max":
120117
x = torch.max(x, dim=0).values
@@ -125,9 +122,13 @@ def forward(self, x, edge_index):
125122
else:
126123
raise ValueError("Unsupported pooling method. Use 'max', 'mean' or 'sum'.")
127124

128-
x = self.fc(x)
125+
x = self.fc1(x)
126+
x = self.fc_norm(x)
127+
x = F.leaky_relu(x)
128+
x = self.fc2(x)
129+
129130
if self.output_dim == 1:
130-
x = nn.Sigmoid()(x)
131+
x = torch.sigmoid(x)
131132
return x
132133

133134

@@ -373,7 +374,6 @@ def train_model_diversity(
373374
marker="o",
374375
label="Valid Loss",
375376
)
376-
plt.title("Training and Validation Loss Over Epochs")
377377
plt.xlabel("Epoch")
378378
plt.ylabel("Loss")
379379
plt.grid(True)
@@ -481,7 +481,6 @@ def train_model_accuracy(
481481
marker="o",
482482
label="Valid Loss",
483483
)
484-
plt.title("Training and Validation Loss Over Epochs")
485484
plt.xlabel("Epoch")
486485
plt.ylabel("Loss")
487486
plt.ylim(0, 0.002)

code/dependecies.zip

5.63 KB
Binary file not shown.

code/gcn-training.ipynb

+816-728
Large diffs are not rendered by default.
Loading
-17 Bytes
Loading
10.6 KB
Loading

code/weights.zip

1.32 MB
Binary file not shown.
Binary file not shown.
Binary file not shown.

figures/surrogate_arch.png

48.1 KB
Loading

0 commit comments

Comments
 (0)