Skip to main content

tinynet

A tiny neural network library

No training

This library provides no training algorithm. You can easily set up a neuroevolution framework by including any black-box search algorithm, from RWG (see example below) to CMA-ES.

Installation

pip install tinynet

Usage

from tinynet import RNN1L
import numpy as np
net_struct = [3, 5, 2]
net = RNN(net_struct) # try also FFNN
net.set_weights(np.random.randn(net.nweights))
out = net.activate(np.zeros(net.ninputs))
assert len(out) == net.noutputs
assert len(net.state) == net.ninputs + 1 + net.noutputs # input, bias, recursion

Neuroevolution application on the OpenAI Gym

Check out this GitHub gist to run the Bipedal Walker using pre-trained weights.

The example below tackles the CartPole from scratch using RWG.

import numpy as np
import tinynet
import gym # just `pip install gym`
from time import sleep # slow down rendering

# Environment setup
env = gym.make("CartPole-v1")
# env = gym.wrappers.Monitor(env, 'video', force = True) # Uncomment to save video

# Get input size and output size from the environment
nactions = env.action_space.n
ninputs = env.reset().size
# Hidden layers are arbitrarily added
# hidden = [20, 10, 20]
hidden = [] # ... but unnecessary with the CartPole
net_struct = [ninputs, *hidden, nactions]

# Network setup is straightforward (defaults: `act_fn=np.tanh, init_weights=None`)
net = tinynet.FFNN(net_struct) # also try `RNN(net_struct)`

# Get random seed for deterministic fitness (for simplicity)
rseed = np.random.randint(1e10)

# Fitness function: gameplay loop
def fitness(ind, render=False):
    env.seed(rseed)  # makes fitness deterministic
    obs = env.reset()
    score = 0
    done = False
    net.set_weights(ind)
    while not done:
        if render:
            env.render()
            sleep(0.5)
        action = net.activate(obs).argmax()
        obs, rew, done, info = env.step(action)
        score += rew
    if render: env.render() # render last frame
    print(f"Score: {score}")
    return score

# RWG does not distinguish between populations and generations
max_ninds = 1000

# Neuroevolution (RWG) loop
for nind in range(max_ninds):
    ind = np.random.randn(net.nweights)
    score = fitness(ind)
    if score >= 195:
        print(f"Game solved in {nind} trials")
        break

# Replay winning individual
fitness(ind, render=True)

# You may want to drop into a console here to examine the results
import IPython; IPython.embed()

Metadata

Release files for tinynet 0.1.2

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for tinynet 0.1.2
File Size Uploaded Provenance Actions
tinynet-0.1.2.tar.gz 5.3 kB Without provenance Details for tinynet-0.1.2.tar.gz

Release files / tinynet-0.1.2.tar.gz

Download URL tinynet-0.1.2.tar.gz
Size 5.3 kB
Tags Source
SHA-256 checksum
How to use checksums
6dfe4a75891a6e05a2932577bafbb92981934219198e897bc18ebc00926c3bca
BLAKE2b-256 checksum
How to use checksums
2b13175970b0cbacd36722d5c70ad585b857c684cc5a874b9751e9d8551770f9
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/3.1.1 pkginfo/1.5.0.1 requests/2.23.0 setuptools/46.4.0 requests-toolbelt/0.9.1 tqdm/4.46.0 CPython/3.6.9

Security for tinynet 0.1.2

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page