I am trying to solve the following problem:

Let's say I have a chess position:

enter image description here

I encode each square as one-hot encoded vector of length 13

  • index 0 for empty square
  • index 1 for white pawn
  • index 2 for black pawn
  • index 3 for white bishop
  • index 4 for black bishop
  • etc..

So, at the end I have a matrix ${M}$ with a shape 64x13, which I intend to use as an input of the model. I want to create a model able to map matrix ${M}$ to FEN representation of the position.

Given the example above, FEN representation of the position is: 1b1N4/7k/4b3/8/8/8/3P4/3K4

The length of a FEN-string is not fixed. I failed to find a ML algorithm which accepts fixed number of inputs and produces variable number of outputs. What machine learning algorithm can I use in that case ? Is RNN appropriate for that task ?

P.S. I do understand, that it is trivial to just write a function to map ${M}$ to FEN, but I do not want it, I want the model to learn it from data.


RNN (and LSTM / GRU)can generate variable length output. For example, generation of text.

In such problems, RNN is designed to generate next character (It keeps track of what characters have generated in past). RNN should generate "End Of Output" character to indicate end of sequence.

For example : https://chunml.github.io/ChunML.github.io/project/Creating-Text-Generator-Using-Recurrent-Neural-Network/

Following method will have to be changed :

def generate_text(model, length):
    ix = [np.random.randint(VOCAB_SIZE)]
    y_char = [ix_to_char[ix[-1]]]
    X = np.zeros((1, length, VOCAB_SIZE))
    for i in range(length):
        X[0, i, :][ix[-1]] = 1
        print(ix_to_char[ix[-1]], end="")
        ix = np.argmax(model.predict(X[:, :i+1, :])[0], 1)
    return ('').join(y_char)

to :

     stop_characters = set(['.','?'])
     ix = np.argmax(model.predict(X[:, :i+1, :])[0], 1)
     if(predicted_char in stop_characters ):
|improve this answer|||||

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.