Number recognition Tuning

This commit is contained in:
IlyaShurupov 2023-10-25 22:42:20 +03:00
parent 826713c212
commit fc20f3594d
3 changed files with 24 additions and 12 deletions

View file

@ -48,7 +48,7 @@ bool loadDataset(Dataset& out, const String& location) {
struct NumberRec { struct NumberRec {
NumberRec() { NumberRec() {
Buffer<halni> layers = { 784, 128, 10 }; Buffer<halni> layers = { 784, 10 };
nn.initializeRandom(layers); nn.initializeRandom(layers);
Dataset dataset; Dataset dataset;
@ -159,7 +159,7 @@ public:
FCNN nn; FCNN nn;
Buffer<halnf> output; Buffer<halnf> output;
halnf step = 0.01f; halnf step = 1.f;
}; };
int main() { int main() {
@ -173,14 +173,26 @@ int main() {
{ {
NumberRec app; NumberRec app;
auto trainRange = Range(0, 100); auto numBatches = 10;
auto testRange = Range(0, 100); auto trainRange = Range(0, 50000);
auto testRange = Range(50000, 70000);
halnf cost = 100; auto batchSize = trainRange.idxDiff() / numBatches;
while (cost > 0.1f) {
cost = app.test(trainRange); for (auto epoch : Range(10)) {
app.trainStep(trainRange); printf("Epoch %i\n", epoch.index());
printf("Cost - %f\n", cost);
for (auto batchIdx : Range(trainRange.idxDiff() / batchSize)) {
printf(" - Batch :%i \n", batchIdx.index());
auto batchRange = Range(trainRange.idxBegin() + batchSize * batchIdx, trainRange.idxBegin() + batchSize * (batchIdx + 1));
app.trainStep(batchRange);
printf("Cost on batch data : %f\n", app.test(batchRange));
}
printf("Cost on test data : %f\n\n", app.test(testRange));
} }
auto errors = 0; auto errors = 0;
@ -192,7 +204,7 @@ int main() {
// app.displayImage(i); // app.displayImage(i);
} }
printf("\n\nIncorrect - %i out of %i\n\n", errors, testRange.idxDiff()); printf("\n\nIncorrect - %i out of %i (%f)\n\n", errors, testRange.idxDiff(), (halnf) errors / (halnf) testRange.idxDiff() );
} }
module.deinitialize(); module.deinitialize();

View file

@ -22,9 +22,9 @@ static halnf relu(halnf val) { return val < 0 ? 0 : val; }
static halnf reluDerivative(halnf val) { return val < 0 ? 0 : 1; } static halnf reluDerivative(halnf val) { return val < 0 ? 0 : 1; }
static halnf activationFunction(halnf val) { return relu(val); } static halnf activationFunction(halnf val) { return sigmoid(val); }
static halnf activationFunctionDerivative(halnf val) { return reluDerivative(val); } static halnf activationFunctionDerivative(halnf val) { return sigmoidDerivative(val); }
FCNN::FCNN(const Buffer<halni>& description) { initializeRandom(description); } FCNN::FCNN(const Buffer<halni>& description) { initializeRandom(description); }