<commit_before>#ifndef TEST_NLL
#define TEST_NLL

#include "nnlib/critics/nll.h"
using namespace nnlib;

void TestNLL()
{
	Tensor<> inp = Tensor<>({ 1, 2, 3, 4, 5, 6 }).resize(2, 3);
	Tensor<> tgt = Tensor<>({ 2, 0 }).resize(2, 1);
	Tensor<> dif = Tensor<>({ -3, -4 }).resize(2, 1);
	Tensor<> grd = Tensor<>({ 0, 0, -1.0, -1.0, 0, 0 }).resize(2, 3);
	NLL<> critic(inp.shape(), false);
	
	double nll = critic.forward(inp, tgt);
	NNHardAssert(fabs(nll - dif.sum()) < 1e-12, "NLL<>::forward with no average failed!");
	
	critic.average(true);
	nll = critic.forward(inp, tgt);
	dif.scale(1.0/6.0);
	NNHardAssert(fabs(nll - dif.sum()) < 1e-12, "NLL<>::forward with average failed!");
	
	critic.inputs({ 10, 10 });
	nll = critic.safeForward(inp, tgt);
	NNHardAssert(fabs(nll - dif.sum()) < 1e-12, "NLL<>::safeForward failed!");
	
	critic.average(false);
	critic.backward(inp, tgt);
	NNHardAssert(critic.inGrad().addM(grd, -1).square().sum() < 1e-12, "NLL<>::backward failed!");
	
	critic.batch(12);
	NNHardAssert(critic.batch() == 12, "NLL<>::batch failed!");
}

#endif
<commit_msg>Better NLL test.<commit_after>#ifndef TEST_NLL
#define TEST_NLL

#include "nnlib/critics/nll.h"
using namespace nnlib;

void TestNLL()
{
	Tensor<> inp = Tensor<>({ 1, 2, 3, 4, 5, 6 }).resize(2, 3);
	Tensor<> tgt = Tensor<>({ 2, 0 }).resize(2, 1);
	Tensor<> dif = Tensor<>({ -3, -4 }).resize(2, 1);
	Tensor<> grd = Tensor<>({ 0, 0, -1.0, -1.0, 0, 0 }).resize(2, 3);
	NLL<> critic(inp.shape(), false);
	
	double nll = critic.forward(inp, tgt);
	NNHardAssert(fabs(nll - dif.sum()) < 1e-12, "NLL<>::forward with no average failed!");
	
	critic.average(true);
	nll = critic.forward(inp, tgt);
	dif.scale(1.0/6.0);
	NNHardAssert(fabs(nll - dif.sum()) < 1e-12, "NLL<>::forward with average failed!");
	
	critic.inputs({ 10, 10 });
	nll = critic.safeForward(inp, tgt);
	NNHardAssert(fabs(nll - dif.sum()) < 1e-12, "NLL<>::safeForward failed!");
	
	critic.average(false);
	critic.backward(inp, tgt);
	NNHardAssert(critic.inGrad().addM(grd, -1).square().sum() < 1e-12, "NLL<>::backward with no average failed!");
	
	critic.average(true);
	critic.backward(inp, tgt);
	NNHardAssert(critic.inGrad().addM(grd, -1.0 / inp.size()).square().sum() < 1e-12, "NLL<>::backward with average failed!");
	
	critic.batch(12);
	NNHardAssert(critic.batch() == 12, "NLL<>::batch failed!");
}

#endif

{'fraction_non_alphanumeric': 0.22998296422487224, 'fraction_numerical': 0.04557069846678024, 'mean_word_length': 4.619617224880383, 'pattern_counts': {'":': 0, '<': 41, '<?xml version=': 0, '>': 32, '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': '626980', 'n_tokens_mistral': 1067, 'n_tokens_neox': 974, 'n_words': 214}