OpenANN  1.1.0
An open source library for artificial neural networks.
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
Sine

Data Set

In this example, a sine function will be approximated from noisy measurements. This is an example for nonlinear regression. To run this example, you have to install matplotlib. It is a plotting library for Python.

Code

1 ## \page Sine Sine
2 #
3 # \section DataSet Data Set
4 #
5 # In this example, a sine function will be approximated from noisy measurements.
6 # This is an example for nonlinear regression. To run this example, you have
7 # to install matplotlib. It is a plotting library for Python.
8 #
9 # \section Code
10 #
11 # \include "sine/sine.py"
12 
13 try:
14  import pylab
15 except:
16  print("Matplotlib is required")
17  exit(1)
18 from openann import *
19 import numpy
20 
21 # Create network
22 net = Net()
23 net.set_regularization(0.0, 0.0001, 0.0)
24 net.input_layer(1)
25 net.fully_connected_layer(10, Activation.LOGISTIC)
26 net.fully_connected_layer(10, Activation.LOGISTIC)
27 net.output_layer(1, Activation.LINEAR)
28 
29 # Create dataset
30 X = numpy.linspace(0, 2*numpy.pi, 500)[:, numpy.newaxis]
31 T = numpy.sin(X) + numpy.random.randn(*X.shape) * 0.1
32 dataset = DataSet(X, T)
33 
34 Log.info("Using %d samples with %d inputs and %d outputs"
35  % (dataset.samples(), dataset.inputs(), dataset.outputs()))
36 
37 # Train network
38 stop = {
39  "maximal_iterations": 50,
40  "minimal_value_differences": 1e-8
41 }
42 lma = LMA(stop)
43 lma.optimize(net, dataset)
44 
45 # Predict data
46 Y = net.predict(X)
47 
48 # Plot dataset and hypothesis
49 pylab.plot(X, T, ".", label="Data Set")
50 pylab.plot(X, Y, label="Prediction", linewidth=3)
51 pylab.legend()
52 pylab.show()