- Source For This Article: Download demo project - 54 Kb
Contents
- 1. Overview
- 2. What is Neural XML
- 2.A) How To Begin
- 2.B) Illustration 1 - A Simple NXML Application - Creating Digital Gates
- 2.C) Illustration 2 - How To Use NXML for Brain Tumor Detection
- Conclusion
1. Overview
![]() |
What about developing an XML based language, to help you create, train and run your own neural networks?
These articles about nxml will essentially explain two important points.
Now, let us see how this articles may help you. If you are a neural network enthusiast, or if you want to get some initial concepts about neural networks, you can use NXML to
|
|
2. What is Neural XML?
In this part, we will discuss what exactly is Neural XML, and how to use it.As you already know, a neural network consists of various layers, and each layer has a number of neurons in it. Initially, we will train the neural network, by providing the inputs to the input neurons, and by providing the output to the output neurons. Once the neural network is trained, you can run it.
2.A) How To Begin
Let us see how to begin with neural xml.Step 1: Get Neural XML
Where to get Neural XML?The binaries for nxml, and images and other files required for this article, is available.
The full source code of Neural XML, and BrainNet Neural Network library is available.
- Download NXML Project And BrainNet library source code (Visual Studio.NET Project)
- See this article about Neural Networks and Brain Net library.
Step 2: Run NXML and have a look at it.
Run nxml tool from the command prompt. If you run nxml with out any parameters, it will give you the following message.| C:\BrainNet\Nxml\Bin> nxml
You can use this program to create a multi layer back prop network, train it and run it All Rights Reserved (C) 2005-2006, Anoop Madhusudanan, http://amazedsaint.blogspot.com Syntax: ----------- Syntax1: nxml -gen NeuronsInLayer1,NeuronsInLayer2,NeuronsInLayerN filename Syntax2: nxml -start filename Use the -start switch to train an run a network using an nxml file. Use the -gen switch to create a back propagation network and save it to a file Examples: ----------- Example1: nxml -gen 10,10,4 mynetwork.xml The above example will create a network with 3 layers - 10 neurons in input layer, 10 neurons in hidden layer, and 4 neurons in output layer. Example2: nxml -gen 3,3,3,2 mynetwork.xml The above example will create a network with 4 layers - 3 neurons in input layer, 3 neurons in first hidden layer, 3 neurons in second hidden layer, and 2 neurons in output layer. Example3: nxml -start mynetworktrian.nxml The above example will execute the mynetworktrain.nxml file to train and run the network |
2.B) Illustration 1 - A Simple NXML Application - Creating Digital Gates
Now, an interesting example. Let us see how we can create a 2-2-1 neural network (2 neurons in input layer, 2 neurons in hidden layer, and 1 neuron in output layer) to represent a digital gate (like and AND gate, or OR gate), using NXML. Then, we will train our 2-2-1 network to perform functions like AND, OR, XOR etc. See the previous articles for more information about neural networks (read them here Part I and Part II). If you examine my previous article, you can see that how we did this earlier, but using .NET code. This time we are doing the same - but using our own language, that is NXML.
Fig: A Simple 2-2-1 Network
What We Are Going To Do:
Training and running an application using NXML involves,- Creating a neural network using nxml tool
- Training the neural network you created
- Running the neural network
Step 1: Creating A 2-2-1 Neural Network
To create a 2-2-1 neural network. You can use the -gen switch of nxml. The syntax is,Syntax: nxml -gen NeuronsInLayer1,NeuronsInLayer2,NeuronsInLayerN filename
As the first parameter, we will pass the number of neurons in each layer, separated with commas. In this case, it is 2,2,1 - i.e, 2 input neurons, 2 hidden layer neurons and 1 output neuron. The second parameter is the file name.
Run nxml with the following arguments to create a 2-2-1 network and store it in gate.xml
| nxml -gen 2,2,1 gate.xml |
Now, just have the look at the neuron L1N0 (0th Neuron in Layer1). You can see a tag 'Connections' - which define which all neurons in previous layer is connected to this neuron.<html> Please note that each connection input holds a 'weight'.
Also, each neuron has a Bias, Output a<html>nd Delta value, which holds these parameters of the neuron.<html>
File: Gate.xml<html>
<code><?xml version="1.0" encoding="utf-8"?>
<Network>
<Layer Name="Layer0">
<Neuron Name="L0N0">
<Bias>0.7982105016708374</Bias>
<Output>0</Output>
<Delta>0</Delta>
</Neuron>
<Neuron Name="L0N1">
<Bias>-0.3051917552947998</Bias>
<Output>0</Output>
<Delta>0</Delta>
</Neuron>
</Layer>
<Layer Name="Layer1">
<Neuron Name="L1N0">
<Bias>-0.74571740627288818</Bias>
<Output>0</Output>
<Delta>0</Delta>
<Connections>
<Input Layer="0" Neuron="1" Weight="0.25023746490478516" />
<Input Layer="0" Neuron="0" Weight="-0.68162941932678223" />
</Connections>
</Neuron>
<Neuron Name="L1N1">
<Bias>0.044701099395751953</Bias>
<Output>0</Output>
<Delta>0</Delta>
<Connections>
<Input Layer="0" Neuron="1" Weight="-0.41200506687164307" />
<Input Layer="0" Neuron="0" Weight="-0.14756286144256592" />
</Connections>
</Neuron>
</Layer>
<Layer Name="Layer2">
<Neuron Name="L2N0">
<Bias>-0.32655441761016846</Bias>
<Output>0</Output>
<Delta>0</Delta>
<Connections>
<Input Layer="1" Neuron="1" Weight="-0.9166187047958374" />
<Input Layer="1" Neuron="0" Weight="-0.4252774715423584" />
</Connections>
</Neuron>
</Layer>
</Network>
Step 2: Training the Neural Network
Now, let us train gate.xml to perform three functions - AND, OR and XOR. To train the network, we'll- Create a 'trainer' nxml file, trainGate.n.xml, which holds information to train the network
- Execute the trainer nxml file on our neural network - i.e, gate.xml
<code><?xml version="1.0" encoding="utf-8"?>
<NXML>
<Network LoadPath="gate.xml" SaveOnFinish="true"
SavePath="OR.xml">
<DataBlock Type="Train" TrainCount="2000">
<PatternData InputType="Pattern" InputValue="00"
OutputType="Pattern" OutputValue="0" />
<PatternData InputType="Pattern" InputValue="01"
OutputType="Pattern" OutputValue="1" />
<PatternData InputType="Pattern" InputValue="10"
OutputType="Pattern" OutputValue="1" />
<PatternData InputType="Pattern" InputValue="11"
OutputType="Pattern" OutputValue="1" />
</DataBlock>
</Network>
<Network LoadPath="gate.xml" SaveOnFinish="true"
SavePath="AND.xml">
<DataBlock Type="Train" TrainCount="2000">
<PatternData InputType="Pattern" InputValue="00"
OutputType="Pattern" OutputValue="0" />
<PatternData InputType="Pattern" InputValue="01"
OutputType="Pattern" OutputValue="0" />
<PatternData InputType="Pattern" InputValue="10"
OutputType="Pattern" OutputValue="0" />
<PatternData InputType="Pattern" InputValue="11"
OutputType="Pattern" OutputValue="1" />
</DataBlock>
</Network>
<Network LoadPath="gate.xml" SaveOnFinish="true"
SavePath="XOR.xml">
<DataBlock Type="Train" TrainCount="4000">
<PatternData InputType="Pattern" InputValue="00"
OutputType="Pattern" OutputValue="1" />
<PatternData InputType="Pattern" InputValue="01"
OutputType="Pattern" OutputValue="0" />
<PatternData InputType="Pattern" InputValue="10"
OutputType="Pattern" OutputValue="0" />
<PatternData InputType="Pattern" InputValue="11"
OutputType="Pattern" OutputValue="1" />
</DataBlock>
</Network>
</NXML>
The above code, to train the network, is written according to the nxml specifications. Kindly see this brief introduction to NXML language.| NXML LANGUAGE - A BRIEF OVERVIEW | |
|
| nxml -start trainGate.n.xml |
Step 3: Running the Neural Network
Now we have three trained neural networks. OR.xml which can perform functions of an OR gate, AND.xml which can perform functions of an AND gate and XOR.xml which can perform functions of an XOR gate.Let us see how to 'run' these networks. Let us create a file, runGate.n.xml, to run these networks.
The only change is that, the Type of Datablock is changed to 'Run' from 'Train'. Also, we are not providing any OutputValues - because we expect the network to predict and write these output values. Kindly note that the network xml files (like or.xml, and.xml and xor.xml) are not undergoing any changes - because we are running the network, and not training it.
File: runGate.n.xml before running it with nxml interpreter
<code><?xml version="1.0" encoding="utf-8"?>
<NXML>
<Network LoadPath="or.xml" SaveOnFinish="true"
SavePath="or.xml">
<DataBlock Type="Run">
<PatternData InputType="Pattern" InputValue="00"
OutputType="Pattern"/>
<PatternData InputType="Pattern" InputValue="01"
OutputType="Pattern"/>
<PatternData InputType="Pattern" InputValue="10"
OutputType="Pattern"/>
<PatternData InputType="Pattern" InputValue="11"
OutputType="Pattern"/>
</DataBlock>
</Network>
<Network LoadPath="AND.xml" SaveOnFinish="true"
SavePath="AND.xml">
<DataBlock Type="Run">
<PatternData InputType="Pattern" InputValue="00"
OutputType="Pattern"/>
<PatternData InputType="Pattern" InputValue="01"
OutputType="Pattern"/>
<PatternData InputType="Pattern" InputValue="10"
OutputType="Pattern"/>
<PatternData InputType="Pattern" InputValue="11"
OutputType="Pattern"/>
</DataBlock>
</Network>
<Network LoadPath="xor.xml" SaveOnFinish="true"
SavePath="xor.xml">
<DataBlock Type="Run">
<PatternData InputType="Pattern" InputValue="00"
OutputType="Pattern"/>
<PatternData InputType="Pattern" InputValue="01"
OutputType="Pattern"/>
<PatternData InputType="Pattern" InputValue="10"
OutputType="Pattern"/>
<PatternData InputType="Pattern" InputValue="11"
OutputType="Pattern"/>
</DataBlock>
</Network>
</NXML>
Now, to run the neural network, we can invoke the nxml interpreter using the following command, in the command prompt.| nxml -start runGate.n.xml |
File: runGate.n.xml after running it with nxml interpreter. Kindly see that OutputValues are predicted correctly.
<code><?xml version="1.0" encoding="utf-8"?>
<NXML>
<Network LoadPath="or.xml" SaveOnFinish="true"
SavePath="or.xml">
<DataBlock Type="Run">
<PatternData InputType="Pattern" InputValue="00"
OutputType="Pattern" OutputValue="0" />
<PatternData InputType="Pattern" InputValue="01"
OutputType="Pattern" OutputValue="1" />
<PatternData InputType="Pattern" InputValue="10"
OutputType="Pattern" OutputValue="1" />
<PatternData InputType="Pattern" InputValue="11"
OutputType="Pattern" OutputValue="1" />
</DataBlock>
</Network>
<Network LoadPath="AND.xml" SaveOnFinish="true"
SavePath="AND.xml">
<DataBlock Type="Run">
<PatternData InputType="Pattern" InputValue="00"
OutputType="Pattern" OutputValue="0" />
<PatternData InputType="Pattern" InputValue="01"
OutputType="Pattern" OutputValue="0" />
<PatternData InputType="Pattern" InputValue="10"
OutputType="Pattern" OutputValue="0" />
<PatternData InputType="Pattern" InputValue="11"
OutputType="Pattern" OutputValue="1" />
</DataBlock>
</Network>
<Network LoadPath="xor.xml" SaveOnFinish="true"
SavePath="xor.xml">
<DataBlock Type="Run">
<PatternData InputType="Pattern" InputValue="00"
OutputType="Pattern" OutputValue="1" />
<PatternData InputType="Pattern" InputValue="01"
OutputType="Pattern" OutputValue="0" />
<PatternData InputType="Pattern" InputValue="10"
OutputType="Pattern" OutputValue="0" />
<PatternData InputType="Pattern" InputValue="11"
OutputType="Pattern" OutputValue="1" />
</DataBlock>
</Network>
</NXML>
Now, change the Output Type from Pattern to Array, and see what happens. You will get the exact output values in comma separated decimals if there are more than one neuron in the output layer (in this case, there is only one neuron in the output layer). Pattern data type always rounds the output to 1 or 0.2.C) Illustration 2 - How To Use NXML for 'Brain Tumor Detection'
Now, let us see how you can create a neural network, train it and run it to detect brain tumors from the image of brain, using Neural XML.I'm just trying to make things interesting. Of course, please understand that the samples used in this illustration are not 'real' images of the brain - they are just some 'virtual' images, and is meant purely for demonstration.
What We Are Going To Do:
Assume that we have the following images, and assume that each image is an x-ray of the brain. Each image has a size of 16 x 16 = 256 pixels.We are using 13 images to train the network. According to the density of white pixels on a corner, we are assigning a value or condition (0,1,2 or 3) to each image, starting counter clockwise from the bottom left corner. The value of each image is shown below.
(1)
=0 (2)
=0 (3)
= 1 (4)
=1 (5)
=2 (6)
=2
(7)
=3 (8)
=3 (9)
=3 (10)
=3 (11)
=2 (12)
=1 (13)
=0
We are using the density of white pixels on a particular corner, to detect the possibility of Brain Tumor. For example, see the first image. It has more white pixels at bottom left.We assume that,
- If the density of white is at bottom left corner, the condition is 0
- If the density of white is at bottom right, the condition is 1
- If the density of white is at top right, the condition is 2
- If the density of white is at top left, the condition is 3
- Condition 0 - No problem, brain has no tumor
- Condition 1 - Some what infected
- Condition 2 - Infected
- Condition 3 - Critical, should change the brain ;)
Once samples are selected and conditions defined, we will,
- Create a network which can learn from the samples
- Train the network with a number of samples (Training phase)
- Give an image to the network, and ask it to predict the condition (Running Phase)
Step 1: Create A Neural Network
To create a network, we should determine- The number of neurons in input layer.
- The number of neurons in hidden layer and the number of hidden layers
- The number of neurons in output layer.
- Neurons in input layer - As I mentioned earlier, we are providing a 16 x 16 image as input. When we digitize this picture, we can see that there are 16 x 16=256 pixels in each image. So, let us take 256 neurons in input layer - we will feed the value of each pixel (1 for white pixel and 0 for black) to each neuron in the input layer.
- Neurons in output layer - We have four conditions - condition 0 (00 in binary), condition 1 (01 in binary), condition 2 (10 in binary), and condition 3 (11 in binary) - so, let us take two neurons in the output layer, because the highest output value we need (i.e, 3) requires two bits to represent it.
- Neurons in hidden layer - Let us blindly assume that we have one hidden layer, and it has the number of neurons equal to the neurons in the input layer.
| nxml -gen 256,256,2 DensityDetect.xml |
Step 2: Training The Neural Network
For training the neural network, let us create an nxml file, train.n.xml. All images are in the samples folder, relative to the folder where train.n.xml resides.<code><?xml version="1.0" encoding="utf-8"?>
<NXML>
<Network LoadPath="DensityDetect.xml" SaveOnFinish="true"
SavePath="DensityDetect.xml">
<DataBlock Type="Train" TrainCount="3000">
<ImageData InputFile="samples\img1.jpg" InputWidth="16"
InputHeight="16" OutputValue="0"
OutputType="Number" />
<ImageData InputFile="samples\img2.jpg" InputWidth="16"
InputHeight="16" OutputValue="0"
OutputType="Number" />
<ImageData InputFile="samples\img3.jpg" InputWidth="16"
InputHeight="16" OutputValue="1"
OutputType="Number" />
<ImageData InputFile="samples\img4.jpg" InputWidth="16"
InputHeight="16" OutputValue="1"
OutputType="Number" />
<ImageData InputFile="samples\img5.jpg" InputWidth="16"
InputHeight="16" OutputValue="2"
OutputType="Number" />
<ImageData InputFile="samples\img6.jpg" InputWidth="16"
InputHeight="16" OutputValue="2"
OutputType="Number" />
<ImageData InputFile="samples\img7.jpg" InputWidth="16"
InputHeight="16" OutputValue="3"
OutputType="Number" />
<ImageData InputFile="samples\img8.jpg" InputWidth="16"
InputHeight="16" OutputValue="3"
OutputType="Number" />
<ImageData InputFile="samples\img9.jpg" InputWidth="16"
InputHeight="16" OutputValue="3"
OutputType="Number" />
<ImageData InputFile="samples\img11.jpg" InputWidth="16"
InputHeight="16" OutputValue="2"
OutputType="Number" />
<ImageData InputFile="samples\img12.jpg" InputWidth="16"
InputHeight="16" OutputValue="1"
OutputType="Number" />
<ImageData InputFile="samples\img13.jpg" InputWidth="16"
InputHeight="16" OutputValue="0"
OutputType="Number" />
</DataBlock>
</Network>
</NXML>
Kindly note that, we used Image Data tag instead of Pattern Data tag. For Image Data tag, you can specify the InputFile attribute, which specifies the image. Along with the image path, you should specify the input image's width and height. If the image size is greater than this, the image will be resized to this size.For each image, we specified the OutputValue as the number corresponding to the condition we discussed earlier. Now, let us start training. For example, for Image 1, the output value we provided to train the network is zero.
| nxml -start train.n.xml |
Step 3: Running The Neural Network
Once the network is trained, you can use it to check image samples. To illustrate this, let us create an nxml file to run the network. We are going to detect the following images.(1)
The run.n.xml file is given below. All files are in test folder, relative to the folder where run.n.xml resides. Please note that any of these images are not among the list of images we used for training.
File: run.n.xml before executing it using nxml
<code><?xml version="1.0" encoding="utf-8"?>
<NXML>
<Network LoadPath="DensityDetect.xml" SaveOnFinish="true"
SavePath="DensityDetect.xml">
<DataBlock Type="Run">
<ImageData InputFile="test\testimg1.jpg" InputWidth="16"
InputHeight="16" OutputValue="3"
OutputType="Number" />
<ImageData InputFile="test\testimg2.jpg" InputWidth="16"
InputHeight="16" OutputValue="2"
OutputType="Number" />
<ImageData InputFile="test\testimg3.jpg" InputWidth="16"
InputHeight="16" OutputValue="2"
OutputType="Number" />
<ImageData InputFile="test\testimg4.jpg" InputWidth="16"
InputHeight="16" OutputValue="1"
OutputType="Number" />
<ImageData InputFile="test\testimg5.jpg" InputWidth="16"
InputHeight="16" OutputValue="2"
OutputType="Number" />
<ImageData InputFile="test\testimg6.jpg" InputWidth="16"
InputHeight="16" OutputValue="3"
OutputType="Number" />
<ImageData InputFile="test\testimg7.jpg" InputWidth="16"
InputHeight="16" OutputValue="1"
OutputType="Number" />
<ImageData InputFile="test\testimg8.jpg" InputWidth="16"
InputHeight="16" OutputValue="1"
OutputType="Number" />
</DataBlock>
</Network>
</NXML>
Now, let us run the network by issuing the following command.| nxml -start run.n.xml |
File: run.n.xml after executing it using nxml. You can find that the neural network wrote all the output values correctly, by detecting each image properly.
<code><?xml version="1.0" encoding="utf-8"?>
<NXML>
<Network LoadPath="DensityDetect.xml" SaveOnFinish="true"
SavePath="DensityDetect.xml">
<DataBlock Type="Run">
<ImageData InputFile="test\testimg1.jpg" InputWidth="16"
InputHeight="16" OutputValue="3"
OutputType="Number" />
<ImageData InputFile="test\testimg2.jpg" InputWidth="16"
InputHeight="16" OutputValue="2"
OutputType="Number" />
<ImageData InputFile="test\testimg3.jpg" InputWidth="16"
InputHeight="16" OutputValue="2"
OutputType="Number" />
<ImageData InputFile="test\testimg4.jpg" InputWidth="16"
InputHeight="16" OutputValue="1"
OutputType="Number" />
<ImageData InputFile="test\testimg5.jpg" InputWidth="16"
InputHeight="16" OutputValue="2"
OutputType="Number" />
<ImageData InputFile="test\testimg6.jpg" InputWidth="16"
InputHeight="16" OutputValue="3"
OutputType="Number" />
<ImageData InputFile="test\testimg7.jpg" InputWidth="16"
InputHeight="16" OutputValue="1"
OutputType="Number" />
<ImageData InputFile="test\testimg8.jpg" InputWidth="16"
InputHeight="16" OutputValue="1"
OutputType="Number" />
</DataBlock>
</Network>
</NXML>


4 comments:
Hi again,
My name is Behnam, I'm 25, from Iran and I studied computer science. Artificial intelligence is my love and I am very interested in it. It was more than 3 months ago that I have the chance of finding your articles on Codeproject web site. Your article simply over excited me.
I have used your materials on Neural Networks and your BrainNet library at the university for a lecture. I have mentioned the source and gave this weblog address to my classmates and the professor. They were very excited, just like me.
Please continue to work on such things.
Good Luck
Regards
Behnam
By the way, I've forgot tho tell you that I am working on your Brainnet library and the handwriting recognition application to turn it into a Farsi language OCR. It is currently unable to return the results in Farsi language. Although, it can recognise the patterns very well.
Thanks
Behnam
Hi
I am just beginning to get into this area. My interest is in using NN for refining weather forecast data. I could not get the NXML to download. Nothing happens when I click on the link. Just blank screen.
Have you any information on similar applications of NN.
Thanks
The links should be working now
Post a Comment
Please keep your comments clean.