📄 bpforgaitview.cpp
字号:
// bpforgaitView.cpp : implementation of the CBpforgaitView class
//
#include "stdafx.h"
#include "bpforgait.h"
#include "bpforgaitDoc.h"
#include "bpforgaitView.h"
#include "backprop.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
#define traintimes 100;
/////////////////////////////////////////////////////////////////////////////
// CBpforgaitView
IMPLEMENT_DYNCREATE(CBpforgaitView, CView)
BEGIN_MESSAGE_MAP(CBpforgaitView, CView)
//{{AFX_MSG_MAP(CBpforgaitView)
ON_COMMAND(DIM_BP_XOR, OnBpXor)
//}}AFX_MSG_MAP
// Standard printing commands
ON_COMMAND(ID_FILE_PRINT, CView::OnFilePrint)
ON_COMMAND(ID_FILE_PRINT_DIRECT, CView::OnFilePrint)
ON_COMMAND(ID_FILE_PRINT_PREVIEW, CView::OnFilePrintPreview)
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CBpforgaitView construction/destruction
CBpforgaitView::CBpforgaitView()
{
// TODO: add construction code here
}
CBpforgaitView::~CBpforgaitView()
{
}
BOOL CBpforgaitView::PreCreateWindow(CREATESTRUCT& cs)
{
// TODO: Modify the Window class or styles here by modifying
// the CREATESTRUCT cs
return CView::PreCreateWindow(cs);
}
/////////////////////////////////////////////////////////////////////////////
// CBpforgaitView drawing
void CBpforgaitView::OnDraw(CDC* pDC)
{
CBpforgaitDoc* pDoc = GetDocument();
ASSERT_VALID(pDoc);
// TODO: add draw code for native data here
}
/////////////////////////////////////////////////////////////////////////////
// CBpforgaitView printing
BOOL CBpforgaitView::OnPreparePrinting(CPrintInfo* pInfo)
{
// default preparation
return DoPreparePrinting(pInfo);
}
void CBpforgaitView::OnBeginPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
{
// TODO: add extra initialization before printing
}
void CBpforgaitView::OnEndPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
{
// TODO: add cleanup after printing
}
/////////////////////////////////////////////////////////////////////////////
// CBpforgaitView diagnostics
#ifdef _DEBUG
void CBpforgaitView::AssertValid() const
{
CView::AssertValid();
}
void CBpforgaitView::Dump(CDumpContext& dc) const
{
CView::Dump(dc);
}
CBpforgaitDoc* CBpforgaitView::GetDocument() // non-debug version is inline
{
ASSERT(m_pDocument->IsKindOf(RUNTIME_CLASS(CBpforgaitDoc)));
return (CBpforgaitDoc*)m_pDocument;
}
#endif //_DEBUG
/////////////////////////////////////////////////////////////////////////////
// CBpforgaitView message handlers
void CBpforgaitView::OnBpXor()
{
/*** 定义bp网络 ***/
BPNN *bp1;
//double input_my[4][2]={{0,0},{0,1},{1,0},{1,1}};
double input_my1[3]={0,0,0};
double input_my2[3]={0,0,1};
double input_my3[3]={0,1,0};
double input_my4[3]={0,1,1};
/* 输入层的神经元 */
/* 初始化随机数种子 */
bpnn_initialize(0);
/* 创建BP网络 */
double inputtarget1[2]={0,0};
double inputtarget2[2]={0,1};
double inputtarget3[3]={0,1};
//double inputtarget3[2]={0,1};
double inputtarget4[2]={0,0};
bp1=bpnn_create(2,6,1);
/*把参数传给bp网络,包括输入模式和目标模式*/
//bp1->target=inputtarget;
//double eta是学习系数,momentum是动量因子,二者一般为0。3或是其他的值;
double *eo_my,*eh_my;//eo eh 分别是输出层和隐藏层的误差
eo_my=bp1->hidden_delta;
eh_my=bp1->output_delta;
/* 训练BP网络 */
int i,y=0;
for(i=0;i<10000;i++)
{
bp1->input_units=input_my1;
bp1->target=inputtarget1;
bpnn_train(bp1, 0.3, 0.3, eo_my, eh_my);
bp1->target=inputtarget3;
bp1->input_units=input_my3;
bpnn_train(bp1, 0.3, 0.3, eo_my, eh_my);
bp1->target=inputtarget2;
bp1->input_units=input_my2;
bpnn_train(bp1, 0.3, 0.3, eo_my, eh_my);
bp1->target=inputtarget4;
bp1->input_units=input_my4;
bpnn_train(bp1, 0.3, 0.3, eo_my, eh_my);
if(*eo_my<0.01)
{
y=i;
break;
}
}
//AfxMessageBox("the training process has been over!");
// 保存BP网络到文件中 */
bpnn_save(bp1, "bp1result.txt");
/* 进行前向运算进行识别 */
bp1->input_units=input_my4;
bpnn_feedforward(bp1);
double *result;
result=(bp1->output_units+1);
/* 从文件中读取BP网络参数 */
//BPNN *bpnn_read(char *filename);
/* 释放BP网络所占地内存空间 */
//bpnn_free(bp1);
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -