1

After training one model on a set of data, I'd like to get a copy of it and train each copy on different data sets. Is there a way to clone a model in such a way that after the cloning each of them can be trained separately?

I've already tried to save the model in my localstorage and generate a copy from there but tensorflow complains that some variable names are already in use.

fatmau
  • 87
  • 1
  • 8

2 Answers2

1

In case someone else is interested, I have eventually solved it getting all the weights of the model I wanted to copy with the getweights() function, creating a new model with the same architecture and then copying back the other model's weight to it with setweights()

fatmau
  • 87
  • 1
  • 8
1

Step 1. You extract the model with tf.io.withSaveHandler via save(), it's callback style so you may want to wrap it with a Promise for modern codebases.

const modelArtifacts = await new Promise<tf.io.ModelArtifacts>(resolve => {
  model.save(
    tf.io.withSaveHandler(artifacts => {
      resolve(model);

      // Optional, prevents type error.
      return Promise.resolve({
        modelArtifactsInfo: {
          dateSaved: new Date(), 
          modelTopologyType: "JSON"
        }
      });
    })
  );
});

Step 2. You load the extracted contents into a new model with tf.io.fromMemory.

const newModel = await tf.loadLayersModel(tf.io.fromMemory(modelArtifacts));
Vicary
  • 1,026
  • 1
  • 15
  • 35