Simple NLM
A simple Pytorch implementation of Neural Logic Machines (arxiv.org/abs/1904.11694).
New in version 1.1
- Fixed bug related to the Python
typingmodule. Simple-NLM now also works for Python<3.9. - Improved efficiency of both padded and unpadded NLMs, by pruning inner predicates which are not used for computing the NLM output.
How to use
Simply download from PyPI by doing pip install neural-logic-machine.
An alternative option is to clone the repository.
Assuming it has been installed from PyPI, using it is as simply as importing the neural-logic-machine package in Python:
from neural_logic_machine import NLM
import torch
# We create an NLM with two layers. The first layer receives different predicates
# and outputs 8 predicates for arities 0-3. The second layer receives these predicates and outputs 2 nullary predicates and one unary predicate.
# We do not need to specify the input size of the NLM (this is automatically inferred from the first forward pass).
nlm = NLM(hidden_features=[[8,8,8,8]], out_features=[2,1,0,0])
# Create a random input for the NLM
# The batch will contain two samples, the first one with 3 objects and the second one with 5 objects
# Note that we are not masking out those tensor positions corresponding to invalid objects (e.g., object 4 for the first batch element,
# despite it only having 3 objects). The reason is that there's no need to, as the NLM does that internally when computing the reduce operations
num_objs = [3,5]
input_NLM = [torch.randn((2,) + (5,)*r + (1,)) for r in range(3)] + [None] # The last position is None, as there are no ternary predicates
# Shapes of tensors in input_NLM: [(2,1), (2,5,1), (2,5,5,1), None]
# Forward pass
output = nlm(input_NLM, num_objs)
print(output) # Note in the result how invalid positions (those correspondings to objects 3 and 4 for the first batch element) are masked to -inf
>>> [tensor([[-0.0104, 0.4631],
[-0.4774, 0.4594]], grad_fn=<AddmmBackward0>), tensor([[[-0.0508],
[-0.0756],
[-0.0551],
[ -inf],
[ -inf]],
[[ 0.0538],
[ 0.1004],
[ 0.1422],
[-0.0173],
[ 0.0232]]], grad_fn=<MaskedFillBackward0>), None, None]
For training, we recommend using Pytorch Lightning.
Dependencies
Python 3 and Pytorch.
Authors
- Carlos Núñez Molina
- Masataro Asai
Release files for neural-logic-machine 1.1.0
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | |
|---|---|---|---|
| neural-logic-machine-1.1.0.tar.gz | 16.0 kB | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| neural_logic_machine-1.1.0-py3-none-any.whl | Python 3 | none | any | Details |
Total release size: 35.7 kB
Release files / neural-logic-machine-1.1.0.tar.gz
| Download URL | neural-logic-machine-1.1.0.tar.gz |
|---|---|
| Size | 16.0 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
e103b612bfb57e5daf70b10cd94082aac3594dd67b83e3a39888fe40efc2f0d7
|
|
BLAKE2b-256 checksum How to use checksums |
ebfc851043c0fb633171a93079f6145b524f0ac0ca4e4f4e3f0ce9ebc35978df
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/5.0.0 CPython/3.9.18
|
Release files / neural_logic_machine-1.1.0-py3-none-any.whl
| Download URL | neural_logic_machine-1.1.0-py3-none-any.whl |
|---|---|
| Size | 19.6 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
12612fd37a878b3a0297a0212726aa0332eb954161376f6d59844e2512b38b8b
|
|
BLAKE2b-256 checksum How to use checksums |
9230ebacf0e8321bc61ba2a53e7fb0130e9c1e928ec4f8a47fa3147f5e45bd63
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/5.0.0 CPython/3.9.18
|