Janeasefor
Janeasefor

Reputation: 33

How to just save tensor not models in Tensorflow2

I have learned Tensorflow2 for several months but I meet some difficulty.For example,I create one tensor like:

import tensorflow as tf
v=tf.random.normal((20,30,40))

Now I just want to save tensor v into suitable file.Actually speaking ,v is created from .nc data.I use packages "netCDF4" to read it and select some variables,whose dimensions are (time,lon,lat),to concat them into v with dimensions of (time,lon,lat,var_num).

But v's size is large (for example,(1000,224,224,5)).So I need to save v in case of reading netcdf for many times. I search some questions but little can help me because they are either about saving variables in tf1.X or saving models(or variables in models) in tf 2.

So I come here and seek handsome persons's help.Thanks a lot in advance.

Upvotes: 3

Views: 1480

Answers (1)

Stewart_R
Stewart_R

Reputation: 14515

You can still use the saved model format to store a single tf.Variable

You would just capture v in a tf.Variable then pass that to tf.saved_model.save.

Maybe something like this:

v=tf.Variable(tf.random.normal((20,30,40)))
tf.saved_model.save(v, '/path/to/my_var')

then to load again from the saved version:

v_from_file = tf.saved_model.load('/path/to/my_var')

Upvotes: 4

Related Questions