神经网络与深度学习cpp(一)

第二章,只为理解感知机 代码较随意

隐藏一些问题,并未深究

核心计算变量名按书 较简

#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;
}


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值