Skip to content

Commit

Permalink
Update neural network trainer and bump to version 1.0
Browse files Browse the repository at this point in the history
  • Loading branch information
Witek902 committed Sep 18, 2022
1 parent 4abd9db commit 175181b
Show file tree
Hide file tree
Showing 8 changed files with 24 additions and 19 deletions.
6 changes: 2 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,9 @@ UCI command-line chess engine written in C++ from scratch. In development since

### Playing strength

Estimated score of the newest version: **~3290 Elo**
CCRL 40/15 Score: **3293** (#44) (version 0.9.0)

CCRL 40/15 Score: **3218** (#45) (version 0.8.0)

CCRL 2+1 Score: **3191** (#62) (version 0.7.0)
CCRL 2+1 Score: **3222** (#46) (version 0.8.0)

### Supported UCI options

Expand Down
4 changes: 2 additions & 2 deletions src/backend/Evaluate.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@
#include <fstream>
#include <memory>

const char* c_DefaultEvalFile = "eval-4.pnn";
const char* c_DefaultEndgameEvalFile = "endgame-3.pnn";
const char* c_DefaultEvalFile = "eval.pnn";
const char* c_DefaultEndgameEvalFile = "endgame.pnn";

#define S(mg, eg) PieceScore{ mg, eg }

Expand Down
2 changes: 1 addition & 1 deletion src/frontend/UCI.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
#include <math.h>
#include <random>

#define VersionNumber "0.9.5"
#define VersionNumber "1.0"

#if defined(USE_BMI2) && defined(USE_AVX2)
#define ArchitectureStr "AVX2/BMI2"
Expand Down
Binary file modified src/frontend/frontend.aps
Binary file not shown.
10 changes: 5 additions & 5 deletions src/frontend/frontend.rc
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,8 @@ LANGUAGE LANG_POLISH, SUBLANG_DEFAULT
//

VS_VERSION_INFO VERSIONINFO
FILEVERSION 0,4,0,0
PRODUCTVERSION 0,4,0,0
FILEVERSION 1,0,0,0
PRODUCTVERSION 1,0,0,0
FILEFLAGSMASK 0x3fL
#ifdef _DEBUG
FILEFLAGS 0x3L
Expand All @@ -42,12 +42,12 @@ BEGIN
BLOCK "000904b0"
BEGIN
VALUE "FileDescription", "Caissa Chess Engine"
VALUE "FileVersion", "0.4.0.0"
VALUE "FileVersion", "1.0.0.0"
VALUE "InternalName", "frontend.exe"
VALUE "LegalCopyright", "Copyright (C) 2021"
VALUE "LegalCopyright", "Copyright (C) 2022"
VALUE "OriginalFilename", "Caissa.exe"
VALUE "ProductName", "Caissa"
VALUE "ProductVersion", "0.4.0.0"
VALUE "ProductVersion", "1.0.0.0"
END
END
BLOCK "VarFileInfo"
Expand Down
4 changes: 4 additions & 0 deletions src/utils/NetworkTrainer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,8 @@ bool TrainNetwork()
networksData[i].runCtx.Init(networksData[i].network);
}

std::ofstream trainingLog("training.log");

std::vector<PositionEntry> entries;
LoadAllPositions(entries);

Expand Down Expand Up @@ -278,6 +280,8 @@ bool TrainNetwork()
networksData[i].packedNet.Save((name + ".pnn").c_str());
networksData[i].packedNet.SaveAsImage((name + ".raw").c_str());
}

trainingLog << iteration << "\t" << nnErrorSum << "\t" << nnPackedErrorSum << std::endl;
}
}

Expand Down
10 changes: 5 additions & 5 deletions src/utils/NeuralNetwork.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -633,11 +633,11 @@ void Layer::UpdateWeights_AdaDelta(float learningRate, const Gradients& gradient

void NeuralNetwork::ClampLayerWeights(size_t layerIndex, float weightRange, float biasRange, float weightQuantizationScale, float biasQuantizationScale)
{
const float cDecay = 1.0e-7f;
const float cDecay = 0.5e-6f;

Layer& layer = layers[layerIndex];

for (uint32_t j = 0; j < layer.numInputs; j++)
for (uint32_t j = 0; j <= layer.numInputs; j++)
{
const bool isBiasWeight = (j == layer.numInputs);

Expand All @@ -649,11 +649,11 @@ void NeuralNetwork::ClampLayerWeights(size_t layerIndex, float weightRange, floa

if (isBiasWeight)
{
w = std::clamp(w * biasQuantizationScale, -biasRange, biasRange) / biasQuantizationScale;
w = std::clamp(w, -biasRange / biasQuantizationScale, biasRange / biasQuantizationScale) ;
}
else
{
w = std::clamp(w * weightQuantizationScale, -weightRange, weightRange) / weightQuantizationScale;
w = std::clamp(w, -weightRange / weightQuantizationScale, weightRange / weightQuantizationScale) ;
}
}
}
Expand Down Expand Up @@ -731,7 +731,7 @@ void NeuralNetworkTrainer::Train(NeuralNetwork& network, const TrainingSet& trai
for (size_t i = 0; i < ctx.tempValues.size(); i++)
{
// gradient of RMS loss function
ctx.tempValues[i] = ctx.tempValues[i] - vec.output[i];
ctx.tempValues[i] = /* 2.0f * */ (ctx.tempValues[i] - vec.output[i]);

// gradient of cross-entropy loss function
//const float target = vec.output[i];
Expand Down
7 changes: 5 additions & 2 deletions src/utils/TrainerCommon.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ static bool LoadPositions(const char* fileName, std::vector<PositionEntry>& entr

if (move.IsQuiet() &&
pos.GetNumPieces() >= 6 &&
pos.GetHalfMoveCount() < 80 &&
pos.GetHalfMoveCount() < 60 &&
whitePawnsMoved && blackPawnsMoved &&
!pos.IsInCheck() && pos.GetNumLegalMoves())
{
Expand All @@ -77,7 +77,7 @@ static bool LoadPositions(const char* fileName, std::vector<PositionEntry>& entr
// blend in future scores into current move score
float scoreSum = 0.0f;
float weightSum = 0.0f;
const size_t maxLookahead = 16;
const size_t maxLookahead = 12;
for (size_t j = 0; j < maxLookahead; ++j)
{
if (i + j >= game.GetMoves().size()) break;
Expand All @@ -99,6 +99,9 @@ static bool LoadPositions(const char* fileName, std::vector<PositionEntry>& entr
entry.score = std::lerp(score, scoreSum, lambda);
}

const float offset = 0.00001f;
entry.score = offset + entry.score * (1.0f - 2.0f * offset);

Position normalizedPos = pos;
if (pos.GetSideToMove() == Color::Black)
{
Expand Down

0 comments on commit 175181b

Please sign in to comment.