 |
Read a CSV file and do backpropogation with Encog, and then analyse it
Source code name: "neural_net_encog2.py"
Programming language: Python
Topic: Artificial Intelligence/neural net
DMelt Version 1. Last modified: 12/18/2016. License: Pro
https://datamelt.org/code/cache/neural_net_encog2_3587.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.
# Licensed under the Apache License, Version 2.0 (the "License");
# based on http://www.heatonresearch.com/encog/
"""
/**
* XOR: This example is essentially the "Hello World" of neural network
* programming. This example shows how to construct an Encog neural network to
* predict the output from the XOR operator. This example uses resilient
* propagation (RPROP) to train the neural network. RPROP is the best general
* purpose supervised training method provided by Encog.
*
* For the XOR example with RPROP I use 4 hidden neurons. XOR can get by on just
* 2, but often the random numbers generated for the weights are not enough for
* RPROP to actually find a solution. RPROP can have issues on really small
* neural networks, but 4 neurons seems to work just fine.
*
* This example reads the XOR data from a CSV file. This file should be
* something like:
*
**/
"""
from org.encog import Encog
from org.encog.util.csv import CSVFormat
from org.encog.util.simple import EncogUtility,TrainingSetUtil
from org.encog.visualize import NetworkVisualizeFrame
from org.encog.neural.networks.structure import AnalyzeNetwork
from org.encog.util.obj import SerializeObject
from java.io import *
# create data file
data="""
0,0,0
1,0,1
0,1,1
1,1,0
"""
name="test.csv"
fi=open(name, "w")
fi.write(data)
fi.close()
trainingSet=TrainingSetUtil.loadCSVTOMemory(CSVFormat.ENGLISH,name,False,2,1)
net=EncogUtility.simpleFeedForward(2,4,0,1,True)
EncogUtility.trainToError(net, trainingSet, 0.01)
EncogUtility.evaluate(net, trainingSet)
SerializeObject.save(File("network.eg"), net) # save
# restore the network
network=SerializeObject.load(File("network.eg"))
f=NetworkVisualizeFrame(network)
f.setDefaultCloseOperation(1)
f.setVisible(1)
print "analyze network"
a=AnalyzeNetwork(network)
print "Analyze network:"
a=AnalyzeNetwork(net)
print a.toString()
print "Values=",a.getAllValues()
print "Weights=",a.getWeightValues()
print "Nr of connections=",a.getTotalConnections()
You see the box below because you did not login.