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

📄 trset2matlab.cpp

📁 几种神经网络的源程序
💻 CPP
字号:
/*
 * trset2matlab - Convert a traning set file (binary) into two
 * text files that can be imported into Matlab as two matrices.
 * One contains the input vectors and the other the corresponding
 * desired output vectors. These matrices can be given to the net.train()
 * function of Matlab's Neural Network toolbox to train the network.
 *
 * http://annie.sourceforge.net/
 *
 * Author(s):
 *		Asim Shankar
 *
 * Last Modified On:
 *		January 12, 2003
 */
#include <annie.h>
#include <fstream>
#include <cstdio>

using namespace std;
using namespace annie;

#define MAX_FILENAME_SIZE 255

void trset2matlab(const char *trset, const char *input, const char *output)
{
    	TrainingSet T(trset,annie::BINARY_FILE);
	T.initialize();

	FILE *ifile = fopen(input,"w");
	FILE *ofile = fopen(output,"w");

	if (ifile == NULL)
	{
		fprintf(stderr,"Unable to open %s for writing.\n", input);
		exit(-1);
	} // if (ifile == NULL){
	else if (ofile == NULL)
	{
		fprintf(stderr,"Unable to open %s for writing.\n", output);
		exit(-1);
	}

	Vector in, out;
	int count=0;
	while(!T.epochOver())
	{
		if (count % 100 == 0) //print a dot after every 100 vectors
			cout<<".";
		T.getNextPair(in, out);
		Vector::iterator it = in.begin();
		for(it = in.begin(); it != in.end(); it++)
			fprintf(ifile, "%f ", *it);
		fprintf(ifile, "\n");
		for(it = out.begin(); it != out.end(); it++)
			fprintf(ofile, "%f ", *it);
		fprintf(ofile, "\n");
		count++;
	} // while(!T.epochOver()){
	cout<<endl;
	cout<<"Read a total of "<<T.getSize()<<" input/output pairs.\n";
	fclose(ifile);
	fclose(ofile);
}

int main(int argc, char *argv[])
{
	char trset[MAX_FILENAME_SIZE],
		inputs[MAX_FILENAME_SIZE],
		outputs[MAX_FILENAME_SIZE];

	cout<<"Enter name of TrainingSet file          : "; cin>>trset;
	cout<<"Store input vector matrix in (filename) : "; cin>>inputs;
	cout<<"Store desired output vector matrix in   : "; cin>>outputs;

	try
	{
		trset2matlab(trset,inputs,outputs);
	}
	catch (Exception &e)
	{
		cerr<<e.what()<<endl;
	}

	cout<<"REMINDER: Use loadTrset.m to load these matrices\n";
	cout<<"into Matlab and take their transpose. These matrices\n";
	cout<<"are passed to the net.train() function in Matlab\n";

	return 0;
}

⌨️ 快捷键说明

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