a compact neuron network model definition grammar
Project description
NEURLINK
A compact grammar for neural network definition based on PyTorch.
Basics
Neurlink asks for a sequence of nndef
specification lines to define a flexibly connected neural network.
Each line of nndef
syntax follows the pattern ((C1, S2), ..., (Cn, Sn), NerveClass[input_selector, tag](...))
that says a nerve
takes inputs determined by input_selector
and output n
tensors, each of which has channel size C
and spatial shape S
.
input selector
Every nndef
node can select its input from the set of all previous lines. By default, input_selector
can be omitted and means outputs of previous nndef
; it can also be a integer index, a slice
, or a list
, so that one can specify multiple input nodes. The implementation of each layer is a subclass of Nerve
and can get informations of the input specifications by attribute self.input_links
.
tag
can be optionally specified to allow you refer to a nndef
specification by a string alias.
output dimensions
Output dimensions specify channels and spatial shapes seperately. The spatial shape written as a (tuple of) integer/float number is a relative down sampling ratio of the base_shape
(specified by nv.Input
). It can also be written as a string so that it evaluates to absolute shape. The dimensions are written out to give a straightforward impression of the computation flow of the entire network, and shape transformation is done automatically so that you don't bother to derive how many stride or padding you might want.
Example (resnet)
def resnet50(num_classes: int = 1000, **block_keywords):
block = BottleNeck(**block_keywords, expansion=4)
expansion = 4
return build(
[
((3, 1), nv.Input()),
((64, 2), Conv2d_ReLU_BN(7)), # 7x7 conv, stride 2
((64, 4), MaxPool2d(3)), # 3x3 maxpool, stride 2
[((64 * expansion, 4), block)] * 3, # 3 layers of residual blocks, w/o downsampling
[((128 * expansion, 8), block)] * 4, # 4 layers of residual blocks, downsampling (x2) happens at the first block
[((256 * expansion, 16), block)] * 6, # 256 * expansion is the actual output channel, the bottleneck shape is interpreted inside of the block.
[((512 * expansion, 32), block)] * 3, # finally downsamped to 1/32 of origin, you may have noted that you can easily figure out the global downsample ratio.
((512 * expansion, "(1, 1)"), AvgPool2d()), # a average pooling layer downsamples to an absolute shape.
((num_classes, "(1, 1)"), Conv2d(1)), # final linear layer.
]
)
# in tests/test_build_models.py
import torch
import neurlink
model = neurlink.resnet18()
x = torch.randn((2, 3, 224, 224))
out = model(x, output_intermediate=True)
for y in out:
print(y.shape)
$ python tests/test_build_models.py
torch.Size([2, 3, 224, 224])
torch.Size([2, 64, 112, 112])
torch.Size([2, 64, 56, 56])
torch.Size([2, 64, 56, 56])
torch.Size([2, 64, 56, 56])
torch.Size([2, 128, 28, 28])
torch.Size([2, 128, 28, 28])
torch.Size([2, 256, 14, 14])
torch.Size([2, 256, 14, 14])
torch.Size([2, 512, 7, 7])
torch.Size([2, 512, 7, 7])
torch.Size([2, 512, 1, 1])
torch.Size([2, 1000, 1, 1])
Project details
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
File details
Details for the file neurlink-0.2.2.tar.gz
.
File metadata
- Download URL: neurlink-0.2.2.tar.gz
- Upload date:
- Size: 19.8 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.0 CPython/3.9.5
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 52511984368d339882489aa36b43c3f2af682ddc43d1b15ca07fc0c984708892 |
|
MD5 | be27833607ad25a5e720a8cc961a662f |
|
BLAKE2b-256 | 7093e63d0b233a1de7b0dbb6dd15d39408e22d5bcdc8f04e42351761feb7dc07 |
File details
Details for the file neurlink-0.2.2-py3-none-any.whl
.
File metadata
- Download URL: neurlink-0.2.2-py3-none-any.whl
- Upload date:
- Size: 22.6 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.0 CPython/3.9.5
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | c624541737b96c5db83e44d89d8ed3f4dbcfad6fbcea8b08e2e3e080fa5b6a73 |
|
MD5 | 3161d8bf51069a5909849db157fef5e5 |
|
BLAKE2b-256 | ea08a071f9debdc3dd5a7a61fc62a186bfcd729ae29df9a8e6971a8bac9cc807 |