智能系统课程设计,老师要求做一个识别手写数字的神经网络算法,数据集是Mnist-image数据集。
在网上找了好多教程,结果仍然不清楚,后来在B站找了个视频才弄懂,传上来代码,以备以后回顾。
Minss-image
// newBpLearn.cpp : 定义控制台应用程序的入口点。
//
#include "stdafx.h"
#include<iostream>
#include<math.h>
#include<cmath>
#include<cstdlib>
#include<ctime>
#include <sstream>
#include<opencv2\calib3d\calib3d.hpp>
#include <opencv2\core\core.hpp>
#include<opencv2\highgui\highgui.hpp>
using namespace std;
using namespace cv;
#define INPUT_TYPE 0
#define HIDDEN_TYPE 1
#define OUTPUT_TYPE 2
class neuron
{
private:
int type;
//前向传播
float InputValueForward;
float OutputValueForward;
//反向传播
float errorInputBack;
float errorOutputBack;
//偏差值
float bias;
public:
float log_sigmoid(float in)
{
return (1/(exp(-in)+1));
}
float log_sigD(float in)
{
return OutputValueForward*(1-OutputValueForward)*in;
}
float Forward(float in)
{
switch(type)
{
case INPUT_TYPE:
return in;
case HIDDEN_TYPE:
case OUTPUT_TYPE:
return log_sigmoid(in);
}
}
float Backward(float in)
{
switch(type)
{
case INPUT_TYPE:
return in;
case HIDDEN_TYPE:
case OUTPUT_TYPE:
return log_sigD(in);
}
}
/
void setType(int n)
{
type=n;
}
float getForwardInputValue()
{
return InputValueForward;
}
void setForwardInputValue(float in)
{
InputValueForward=in;
OutputValueForward=setForwardOutputValue(in);
}
float getForwardOutputValue()
{
return OutputValueForward;
}
float setForwardOutputValue(float in)
{
return Forward(in);
}
float getBackwardInputValue()
{
return errorInputBack;
}
float ge