Neural networks

Artificial Neural Networks (ANN) are models inspired by the animal brain (highly evolved animals). A neural network is a network of neurons—units with inputs and outputs. For example, the input can be a value related to the pixel of an image and the output of a neuron can be passed to another neuron and so on, thus creating a multilayered network. Neural networks contain adaptive elements making them suitable to deal with nonlinear models and pattern recognition problems. We will again try to predict whether it is going to rain based on day-of-the-year and previous day values. Let's use the theanets Python library, which can be installed as follows:

$ sudo pip install theanets
$ pip freeze|grep theanets
theanets==0.2.0

One of the technical reviewers encountered an error, which was resolved by updating NumPy and SciPy. We first create an Experiment corresponding to a neural network and then train the network. Create a network with two input neurons and one output neuron:

e = theanets.Experiment(theanets.Regressor,
                        layers=(2, 3, 1),
                        learning_rate=0.1,
                        momentum=0.5,
                        patience=300,
                        train_batches=multiprocessing.cpu_count(),
                        num_updates=500)

The network has a hidden layer with three neurons and uses the standard Python multiprocessing API to speed up computations. Train using a training and validation dataset:

train = [x[:N], y[:N]]
valid = [x[N:], y[N:]]
e.run(train, valid)

Get predictions for the validation data, as follows:

pred = e.network(x[N:]).ravel()

The scikit-learn library has a utility function, which computes the accuracy of a classifier. Compute the accuracy as follows:

print "Pred Min", pred.min(), "Max", pred.max()
print "Y Min", y.min(), "Max", y.max()
print "Accuracy", accuracy_score(y[N:], pred >= .5)

Due to the nature of neural nets, the output values can vary. The output may look like the following:

Pred Min 0.303503170562 Max 0.737862165479
Y Min 0.0 Max 1.0
Accuracy 0.632345426673

Refer to the neural_net.py file in this book's code bundle:

import numpy as np
import theanets
import multiprocessing
from sklearn import datasets
from sklearn.metrics import accuracy_score


rain = .1 * np.load('rain.npy')
rain[rain < 0] = .05/2
dates = np.load('doy.npy')
x = np.vstack((dates[:-1], np.sign(rain[:-1])))
x = x.T

y = np.vstack(np.sign(rain[1:]),)
N = int(.9 * len(x))

e = theanets.Experiment(theanets.Regressor,
                        layers=(2, 3, 1),
                        learning_rate=0.1,
                        momentum=0.5,
                        patience=300,
                        train_batches=multiprocessing.cpu_count(),
                        num_updates=500)

train = [x[:N], y[:N]]
valid = [x[N:], y[N:]]
e.run(train, valid)

pred = e.network(x[N:]).ravel()
print "Pred Min", pred.min(), "Max", pred.max()
print "Y Min", y.min(), "Max", y.max()
print "Accuracy", accuracy_score(y[N:], pred >= .5)
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.218.239.182