MGNet
MultiGrid Methods + Neural Networks = MgNet
tl;dr
MultiGrid methods cool. Repository implements paper with pytorch.
Usage
from mgnet.model import mgnet, MGNet
import torch
supported_smoothers = MGNet.supported_smoothers()
print(f'{supported_smoothers=}')
n_image_chan = 3
batch_size = 1
image_width = 28
image_height = 28
model = mgnet(smoother=supported_smoothers[0],
pretrained=False,
progress=False,
in_channels=n_image_chan,
n_chan_u=128,
n_chan_f=128,
n_classes=10,
n_iter=[2,2,2,2]
)
x = torch.rand((batch_size, n_image_chan, image_width, image_height))
y = model(x)
print(f'{y=}')
Background
I first came up with the idea of using multigrid methods in deep learning after attending the seminar algebraic multigrid, in which we studied the publication by Jinchao Xu and Ludmil Zikatanov.
If multigrid methods are one of the fastest solvers for partial differential equality problems by use of grids, then knowing that images are functions on grids, multigrid methods can be used in machine learning, especially in FNNs and CNNs ...
i thought. Unfortunately, Sir Xu beat me up to the idea and published a paper 2 years before.
As I couldn't find a ready-to-use implementation of the paper, the work in this repository came to be.
Theory of operation
This type of network generalizes multiple preexisting models like the famous ResNet and DenseNet into a single configurable system.
The premise is built upon the idea of multigrid: the solution u is smoothened and
its errors are projected on layers of various sizes.
A single step consists of three phases:
- Relaxation: Given a smooth
S, the solution of the problemuis computed usingS(u) = fseveral time, to obtain an improved solutionv. The residuum of the equation is computedr = f - S(v) - Restriction: the residuum
ris transferred on a layer of lower size and the approximation of the errorA_{r}e_{r} = ris computed - Prolongation or interpolation: the error
eis projected back on the original layerA_{p}e_{r} = r_{p}and the original solution updated:v = v + e_{r}
---
title: Basic multigrid
---
flowchart TD;
s[Start: v<sup>h</sup> = 0] --> v1
subgraph "Relaxation"
v1["Solve: S<sup>h</sup> * u<sup>h</sup> = f<sup>h</sup>"]--> v["Compute residuum: r = f - S * v"]
end
subgraph Restriction
v["Compute residuum: r<sup>h</sup> = f<sup>h</sup> - S<sup>h</sup>v<sup>h</sup>"] --> r1
r1["Project residuum on coarse layer: G<sup>2h</sup>: r<sup>2h</sup>"] --> r2
end
subgraph Prolongation
r2["Compute error: S<sup>2h</sup> * e<sup>2h</sup>= r<sup>2h</sup>"] --> r3
end
r3["Project error on finer layer: G<sup>h</sup>: e<sup>h</sup>"] --> p1["Updated solution: v<sup>h</sub> = v<sup>h</sub> + e<sup>h</sub>"]
Testing
Local:
Install dependencies:
uv sync --dev
Run inference test (default values already set. Use --help option for help):
PYTHONPATH="${PYTHONPATH}:${PWD}" uv run tests/test_inference.py
Docker:
Build image:
docker build . -t mgnet
Run image in container:
docker run mgnet --help
Literatur
- "Algebraic Multigrid Methods", Jinchao Xu, Ludmil T Zikatanov
- "MgNet: A Unified Framework of Multigrid and Convolutional Neural Network", Juncai He, Jinchao Xu
- "Iterative Solution of Large Sparse Systems of Equations", Wolfgang Hackbusch
Metadata
Release files for mgnet 0.1.2
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | |
|---|---|---|---|
| mgnet-0.1.2.tar.gz | 6.2 kB | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| mgnet-0.1.2-py3-none-any.whl | Python 3 | none | any | Details |
Total release size: 13.2 kB
Release files / mgnet-0.1.2.tar.gz
| Download URL | mgnet-0.1.2.tar.gz |
|---|---|
| Size | 6.2 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
c15a9db7b5d8b40406d4b1b3f148ef8a62541eb2acbce15616ba67612c8420ae
|
|
BLAKE2b-256 checksum How to use checksums |
8144dd4ca49d2a84913689924fd5a2a1a5de63328209dd4def57e9597ddcac63
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
uv/0.9.18 {"installer":{"name":"uv","version":"0.9.18","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"20.04","id":"focal","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":null}
|
Release files / mgnet-0.1.2-py3-none-any.whl
| Download URL | mgnet-0.1.2-py3-none-any.whl |
|---|---|
| Size | 7.0 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
fd9a31aa1957315dafcd88580fc9a5c836b379512a3558b1b6048e094e8e682a
|
|
BLAKE2b-256 checksum How to use checksums |
1b1307118f505dbd97c06d3c00e1d58d63f8e267ea9114484ded97f5f193416c
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
uv/0.9.18 {"installer":{"name":"uv","version":"0.9.18","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"20.04","id":"focal","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":null}
|