Search code examples
machine-learningkerasformatembedding

Dynamic name formatting with epoch number for .tsv file creation


I 've made this custom callback in Keras for obtaining the embedding vectors at the end of each epoch. It should save the vectors on a .tsv which it does but since the name does not update on each epoch, it results in rewriting the first file. So by the end of training I only get the vectors of the last epoch. I need a way to add the epoch number on the .tsv file name but I have no idea how to do it.

The code:

import io

encoder = info.features['text'].encoder

class CustomCallback(keras.callbacks.Callback):
    def on_epoch_end(self, epoch, logs=None):
        out_v = io.open('vecs.tsv', 'w', encoding='utf-8')
        vec = model.layers[0].get_weights()[0]
        out_v.write('\t'.join([str(x) for x in vec]) + "\n")
        out_v.close()

Any suggestions would be much appreciated. Thanks a lot!


Solution

  • You can do that by chaging following:

    out_v = io.open('vecs_{}.tsv'.format(epoch), 'w', encoding='utf-8')
    

    The filename will be vec_1.tsv, vec_2.tsv and so on.