I'm trying to create a simple network with perceptions and it doesn't seem to be working. I was wondering if someone could correct my mistakes. Thanks!

#include<iostream>
#include<cstdlib>
using namespace std;

int pWeights[] = {0,0};

int Classify(int pInput, int pThreshold, int i, int Bias){

	int classOut;
	
	if( pInput*pWeights[i] + Bias >= pThreshold ){
		classOut = 1;
	}else if(pInput*pWeights[i] + Bias < pThreshold){
		classOut = -1;
	}
	return classOut;

}

int main()
{
	int Bias = 1;

	int i;
	int dOutput[] = {1,0};
	int pInputs[] = {1,2};

	int pClass1 = Classify(pInputs[0],0,0,Bias);
	int pClass2 = Classify(pInputs[1],0,1,Bias);

		do
		{

			if(pClass1 != dOutput[0]){
				pWeights[0] = pWeights[0] + ((dOutput[0] - pClass1) * pInputs[0]);
				pWeights[1] = pWeights[1] + ((dOutput[1] - pClass2) * pInputs[1]);
			}else if(pClass2 != dOutput[1]){
				pWeights[0] = pWeights[0] + ((dOutput[0] - pClass1) * pInputs[0]);
				pWeights[1] = pWeights[1] + ((dOutput[1] - pClass2) * pInputs[1]);
			}

			cout << pWeights[0] << endl;
			cout << pWeights[1] << endl;

		}while(pClass1 != dOutput[0] || pClass2 != dOutput[1]);

	system("PAUSE");
	return 0;
}

Recommended Answers

All 5 Replies

Perhaps you could paste your current output, and maybe use say the bold or the red to pick out the parts of the output you consider to be wrong, and what you think the right answer should be.

Sure I can run the code, but how would I know what YOUR correct answer is going to be?

Hey, sorry it wasn't clear. Here, I fixed it up a little and added captions. I hope if you just ALT-A and CTRL-C it you'll get all of them, if not just say. Thanks!

#include<iostream>
#include<cstdlib>
using namespace std;

int pWeights[] = {0,0}; // the weights

int Classify(int pInput, int pThreshold, int i, int Bias){ // the function

	int classOut;
	
	if( pInput*pWeights[i] + Bias >= pThreshold ){ // if the input times the weight plus the bias is greater than or eqaul to the threshold...
		classOut = 1; // the output eqauls 1
	}else if(pInput*pWeights[i] + Bias < pThreshold){ // the opposite
		classOut = -1;
	}
	return classOut; // the output

}

void main()
{

	int Bias = 1; // the bias

	int i = 1; // the deciding variable
	int pInputs[] = {1,2}; // the inputs, in an array
	int dOutput[] = {1,0}; // the desired output, it corresponds with the two inputs (input1 -- output1, etc.)

		while(i = 1) // the beginning of the loop
			{
			int pClass1 = Classify(pInputs[0],0,0,Bias); // at the beginning it runs both the inputs through the function, and comes back with either 1 or -1
			int pClass2 = Classify(pInputs[1],0,1,Bias); // the second input

			cout << pWeights[0] << endl; // it outputs the current first weight
			cout << pWeights[1] << endl; // and the second

			if(pClass1 != dOutput[0]){ // if the output for the first input is not the same as the desired input, the weights are changed
				pWeights[0] = pWeights[0] + ((dOutput[0] - pClass1) * pInputs[0]); // change
				pWeights[1] = pWeights[1] + ((dOutput[1] - pClass2) * pInputs[1]); // change
				break; // end of loop
				i = 1;
			}else if(pClass2 != dOutput[1]){ // same as the above
				pWeights[0] = pWeights[0] + ((dOutput[0] - pClass1) * pInputs[0]);
				pWeights[1] = pWeights[1] + ((dOutput[1] - pClass2) * pInputs[1]);
				i = 1; // end of loop
			}else if(pClass1 == dOutput[0] && pClass2 == dOutput[1]){ // if both meet the desired
				cout << "Learned!" << endl; // it says it learned
				cout << pWeights[0] << endl; // it outputs the first correct weight
				cout << pWeights[1] << endl; // the second
				i = 0; // it changes i to 0 to fully end the loop
			}

		}

	system("PAUSE"); // program ends
}

> while(i = 1)
Maybe it now loops forever, rather than previously not looping at all?

Try while(i [B]==[/B] 1) Or better

bool finished = false;
while ( !finished )

Then at the moment in the code where you have finished, write finished = true;

Ok, now it's working, although the loop doesn't seem to work twice, if you test it it'll say it's learned, but in 1 loop, that's mathematically impossible! I'm not sure why it's saying that it's learned, but I'm guessing it's something small I've looked over. Thanks again for your help! Almost there!

#include<iostream>
#include<cstdlib>
using namespace std;

int pWeights[] = {0,0}; // the weights

int Classify(int pInput, int pThreshold, int i, int Bias){ // the function

	int classOut;
	
	if( pInput*pWeights[i] + Bias >= pThreshold ){ // if the input times the weight plus the bias is greater than or eqaul to the threshold...
		classOut = 1; // the output eqauls 1
	}else if(pInput*pWeights[i] + Bias < pThreshold){ // the opposite
		classOut = -1;
	}
	return classOut; // the output

}

void main()
{

	int Bias = 1; // the bias
	int i = 1;

	bool finished = false; // the deciding variable
	int pInputs[] = {1,2}; // the inputs, in an array
	int dOutput[] = {1,-1}; // the desired output, it corresponds with the two inputs (input1 -- output1, etc.)

		while(!finished) // the beginning of the loop
			{
			int pClass1 = Classify(pInputs[0],0,0,Bias); // at the beginning it runs both the inputs through the function, and comes back with either 1 or -1
			int pClass2 = Classify(pInputs[1],0,1,Bias); // the second input

			if(pClass1 != dOutput[0]){ // if the output for the first input is not the same as the desired input, the weights are changed
				cout << "On the " << i << " try, the weights are: " << endl;
				pWeights[0] = pWeights[0] + ((dOutput[0] - pClass1) * pInputs[0]); // change
				pWeights[1] = pWeights[1] + ((dOutput[1] - pClass2) * pInputs[1]); // change
				cout << pWeights[0] << endl; // it outputs the current first weight
				cout << pWeights[1] << endl; // and the second
				finished = false;

			}else if(pClass2 != dOutput[1]){ // same as the above
				cout << "On the " << i << " try, the weights are: " << endl;
				pWeights[0] = pWeights[0] + ((dOutput[0] - pClass1) * pInputs[0]);
				pWeights[1] = pWeights[1] + ((dOutput[1] - pClass2) * pInputs[1]);
				cout << pWeights[0] << endl; // it outputs the current first weight
				cout << pWeights[1] << endl; // and the second
				finished = false;

			}else if(pClass1 == dOutput[0] && pClass2 == dOutput[1]){ // if both meet the desired
				cout << "Learned after " << i-1 << " trys!" << endl; // it says it learned
				cout << "The weights are: " << endl;
				cout << pWeights[0] << endl; // it outputs the first correct weight
				cout << pWeights[1] << endl; // the second
				finished = true; // it changes i to 0 to fully end the loop
			}
			i++;
		}

	system("PAUSE"); // program ends
}

Aha! Thanks to you I've found the problem. A little re-writing and it'll be done!

commented: Nice +20
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.