/***********************************************************************/ /* File: Mc_neuron.h * * 2014-06-04 //////* Description: Single-layer perceptron neural network header file */
/************************************************ /
#ifndef _afx_mc_neuron_include_h_
#define _AFX_MC_NEURON_INCLUDE_H_
Class Neuron
{public
:
Neuron ();
Public:
bool Train (float expect, float mX1, float mX2);
Float Compute (float mX1, float mX2);
void getweight (float &mx1, float &mx2);
Private:
float activatefunc (float fsum);
Private:
float m_weight[2]; Weight vector
float M_bias; Deviation
float M_alpha; Learning Rate
};
/***********************************************************************//* File: Mc_neuron.cpp * * Copyright: Reprint please specify from mni2005 blog [http://blog.csdn.net/mni2005]///* Author: mo * * * Date: 2014-06-04/* Description: Single-layer Perceptron God
Through the network implementation * * */***********************************************************************/
#include "Mc_neuron.h" Neuron::neuron () {m_weight[0] = 0.01f;
M_WEIGHT[1] = 0.01f;
M_bias = 0.01f;
M_alpha = 0.01f;
}//Activation function float Neuron::activatefunc (float Yn) {if (Yn >=0.0f) {return 1.0f;
}else{return 0.0f;
} float Neuron::compute (float mX0, float mX1) {float Yn = m_bias*1.0f + m_weight[0]*mx0 + m_weight[1]* mX1;
Return Activatefunc (Yn);
} void Neuron::getweight (float &mx1, float &mx2) {mX1 = m_weight[0];
mX2 = m_weight[1]; BOOL Neuron::train (float Expect, float mX0, float mX1) {Float Yn = Compute (mX0, mX1);
Adjust weights perceptron vector if (Yn!= expect) {m_weight[0] + = (M_alpha * (expect-yn) * mX0);
M_WEIGHT[1] + + (M_alpha * (expect-yn) * mX1);
M_bias + + (M_alpha * (expect-yn) * 1.0f);
return false;
return true;
}
/***********************************************************************/ /* File:neurontest.cpp */* Copyright: Reprint Please specify the blog from mni2005 [http://blog.csdn.net/ mni2005] * */* author: mo Q */* Date: 2014-06-04 //* Description: Complete single layer perceptron neural network test with logic and/or operation */***************************** / #include <stdio.h> #include <stdlib.h> #
Include "Mc_neuron.h" int main (int argc, char* argv[]) {//Logic and Training sample float slps[][3] = {1.0, 1.0, 1.0},
{0.0, 1.0, 0.0}, {1.0, 0.0, 0.0}, {0.0, 0.0, 0.0},};
*///logic or training samples float slps[][3] = {1.0, 1.0, 1.0}, {0.0, 1.0, 1.0}, {1.0, 0.0, 1.0}, {0.0, 0.0, 0.0},
};
Neuron Neuron;
BOOL BOK;
Start training do{BOK = true;
for (int i=0; i<4; i++) {float* PSLP = slps[i]; if (!neuron.
Train (Pslp[2], pslp[0], pslp[1]) {BOK = false;
}}while (!bok); Training over, losingThe weight value float weight1, weight2; Neuron.
Getweight (weight1, weight2);
printf ("Weight1=%f, weight2=%f\n", weight1, weight2);
Validation Perceptron training result for (int i=0; i<4; i++) {float* PSLP = slps[i];
float Fvalue = Neuron.compute (Pslp[0], pslp[1]);
printf ("Value=%f, expect=%f\n", Fvalue, pslp[2]);
System ("pause");
return 0;
}