AND Logic Perceptrons [Archive] - C Board

PDA

View Full Version : AND Logic Perceptrons


Highland Laddie
07-02-2005, 07:33 PM
Hello, I've decided to look into the subject of AI programming, and am having trouble with changing my weights with the delta rule. I'm trying to make a simple Boolean AND logic perceptron, which will learn to recognize whether both the inputs are the same.

Here's my code that I have so far:


// Include header files
#include <iostream>

using namespace std;

int main()
{
// Delcare vars
double threshold = 1.0;
double weight = -0.2;
double activation;
int input1, input2;
bool output;

// Keep looping
while (1)
{
// Get the input vars
cout << "Please enter the first boolean input: ";
cin >> input1;
cout << "Please enter the second boolean input: ";
cin >> input2;

// Get the activation
activation = (input1 * weight) + (input2 * weight);

// If the activation is greater than the threshold, fire the perceptron
if (activation >= threshold)
{
cout << "Perceptron is firing" << endl;
output = true;
}
// If the activation is less than the threshold, don't fire
else if (activation < threshold)
{
output = false;
cout << "Perceptron isn't firing" << endl;
cout << "Changing Weights..." << endl;
cout << "Old Weight: " << weight << endl;

// Change the weights
weight += (input1 + input2) * (1 - output);

cout << "New Weight: " << weight << endl;
}
system("pause");
system("cls");
}

return 0;
}


There is no problem, except once I "train" the perceptron, and the feed it 2 inputs like 1 and 0, it would evaluate to true, but seeing both the inputs aren't true, it shouldn't.

I don't know, but it may be the way I'm adjusting the weights, I'm using the delta rule off the cprogramming tutorials, and I may have interpreted it wrong.

I will admit right now that I'm just starting in this subject, so it's probably some simple mistake, or something that I've overlooked. If anyone has any suggestions, or a solution, it would be appreciated.

dwks
07-13-2005, 02:04 PM
(1 - output);

output is a bool, which is assigned to false a little earlier on. So that's sort of like saying 1.

Therefore,

weight += (input1 + input2) * (1 - output);

is like

weight += input1 + input1;


On second thought, you don't even really need output. You only use it once, and where you do, you initialize it just beforehand.


if (activation >= threshold)
// ...
else if (activation < threshold)

That else if could be an else.