How to save a tensor

587 Views Asked by At

I have a dataset of 1000 items. I normalize the data before I train the model against it.

I would now like to use the model to make predictions. However, from what I understand, I need to normalize the inputs that I will feed to the model for which I need the predictions for. In order to carry this out, I would need the mean and std calculated at the time of training.

While I am able to print it to the console, how does one "save" it - to be used later? I am trying to understand the procedure here on how to save the mean and std used at the time of normalization of the training data - so that I can use it again at the time of making predictions.

1

There are 1 best solutions below

0
On BEST ANSWER

I determined that we could first get the array representation of the tensor through:

// tensor here is the tensor variable that contains the tensor
const tensorAsArray = tensor.arraySync()

and then, we save it to a file like any other string

fs.writeFile(myFilePath, JSON.stringify(tensorAsArray), 'utf-8')

To read it back and use it as a tensor, we would do the opposite:

const tensorAsArray = JSON.parse(fs.readFile(myFilePath, 'utf-8'))
const tensor = tf.tensor(tensorAsArray)

This allowed me to save the mean and std for use later.