第二章,只为理解感知机 代码较随意
隐藏一些问题,并未深究
核心计算变量名按书 较简
#include <iostream>
#include <vector>
#include <cstdlib>
#include <cstdio>
using namespace std;
//数据集
class DataSet
{
private:
int InputNum;
int OutputNum;
vector<int*> DataMap;
public:
DataSet(int inputnum, int outputnum)
{
this->InputNum = inputnum;
this->OutputNum = outputnum;
}
void AddRow(int *inputArray, int *outputArray)
{
int *data = new int[this->InputNum + this->OutputNum];
for (int i = 0; i<this->InputNum; ++i)
{
data[i] = inputArray[i];
}
for (int i = 0; i<this->OutputNum; ++i)
{
data[InputNum + i] = outputArray[i];
}
this->DataMap.push_back(data);
}
int GetInputNum()
{
return this->InputNum;
}
int GetRows()
{
return DataMap.size();
}
vector<int*> GetDataMap()
{
return DataMap;
}
void Clear()
{
DataMap.clear();
}
};
//神经元
class Neuron
{
private:
int Input;
int Bound;
int Output;
int Slope;
public:
Neuron()
{
this->Slope = 1;
this->Input = 0;
this->Bound = 0;
this->Output = 0;
}
void SetBound(int bound)
{
this->Bound = bound;
}
void SetSlope(int slope)
{
this->Slope = slope;
}
void GetInput(int input)
{
this->Input = input;
}
int GetOutput()
{
if (this->Input >= Bound)
return Input * Slope;
else
return 0;
}
};
//传输函数
class StepTransferFunction
{
public:
int GetResult(int data)
{
if (data<0)
return 0;
else
return 1;
}
};
//感知机类型神经网络
class Perceptron
{
private:
int InputNeuronNum;
int OutputNeuronNum;
StepTransferFunction transferfunction;
vector<Neuron*> inputneutrons;
Neuron* outputneutrons;
int *w;
int b;
public:
Perceptron(int inputneuronnum, int outputneuronnum)
{
this->InputNeuronNum = inputneuronnum;
this->OutputNeuronNum = outputneuronnum;
for (int i = 0; i<inputneuronnum; ++i)
{
this->inputneutrons.push_back(new Neuron());
}
for (int i = 0; i<outputneuronnum; ++i)
{
outputneutrons = new Neuron();
}
w = (int*)malloc(inputneuronnum * sizeof(int));
memset(w, 0, sizeof(int) * inputneuronnum);
b = 0;
}
void Learn(DataSet *trainingSet)
{
int num = 1;
while (num)
{
bool flag = true;
for (int i = 0; i<trainingSet->GetRows(); ++i)
{
int *data = trainingSet->GetDataMap()[i];
int s, f, t, z, e, a;
while (1)
{
s = 0;
for (int j = 0; j<this->InputNeuronNum; ++j)
{
s += (data[j] * w[j]);
}
s += b;
f = this->transferfunction.GetResult(s);
outputneutrons->GetInput(f);
z = outputneutrons->GetOutput();
z = data[this->InputNeuronNum];
a = f;
e = z - a;
if (e == 0)
break;
else
flag = false;
for (int j = 0; j<this->InputNeuronNum; ++j)
{
w[j] = w[j] + e * data[j];
}
b = b + e;
}
}
cout << "第" << num++ << "轮迭代" << endl;
if (flag)
{
cout << "学习结束" << endl;
break;
}
}
}
void Test(DataSet *testSet)
{
for (int i = 0; i < testSet->GetRows(); ++i)
{
int *data = testSet->GetDataMap()[i];
int s, f, z;
s = 0;
for (int j = 0; j < this->InputNeuronNum; ++j)
s += (w[j] * data[j]);
s += this->b;
f = transferfunction.GetResult(s);
outputneutrons->GetInput(f);
z = outputneutrons->GetOutput();
cout <<"Case "<<i+1<<" "<< "Input: ";
for (int j = 0; j < this->InputNeuronNum; ++j)
cout << data[j] << " ";
cout << "Output: " << z << " ";
cout << "Expected: " << data[InputNeuronNum]<<" ";
if (data[InputNeuronNum] == z)
cout << "符合" << endl;
else
{
cout << "不符合 中止" << endl;
break;
}
}
}
vector<Neuron*> GetInputNeurons()
{
return this->inputneutrons;
}
Neuron* GetOutputNeurons()
{
return this->outputneutrons;
}
int *GetWeight()
{
return w;
}
int GetB()
{
return b;
}
};
int main()
{
Perceptron *perceptron = new Perceptron(2, 1);
Neuron* pout = perceptron->GetOutputNeurons();
pout->SetSlope(1);
pout->SetBound(0);
//与运算
DataSet *trainingSet = new DataSet(2, 1);
trainingSet->AddRow(new int[2]{ 0,0 }, new int[1]{ 0 });
trainingSet->AddRow(new int[2]{ 0,1 }, new int[1]{ 0 });
trainingSet->AddRow(new int[2]{ 1,0 }, new int[1]{ 0 });
trainingSet->AddRow(new int[2]{ 1,1 }, new int[1]{ 1 });
perceptron->Learn(trainingSet);
perceptron->Test(trainingSet);
system("pause");
//或运算
trainingSet->Clear();
trainingSet->AddRow(new int[2]{ 0,0 }, new int[1]{ 0 });
trainingSet->AddRow(new int[2]{ 0,1 }, new int[1]{ 1 });
trainingSet->AddRow(new int[2]{ 1,0 }, new int[1]{ 1 });
trainingSet->AddRow(new int[2]{ 1,1 }, new int[1]{ 1 });
perceptron->Learn(trainingSet);
perceptron->Test(trainingSet);
system("pause");
//异或运算
trainingSet->Clear();
trainingSet->AddRow(new int[2]{ 0,0 }, new int[1]{ 0 });
trainingSet->AddRow(new int[2]{ 0,1 }, new int[1]{ 1 });
trainingSet->AddRow(new int[2]{ 1,0 }, new int[1]{ 1 });
trainingSet->AddRow(new int[2]{ 1,1 }, new int[1]{ 0 });
perceptron->Learn(trainingSet);
perceptron->Test(trainingSet);
system("pause");
return 0;
}