Skip to content

Using the Neural Network

Drew French edited this page Jan 26, 2014 · 4 revisions

A neural network is a pattern recognition algorithm capable of recognizing patterns in new/unseen data based off of previous experience.

The snippet below makes a nonrecurrent neural network object with three input nodes, which hold the strings a, b, and c. A single hidden layer with four nodes. And an output layer that holds the strings d, e, and f. The name argument assigns a name to the network, so it can easily be retrieved from the database later on.

import random

import nlplib

# Neural network connection weights are initialized pseudorandomly, this call makes the results deterministic.
# This isn't necessary but, can be useful for testing.
random.seed(0)

nn = nlplib.NeuralNetwork(['a', 'b', 'c'], 4, ['d', 'e', 'f'],
                          name='some neural network')

About Names

Neural network names can be more than just strings. Internally the name is encoded in JSON format, using Python's awesome json library. So they can be any object that the function json.dumps will accept. However because of this, there are a few minor quirks. For instance, if the name is a dictionary with numeric keys, the keys will be converted to strings when storing the network in the database. Names must also be unique, unless they're None (the default). Attempting to store a network with a non-unique name will result in an error.

Now, back on topic...

# These scores are pretty worthless because the network hasn't been trained yet.
print('before training')
for score in nn.predict(('a', 'b')) :
    print(score)

Here we asked the neural network to make a prediction about d, e, and f, given a and b as inputs. This returns an iterable of score objects which denote how “confident” the network is in each one of the output strings. 1.0 is a high score meaning the network is very confident in the choice, -1.0 is a low score meaning the network has very little confidence in the choice. Because the network hasn't been trained yet, the resulting scores are pretty meaningless.

Training the network is pretty straight forward. Simply supplying the input nodes, with the desired output is all that's needed. An optional rate argument adjusts how quickly the network learns. Though it's worth mentioning that if the rate is too high, the network may over-fit the data, resulting in poor prediction performance.

# Do some training!
rate = 0.2
for _ in range(100) :
    nn.train(('a', 'b'), ('f',), rate=rate)
    nn.train(('b'), ('e',), rate=rate)

The results below are what we'd expect considering the training data.

# "f" gets the highest score here, as expected.
print('testing a and b')
for score in nlplib.Scored(nn.predict(('a', 'b'))).sorted() :
    print(score)

print()

# "e" gets the highest score here.
print('testing only b')
for score in nlplib.Scored(nn.predict(('b',))).sorted() :
    print(score)

Here is where things get interesting. The network has never seen a on its own before; however, it gives a reasonable guess of f.

print('testing only a')
for score in nlplib.Scored(nn.predict(('a',))).sorted() :
    print(score)

It's trivial to store the network in the database for later usage. More information on how to use the database can be found here.

db = nlplib.Database()
with db as session :
    session.add(nn)

with db as session :
    # Here we retrieve the network from the database. The shortened form <session.access.nn> can also be used     
    # here.
    nn_from_db = session.access.neural_network('some neural network')

    print('testing a and b, again')
    for score in nlplib.Scored(nn_from_db.predict(('a', 'b'))).sorted() :
        print(score)

The full source text for this demo can be found here.

Clone this wiki locally