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

📄 layer.cpp

📁 多层神经网络范例 http://www.codeproject.com/cpp/MLP.asp?df=100&forumid=148477&exp=0&select=1141594#xx114159
💻 CPP
字号:
#include "StdAfx.h"
#include ".\layer.h"

Layer::~Layer(void)
{
}

Layer::Layer(CString strLbl,int size)
{
	name = strLbl;
	CString label;
	this->size    = size;
	neurons.RemoveAll();
	for(int i=0;i<size;i++)
	{
		label.Format(_T("%s%d"), strLbl,i);
		//neurons.AddTail(new Neuron(label));
		Neuron * p = new Neuron(label);
		neurons.AddTail(p);
	}
}
Neuron* Layer::getNeuron(int i)
{
	int         j=0;
	bool     found=false;
	POSITION pos = neurons.GetHeadPosition();
	Neuron* neuron = (Neuron*)neurons.GetAt(pos);

	for (int j = 0; j < neurons.GetCount(); j++) 
	{
		neuron = (Neuron*)neurons.GetNext(pos);
		if (i==j)
		{
			found = true;
			break;
		} 
	}
	if (found==false) neuron = NULL;
	return neuron;
}

void Layer::computeOutputs()
{
	POSITION pos = neurons.GetHeadPosition();
	Neuron* neuron = (Neuron*)neurons.GetAt(pos);
	for (int j = 0; j < neurons.GetCount(); j++) 
	{
		neuron = (Neuron*)neurons.GetNext(pos);
		neuron->computeOutput();
	}
}
void Layer::computeBackpropDeltas(Samples oS) // for output neurons
{
	ASSERT(oS.samples);
	POSITION pos1 = neurons.GetHeadPosition();
	Neuron* neuron = (Neuron*)neurons.GetAt(pos1);
	ASSERT(oS.nLenght == neurons.GetCount());
	for (int j = 0; j < neurons.GetCount(); j++) 
	{
		neuron = (Neuron*)neurons.GetNext(pos1);
		neuron->computeBackpropDelta(oS.samples[j]);
	}
}
void Layer::computeBackpropDeltas() // for hidden neurons
{
	POSITION pos = neurons.GetHeadPosition();
	Neuron* neuron = (Neuron*)neurons.GetAt(pos);
	for (int j = 0; j < neurons.GetCount(); j++) 
	{
		neuron = (Neuron*)neurons.GetNext(pos);
		neuron->computeBackpropDelta();
	}
}
void Layer::computeWeights()
{
	POSITION pos = neurons.GetHeadPosition();
	Neuron* neuron = (Neuron*)neurons.GetAt(pos);
	for (int j = 0; j < neurons.GetCount(); j++) 
	{
		neuron = (Neuron*)neurons.GetNext(pos);
		neuron->computeWeight();
	}
}
CString  Layer::print()
{
	CString str = _T("");
	POSITION pos = neurons.GetHeadPosition();
	Neuron* neuron = (Neuron*)neurons.GetAt(pos);
	for (int j = 0; j < neurons.GetCount(); j++) 
	{
		neuron = (Neuron*)neurons.GetNext(pos);
		str += neuron->print();
	}
	return str;
}

⌨️ 快捷键说明

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