<commit_before>#include "neuralLayer.h"
#include "neuralNetwork.h"
#include "trainingData.h"
#include "matrix.h"
#include "utils.h"
#include "coreDefs.h"
#include "vector.h"
#include <stdlib.h>
#include <stdio.h>
#include <math.h>
#include <string.h>

#define NEURAL_NETWORK_INCLUDE_DEBUG_LOGS 1
#define MAX_NEURAL_NETWORK_SIZE (1024 * 1024 * 100)
#define NEURAL_NETWORK_LOG_ROUND_INTERVAL (1000)

static void deleteIntermediates(matrix* intermediates, neuralNetwork* network) {
	int idx;
	int numInters = network->numLayers - 1;
	for (idx = 0; idx < numInters; idx++) {
		clearMatrix(intermediates + idx);
	}
	free(intermediates);
}

static matrix* createNetworkIntermediates(neuralNetwork* network, int inputs) {
	int idx;
	int numInters = network->numLayers - 1;

 	matrix* intermediates = (matrix*)malloc(sizeof(matrix) * numInters);
	for (idx = 0; idx < numInters; idx++) {
		int size = getLayerOutputs(network->layers[idx]);
		initMatrix(intermediates + idx, inputs, size);
	}
	return intermediates;
}

matrix* applyNetwork(neuralNetwork* network, matrix* input, matrix* output, matrix* intermediates) {

	if (output == NULL) {
		output = createMatrix(input->height, getNetworkOutputs(network));
	}
	int needsIntermediates = intermediates == NULL;
	if (needsIntermediates) {
		intermediates = createNetworkIntermediates(network, input->height);
	}

	int idx;
	matrix* old = input;
	matrix* inter;
	for (idx = 0; idx < network->numLayers - 1; idx++) {
		inter = intermediates + idx;
		applyLayer(network->layers[idx], old, inter , 1, NULL);
		old = inter;
	}
	applyLayer(network->layers[idx], old, output, 1, NULL);
	if (needsIntermediates) {
		deleteIntermediates(intermediates, network);
	}
	return output;
}

static netF determineAcceptanceThreshold(netF temp, netF oldErr, netF newErr) {
	return expf((oldErr - newErr) * 50 / temp);
	//return exp((oldErr - newErr) * 500 / temp);
}

static netF calculateTrainingDataError(neuralNetwork* network, trainingData* trains, matrix* output, matrix* intermediates) {
	applyNetwork(network, trains->input, output, intermediates);
	subtractMatrices(output, trains->output, output);
	return sqrtf(sumSquareMatrix(output) / (output->height * output->width));
}

static neuralNetwork* mutateNetwork(neuralNetwork* mutant, netF mutationSize) {
	int layerIdx;
	for (layerIdx = 0; layerIdx < mutant->numLayers; layerIdx++) {
		neuralLayer* layer = mutant->layers[layerIdx];
		matrix* mat = layer->matrix;
		int rowIdx;
		for (rowIdx = 0; rowIdx < mat->height; rowIdx++) {
			int colIdx;
			for (colIdx = 0; colIdx < mat->width; colIdx++) {
				netF* matVal = getMatrixVal(mat, rowIdx, colIdx);
				//*matVal += (randomNetF() - 0.5);
				*matVal += mutationSize * (randomNetF() - 0.5f) / 2;
			}
		}

		int biases = getLayerOutputs(layer);
		int biasIdx;
		for (biasIdx = 0; biasIdx < biases; biasIdx++) {
			layer->biases[biasIdx] += mutationSize * (randomNetF() - 0.5f);
		}
	}
	return mutant;
}

static neuralNetwork* finishOptimizingNetwork(neuralNetwork* original, neuralNetwork* current,
		neuralNetwork* best, neuralNetwork* mutant, neuralNetwork* output, matrix* intermediates) {
	deleteNetwork(current);
	deleteNetwork(mutant);
	deleteMatrix(output);
	deleteIntermediates(intermediates, original);
	return best;
}

static neuralNetwork* addVectorToNetwork(vector* change, neuralNetwork* net) {
	int layerIdx;
	int i = 0;
	for (layerIdx = 0; layerIdx < net->numLayers; layerIdx++) {
		neuralLayer* layer = net->layers[layerIdx];
		matrix* mat = layer->matrix;
		int rowIdx;
		for (rowIdx = 0; rowIdx < mat->height; rowIdx++) {
			int colIdx;
			for (colIdx = 0; colIdx < mat->width; colIdx++) {
				netF* matVal = getMatrixVal(mat, rowIdx, colIdx);
				*matVal += change->vals[i++];
			}
		}

		int biases = getLayerOutputs(layer);
		int biasIdx;
		for (biasIdx = 0; biasIdx < biases; biasIdx++) {
			layer->biases[biasIdx] += change->vals[i++];
		}
	}
	return net;
}
<commit_msg>Fixed an incorrect parameter type.<commit_after>#include "neuralLayer.h"
#include "neuralNetwork.h"
#include "trainingData.h"
#include "matrix.h"
#include "utils.h"
#include "coreDefs.h"
#include "vector.h"
#include <stdlib.h>
#include <stdio.h>
#include <math.h>
#include <string.h>

#define NEURAL_NETWORK_INCLUDE_DEBUG_LOGS 1
#define MAX_NEURAL_NETWORK_SIZE (1024 * 1024 * 100)
#define NEURAL_NETWORK_LOG_ROUND_INTERVAL (1000)

static void deleteIntermediates(matrix* intermediates, neuralNetwork* network) {
	int idx;
	int numInters = network->numLayers - 1;
	for (idx = 0; idx < numInters; idx++) {
		clearMatrix(intermediates + idx);
	}
	free(intermediates);
}

static matrix* createNetworkIntermediates(neuralNetwork* network, int inputs) {
	int idx;
	int numInters = network->numLayers - 1;

 	matrix* intermediates = (matrix*)malloc(sizeof(matrix) * numInters);
	for (idx = 0; idx < numInters; idx++) {
		int size = getLayerOutputs(network->layers[idx]);
		initMatrix(intermediates + idx, inputs, size);
	}
	return intermediates;
}

matrix* applyNetwork(neuralNetwork* network, matrix* input, matrix* output, matrix* intermediates) {

	if (output == NULL) {
		output = createMatrix(input->height, getNetworkOutputs(network));
	}
	int needsIntermediates = intermediates == NULL;
	if (needsIntermediates) {
		intermediates = createNetworkIntermediates(network, input->height);
	}

	int idx;
	matrix* old = input;
	matrix* inter;
	for (idx = 0; idx < network->numLayers - 1; idx++) {
		inter = intermediates + idx;
		applyLayer(network->layers[idx], old, inter , 1, NULL);
		old = inter;
	}
	applyLayer(network->layers[idx], old, output, 1, NULL);
	if (needsIntermediates) {
		deleteIntermediates(intermediates, network);
	}
	return output;
}

static netF determineAcceptanceThreshold(netF temp, netF oldErr, netF newErr) {
	return expf((oldErr - newErr) * 50 / temp);
	//return exp((oldErr - newErr) * 500 / temp);
}

static netF calculateTrainingDataError(neuralNetwork* network, trainingData* trains, matrix* output, matrix* intermediates) {
	applyNetwork(network, trains->input, output, intermediates);
	subtractMatrices(output, trains->output, output);
	return sqrtf(sumSquareMatrix(output) / (output->height * output->width));
}

static neuralNetwork* mutateNetwork(neuralNetwork* mutant, netF mutationSize) {
	int layerIdx;
	for (layerIdx = 0; layerIdx < mutant->numLayers; layerIdx++) {
		neuralLayer* layer = mutant->layers[layerIdx];
		matrix* mat = layer->matrix;
		int rowIdx;
		for (rowIdx = 0; rowIdx < mat->height; rowIdx++) {
			int colIdx;
			for (colIdx = 0; colIdx < mat->width; colIdx++) {
				netF* matVal = getMatrixVal(mat, rowIdx, colIdx);
				//*matVal += (randomNetF() - 0.5);
				*matVal += mutationSize * (randomNetF() - 0.5f) / 2;
			}
		}

		int biases = getLayerOutputs(layer);
		int biasIdx;
		for (biasIdx = 0; biasIdx < biases; biasIdx++) {
			layer->biases[biasIdx] += mutationSize * (randomNetF() - 0.5f);
		}
	}
	return mutant;
}

static neuralNetwork* finishOptimizingNetwork(neuralNetwork* original, neuralNetwork* current,
		neuralNetwork* best, neuralNetwork* mutant, matrix* output, matrix* intermediates) {
	deleteNetwork(current);
	deleteNetwork(mutant);
	deleteMatrix(output);
	deleteIntermediates(intermediates, original);
	return best;
}

static neuralNetwork* addVectorToNetwork(vector* change, neuralNetwork* net) {
	int layerIdx;
	int i = 0;
	for (layerIdx = 0; layerIdx < net->numLayers; layerIdx++) {
		neuralLayer* layer = net->layers[layerIdx];
		matrix* mat = layer->matrix;
		int rowIdx;
		for (rowIdx = 0; rowIdx < mat->height; rowIdx++) {
			int colIdx;
			for (colIdx = 0; colIdx < mat->width; colIdx++) {
				netF* matVal = getMatrixVal(mat, rowIdx, colIdx);
				*matVal += change->vals[i++];
			}
		}

		int biases = getLayerOutputs(layer);
		int biasIdx;
		for (biasIdx = 0; biasIdx < biases; biasIdx++) {
			layer->biases[biasIdx] += change->vals[i++];
		}
	}
	return net;
}

{'fraction_non_alphanumeric': 0.14008128016256033, 'fraction_numerical': 0.01143002286004572, 'mean_word_length': 4.921052631578948, 'pattern_counts': {'":': 0, '<': 33, '<?xml version=': 0, '>': 63, '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': '3920658', 'n_tokens_mistral': 2954, 'n_tokens_neox': 2651, 'n_words': 666}