0% found this document useful (0 votes)
30 views

Soft Computing Lab

The document contains 5 programs related to machine learning algorithms: 1. A program to implement the Perceptron training algorithm for a single-layer perceptron. 2. A program to implement Hebb's learning rule. 3. A program to implement logic gates (AND, OR, NOT, XOR) using a switch case structure. 4. A program for the Backpropagation algorithm to train a multi-layer perceptron. 5. A program to implement the delta rule for updating weights in a perceptron.

Uploaded by

Rituraj Singh
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
30 views

Soft Computing Lab

The document contains 5 programs related to machine learning algorithms: 1. A program to implement the Perceptron training algorithm for a single-layer perceptron. 2. A program to implement Hebb's learning rule. 3. A program to implement logic gates (AND, OR, NOT, XOR) using a switch case structure. 4. A program for the Backpropagation algorithm to train a multi-layer perceptron. 5. A program to implement the delta rule for updating weights in a perceptron.

Uploaded by

Rituraj Singh
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 11

Program : 1

Write a program of Perceptron Training Algorithm.

#include<conio.h>
#include<iostream.h>
Void main( ) {
clrscr( );
int in[3],d,w[3],a=0;
for(inti=0;i<3,i++)
{
cout<<‖\n initialize the weight vector w‖<<i;
cin>>w[i] }
for(i=0;i<3:i++}
{
cout<<‖\n enter the input vector i‖<<i;
cin>>in[i];
}
cout<<‖\n enter the desined output‖;
cin>>d;
intans=1;
while(ans= = 1)
{
for (a= 0, i==0;i<3;i++)
{
a = a + w[i] * in[i];
}
clrscr( );
cout<<‖\n desired output is‖<<d;
cout<<‖\n actual output is ―<<a;
int e;
e=d-a;
cout<<‖\n error is ―<<e;
cout<<‖\n press 1 to adjust weight else 0‖;
cin>>ans;
if (e<0)
{
for(i=0;i<3;i++)
{
w[i]=w[i]-1;
}
else if (e>0)
{
for(i=0;i<3:i++)
{
w[i]=w[i]+1;
}
}
}
getch( );
}
OUTPUT:
Desire output is 2
Actual output is 17
Error is -15
Press 1 to adjust weight else 0
PROGRAM:2
Write a program to implement Hebb’s rule

#include<<iostream.h>>
#include<<conio.h>>
void main()
{
floatn,w,t,net,div,a,al;
cout<<‖consider o single neuron percetron with a single i/p‖;
cin>>w;
cout<<‖enter the learning cofficient‖;
cin>>d;
for (i=0;i<10;i++)
{
net = x+w;
if(wt<0)
a=0;
else
a=1;
div=at+a+w;
w=w+div;
cout<<‖i+1 in fraction are i‖<<a<<‖change in weight‖<<dw<<‖adjustment at=‖<<w;
}
}
OUTPUT:
Consider 0 single neuron perceptron with a single i/p1
Enter the learning Co-efficient 2
i+1 in fraction are i1change in weight2adjustment at 3i+1
In fraction are i1change in weigh4adjustment at i+1
In fraction are i1change in weight8adjustment at i= 15i+1
In fraction are i1change in weight16adjustment at=31i+1
In fraction are i1change in weight 32adjustment at=63i+1
In fraction are i1change in weight 64adjustment at=127i+1
In fraction are i1change in weight 128adjustment at=255i+1
In fraction are i1change in weight 256adjustment at=511i+1
In fraction are i1change in weight 512adjustment at=1023i+1
In fraction are i1change in weight 1024adjustment at=2047i+1
Consider 0 single neuron perceptron with a single i/p
PROGRAM:3
Write a program to implement logic gates.

#include <iostream>
int main()
{
char menu; //Menu control variable
int result; //final output variable
int dataValue1;
int dataValue2;
cout<< "enter your Boolean operator code: (A,O,N,X): ";
cin>> menu;
switch (menu) //Menu control variable
{
case 'A':
cout<< "Enter first Boolean value:";
cin>> dataValue1;
cout<< "Enter second Boolean value:";
cin>> dataValue2;
if(dataValue1 == 1 && dataValue2 == 1)
{
result = 1;
}
else
{
result = 0;
}
cout<< "show result:" << result;
break;
case 'O':
cout<< "Enter first Boolean value:";
cin>> dataValue1;
cout<< "Enter second Boolean value:";
cin>> dataValue2;
if(dataValue1 == 1 || dataValue2 == 1)
{
result = 1;
}else
{
result = 0;
}
cout<< "show result:" << result;
break;
case 'N':
cout<< "Enter first Boolean value:";
cin>> dataValue1;
result = !dataValue1;
cout<< "show result:" << result;
break;
case 'X':
cout<< "Enter first Boolean value:";
cin>> dataValue1;
cout<< "Enter second Boolean value:";
cin>> dataValue2;
if(dataValue1 = !dataValue1)
{
result = 1;
}else
{
result = 0;
}
cout<< "show result:" << result;
break;
default:
result = 0;
break;
}//end switch
cin.ignore(2);
return 0;
}//end main
OUTPUT:
Enter your Boolean operator code :(A,O,N,X) : A
Enter First Boolean value :1
Enter Second Boolean value :1
Show Result :1
Enter your Boolean operator code :(A,O,N,X) : O
Enter First Boolean value :1
Enter Second Boolean value :0
Show Result :1
PROGRAM:4
Write a program for Back Propagation Algorithm

# include <iostream.h>
#include <conio.h>
void main ()
{
inti ;
float delta, com, coeff = 0.1;
struct input
{
floatval,out,wo, wi;
int top;
} s[3] ;
cout<< ―\n Enter the i/p value to target o/p‖ << ―\t‖;
for (i=0; i<3 ; i++)
cin>> s [i], val>> s[i], top);
i = 0;
do
{
if (i = = 0)
{
W0 = -1.0;
W1 = -0.3;
}
else
{
W0 = del [i - 1], W0 ;
W1 = del [i - 1] , Wi ;
}
del [i]. aop = w0 + (wi * del [i]. val);
del [i].out = del [i]. aop);
delta = (top – del [i]. out) * del [i].out * (1 – del [i].out);
corr = coeff * delta * del [i].[out];
del [i].w0 = w1 + corr;
del [i]. w1 = w1 + corr;
i++;
}While ( i ! = 3)
cout<< ―VALUE‖<<‖Target‖<<‖Actual‖<<‖w0‖ <<‖w1‖<<’\n;
for (i=0; i=3; i++)
{
cout<< s [i].val<< s[i].top<<s[i].out << s[i]. w0<< s[i]. w1;
cout<< ―\n‖;
}
getch ();
}
OUTPUT:
Back Propagation network
1. Load data
2. Learn from the data
3. Compute output pattern
4. Make new data file
5. Save data
6. Print data
7. Change learning rate
8. Exit
Enter your Choice (1-8)
Program 5
Write a program to implement of delta rule.

#include<<iostream.h>>
#include<<conio.h>>
void main()
{
clrscr( );
float input[3],d,weight[3],delta;
for(int i=0;i < 3 ; i++)
{
cout<<”\n initilize weight vector “<<i<<”\t”;
cin>>input[i];}
cout<<””\n enter the desired output\t”;
cin>>d;
do{del=d-a;if(del<0)
for(i=0 ;i<3 ;i++)
w[i]=w[i]-input[i];
else if(del>0)
for(i=0;i<3;i++)
weight[i]=weight[i]+input[i];
for(i=0;i<3;i++)
{
val[i]=del*input[i];
weight[+1]=weight[i]+val[i];
}
cout<<”\value of delta is “<<del;
cout<<”\n weight have been adjusted”;
}
while(del ≠ 0)
if(del=0)
cout<<”\n output is correct”;
}

You might also like