aprender 0.30.0

Next-generation ML framework in pure Rust — `cargo install aprender` for the `apr` CLI
Documentation
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
<!-- PCU: examples-gnn-node-classification | contract: contracts/apr-page-examples-gnn-node-classification-v1.yaml -->
<!-- Example: cargo run -p aprender-core --example none -->
<!-- Status: enforced -->

# Case Study: Graph Neural Networks for Node Classification

This case study demonstrates Aprender's GNN module for learning on graph-structured data.

## Overview

The `gnn` module provides:
- **GCNConv**: Graph Convolutional Network layer
- **GATConv**: Graph Attention Network layer
- **GNNModule trait**: Interface for graph-aware layers

## Basic GCN Usage

```rust
use aprender::gnn::{GCNConv, GNNModule, EdgeIndex};
use aprender::autograd::Tensor;

fn main() {
    // Create GCN layer: 16 input features → 32 output features
    let gcn = GCNConv::new(16, 32);

    // Node features: 4 nodes, 16 features each
    let x = Tensor::ones(&[4, 16]);

    // Graph structure: a simple cycle 0 → 1 → 2 → 3 → 0
    let edge_index: Vec<EdgeIndex> = vec![
        (0, 1), (1, 0),  // Edge 0-1 (bidirectional)
        (1, 2), (2, 1),  // Edge 1-2
        (2, 3), (3, 2),  // Edge 2-3
        (3, 0), (0, 3),  // Edge 3-0
    ];

    // Forward pass
    let out = gcn.forward_gnn(&x, &edge_index);

    assert_eq!(out.shape(), &[4, 32]);
    println!("Output shape: {:?}", out.shape());
}
```

## Multi-Layer GCN

```rust
use aprender::gnn::{GCNConv, GNNModule, EdgeIndex};
use aprender::autograd::Tensor;
use aprender::nn::Module;

struct GCN {
    conv1: GCNConv,
    conv2: GCNConv,
}

impl GCN {
    fn new(in_features: usize, hidden: usize, out_features: usize) -> Self {
        Self {
            conv1: GCNConv::new(in_features, hidden),
            conv2: GCNConv::new(hidden, out_features),
        }
    }

    fn forward(&self, x: &Tensor, edge_index: &[EdgeIndex]) -> Tensor {
        // Layer 1: Input → Hidden with ReLU
        let h = self.conv1.forward_gnn(x, edge_index).relu();

        // Layer 2: Hidden → Output (no activation for logits)
        self.conv2.forward_gnn(&h, edge_index)
    }

    fn parameters(&self) -> Vec<&Tensor> {
        let mut params = self.conv1.parameters();
        params.extend(self.conv2.parameters());
        params
    }
}
```

## Node Classification Task

```rust
use aprender::gnn::{GCNConv, GNNModule, EdgeIndex};
use aprender::autograd::{Tensor, clear_graph, no_grad};
use aprender::nn::Module;

fn train_node_classifier() {
    // Karate Club graph (simplified)
    // 34 nodes, 2 classes (communities)
    let num_nodes = 34;
    let num_features = 34;  // One-hot encoding
    let num_classes = 2;

    // Create model
    let mut model = GCN::new(num_features, 16, num_classes);

    // Node features: identity matrix (each node is unique)
    let x = Tensor::eye(num_nodes);

    // Graph edges (simplified subset)
    let edge_index: Vec<EdgeIndex> = vec![
        (0, 1), (1, 0), (0, 2), (2, 0), (0, 3), (3, 0),
        (1, 2), (2, 1), (2, 3), (3, 2),
        // ... more edges
    ];

    // Labels for some nodes (semi-supervised)
    let labeled_nodes = vec![0, 33];  // First and last node
    let labels = vec![0, 1];  // Different communities

    let lr = 0.01;
    let epochs = 200;

    for epoch in 0..epochs {
        // Forward pass
        let logits = model.forward(&x, &edge_index);

        // Compute loss only on labeled nodes
        let mut loss_val = 0.0;
        for (&node, &label) in labeled_nodes.iter().zip(labels.iter()) {
            let node_logits = logits.select(0, node);
            let probs = node_logits.softmax();

            // Cross-entropy loss
            let log_prob = probs.log();
            loss_val -= log_prob.data()[label];
        }

        let loss = Tensor::from_slice(&[loss_val as f32]);
        loss.backward();

        // Update parameters
        no_grad(|| {
            for param in model.parameters() {
                if let Some(grad) = param.grad() {
                    let update = grad.mul(&Tensor::from_slice(&[lr]));
                    // param = param - lr * grad
                }
            }
        });

        clear_graph();

        if epoch % 50 == 0 {
            println!("Epoch {}: loss = {:.4}", epoch, loss_val);
        }
    }

    // Inference
    no_grad(|| {
        let logits = model.forward(&x, &edge_index);
        let predictions = logits.argmax(1);
        println!("Predictions: {:?}", predictions.data());
    });
}
```

## Graph Attention Network

```rust
use aprender::gnn::{GATConv, GNNModule, EdgeIndex};
use aprender::autograd::Tensor;

fn main() {
    // GAT with 4 attention heads
    let gat = GATConv::new(16, 8, 4);  // 16 in → 8*4=32 out

    let x = Tensor::ones(&[4, 16]);
    let edge_index: Vec<EdgeIndex> = vec![
        (0, 1), (1, 2), (2, 3), (3, 0),
    ];

    let out = gat.forward_gnn(&x, &edge_index);
    println!("GAT output: {:?}", out.shape());  // [4, 32]

    // Access attention weights for interpretability
    let attention = gat.get_attention_weights();
    println!("Attention on edge (0,1): {:.3}", attention[&(0, 1)]);
}
```

## Building Graph from Data

```rust
use aprender::gnn::EdgeIndex;

/// Build edge index from adjacency list
fn adjacency_list_to_edges(adj: &[Vec<usize>]) -> Vec<EdgeIndex> {
    let mut edges = Vec::new();
    for (src, neighbors) in adj.iter().enumerate() {
        for &tgt in neighbors {
            edges.push((src, tgt));
        }
    }
    edges
}

/// Build edge index from adjacency matrix
fn adjacency_matrix_to_edges(adj: &[Vec<f32>]) -> Vec<EdgeIndex> {
    let mut edges = Vec::new();
    for (i, row) in adj.iter().enumerate() {
        for (j, &val) in row.iter().enumerate() {
            if val > 0.0 {
                edges.push((i, j));
            }
        }
    }
    edges
}

fn main() {
    // From adjacency list
    let adj_list = vec![
        vec![1, 2],     // Node 0 connects to 1, 2
        vec![0, 2],     // Node 1 connects to 0, 2
        vec![0, 1, 3],  // Node 2 connects to 0, 1, 3
        vec![2],        // Node 3 connects to 2
    ];
    let edges = adjacency_list_to_edges(&adj_list);
    println!("Edges: {:?}", edges);
}
```

## Handling Self-Loops

```rust
use aprender::gnn::{GCNConv, GNNModule, EdgeIndex};
use aprender::autograd::Tensor;

fn main() {
    // GCN with self-loops (default)
    let gcn_with_loops = GCNConv::new(16, 32);

    // GCN without self-loops
    let gcn_no_loops = GCNConv::without_self_loops(16, 32);

    let x = Tensor::ones(&[4, 16]);
    let edges: Vec<EdgeIndex> = vec![(0, 1), (1, 2), (2, 3)];

    // With self-loops: nodes aggregate their own features
    let out1 = gcn_with_loops.forward_gnn(&x, &edges);

    // Without: only neighbor features (isolated nodes get zero)
    let out2 = gcn_no_loops.forward_gnn(&x, &edges);

    println!("With self-loops: node features preserved");
    println!("Without: isolated nodes may lose information");
}
```

## Graph Batching

Process multiple graphs as a single disconnected graph:

```rust
use aprender::gnn::EdgeIndex;

struct BatchedGraph {
    x: Vec<f32>,           // Concatenated node features
    edge_index: Vec<EdgeIndex>,
    batch: Vec<usize>,     // Graph ID for each node
}

fn batch_graphs(graphs: &[(Vec<f32>, Vec<EdgeIndex>, usize)]) -> BatchedGraph {
    let mut x = Vec::new();
    let mut edge_index = Vec::new();
    let mut batch = Vec::new();

    let mut node_offset = 0;

    for (graph_id, (features, edges, num_nodes)) in graphs.iter().enumerate() {
        // Add node features
        x.extend(features);

        // Add edges with offset
        for &(src, tgt) in edges {
            edge_index.push((src + node_offset, tgt + node_offset));
        }

        // Record which graph each node belongs to
        for _ in 0..*num_nodes {
            batch.push(graph_id);
        }

        node_offset += num_nodes;
    }

    BatchedGraph { x, edge_index, batch }
}
```

## Graph-Level Prediction

```rust
use aprender::gnn::{GCNConv, GNNModule, EdgeIndex};
use aprender::autograd::Tensor;
use aprender::nn::{Linear, Module};

struct GraphClassifier {
    conv1: GCNConv,
    conv2: GCNConv,
    fc: Linear,
}

impl GraphClassifier {
    fn new(in_features: usize, hidden: usize, num_classes: usize) -> Self {
        Self {
            conv1: GCNConv::new(in_features, hidden),
            conv2: GCNConv::new(hidden, hidden),
            fc: Linear::new(hidden, num_classes),
        }
    }

    fn forward(&self, x: &Tensor, edge_index: &[EdgeIndex], batch: &[usize]) -> Tensor {
        // Node-level embeddings
        let h = self.conv1.forward_gnn(x, edge_index).relu();
        let h = self.conv2.forward_gnn(&h, edge_index).relu();

        // Global mean pooling per graph
        let graph_embeddings = global_mean_pool(&h, batch);

        // Graph-level prediction
        self.fc.forward(&graph_embeddings)
    }
}

fn global_mean_pool(h: &Tensor, batch: &[usize]) -> Tensor {
    let num_graphs = batch.iter().max().map(|&m| m + 1).unwrap_or(0);
    let hidden_dim = h.shape()[1];

    let mut pooled = vec![0.0f32; num_graphs * hidden_dim];
    let mut counts = vec![0usize; num_graphs];

    let h_data = h.data();
    for (node_idx, &graph_id) in batch.iter().enumerate() {
        counts[graph_id] += 1;
        for f in 0..hidden_dim {
            pooled[graph_id * hidden_dim + f] += h_data[node_idx * hidden_dim + f];
        }
    }

    // Average
    for graph_id in 0..num_graphs {
        if counts[graph_id] > 0 {
            for f in 0..hidden_dim {
                pooled[graph_id * hidden_dim + f] /= counts[graph_id] as f32;
            }
        }
    }

    Tensor::new(&pooled, &[num_graphs, hidden_dim])
}
```

## Feature Initialization

```rust
use aprender::autograd::Tensor;

/// One-hot encoding for node IDs
fn one_hot_features(num_nodes: usize) -> Tensor {
    Tensor::eye(num_nodes)
}

/// Degree-based features
fn degree_features(edge_index: &[(usize, usize)], num_nodes: usize) -> Tensor {
    let mut degrees = vec![0.0f32; num_nodes];
    for &(src, _) in edge_index {
        degrees[src] += 1.0;
    }

    // Normalize
    let max_deg = degrees.iter().cloned().fold(1.0, f32::max);
    for d in &mut degrees {
        *d /= max_deg;
    }

    Tensor::new(&degrees, &[num_nodes, 1])
}

/// Random features (for structure-only learning)
fn random_features(num_nodes: usize, dim: usize) -> Tensor {
    let data: Vec<f32> = (0..num_nodes * dim)
        .map(|_| rand::random::<f32>())
        .collect();
    Tensor::new(&data, &[num_nodes, dim])
}
```

## Running Examples

```bash
# Basic GCN
cargo run --example gnn_basic

# Node classification
cargo run --example gnn_node_classification

# Graph classification
cargo run --example gnn_graph_classification
```

## References

- Kipf & Welling (2017). "Semi-Supervised Classification with Graph Convolutional Networks." ICLR.
- Velickovic et al. (2018). "Graph Attention Networks." ICLR.
- Hamilton et al. (2017). "Inductive Representation Learning on Large Graphs." NeurIPS.