File size: 17,256 Bytes
484e3bc |
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 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 |
"""
Graph Neural Networks for Causal Graphs and Geopolitical Networks
Implements GNNs for:
- Alliance and trade network analysis
- Causal graph representation learning
- Message passing on DAGs
- Attention mechanisms for influence propagation
- Graph classification and regression
Respects identifiability and invariance constraints from causal theory.
"""
import numpy as np
from typing import List, Dict, Tuple, Optional, Callable
import networkx as nx
try:
import torch
import torch.nn as nn
import torch.nn.functional as F
HAS_TORCH = True
except ImportError:
HAS_TORCH = False
print("PyTorch not available. GNN functionality limited.")
try:
import torch_geometric
from torch_geometric.nn import GCNConv, GATConv, MessagePassing
from torch_geometric.data import Data
HAS_TORCH_GEOMETRIC = True
except ImportError:
HAS_TORCH_GEOMETRIC = False
print("torch_geometric not available. Install with: pip install torch-geometric")
if HAS_TORCH:
class CausalGNN(nn.Module):
"""
Graph Neural Network for causal graphs.
Respects causal ordering (topological) and propagates information
along causal edges.
"""
def __init__(
self,
node_features: int,
hidden_dim: int,
output_dim: int,
num_layers: int = 2,
attention: bool = False
):
"""
Initialize Causal GNN.
Parameters
----------
node_features : int
Dimension of input node features
hidden_dim : int
Hidden layer dimension
output_dim : int
Output dimension
num_layers : int
Number of GNN layers
attention : bool
Use attention mechanism (GAT)
"""
super(CausalGNN, self).__init__()
self.num_layers = num_layers
self.attention = attention
# Input layer
if attention and HAS_TORCH_GEOMETRIC:
self.conv1 = GATConv(node_features, hidden_dim, heads=4, concat=True)
self.convs = nn.ModuleList([
GATConv(hidden_dim * 4, hidden_dim, heads=4, concat=True)
for _ in range(num_layers - 2)
])
self.conv_final = GATConv(hidden_dim * 4, output_dim, heads=1, concat=False)
elif HAS_TORCH_GEOMETRIC:
self.conv1 = GCNConv(node_features, hidden_dim)
self.convs = nn.ModuleList([
GCNConv(hidden_dim, hidden_dim)
for _ in range(num_layers - 2)
])
self.conv_final = GCNConv(hidden_dim, output_dim)
else:
# Fallback to simple linear layers
self.linear1 = nn.Linear(node_features, hidden_dim)
self.linears = nn.ModuleList([
nn.Linear(hidden_dim, hidden_dim)
for _ in range(num_layers - 2)
])
self.linear_final = nn.Linear(hidden_dim, output_dim)
def forward(self, data):
"""
Forward pass.
Parameters
----------
data : torch_geometric.data.Data
Graph data with x (node features) and edge_index
Returns
-------
torch.Tensor
Node embeddings
"""
if HAS_TORCH_GEOMETRIC:
x, edge_index = data.x, data.edge_index
# First layer
x = self.conv1(x, edge_index)
x = F.relu(x)
x = F.dropout(x, p=0.2, training=self.training)
# Hidden layers
for conv in self.convs:
x = conv(x, edge_index)
x = F.relu(x)
x = F.dropout(x, p=0.2, training=self.training)
# Output layer
x = self.conv_final(x, edge_index)
else:
x = data.x
x = self.linear1(x)
x = F.relu(x)
x = F.dropout(x, p=0.2, training=self.training)
for linear in self.linears:
x = linear(x)
x = F.relu(x)
x = F.dropout(x, p=0.2, training=self.training)
x = self.linear_final(x)
return x
class GeopoliticalNetworkGNN(nn.Module):
"""
GNN for geopolitical networks (alliances, trade, etc.).
Models influence propagation and network effects.
"""
def __init__(
self,
node_features: int,
edge_features: int,
hidden_dim: int,
output_dim: int
):
"""
Initialize geopolitical network GNN.
Parameters
----------
node_features : int
Node feature dimension
edge_features : int
Edge feature dimension
hidden_dim : int
Hidden dimension
output_dim : int
Output dimension
"""
super(GeopoliticalNetworkGNN, self).__init__()
if HAS_TORCH_GEOMETRIC:
self.conv1 = GCNConv(node_features, hidden_dim)
self.conv2 = GCNConv(hidden_dim, hidden_dim)
self.conv3 = GCNConv(hidden_dim, output_dim)
else:
self.linear1 = nn.Linear(node_features, hidden_dim)
self.linear2 = nn.Linear(hidden_dim, hidden_dim)
self.linear3 = nn.Linear(hidden_dim, output_dim)
# Edge feature processing
self.edge_mlp = nn.Sequential(
nn.Linear(edge_features, hidden_dim),
nn.ReLU(),
nn.Linear(hidden_dim, hidden_dim)
)
def forward(self, data):
"""
Forward pass.
Parameters
----------
data : Data
Graph data
Returns
-------
torch.Tensor
Node embeddings
"""
if HAS_TORCH_GEOMETRIC:
x, edge_index = data.x, data.edge_index
x = self.conv1(x, edge_index)
x = F.relu(x)
x = self.conv2(x, edge_index)
x = F.relu(x)
x = self.conv3(x, edge_index)
else:
x = data.x
x = self.linear1(x)
x = F.relu(x)
x = self.linear2(x)
x = F.relu(x)
x = self.linear3(x)
return x
class MessagePassingCausalGNN(MessagePassing if HAS_TORCH_GEOMETRIC else nn.Module):
"""
Custom message passing for causal graphs.
Implements directed message passing that respects causal structure:
- Messages flow only in direction of causal edges
- Aggregation respects causal mechanisms
"""
def __init__(self, node_dim: int, edge_dim: int, hidden_dim: int):
"""
Initialize message passing GNN.
Parameters
----------
node_dim : int
Node feature dimension
edge_dim : int
Edge feature dimension
hidden_dim : int
Hidden dimension
"""
if HAS_TORCH_GEOMETRIC:
super(MessagePassingCausalGNN, self).__init__(aggr='add')
else:
super(MessagePassingCausalGNN, self).__init__()
self.node_dim = node_dim
self.edge_dim = edge_dim
self.hidden_dim = hidden_dim
# Message function
self.message_mlp = nn.Sequential(
nn.Linear(node_dim + node_dim + edge_dim, hidden_dim),
nn.ReLU(),
nn.Linear(hidden_dim, hidden_dim)
)
# Update function
self.update_mlp = nn.Sequential(
nn.Linear(node_dim + hidden_dim, hidden_dim),
nn.ReLU(),
nn.Linear(hidden_dim, node_dim)
)
def forward(self, x, edge_index, edge_attr):
"""
Forward pass.
Parameters
----------
x : torch.Tensor
Node features
edge_index : torch.Tensor
Edge indices
edge_attr : torch.Tensor
Edge attributes
Returns
-------
torch.Tensor
Updated node features
"""
if HAS_TORCH_GEOMETRIC:
return self.propagate(edge_index, x=x, edge_attr=edge_attr)
else:
# Fallback implementation
return x
def message(self, x_i, x_j, edge_attr):
"""
Construct messages.
x_i: target node features
x_j: source node features
edge_attr: edge features
Returns
-------
torch.Tensor
Messages
"""
# Concatenate source, target, and edge features
msg_input = torch.cat([x_i, x_j, edge_attr], dim=-1)
return self.message_mlp(msg_input)
def update(self, aggr_out, x):
"""
Update node features.
Parameters
----------
aggr_out : torch.Tensor
Aggregated messages
x : torch.Tensor
Current node features
Returns
-------
torch.Tensor
Updated node features
"""
# Concatenate aggregated messages with current features
update_input = torch.cat([x, aggr_out], dim=-1)
return self.update_mlp(update_input)
class AttentionGNN(nn.Module):
"""
Graph Attention Network for geopolitical influence.
Uses attention to weight importance of different neighbors/allies.
"""
def __init__(
self,
node_features: int,
hidden_dim: int,
output_dim: int,
num_heads: int = 4
):
"""
Initialize attention GNN.
Parameters
----------
node_features : int
Input node feature dimension
hidden_dim : int
Hidden dimension
output_dim : int
Output dimension
num_heads : int
Number of attention heads
"""
super(AttentionGNN, self).__init__()
if HAS_TORCH_GEOMETRIC:
self.conv1 = GATConv(node_features, hidden_dim, heads=num_heads, concat=True)
self.conv2 = GATConv(hidden_dim * num_heads, output_dim, heads=1, concat=False)
else:
# Fallback
self.linear1 = nn.Linear(node_features, hidden_dim * num_heads)
self.linear2 = nn.Linear(hidden_dim * num_heads, output_dim)
def forward(self, data):
"""
Forward pass with attention.
Parameters
----------
data : Data
Graph data
Returns
-------
torch.Tensor
Node embeddings with attention weights
"""
if HAS_TORCH_GEOMETRIC:
x, edge_index = data.x, data.edge_index
# First layer with multi-head attention
x, attention_weights1 = self.conv1(x, edge_index, return_attention_weights=True)
x = F.elu(x)
x = F.dropout(x, p=0.3, training=self.training)
# Second layer
x, attention_weights2 = self.conv2(x, edge_index, return_attention_weights=True)
return x, (attention_weights1, attention_weights2)
else:
x = data.x
x = self.linear1(x)
x = F.elu(x)
x = F.dropout(x, p=0.3, training=self.training)
x = self.linear2(x)
return x, None
class GNNTrainer:
"""
Trainer for GNN models.
Handles training, validation, and evaluation.
"""
def __init__(
self,
model,
learning_rate: float = 0.01,
weight_decay: float = 5e-4
):
"""
Initialize GNN trainer.
Parameters
----------
model : nn.Module
GNN model
learning_rate : float
Learning rate
weight_decay : float
Weight decay (L2 regularization)
"""
if not HAS_TORCH:
raise ImportError("PyTorch required for GNN training")
self.model = model
self.optimizer = torch.optim.Adam(
model.parameters(),
lr=learning_rate,
weight_decay=weight_decay
)
def train_step(
self,
data,
labels,
loss_fn: Callable
) -> float:
"""
Single training step.
Parameters
----------
data : Data
Graph data
labels : torch.Tensor
Labels
loss_fn : callable
Loss function
Returns
-------
float
Loss value
"""
self.model.train()
self.optimizer.zero_grad()
# Forward pass
out = self.model(data)
# Compute loss
loss = loss_fn(out, labels)
# Backward pass
loss.backward()
self.optimizer.step()
return loss.item()
def evaluate(
self,
data,
labels,
metric_fn: Callable
) -> float:
"""
Evaluate model.
Parameters
----------
data : Data
Graph data
labels : torch.Tensor
True labels
metric_fn : callable
Evaluation metric
Returns
-------
float
Metric value
"""
self.model.eval()
with torch.no_grad():
out = self.model(data)
metric = metric_fn(out, labels)
return metric
class NetworkToGraph:
"""
Convert NetworkX graph to PyTorch Geometric format.
"""
@staticmethod
def convert(
G: nx.Graph,
node_features: Optional[Dict] = None,
edge_features: Optional[Dict] = None
):
"""
Convert NetworkX graph to PyTorch Geometric Data.
Parameters
----------
G : nx.Graph
NetworkX graph
node_features : dict, optional
Node feature dictionary
edge_features : dict, optional
Edge feature dictionary
Returns
-------
Data
PyTorch Geometric Data object
"""
if not HAS_TORCH or not HAS_TORCH_GEOMETRIC:
raise ImportError("PyTorch and torch_geometric required")
# Node features
if node_features:
x = torch.tensor([
node_features[node]
for node in G.nodes()
], dtype=torch.float)
else:
# Default: one-hot encoding
n_nodes = G.number_of_nodes()
x = torch.eye(n_nodes)
# Edge index
edge_index = torch.tensor(
list(G.edges()),
dtype=torch.long
).t().contiguous()
# Edge features
if edge_features:
edge_attr = torch.tensor([
edge_features[(u, v)]
for u, v in G.edges()
], dtype=torch.float)
else:
edge_attr = None
data = Data(x=x, edge_index=edge_index, edge_attr=edge_attr)
return data
def example_geopolitical_gnn():
"""
Example: GNN for geopolitical alliance network.
"""
if not HAS_TORCH or not HAS_TORCH_GEOMETRIC:
print("PyTorch and torch_geometric required for GNN examples")
return
# Create example graph (alliance network)
G = nx.DiGraph()
countries = ['USA', 'China', 'Russia', 'EU', 'India']
G.add_nodes_from(countries)
# Add alliance edges
alliances = [
('USA', 'EU'),
('USA', 'India'),
('China', 'Russia'),
]
G.add_edges_from(alliances)
# Node features (e.g., GDP, military strength, etc.)
node_features = {
'USA': [1.0, 0.9, 0.8],
'China': [0.8, 0.7, 0.9],
'Russia': [0.5, 0.7, 0.6],
'EU': [0.9, 0.6, 0.7],
'India': [0.6, 0.6, 0.7]
}
# Convert to PyTorch Geometric format
data = NetworkToGraph.convert(G, node_features)
# Create model
model = CausalGNN(
node_features=3,
hidden_dim=16,
output_dim=8,
num_layers=2
)
# Forward pass
embeddings = model(data)
print("Node embeddings shape:", embeddings.shape)
print("Node embeddings:", embeddings)
return model, data, embeddings
|