|
| 1 | +<html> |
| 2 | + |
| 3 | +<head> |
| 4 | +<linkrel="stylesheet"href="https://pyscript.net/alpha/pyscript.css"/> |
| 5 | +<scriptdefersrc="https://pyscript.net/alpha/pyscript.js"></script> |
| 6 | +</head> |
| 7 | + |
| 8 | +<body> |
| 9 | +<py-env> |
| 10 | + - numpy |
| 11 | + - networkx |
| 12 | + - matplotlib |
| 13 | +</py-env> |
| 14 | +<py-script> |
| 15 | +import numpy as np |
| 16 | +import networkx as nx |
| 17 | +</py-script> |
| 18 | + |
| 19 | +<p>Message passing with linear algebra: a demo.</p> |
| 20 | +<p>Imagine we have a chain graph that looks like this:</p> |
| 21 | +<pre><code>O --> 1 --> 2 --> 3</code></pre> |
| 22 | +<p>In NetworkX this graph would look like the following:</p> |
| 23 | +<pre> |
| 24 | +<py-script> |
| 25 | +G = nx.Graph() |
| 26 | +nodes = list(range(4)) |
| 27 | +G.add_edges_from(zip(nodes[0:-1], nodes[1:])) |
| 28 | +print(G.edges()) |
| 29 | +</py-script></pre> |
| 30 | +<p>This chain graph has the following adjacency matrix:</p> |
| 31 | +<pre> |
| 32 | +<py-script> |
| 33 | +adj_mat = np.eye(4, k=1) |
| 34 | +print(f"A: {adj_mat}") |
| 35 | +</py-script> |
| 36 | +</pre> |
| 37 | +<p>And imagine that we have a message that lives on the graph:</p> |
| 38 | +<pre> |
| 39 | +<py-script> |
| 40 | +message = np.array([1.0, 0.0, 0.0, 0.0]) |
| 41 | +print(f"message: {message}") |
| 42 | +</py-script> |
| 43 | +</pre> |
| 44 | +<p>Try out message passing below by doing any one of the following steps:</p> |
| 45 | +<pre><code>message @ adj_mat</code></pre> |
| 46 | +<pre><code>message @ adj_mat @ adj_mat</code></pre> |
| 47 | +<pre><code>message @ adj_mat @ adj_mat @ adj_mat</code></pre> |
| 48 | +<div> |
| 49 | +<py-replid="my-repl"auto-generate="true"></py-repl> |
| 50 | +</div> |
| 51 | +</body> |
| 52 | + |
| 53 | +</html> |