neuralist
A Python interface to access neural-redis, a neural networks module for Redis.
🚨 This stuff is an alpha stage (like neural-redis), so use it with caution!
Installation 🚀
To install simply do following:
$ pip install neuralist
Example usage
from neuralist import NeuralNetwork
nn = NeuralNetwork('additions',
type='regressor',
inputs=('number1', 'number2'),
outputs=('result',),
hidden_layers=(3, ),
dataset_size=50,
testset_size=10)
nn.observe_train(input={'number1': 1, 'number2': 1},
output={'result': 2})
nn.train()
while nn.is_training():
print 'Training...'
time.sleep(1)
print nn.run({'number': 1, 'number': 2})
API spec
NeuralNetwork class
The NeuralNetwork object follows the arguments of the nn.create
command.
Required arguments:
name
: (string) The name of the neural networktype
: (string) Type of network, can be'classifier'
or'regressor'
inputs
: (tuple) The keys of the input dictoutputs
: (tuple) The keys of the output dicthidden_layers
: (tuple) zero or more arguments indicating the number of hidden units, one number for each layer.dataset_size
: (int) Max number of data samples in the training datasettestset_size
: (int) Max number of data samples in the testing dataset
Optional arguments.
normalize
: (bool) DefaultTrue
. Specify if you want the network to normalize your inputs. Use this if you don't knowredis_client
: (Redis.Client) Specify which Redis client to useprefix
: (string) Defaultnn:
. Ability to prefix the neural networks, so they don't collide with other keysauto_create
: (bool) DefaultTrue
. Neural network will be auto created if it isn't found
NeuralNetwork.run(input)
Run the network on the input
, returning a dict
as the result.
Required arguments:
input
: (dict) The input vector encoded asdict
Example:
print(nn.run({'number1': 1, 'number2': 2}))
# =>
{'result': '3.7173097692441601'}
NeuralNetwork.classify(input)
Run the network on the input
, returning a class as the result.
Required arguments:
input
: (dict) The input vector encoded asdict
Example (from Titanic example):
print(nn.classify({...}))
# =>
'dead'
NeuralNetwork.observe_train(input, output)
Add a data sample into the training dataset.
Required arguments:
input
: (dict) The input vector encoded asdict
output
: (dict) The output vector encoded asoutput
Example:
nn.observe_train(input={'number1': 1, 'number2': 2},
output={'result': 3})
nn.observe_train(input={'age': 25, 'male': 0, 'female': 1},
output={'dead': 0, 'alive': 1})
NeuralNetwork.observe_test(input, output)
Add a data sample into the testing dataset.
Required arguments:
input
: (dict) The input vector encoded asdict
output
: (dict) The output vector encoded asoutput
NeuralNetwork.is_training()
Returns True
if the network is being trained.
NeuralNetwork.train()
Train a network in a background thread.
Optional arguments.
max_cycles
: (int) DefaultNone
. Max cycles countmax_time
: (int) DefaultNone
. Max time to trainautostop
: (bool) DefaultTrue
. If noautostop
isTrue
, trains the network till the maximum number of cycles or milliseconds are reached. If no maximum number of cycles is specified there are no cycles limits. If no milliseconds are specified, the limit is set to 10 seconds.backtrack
: (bool) DefaultTrue
. Ifbacktrack
isTrue
, andautostop
is alsoTrue
, while the network is trained, the trainer thread saves a copy of the neural network every time it has a better score compared to the previously saved one and there are hints suggesting that overfitting may happen soon. This network is used later if it is found to have a smaller error.
NeuralNetwork.is_created()
Returns True
if the neural network is created.
NeuralNetwork.create()
Create the neural network.
NeuralNetwork.recreate()
Recreate the neural network.
NeuralNetwork.delete()
Delete the neural network.
NeuralNetwork.info()
Returns a dict
with info about the neural network. The keys are the same that are found on nr.info
command.
More examples
To see how to use this library please see the tests directory -- there's an example of a neural network that can do additions and one that can answer if a Titanic passenger will survive or not.
To run the examples:
$ python -m tests.test_addition
$ python -m tests.test_titanic