Skip to main content

A simple implementation of Neural Logic Machines in Pytorch.

Project description

PyPI version Downloads

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 typing module. 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

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

neural-logic-machine-1.1.0.tar.gz (16.0 kB view details)

Uploaded Source

Built Distribution

neural_logic_machine-1.1.0-py3-none-any.whl (19.6 kB view details)

Uploaded Python 3

File details

Details for the file neural-logic-machine-1.1.0.tar.gz.

File metadata

  • Download URL: neural-logic-machine-1.1.0.tar.gz
  • Upload date:
  • Size: 16.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.0.0 CPython/3.9.18

File hashes

Hashes for neural-logic-machine-1.1.0.tar.gz
Algorithm Hash digest
SHA256 e103b612bfb57e5daf70b10cd94082aac3594dd67b83e3a39888fe40efc2f0d7
MD5 373ebe692e7c09a07e1f8b29bfcfa640
BLAKE2b-256 ebfc851043c0fb633171a93079f6145b524f0ac0ca4e4f4e3f0ce9ebc35978df

See more details on using hashes here.

File details

Details for the file neural_logic_machine-1.1.0-py3-none-any.whl.

File metadata

File hashes

Hashes for neural_logic_machine-1.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 12612fd37a878b3a0297a0212726aa0332eb954161376f6d59844e2512b38b8b
MD5 c9e109f8f3becc5051bf8d47ceeca8c7
BLAKE2b-256 9230ebacf0e8321bc61ba2a53e7fb0130e9c1e928ec4f8a47fa3147f5e45bd63

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page