question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Save a model in TensorFlowSharp

See original GitHub issue

I can load python-built models or build models myself but is there currently a good way of saving the whole graph that I do not know of? Right now i am loading a pretrained model, run the init operation and then train it myself which is working fine. However, i would like to save it to disc also, as is possible in python.

Only thing i found was the TFGraph.Save() method, but i would have to specify what tensors to save and wouldnt know how to reload it then (or if it is even doing what i think its doing).

Any help would be appreciated!

Issue Analytics

  • State:open
  • Created 6 years ago
  • Comments:5 (2 by maintainers)

github_iconTop GitHub Comments

1reaction
migueldeicazacommented, Mar 3, 2018

I added these convenience methods to TFSession

0reactions
plasticalligatorcommented, Feb 26, 2018

I was frustrated earlier and worked out a temporary solution until the library is in a better state.

This works for what I need, but it can be improved a lot: there is a RestoreV2 function which can restore multiple tensors in a single go, but it wants a parameter named shape_and_slices, I haven’t a clue what formatting it wants the numbers in, and right now I want to throw my laptop out a window.

    public static class Extensions
    {
        static public TFOutput RestoreTensor(this TFSession session, string filename, string tensor, TFDataType type)        
            => session.Graph.Restore(session.Graph.Const(TFTensor.CreateString(Encoding.ASCII.GetBytes(filename))), 
                                     session.Graph.Const(TFTensor.CreateString(Encoding.ASCII.GetBytes(tensor))), 
                                     type);

        static public TFTensor[] SaveTensors(this TFSession session, string filename,params ValueTuple<string,TFOutput>[] tensors) =>
            session.GetRunner().AddTarget(session.Graph.Save(session.Graph.Const(TFTensor.CreateString(Encoding.ASCII.GetBytes(filename)), TFDataType.String),
                                          session.Graph.Concat(session.Graph.Const(0), tensors.Select(T =>
                                          {
                                              TFTensor clone = TFTensor.CreateString(Encoding.ASCII.GetBytes(T.Item1));
                                              return session.Graph.Const(new TFTensor(TFDataType.String,
                                                                                      new long[] { 1 },
                                                                                      clone.Data,
                                                                                      clone.TensorByteSize, 
                                                                                      null, IntPtr.Zero));
                                          }).ToArray()), tensors.Select(d=>d.Item2).ToArray() )).Run();
    }

e.g

            using (var session = new TFSession())
            {
                    var a = session.Graph.Const(30, "a");
                    var b = session.Graph.Const(12, "b");
                    var multiplyResults = session.GetRunner().Run(session.Graph.Add(a, b));
                    var multiplyResultValue = multiplyResults.GetValue();
                    Console.WriteLine("a*b={0}", multiplyResultValue);
                    session.SaveTensors($"saved.tsf", ("a", a), ("b", b));
            }

            using (var session = new TFSession())
            {
                    var a = session.RestoreTensor("saved.tsf", "a", TFDataType.Int32);
                    var b = session.RestoreTensor("saved.tsf", "b", TFDataType.Int32);                
                    var multiplyResults = session.GetRunner().Run(session.Graph.Add(a, b));
                    var multiplyResultValue = multiplyResults.GetValue();
                    Console.WriteLine("a*b={0}", multiplyResultValue);
            }
Read more comments on GitHub >

github_iconTop Results From Across the Web

Save and load models
Model progress can be saved during and after training. This means a model can resume where it left off and avoid long training...
Read more >
Save and load models in Tensorflow
Now you can simply save the weights of all the layers using the save_weights() method. It saves the weights of the layers contained...
Read more >
Saving and loading models in TensorFlow
The simple way to save the model in TensorFlow is that we can use the built-in function of Tensorflow.Keras.models “Model saving & serialization ......
Read more >
Save and load
An entire model can be saved in two different file formats ( SavedModel and HDF5 ). The TensorFlow SavedModel format is the default...
Read more >
Save and Load Models with TensorFlow
Save and restore the entire model. To save the model, we can either use the function model.save() or tf.keras.models.save_model(). We also have ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found