Introduction
This example implements a Multi Layer Neural Network, which will behave as an XOR gate after being trained using the Back Propergation Algorithm.
The network has only two layers, the input layer and the output layer. It does not have any hidden layers. The input layer consist of two neurons and the output layer consist of only one neuron
Complete Source Code
import java.awt.*;
import java.awt.event.*;
import java.awt.image.*;
import javax.swing.*;
import java.io.*;
//*********************************************************************
class ImagePanel extends JPanel {
Image image;
public ImagePanel(Image image) {
this.image = image;
}
public void paintComponent(Graphics g) {
super.paintComponent(g); //paint background
//Draw image at its natural size.
g.drawImage(image, 0, 0, this); //85x62 image
}
}
//*********************************************************************
public class MultiLayer{
public static void main(String args[]){
Operations op=new Operations();
op.getInterface();
}
}
//*********************************************************************
class Operations{
//create arrays to hold the weights
double w1[]=new double[3];
double w2[]=new double[3];
double w3[]=new double[3];
double u;
void getInterface(){
String imageFile = "MultiLayer.gif";
String[] values={"0","1"};
Image image = Toolkit.getDefaultToolkit().getImage(imageFile);
if (image!=null){
final JFrame tstFrame = new JFrame("Multi Layer Neural Network");
tstFrame.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
Container tstPane=tstFrame.getContentPane();
ImagePanel imagePanel = new ImagePanel(image);
SpringLayout layout = new SpringLayout();
imagePanel.setLayout(layout);
tstPane.add(imagePanel, BorderLayout.CENTER);
//text fields for the weights
final JTextField w[]=new JTextField[9];
w[0]=new JTextField("", 5);
w[0].setEditable(false);
imagePanel.add(w[0]);
w[1]=new JTextField("", 5);
w[1].setEditable(false);
imagePanel.add(w[1]);
w[2]=new JTextField("", 5);
w[2].setEditable(false);
imagePanel.add(w[2]);
w[3]=new JTextField("", 5);
w[3].setEditable(false);
imagePanel.add(w[3]);
w[4]=new JTextField("", 5);
w[4].setEditable(false);
imagePanel.add(w[4]);
w[5]=new JTextField("", 5);
w[5].setEditable(false);
imagePanel.add(w[5]);
w[6]=new JTextField("", 5);
w[6].setEditable(false);
imagePanel.add(w[6]);
w[7]=new JTextField("", 5);
w[7].setEditable(false);
imagePanel.add(w[7]);
w[8]=new JTextField("", 5);
w[8].setEditable(false);
imagePanel.add(w[8]);
final JTextField outText=new JTextField("", 5);
outText.setEditable(false);
imagePanel.add(outText);
final JTextField rateText=new JTextField("0.5", 5);
imagePanel.add(rateText);
final JComboBox tstcombo1=new JComboBox(values);
imagePanel.add(tstcombo1);
final JComboBox tstcombo2=new JComboBox(values);
imagePanel.add(tstcombo2);
JButton tstButton = new JButton("Run");
imagePanel.add(tstButton);
tstButton.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent event){
int x1=Integer.parseInt((String)tstcombo1.getSelectedItem());
int x2=Integer.parseInt((String)tstcombo2.getSelectedItem());
outText.setText(""+run(x1,x2)+"");
}
});
JButton trnButton = new JButton("Train");
imagePanel.add(trnButton);
trnButton.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent event) {
u=Double.parseDouble((String)rateText.getText());
performTrain(w);
JOptionPane.showMessageDialog(tstFrame, "Successfully Trained");
}
});
layout.putConstraint(SpringLayout.WEST,w[0],230,SpringLayout.WEST, imagePanel);
layout.putConstraint(SpringLayout.NORTH,w[0],121,SpringLayout.NORTH, imagePanel);
layout.putConstraint(SpringLayout.WEST,w[1],230,SpringLayout.WEST, imagePanel);
layout.putConstraint(SpringLayout.NORTH,w[1],78,SpringLayout.NORTH, imagePanel);
layout.putConstraint(SpringLayout.WEST,w[2],230,SpringLayout.WEST, imagePanel);
layout.putConstraint(SpringLayout.NORTH,w[2],171,SpringLayout.NORTH, imagePanel);
layout.putConstraint(SpringLayout.WEST,w[3],230,SpringLayout.WEST, imagePanel);
layout.putConstraint(SpringLayout.NORTH,w[3],292,SpringLayout.NORTH, imagePanel);
layout.putConstraint(SpringLayout.WEST,w[4],230,SpringLayout.WEST, imagePanel);
layout.putConstraint(SpringLayout.NORTH,w[4],352,SpringLayout.NORTH, imagePanel);
layout.putConstraint(SpringLayout.WEST,w[5],230,SpringLayout.WEST, imagePanel);
layout.putConstraint(SpringLayout.NORTH,w[5],403,SpringLayout.NORTH, imagePanel);
layout.putConstraint(SpringLayout.WEST,w[6],379,SpringLayout.WEST, imagePanel);
layout.putConstraint(SpringLayout.NORTH,w[6],170,SpringLayout.NORTH, imagePanel);
layout.putConstraint(SpringLayout.WEST,w[7],379,SpringLayout.WEST, imagePanel);
layout.putConstraint(SpringLayout.NORTH,w[7],221,SpringLayout.NORTH, imagePanel);
layout.putConstraint(SpringLayout.WEST,w[8],379,SpringLayout.WEST, imagePanel);
layout.putConstraint(SpringLayout.NORTH,w[8],280,SpringLayout.NORTH, imagePanel);
layout.putConstraint(SpringLayout.WEST,rateText,535,SpringLayout.WEST, imagePanel);
layout.putConstraint(SpringLayout.NORTH,rateText,78,SpringLayout.NORTH, imagePanel);
layout.putConstraint(SpringLayout.WEST,outText,540,SpringLayout.WEST, imagePanel);
layout.putConstraint(SpringLayout.NORTH,outText,218,SpringLayout.NORTH, imagePanel);
layout.putConstraint(SpringLayout.WEST,tstButton,430,SpringLayout.WEST, imagePanel);
layout.putConstraint(SpringLayout.NORTH,tstButton,130,SpringLayout.NORTH, imagePanel);
layout.putConstraint(SpringLayout.WEST,trnButton,500,SpringLayout.WEST, imagePanel);
layout.putConstraint(SpringLayout.NORTH,trnButton,130,SpringLayout.NORTH, imagePanel);
layout.putConstraint(SpringLayout.WEST,tstcombo1,70,SpringLayout.WEST, imagePanel);
layout.putConstraint(SpringLayout.NORTH,tstcombo1,118,SpringLayout.NORTH, imagePanel);
layout.putConstraint(SpringLayout.WEST,tstcombo2,75,SpringLayout.WEST, imagePanel);
layout.putConstraint(SpringLayout.NORTH,tstcombo2,353,SpringLayout.NORTH, imagePanel);
tstFrame.setSize(new Dimension(650,510));
tstFrame.setLocationRelativeTo(null);
tstFrame.setVisible(true);
}
}
void performTrain(JTextField w[]){
for(int i=0;i<3;i++){
w1[i]=Math.random();
w2[i]=Math.random();
w3[i]=Math.random();
}
w[0].setText((Math.round(10000*w1[0])/10000)+"");
w[1].setText((Math.round(10000*w1[1])/10000)+"");
w[2].setText((Math.round(10000*w1[2])/10000)+"");
w[3].setText((Math.round(10000*w2[0])/10000)+"");
w[4].setText((Math.round(10000*w2[1])/10000)+"");
w[5].setText((Math.round(10000*w2[2])/10000)+"");
w[6].setText((Math.round(10000*w3[0])/10000)+"");
w[7].setText((Math.round(10000*w3[1])/10000)+"");
w[8].setText((Math.round(10000*w3[2])/10000)+"");
for (int i=0;i<100000;i++){
train(0,0,0,w);
train(0,1,1,w);
train(1,0,1,w);
train(1,1,0,w);
}
w[0].setText((Math.round(10000*w1[0])/10000)+"");
w[1].setText((Math.round(10000*w1[1])/10000)+"");
w[2].setText((Math.round(10000*w1[2])/10000)+"");
w[3].setText((Math.round(10000*w2[0])/10000)+"");
w[4].setText((Math.round(10000*w2[1])/10000)+"");
w[5].setText((Math.round(10000*w2[2])/10000)+"");
w[6].setText((Math.round(10000*w3[0])/10000)+"");
w[7].setText((Math.round(10000*w3[1])/10000)+"");
w[8].setText((Math.round(10000*w3[2])/10000)+"");
}
void train(double a,double b,double c,JTextField w[]){
double x1=a;
double x2=b;
double t=c;
double m=(1*w1[0])+(x1*w1[1])+(x2*w1[2]);
double y1=1/(1+Math.exp(-m));
double n=(1*w2[0])+(x1*w2[1])+(x2*w2[2]);
double y2=1/(1+Math.exp(-n));
double p=(1*w3[0])+(y1*w3[1])+(y2*w3[2]);
double z =1/(1+Math.exp(-p));
if (t==1 && z>0.95){
return;
}
else{
double d3=z*(1-z)*(t-z);
double d2=y2*(1-y2)*w3[2]*d3;
double d1=y1*(1-y1)*w3[1]*d3;
w1[0]=w1[0]+(u*1*d1);
w1[1]=w1[1]+(u*x1*d1);
w1[2]=w1[2]+(u*x2*d1);
w2[0]=w2[0]+(u*1*d2);
w2[1]=w2[1]+(u*x1*d2);
w2[2]=w2[2]+(u*x2*d2);
w3[0]=w3[0]+(u*1*d3);
w3[1]=w3[1]+(u*x1*d3);
w3[2]=w3[2]+(u*x2*d3);
}
if (t==0 && z<0.05){
return;
}
else{
double d3=z*(1-z)*(t-z);
double d2=y2*(1-y2)*w3[2]*d3;
double d1=y1*(1-y1)*w3[1]*d3;
w1[0]=w1[0]+(u*1*d1);
w1[1]=w1[1]+(u*x1*d1);
w1[2]=w1[2]+(u*x2*d1);
w2[0]=w2[0]+(u*1*d2);
w2[1]=w2[1]+(u*x1*d2);
w2[2]=w2[2]+(u*x2*d2);
w3[0]=w3[0]+(u*1*d3);
w3[1]=w3[1]+(u*x1*d3);
w3[2]=w3[2]+(u*x2*d3);
}
}
double run(double a,double b){
double x1=a;
double x2=b;
double m=(1*w1[0])+(x1*w1[1])+(x2*w1[2]);
double y1=1/(1+Math.exp(-m));
double n=(1*w2[0])+(x1*w2[1])+(x2*w2[2]);
double y2=1/(1+Math.exp(-n));
double p=(1*w3[0])+(y1*w3[1])+(y2*w3[2]);
double z =1/(1+Math.exp(-p));
return Math.round(z);
}
}
Background Image (MultiLayer.gif)
Use above image as the Background Image. Sorry for not uploading a complete description of the coding. But sure that all you smart guys out there will be able to understand this simple code and if I find time I will upload a complete description of the source and the back propagation algorithm.

Hi, please add the discprion also
ReplyDeleteit will help up us and if u have any more info please share here regarding ANN
Thanks in advance
Satya
Please go to the following link which has a better source code and will be easier to derive the equations from it
ReplyDeletehttp://clubsking.blogspot.com/2013/02/complete-source-code-of-back.html
code works. Keep sharing Artificial intelligence Online Trining
ReplyDeleteperde modelleri
ReplyDeletesms onay
mobil ödeme bozdurma
Nft Nasil Alınır
ankara evden eve nakliyat
trafik sigortası
DEDEKTOR
web sitesi kurma
ASK KİTAPLARİ
yurtdışı kargo
ReplyDeletelisans satın al
minecraft premium
uc satın al
en son çıkan perde modelleri
nft nasıl alınır
özel ambulans
en son çıkan perde modelleri
Good content. You write beautiful things.
ReplyDeletevbet
vbet
hacklink
hacklink
sportsbet
korsan taksi
sportsbet
mrbahis
mrbahis
Good text Write good content success. Thank you
ReplyDeletekralbet
slot siteleri
bonus veren siteler
kibris bahis siteleri
poker siteleri
mobil ödeme bahis
tipobet
betmatik
dijital kartvizit
ReplyDeletereferans kimliği nedir
binance referans kodu
referans kimliği nedir
bitcoin nasıl alınır
resimli magnet
Y4X7
شركة تنظيف بالجبيل
ReplyDeleteشركة تنظيف
شركة مكافحة الصراصير بالاحساء YTLmD9MTZf
ReplyDeleteشركة تسليك مجاري بالاحساء KQi1U7jCaK
ReplyDeleteتنظيف الجبيل
ReplyDeleteShxA7IH8YJ