Skip to main content

A neural network library built on top of TensorFlow for quickly building deep learning models.

Project description

A neural network library built on top of TensorFlow for quickly building deep learning models.

Build Status

Usage

nn.Tensor is the core data structure which is a wrapper for tf.Tensor and provides additional functionality. It can be created using the nn.tensor() function:

import nn

a = nn.tensor([1, 2, 3])
assert isinstance(a, nn.Tensor)
assert a.shape == (3, )

It supports method chaining:

c = a.square().sum()
assert c.numpy() == 14

and can be used with tf.Tensor objects:

import tensorflow as tf

b = tf.constant(2)
c = (a - b).square().sum()
assert c.numpy() == 2

It can also be used with high level APIs such as tf.keras:

model = nn.Sequential([
  nn.Dense(128, activation='relu'),
  nn.Dropout(0.2),
  nn.Dense(10)
])

y = model(x)
assert isinstance(y, nn.Tensor)

and to perform automatic differentiation and optimization:

optimizer = nn.Adam()
with nn.GradientTape() as tape:
    outputs = model(inputs)
    loss = (targets - outputs).square().mean()
grads = tape.gradient(loss, model.trainable_variables)
optimizer.apply_gradients(zip(grads, model.trainable_variables))

To use it with ops that expect tf.Tensor objects as inputs, wrap the ops using nn.op():

mean = nn.op(tf.reduce_mean)
c = mean(a)
assert isinstance(c, nn.Tensor)

maximum = nn.op(tf.maximum, binary=True)
c = maximum(a, b)
assert isinstance(c, nn.Tensor)

or convert it to a tf.Tensor object using the tf() method or nn.tf() function:

b = a.tf()
assert isinstance(b, tf.Tensor)

b = nn.tf(a)
assert isinstance(b, tf.Tensor)

See more examples here.

Installation

Requirements:

  • TensorFlow >= 2.0
  • Python >= 3.6

Install from PyPI (recommended):

pip install nn

Alternatively, install from source:

git clone https://github.com/marella/nn.git
cd nn
pip install -e .

TensorFlow should be installed separately.

Testing

To run tests, install dependencies:

pip install -e .[tests]

and run:

pytest tests

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

nn-0.1.0.tar.gz (4.8 kB view details)

Uploaded Source

File details

Details for the file nn-0.1.0.tar.gz.

File metadata

  • Download URL: nn-0.1.0.tar.gz
  • Upload date:
  • Size: 4.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.11.0 pkginfo/1.4.2 requests/2.22.0 setuptools/44.0.0 requests-toolbelt/0.8.0 tqdm/4.20.0 CPython/3.6.4

File hashes

Hashes for nn-0.1.0.tar.gz
Algorithm Hash digest
SHA256 02ef51675c910bd19d63d9700ee8e8c6a56787f8b786f98f1ef5b6163ac360ee
MD5 90a34a720263d963fd04a18a416685bf
BLAKE2b-256 00f7a1b7c670ecdb03296edb9f539c4d310b9524993e1eb5e0e4686d6617151e

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