当前位置: 首页>>代码示例>>C++>>正文


C++ OutputFile::WriteString方法代码示例

本文整理汇总了C++中OutputFile::WriteString方法的典型用法代码示例。如果您正苦于以下问题:C++ OutputFile::WriteString方法的具体用法?C++ OutputFile::WriteString怎么用?C++ OutputFile::WriteString使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在OutputFile的用法示例。


在下文中一共展示了OutputFile::WriteString方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。

示例1: GenerateCCode

// Write C code for corresponding to the feed forward network into a given file.
void MultipleBackPropagation::GenerateCCode(OutputFile & f, VariablesData & trainVariables, BOOL inputLayerIsConnectedWithOutputLayer, BOOL spaceInputLayerIsConnectedWithOutputLayer) {
	CString s;

	CString MBPVersion;
	MBPVersion.LoadString(IDS_VERSION);

	f.WriteLine("/**");
	f.WriteLine(_TEXT(" Generated by ") + MBPVersion);
	f.WriteLine(" Multiple Back-Propagation can be freely obtained at http://dit.ipg.pt/MBP");
	f.WriteLine("*/\n");

	f.WriteLine("#include <math.h>");
	
	f.WriteLine("/**");
	s.Format(_TEXT(" inputs  - should be an array of %d element(s), containing the network input(s)."), inputs);

	bool hasMissingValues = false;
	for(int i = 0; i < inputs; i++) {
		if (trainVariables.HasMissingValues(i)) {
			s += " Inputs with NaN value are considered missing values.";
			hasMissingValues = true;
			break;
		}
	}

	f.WriteLine(s);
		
	s.Format(_TEXT(" outputs - should be an array of %d element(s), that will contain the network output(s)."), outputs);
	f.WriteLine(s);
	s = " Note : The array inputs will also be changed.";
	if (!hasMissingValues) s += "Its values will be rescaled between -1 and 1.";
	s += "\n*/";
	f.WriteLine(s);

	s = f.GetFileName();
	int p = s.Find('.');
	if (p != -1) s = s.Left(p);
	f.WriteLine(_TEXT("void ") +  s + _TEXT("(double * inputs, double * outputs) {"));

	f.WriteString("\tdouble mainWeights[] = {");
	SaveWeights(f, ", ");
	f.WriteLine("};");
	f.WriteLine("\tdouble * mw = mainWeights;");
	if (hasMissingValues) f.WriteLine("\tdouble b;");

	if (!spaceNetwork.IsNull()) {
		f.WriteString("\tdouble spaceWeights[] = {");
		spaceNetwork->SaveWeights(f, ", ");
		f.WriteLine("};");
		f.WriteLine("\tdouble * sw = spaceWeights;");
		s.Format(_TEXT("\tdouble mk[%d];"), spaceNetwork->Outputs());
		f.WriteLine(s);
		f.WriteLine("\tdouble *m = mk;");
		
		if (hasMissingValues) {
			s.Format(L"\tdouble spaceInputs[%d];", inputs);
			f.WriteLine(s);
		}
	}

	int numberLayers = layers.Lenght();

	for (int l = 1; l < numberLayers - 1; l++) {
		s.Format(L"\tdouble hiddenLayer%doutputs[%d];", l, layers.Element(l)->neurons.Lenght());
		f.WriteLine(s);
	}

	int numberSpaceLayers = (spaceNetwork.IsNull()) ? 0 : spaceNetwork->layers.Lenght();

	for (int l = 1; l < numberSpaceLayers - 1; l++) {
		s.Format(_TEXT("\tdouble spaceHiddenLayer%doutputs[%d];"), l, spaceNetwork->layers.Element(l)->neurons.Lenght());
		f.WriteLine(s);
	}

	f.WriteLine("\tint c;");

	f.WriteString("\n");

	// input variables will be rescaled between -1 and 1
	if (trainVariables.Number() == inputs + outputs) {
		for (int i = 0; i < inputs; i++) { 
			double min = trainVariables.Minimum(i);
			double max = trainVariables.Maximum(i);

			if (trainVariables.HasMissingValues(i)) {
				s.Format(L"\tif(inputs[%d] == inputs[%d]) { /* compiler must have support for NaN numbers */\n\t", i, i);
				f.WriteString(s);
			}

			if (min != max) {
				s.Format(L"\tinputs[%d] = -1.0 + (inputs[%d] - %1.15f) / %1.15f;", i, i, min, (max - min)/2);				
			} else {
				s.Format(L"\tinputs[%d] = inputs[%d] / %1.15f; /* WARNING: During the training this variable remain always constant */", i, i, max);
			}
			f.WriteLine(s);

			if (hasMissingValues && !spaceNetwork.IsNull()) {
				if (trainVariables.HasMissingValues(i)) f.WriteString("\t");
				s.Format(L"\tspaceInputs[%d] = inputs[%d];", i, i);
//.........这里部分代码省略.........
开发者ID:carbon9001,项目名称:multiplebackpropagation,代码行数:101,代码来源:MultipleBackPropagation.cpp

示例2: WriteActivationFunctionCCode

/**
	Method   : void WriteActivationFunctionCCode(OutputFile & f, NeuronWithInputConnections * n, const char * outputNeuronVariable)
	Purpose  : Write C code for the activation function of a given neuron.
	Version  : 1.0.1
*/
void MultipleBackPropagation::WriteActivationFunctionCCode(OutputFile & f, NeuronWithInputConnections * n, const char * outputNeuronVariable) {
	CString s;

	ActivationFunction * a = (ActivationFunction *) (n->function);

	if (a->Alpha() == 1 && a->id == Linear) return;

	f.WriteString("\t");
	f.WriteString(outputNeuronVariable);

	switch (a->id) {
		case Sigmoid :
			f.WriteString(" = 1.0 / (1.0 + exp(");
			if (a->Alpha() == 1.0) {
				f.WriteString("-");
			} else {
				s.Format(_TEXT("%1.15f"), -a->Alpha());
				f.WriteString(s);
				f.WriteString(" * ");
			}
			f.WriteString(outputNeuronVariable);
			f.WriteLine("));");
			break;
		case Tanh :
			f.WriteString(" = tanh(");
			if (a->Alpha() != 1.0) {
				s.Format(_TEXT("%1.15f"), a->Alpha());
				f.WriteString(s);
				f.WriteString(" * ");
			}
			f.WriteString(outputNeuronVariable);
			f.WriteLine(");");
			break;
		case Gaussian :
			f.WriteString(" = exp(-(");
			f.WriteString(outputNeuronVariable);
			f.WriteString(" * ");
			f.WriteString(outputNeuronVariable);
			f.WriteString(")");
			if (a->Alpha() != 1.0) {
				f.WriteString(" / ");
				s.Format(_TEXT("%1.15f"), a->Alpha());
				f.WriteString(s);
			}
			f.WriteLine(");");
			break;
		default : // linear	
			if (a->Alpha() != 1.0) {
				s.Format(_TEXT(" *= %1.15f"), a->Alpha());
				f.WriteString(s);
				f.WriteLine(";");
			}
	}
}
开发者ID:carbon9001,项目名称:multiplebackpropagation,代码行数:59,代码来源:MultipleBackPropagation.cpp


注:本文中的OutputFile::WriteString方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。