<commit_before>// File: nnet.cpp

#include <cstdlib>
#include <iostream>
#include <fstream>

#include "util.h"
#include "nnet.hpp"

using namespace std;

nnetmodel::nnetmodel(): 
		input_min(NULL), input_range(NULL),
		output_min(0), output_range(0),
		inter_output(NULL),
		IW(NULL), b1(NULL),
		LW(NULL), b2(0), input(NULL) {}

void 
nnetmodel::readnnetmodel(const char *name) {
	
	ifstream infile(name);
	if (!infile) {
		cerr << "Can not open nnet model file!\n" << endl;
		exit(1);
	}
	string varname;
	// Read number of neurons
	infile >> varname;
	infile >> NUM_OF_NEURONS;

	// Read the input dimension and output dimension
	infile >> varname;
	infile >> INPUT_DIM;
	infile >> varname;
	infile >> OUTPUT_DIM;

	// Read the input min and input range for normalization
	input_min = dvector(INPUT_DIM);
	input_range = dvector(INPUT_DIM);
	infile >> varname;
	readvectorfromfile(infile, input_min, INPUT_DIM);
	infile >> varname;
	readvectorfromfile(infile, input_range, INPUT_DIM);

	// Read the output min and range for normalization
	infile >> varname;
	infile >> output_min;
	infile >> varname;
	infile >> output_range;

	// Read the IW
	IW = dmatrix(NUM_OF_NEURONS, INPUT_DIM);
	infile >> varname;
	readmatrixfromfile(infile, IW, NUM_OF_NEURONS, INPUT_DIM);

	// Read the b1
	b1 = dvector(NUM_OF_NEURONS);
	infile >> varname;
	readvectorfromfile(infile, b1, NUM_OF_NEURONS);

	// Read the LW
	LW = dvector(NUM_OF_NEURONS);
	infile >> varname;
	readvectorfromfile(infile, LW, NUM_OF_NEURONS);

	// Read the b2
	infile >> varname;
	infile >> b2;

	infile.close();
	infile.clear();

	input = dvector(INPUT_DIM);
	inter_output = dvector(NUM_OF_NEURONS);

	return;
}

double 
nnetmodel::simnnet(double *input) {
	
	double result = 0.0;

	// Preprocessing the input
	// Down scale
	preprocessing(input, input_min, input_range, INPUT_DIM);

#ifdef DEBUG_NNET
	printf("Scaled input: toolbox/nnet/nnutils/+nnsim/y.m:29 Pc\n");
	dump_dvector(input, INPUT_DIM);
#endif

	// The first level multiplication
	// IW * input
	mbynmatvecmult(inter_output, IW, input, NUM_OF_NEURONS, INPUT_DIM);

#ifdef DEBUG_NNET
	printf("IW * input:\n");
	dump_dvector(inter_output, NUM_OF_NEURONS);
#endif

	// First level bias
	vecadd(inter_output, b1, NUM_OF_NEURONS);

#ifdef DEBUG_NNET
	printf("Added first level bias: toolbox/nnet/nnutils/+nnsim/a.m:114 n\n");
	dump_dvector(inter_output, NUM_OF_NEURONS);
#endif

	// Transfer function: tansig
	tansig(inter_output, NUM_OF_NEURONS);

#ifdef DEBUG_NNET
	printf("After tansig function: toolbox/nnet/nnutils/+nnsim/a.m:120 Ac\n");
	dump_dvector(inter_output, NUM_OF_NEURONS);
#endif

	// The second level multiplication
	// LW * first level output
	// Vector to vector multiplication
	for (int i = 0; i < NUM_OF_NEURONS; ++i) {
		result += inter_output[i] * LW[i];
	}

	// Second level bias
	result += b2;

#ifdef DEBUG_NNET
	printf("After second layer: toolbox/nnet/nnutils/+nnsim/a.m:120 Ac\n");
	dump_dvector(&result, 1);
#endif

	// Postprocessing the output
	// Up scale
	result = postprocessing(result, output_min, output_range);

#ifdef DEBUG_NNET
	printf("After post scale: toolbox/nnet/nnutils/+nnsim/y.m:49 Y and \n");
	dump_dvector(&result, 1);
#endif

	return result;
}

void readmatrixfromfile(ifstream &infile, double **matrix, int row, int col) {
		if (!infile) {
				cerr << "The matrix file is not valid! EXIT!" << endl;
				exit(1);
		}

		int i = 0, j = 0;
		for (i = 0; i < row; ++i) {<commit_msg>Added code when predictor doesn't work<commit_after>// File: nnet.cpp

#include <cstdlib>
#include <iostream>
#include <fstream>

#include "util.h"
#include "nnet.hpp"

using namespace std;

nnetmodel::nnetmodel(): 
		input_min(NULL), input_range(NULL),
		output_min(0), output_range(0),
		inter_output(NULL),
		IW(NULL), b1(NULL),
		LW(NULL), b2(0), input(NULL) {}

void 
nnetmodel::readnnetmodel(const char *name) {
	
	ifstream infile(name);
	if (!infile) {
		cerr << "Can not open nnet model file!\n" << endl;
		exit(1);
	}
	string varname;
	// Read number of neurons
	infile >> varname;
	infile >> NUM_OF_NEURONS;

	// Read the input dimension and output dimension
	infile >> varname;
	infile >> INPUT_DIM;
	infile >> varname;
	infile >> OUTPUT_DIM;

	// Read the input min and input range for normalization
	input_min = dvector(INPUT_DIM);
	input_range = dvector(INPUT_DIM);
	infile >> varname;
	readvectorfromfile(infile, input_min, INPUT_DIM);
	infile >> varname;
	readvectorfromfile(infile, input_range, INPUT_DIM);

	// Read the output min and range for normalization
	infile >> varname;
	infile >> output_min;
	infile >> varname;
	infile >> output_range;

	// Read the IW
	IW = dmatrix(NUM_OF_NEURONS, INPUT_DIM);
	infile >> varname;
	readmatrixfromfile(infile, IW, NUM_OF_NEURONS, INPUT_DIM);

	// Read the b1
	b1 = dvector(NUM_OF_NEURONS);
	infile >> varname;
	readvectorfromfile(infile, b1, NUM_OF_NEURONS);

	// Read the LW
	LW = dvector(NUM_OF_NEURONS);
	infile >> varname;
	readvectorfromfile(infile, LW, NUM_OF_NEURONS);

	// Read the b2
	infile >> varname;
	infile >> b2;

	infile.close();
	infile.clear();

	input = dvector(INPUT_DIM);
	inter_output = dvector(NUM_OF_NEURONS);

	return;
}

double 
nnetmodel::simnnet(double *input) {
	
	// Set the result to initialEnergy
	double result = 0.0;
	double inputPower  = input[0];
	double initEnergy = input[1];

	// Preprocessing the input
	// Down scale
	preprocessing(input, input_min, input_range, INPUT_DIM);

	// We return earlier when the input power
	// is less than the min training power
	// input[0]: preprocessed power
	// input[1]: preprocessed initial energy
	if (input[0] < -1.0) {
#ifdef DEBUG_NNET
		printf("We found the input power is less than the training minimum:\n");
		dump_dvector(input, INPUT_DIM);
#endif
		return result;
	}

#ifdef DEBUG_NNET
	printf("Scaled input: toolbox/nnet/nnutils/+nnsim/y.m:29 Pc\n");
	dump_dvector(input, INPUT_DIM);
#endif

	// The first level multiplication
	// IW * input
	mbynmatvecmult(inter_output, IW, input, NUM_OF_NEURONS, INPUT_DIM);

#ifdef DEBUG_NNET
	printf("IW * input:\n");
	dump_dvector(inter_output, NUM_OF_NEURONS);
#endif

	// First level bias
	vecadd(inter_output, b1, NUM_OF_NEURONS);

#ifdef DEBUG_NNET
	printf("Added first level bias: toolbox/nnet/nnutils/+nnsim/a.m:114 n\n");
	dump_dvector(inter_output, NUM_OF_NEURONS);
#endif

	// Transfer function: tansig
	tansig(inter_output, NUM_OF_NEURONS);

#ifdef DEBUG_NNET
	printf("After tansig function: toolbox/nnet/nnutils/+nnsim/a.m:120 Ac\n");
	dump_dvector(inter_output, NUM_OF_NEURONS);
#endif

	// The second level multiplication
	// LW * first level output
	// Vector to vector multiplication
	for (int i = 0; i < NUM_OF_NEURONS; ++i) {
		result += inter_output[i] * LW[i];
	}

	// Second level bias
	result += b2;

#ifdef DEBUG_NNET
	printf("After second layer: toolbox/nnet/nnutils/+nnsim/a.m:120 Ac\n");
	dump_dvector(&result, 1);
#endif

	// Postprocessing the output
	// Up scale
	result = postprocessing(result, output_min, output_range);

#ifdef DEBUG_NNET
	printf("After post scale: toolbox/nnet/nnutils/+nnsim/y.m:49 Y and \n");
	dump_dvector(&result, 1);
#endif

	// FIXME: if the prediction is out of wack
	// Then we have to assume something wrong
	// with the predictor
	if ((result > initEnergy + inputPower*input[2]) || (result < initEnergy)){
		result = initEnergy;
	}

	return result;
}

void readmatrixfromfile(ifstream &infile, double **matrix, int row, int col) {
		if (!infile) {
				cerr << "The matrix file is not valid! EXIT!" << endl;
				exit(1);
		}

		int i = 0, j = 0;
		for (i = 0; i < row; ++i) {
{'fraction_non_alphanumeric': 0.13173890063424948, 'fraction_numerical': 0.010306553911205074, 'mean_word_length': 4.429698708751793, 'pattern_counts': {'":': 0, '<': 31, '<?xml version=': 0, '>': 78, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '599566', 'n_tokens_mistral': 3112, 'n_tokens_neox': 2906, 'n_words': 724}