Issue with Texel Tuning

Discussion of chess software programming and technical issues.

Moderator: Ras

ciorap0
Posts: 23
Joined: Sun Feb 12, 2023 11:10 am
Full name: Vlad Ciocoiu

Issue with Texel Tuning

Post by ciorap0 »

Hello TalkChess!

This is my first post here, I'm developing an engine you can find here: https://github.com/vladciocoiu/ciorap-bot

I am currently working on tuning the evaluation parameters and that's how I found Texel's Tuning method.

This is the code that I use for tuning (please ask me if anything is unclear):

Code: Select all

    const int nParams = initialGuess.size();
    
    // E(params) = sum((sigmoid(eval(params)) - game_result)^2) for all positions
    // where sigmoid(x) = 1 / (1 + exp(-x / 400))
    
    double bestE = E(initialGuess);
    vector<int> bestParValues = initialGuess;
    bool improved = true;
    int iteration = 0;
    while ( improved ) {
        cout << "Iteration " << iteration << " started.\n";
        improved = false;
        for (int pi = 0; pi < nParams; pi++) {
            bool improvedParam;
            do {
                improvedParam = false;
                vector<int> newParValues = bestParValues;
                newParValues[pi] += 1;
                double newE = E(newParValues);

                if (newE < bestE) {
                    improvedParam = true;
                    cout << "Found better value at parameter " << pi << ": " << bestParValues[pi] << " -> " << newParValues[pi] << ", mse=" << newE << '\n';
                    bestE = newE;
                    bestParValues = newParValues;
                    improved = true;
                } else {
                    newParValues[pi] -= 2;
                    newE = E(newParValues);
                    if (newE < bestE) {
                        improvedParam = true;
                        cout << "Found better value at parameter " << pi << ": " << bestParValues[pi] << " -> " << newParValues[pi] << ", mse=" << newE << '\n';
                        bestE = newE;
                        bestParValues = newParValues;
                        improved = true;
                    }
                }
            } while(improvedParam);
      }
      iteration++;
   }
   return bestParValues;
I got the training positions from 9000 matches against a stronger engine (+ 300 elo), and filtered out the positions where the quiescence score != evaluation score.

I have to mention that the parameters I'm trying to tune are the piece-square tables, king safety table, passed pawn bonuses, mobility bonuses, and other smaller features such as trapped minor piece penalties, tempo bonus, rook on open file bonus etc.

The problem is that every time I try and train these parameters, some of them get inexplicably big / small.

For example the value for the queen on g3 in the piece-square table becomes -555 centipawns, or the tempo bonus becomes -107.

Has someone else ever experienced this or knows what could cause it?

I would appreciate your help a lot. Thanks!
AndrewGrant
Posts: 1955
Joined: Tue Apr 19, 2016 6:08 am
Location: U.S.A
Full name: Andrew Grant

Re: Issue with Texel Tuning

Post by AndrewGrant »

Sounds like a classic case of overfitting due to a lack of data.
I would also suggest you work away from Texel tuning, and do something GD based.
Texel is very very dated.
JoAnnP38
Posts: 253
Joined: Mon Aug 26, 2019 4:34 pm
Location: Clearwater, Florida USA
Full name: JoAnn Peeler

Re: Issue with Texel Tuning

Post by JoAnnP38 »

AndrewGrant wrote: Mon Feb 13, 2023 5:09 am Sounds like a classic case of overfitting due to a lack of data.
I would also suggest you work away from Texel tuning, and do something GD based.
Texel is very very dated.
GD == "Gradient Descent" ???
AndrewGrant
Posts: 1955
Joined: Tue Apr 19, 2016 6:08 am
Location: U.S.A
Full name: Andrew Grant

Re: Issue with Texel Tuning

Post by AndrewGrant »

JoAnnP38 wrote: Mon Feb 13, 2023 7:34 am
AndrewGrant wrote: Mon Feb 13, 2023 5:09 am Sounds like a classic case of overfitting due to a lack of data.
I would also suggest you work away from Texel tuning, and do something GD based.
Texel is very very dated.
GD == "Gradient Descent" ???
Yes. Anything that approximates what you would do if you were training an NN.
Which, imo, a hand-crafted evaluation is.
ciorap0
Posts: 23
Joined: Sun Feb 12, 2023 11:10 am
Full name: Vlad Ciocoiu

Re: Issue with Texel Tuning

Post by ciorap0 »

AndrewGrant wrote: Mon Feb 13, 2023 5:09 am Sounds like a classic case of overfitting due to a lack of data.
I would also suggest you work away from Texel tuning, and do something GD based.
Texel is very very dated.
So basically 670k positions aren't enough? I've heard of engines that improved a lot with far less, but maybe they had less complex evaluation functions to begin with.

Thanks for the advice! I'm gonna try and come up with a gradient descent algorithm, but do you think the training data that I currently have would be enough for that?
User avatar
lithander
Posts: 915
Joined: Sun Dec 27, 2020 2:40 am
Location: Bremen, Germany
Full name: Thomas Jahn

Re: Issue with Texel Tuning

Post by lithander »

ciorap0 wrote: Mon Feb 13, 2023 10:37 am So basically 670k positions aren't enough? I've heard of engines that improved a lot with far less, but maybe they had less complex evaluation functions to begin with.

Thanks for the advice! I'm gonna try and come up with a gradient descent algorithm, but do you think the training data that I currently have would be enough for that?
Depends on the quality of the data not only quantity. Your 9000 games against a stronger engine will only create a subset of all the possible positions, especially if no randomization is involved and that means for some rare piece-positions you won't have a lot of data. Let's say you had only a dozen positions with a queen on g3 but all of them were sourced from a lost game for that queen's side. Well, then your tuner will learn that putting a queen on g3 is a terrible idea!

You can verify the quality of your tuner with a proven dataset. And you can verify the quality of your dataset once the tuner is proven to work correctly. I wouldn't go two steps in one.
Minimal Chess (simple, open source, C#) - Youtube & Github
Leorik (competitive, in active development, C#) - Github & Lichess
ciorap0
Posts: 23
Joined: Sun Feb 12, 2023 11:10 am
Full name: Vlad Ciocoiu

Re: Issue with Texel Tuning

Post by ciorap0 »

lithander wrote: Mon Feb 13, 2023 11:06 am You can verify the quality of your tuner with a proven dataset. And you can verify the quality of your dataset once the tuner is proven to work correctly. I wouldn't go two steps in one.
I found the Zurichess dataset. Hope I will get better results with it, and with the GD algorithm :)
Whiskers
Posts: 243
Joined: Tue Jan 31, 2023 4:34 pm
Full name: Adam Kulju

Re: Issue with Texel Tuning

Post by Whiskers »

A good way to test your tuning algorithm is to let it change say only the value of a pawn and see if the results are reasonable, or see if it manages to converge from a starting value that is very small/very large.

As for gradient descent, I found this forum extremely helpful for implementing it: http://www.talkchess.com/forum3/viewtop ... 24150faeca
JVMerlino
Posts: 1396
Joined: Wed Mar 08, 2006 10:15 pm
Location: San Francisco, California

Re: Issue with Texel Tuning

Post by JVMerlino »

Sigh - it took me years to wrap my brain around Texel tuning before I could feel good about trying to implement it. Now I'm back to "square zero" with GD - I don't understand it at all! :oops: :D
Fulvio
Posts: 396
Joined: Fri Aug 12, 2016 8:43 pm

Re: Issue with Texel Tuning

Post by Fulvio »

JVMerlino wrote: Tue Feb 14, 2023 6:58 pm Sigh - it took me years to wrap my brain around Texel tuning before I could feel good about trying to implement it. Now I'm back to "square zero" with GD - I don't understand it at all! :oops: :D
This is a great explanation imho: