Classify data with neural network with many input layers
Code: "classify_neural_net_plot.py". Programming language: Python
DMelt Version 2.2. Last modified: 03/13/2018. License: Pro
https://datamelt.org/code/cache/classify_neural_net_plot_7061.py
To run this script using the DMelt IDE,
copy the above URL link to the menu [File]→[Read script from URL] of the DMelt IDE.
"""
Multilayer perceptron neural network. It can be used for deep learning by
creating muptiple hidden layers.
This MLP consists of several layers of nodes, interconnected through weighted
acyclic arcs from each preceding layer to the following, without lateral or
feedback connections.
It has many input and output layers. Add additional layers iin line 45
This example shows the error rate.
"""
from smile.data import AttributeDataset,NominalAttribute
from smile.data.parser import DelimitedTextParser,IOUtils
from smile.classification import NeuralNetwork
from smile.math import Math
from jarray import zeros,array
from jhplot import *
import java
# this function extract x[][] and y[] array from datasets
def getJavaArrays(dataset):
rows=dataset.size()
lst = [0.0]*rows
twoDimArr = array([lst,[]], java.lang.Class.forName('[D'))
x = dataset.toArray(twoDimArr)
y = dataset.toArray(zeros(rows, "i"))
return x,y
parser =DelimitedTextParser()
parser.setDelimiter("[\t ]+")
parser.setResponseIndex(NominalAttribute("class"), 0)
http="http://datamelt.org/examples/data/usps/"
datasource="zip.train"
print "Reading ",datasource," from from",http
print Web.get(http+datasource)
train=parser.parse("Train",java.io.File(datasource))
x,y=getJavaArrays(train)
print "Normalize data.."
p = len(x[0])
mu = Math.colMeans(x);
sd = Math.colSds(x);
for i in range(len(x)):
for j in range(p):
x[i][j] = (x[i][j] - mu[j]) / sd[j];
nin=len(x[0]); nout=Math.max(y)+1
print "Training: Nr input layers=",nin," Nr of output layers=",nout
nn=[nin,50,nout] # 50 hidden layers. Need another layer? Add integer after 50
print "NN layout=",nn
net = NeuralNetwork(NeuralNetwork.ErrorFunction.LEAST_MEAN_SQUARES, NeuralNetwork.ActivationFunction.LOGISTIC_SIGMOID,nn)
c1 = SPlot()
c1.visible()
c1.setGTitle("Neural Network output error")
c1.setAutoRange()
c1.setMarksStyle('various')
c1.setConnected(1, 0)
c1.setNameX('Epoch')
c1.setNameY('Error')
for j in range(70):
net.learn(x, y)
error=0.0
for i in range(len(x)):
if (net.predict(x[i]) != y[i]): error +=1
error=error / len(x)
c1.addPoint(0,j,error,1)
c1.update()
print "Epoch=",j," Error=",error
datasource="zip.test"
print Web.get(http+datasource)
print "Testing ",datasource," from from",http
test=parser.parse("Test",java.io.File(datasource))
testx,testy=getJavaArrays(test)
for i in range(len(testx)):
for j in range(p):
testx[i][j] = (testx[i][j] - mu[j]) / sd[j];
error=0.0
for i in range(len(testx)):
if (net.predict(testx[i]) != testy[i]): error +=1
print "Error rate =", 100.0 * error / len(testx),"%"
You see the box below because you did not login.