⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 向后传播dlg.cpp

📁 后向传播
💻 CPP
字号:
// 向后传播Dlg.cpp : implementation file
//

#include "stdafx.h"
#include "向后传播.h"
#include "向后传播Dlg.h"
#include "math.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

/////////////////////////////////////////////////////////////////////////////
// CMyDlg dialog

CMyDlg::CMyDlg(CWnd* pParent /*=NULL*/)
	: CDialog(CMyDlg::IDD, pParent)
{
	//{{AFX_DATA_INIT(CMyDlg)
	m_strout = _T("");
	//}}AFX_DATA_INIT
	// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
	m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}

void CMyDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CMyDlg)
	DDX_Text(pDX, IDC_EDIT1, m_strout);
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CMyDlg, CDialog)
	//{{AFX_MSG_MAP(CMyDlg)
	ON_WM_PAINT()
	ON_WM_QUERYDRAGICON()
	ON_BN_CLICKED(IDC_BUTTON1, OnButton1)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CMyDlg message handlers

BOOL CMyDlg::OnInitDialog()
{
	CDialog::OnInitDialog();

	// Set the icon for this dialog.  The framework does this automatically
	//  when the application's main window is not a dialog
	SetIcon(m_hIcon, TRUE);			// Set big icon
	SetIcon(m_hIcon, FALSE);		// Set small icon
	
	// TODO: Add extra initialization here
	
	return TRUE;  // return TRUE  unless you set the focus to a control
}

// If you add a minimize button to your dialog, you will need the code below
//  to draw the icon.  For MFC applications using the document/view model,
//  this is automatically done for you by the framework.

void CMyDlg::OnPaint() 
{
	if (IsIconic())
	{
		CPaintDC dc(this); // device context for painting

		SendMessage(WM_ICONERASEBKGND, (WPARAM) dc.GetSafeHdc(), 0);

		// Center icon in client rectangle
		int cxIcon = GetSystemMetrics(SM_CXICON);
		int cyIcon = GetSystemMetrics(SM_CYICON);
		CRect rect;
		GetClientRect(&rect);
		int x = (rect.Width() - cxIcon + 1) / 2;
		int y = (rect.Height() - cyIcon + 1) / 2;

		// Draw the icon
		dc.DrawIcon(x, y, m_hIcon);
	}
	else
	{
		CDialog::OnPaint();
	}
}
/*******************||||||||||||||||||*********************/
/*******************\/\/\/\/\/\/\/\/\/*********************/

//产生-1到1的随机浮点数
float fRandom()
{
	return (float)((float)(rand())/16384-1.0);
}

void  CMyDlg::Spread(bool D[][4],int D_num,float l,int layer_num,int* network)
{
	float I[6];//输入
	float O[6];//输出
	float W[6][6];//权值
	float Bias[6];//偏倚
	float Err[6];//误差
	float T[6];
	int flag=0;//终止条件:每作一次flag++,如果flag<0则跳出
	int i,j,k;//循环标量
	int nlayer;//j所在的层数
	//初始化权重和偏倚
	for(i=0;i<network[layer_num-1];i++)
	{
		for(j=0;j<network[layer_num-1];j++)
		{
			W[i][j]=fRandom();//权重为-1到1的随机数
		}
		Bias[i]=fRandom();//偏倚为-1到1的随机数
	}
	while(flag<100000)////////////////////////////////////////
	{
		flag++;
		for(i=0;i<D_num;i++)//D的每个训练元组
		{
			//初始化输出值,输入值,目标值
			for(j=0;j<network[layer_num-1];j++)
			{
				O[j]=0;
				I[j]=0;
				T[j]=0;
				Err[j]=0;
			}
			//向前传播输入
			for(j=0;j<network[0];j++)
			{
				I[j]=(float)D[i][j];
				O[j]=I[j];
			}
			//输出
			for(j=network[layer_num-2];j<network[layer_num-1];j++)
			{
				T[j]=(float)D[i][3];
			}
			for(j=network[0];j<network[layer_num-1];j++)//隐藏或输出层的每一个单元
			{

				for(k=0;k<layer_num;k++)//搜索所有层,看当前的节点属于哪一层
				{
					if(j<network[k])//属于第k层
					{
						nlayer=k;
						break;
					}
				}//for(k=0;k<layer_num;k++)
				//计算单元j的净输入
				int start;
				int end;
				if(nlayer==1)
				{
					start=0;
				}
				else
				{
					start=network[nlayer-2];
				}
				end=network[nlayer-1];
				for(k=start;k<end;k++)//j的上一层的节点k/////////////////////////////////////////////error
				{
					I[j]+=W[k][j]*O[k];
				}//for(k=network[nlayer-1];k<network[nlayer];k++)//j的上一层的节点k
				I[j]+=Bias[j];
				//计算单元j的输出
				O[j]=1/(1+exp((double)(-I[j])));
			}//for(j=network[0];j<network[layer_num-1];j++)

			//后向传播误差
			//输出层
			for(j=network[layer_num-2];j<network[layer_num-1];j++)
			{
				Err[j]=O[j]*(1-O[j])*(T[j]-O[j]);
			}
			//隐藏层
			for(j=network[layer_num-2]-1;j>=network[0];j--)
			{
				for(k=0;k<layer_num;k++)//搜索所有层,看当前的节点属于哪一层
				{
					if(j<network[k])//属于第k层
					{
						nlayer=k;
						break;
					}
				}//for(k=0;k<layer_num;k++)
				for(k=network[nlayer];k<network[nlayer+1];k++)//j的下一层的节点k
				{
					Err[j]+=Err[k]*W[j][k];
				}//for(k=network[nlayer];k<network[nlayer+1];k++)//j的下一层的节点k
				Err[j]*=O[j]*(1-O[j]);
			}//隐藏层for(j=network[layer_num-2]-1;j>=network[0];j--)
			//更新权重
			for(j=0;j<6;j++)
			{
				for(k=0;k<6;k++)
				{
					W[j][k]=W[j][k]+l*Err[k]*O[j];
					if(W[j][k]>1)
					{
						W[j][k]=1;
					}
					else if(W[j][k]<-1)
					{
						W[j][k]=-1;					
					}
				}
				
			}//for(j=0;j<6;j++)
			//更新偏倚
			for(j=0;j<6;j++)
			{
				Bias[j]=Bias[j]+l*Err[j];
			}
		}
	}
	UpdateData(1);
	m_strout.Format("W03\t%f\r\nW04\t%f\r\nW13\t%f\r\nW14\t%f\r\nW23\t%f\r\nW24\t%f\r\nW35\t%f\r\nW45\t%f\r\nErr3\t%f\r\nErr4\t%f\r\nErr5\t%f\r\n",W[0][3],W[0][4],W[1][3],W[1][4],W[2][3],W[2][4],W[3][5],W[4][5],Err[3],Err[4],Err[5]);
	UpdateData(0);
}
// The system calls this to obtain the cursor to display while the user drags
//  the minimized window.
HCURSOR CMyDlg::OnQueryDragIcon()
{
	return (HCURSOR) m_hIcon;
}

void CMyDlg::OnButton1() 
{
	// TODO: Add your control notification handler code here
	float l;
	l=(float)0.9;
	bool D[1][4];
	D[0][0]=1;
	D[0][1]=0;
	D[0][2]=1;
	D[0][3]=1;
	int layer_num=3;//神经网络的层数
	int network[3];//神经网络前n层的节点数
	network[0]=3;
	network[1]=5;
	network[2]=6;
	Spread(D,1,l,layer_num,network);
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -