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

📄 matlab2annie.cpp

📁 几种神经网络的源程序
💻 CPP
字号:
/*
 * matlab2annie - Convert a feed-forward neural network in Matlab's
 * Neural Network toolbox to an annie network so that you can use
 * the network in C++ applications.
 *
 * http://annie.sourceforge.net/
 *
 * Author(s):
 *		Asim Shankar
 *
 * Last Modified On:
 *		January 12, 2003
 * Notes:
 *	- This program assumes that the network weights and biases are
 *	  already present in files. To create these files see
 *	  toAnnie.m which shows the Matlab commands to create
 *	  these files.
 *	- Currently only two layer networks are supported
 */
#include <annie.h>
#include <fstream>
#include <cstdio>

using namespace std;
using namespace annie;

#define MAX_FILENAME_SIZE 255

void matlab2annie(int nInputs,
				  int nHidden,
				  int nOutputs,
				  const char *networkFilename, 
				  const char *iwfile,	//Input Weights
				  const char *hwfile,	//Hidden Weights
				  const char *hbias,	//Hidden Biases
				  const char *obias	//Output Biases
				  )
{
	ifstream file;
	TwoLayerNetwork net(nInputs,nHidden,nOutputs);

	file.open(iwfile,ios::in);
	if (!file)
	{
		fprintf(stderr,"Could not open file %s for reading.\n",iwfile);
		exit(-1);
	} // if (!file){
    for (int h = 0; h<nHidden; h++)
	{
		for (int i = 0; i<nInputs; i++)
		{
			if (file.eof())
			{
				fprintf(stderr,"Insufficient weights in %s\n",iwfile);
				exit(-1);
			}
			real wt;
			file>>wt;
			net.connect(0,i,h,wt);
		}
	}
	file.close();

	file.open(hwfile,ios::in);
	if (!file)
	{
		fprintf(stderr,"Could not open file %s for reading.\n",hwfile);
		exit(-1);
	} // if (!file){
	for (int o = 0; o < nOutputs; o++)
	{
		for (int h = 0; h < nHidden; h++)
		{
			if (file.eof())
			{
				fprintf(stderr,"Insufficient weights in %s\n",hwfile);
				exit(-1);
			}
			real wt;
			file>>wt;
			net.connect(1,h,o,wt);
		}
	}
	file.close();
	
	file.open(hbias,ios::in);
    if (!file)
	{
		fprintf(stderr,"Could not open file %s for reading.\n",hbias);
		exit(-1);
	} // if (!file){
	for (int h = 0; h<nHidden; h++)
	{
		real b;
		if (file.eof())
		{
			fprintf(stderr,"Insufficient biases in %s\n",hbias);
			exit(-1);
		} // if (file.eof()){
		file>>b;
		net.setBias(1,h,b);
	}
	file.close();

	file.open(obias,ios::in);
	if (!file)
	{
		fprintf(stderr,"Could not open file %s for reading.\n",obias);
		exit(-1);
	} // if (!file){
	for (int o = 0; o<nOutputs; o++)
	{
		real b;
		if (file.eof())
		{
			fprintf(stderr,"Insufficient biases in %s\n",obias);
			exit(-1);
		} // if (file.eof()){
		file>>b;
		net.setBias(2,o,b);
	}
	file.close();
	net.setMetaData("Created with the Matlab Extensions Pack.");
	net.save(networkFilename);
}

int main(int argc, char *argv[])
{
	int nIn,nHidden,nOut;
	char netFilename[MAX_FILENAME_SIZE];
	char iwfile[MAX_FILENAME_SIZE],
		hwfile[MAX_FILENAME_SIZE],
		hbias[MAX_FILENAME_SIZE],
		obias[MAX_FILENAME_SIZE];

	cout<<"Number of inputs          : "; cin>>nIn;
	cout<<"Number of hidden neurons  : "; cin>>nHidden;
	cout<<"Number of output neurons  : "; cin>>nOut;

	cout<<"Input  -> Hidden Neuron weights in (filename) : "; cin>>iwfile;
	cout<<"Hidden -> Output Neuron weights in (filename) : "; cin>>hwfile;

	cout<<"Hidden neuron biases in (filename)            : "; cin>>hbias;
	cout<<"Output neuron biases in (filename)            : "; cin>>obias;

	cout<<"Save network as (filename)                    : "; cin>>netFilename;

	try
	{
		matlab2annie(nIn,nHidden,nOut,netFilename,iwfile,hwfile,hbias,obias);
	}
	catch (Exception &e)
	{
		cerr<<e.what()<<endl;
	}

	cout<<"REMINDER: If your matlab network used an activation function other than\n";
	cout<<"sigmoid, then do remember that the network file saved does NOT store that\n";
	cout<<"information. So you will have to set the activation function to be used in\n";
	cout<<"your C++ code using the .setActivationFunction() call\n";

	return 0;
}

⌨️ 快捷键说明

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