Restoring Trainable Variables from Saved Model
Nobody has claimed this yet.
Assessment
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Newbie friendliness
- 25/100
- Issue type
- Feature
- Clarity
- Needs clarification
- Activity status
- Stale
- Tech stack
- java, tensorflow
- Domain
- machine-learning
Research direction
Start with the Java loading path shown in SavedModelBundle.load, model.function("serving_default"), and model.metaGraphDef(). Review how ConcreteFunction and SignatureDef expose a saved model, then determine whether trainable variable values are represented there. Done means documenting or enabling a Java-supported way to retrieve those values from the saved model.
Written by the indexing model from the issue text.
Description
I defined a model using Tensorflow 2 with a Model Sublassing API on Python
class BiLSTMModel(tf.keras.Model):
def __init__(self, lstm_dims):
super().__init__()
self.ldims = lstm_dims
self.blockLstm = FirstBlockLSTMModule(lstm_dims)
self.nextBlockLstm = NextBlockLSTM(lstm_dims)
def call(self, inputs):
# Forward pass
block_lstm1_output = self.blockLstm(inputs)
block_lstm2_output = self.nextBlockLstm(block_lstm1_output)
return block_lstm2_output
I implemented a custom training loop and after each epoch, I just save this Keras model
biLSTMModel = BiLSTMModel(lstm_dims)
for epoch in range(epochs):
# Training code
tf.saved_model.save(biLSTMModel, export_dir)
It generates the expected structure with assets and variables folders, along with saved_model.pb and variables files
Later on, when loading the model I can restore all trainable variables values of the model just like this:
loaded_bi_lstm = tf.saved_model.load(model_path)
infer_bi_lstm = loaded_bi_lstm.signatures["serving_default"]
# LSTM weights
w_first_lstm = tf.Variable(infer_bi_lstm.trainable_variables[0])
wig_first_lstm = tf.Variable(infer_bi_lstm.trainable_variables[1])
wfg_first_lstm = tf.Variable(infer_bi_lstm.trainable_variables[2])
wog_first_lstm = tf.Variable(infer_bi_lstm.trainable_variables[3])
The model on tensorflow-java is loading without errors.
@Test
public void shouldRestoreVariablesFromSavedModel() {
SavedModelBundle model = SavedModelBundle.load(SAVED_MODEL_DP_PATH, SavedModelBundle.DEFAULT_TAG);
ConcreteFunction concreteFunction = model.function("serving_default");
//Trying to find some object that stores trainables variables data
MetaGraphDef metaGraphDef = model.metaGraphDef();
SignatureDef sig = metaGraphDef.getSignatureDefOrThrow("serving_default");
}
But I don't find a way to restore the trainable variables. When debugging, it seems there is no object that stores these data. Could you please guide me on how to get all trainable variables values from a saved model on Java.
Thanks
- Dominant language
- Java
- Stars
- 928
- Forks
- 227
- PR merge metrics
- No merged PRs in 30d
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
More from tensorflow/java
-
Difficulty 2/5 1-3 hours Newbie friendliness 65/100
tensorflow/java#653 · 1 comment · 4 reactions ·
-
Difficulty 5/5 Over a week Newbie friendliness 25/100
tensorflow/java#621 · 4 comments ·
-
Difficulty 5/5 Over a week Newbie friendliness 25/100
tensorflow/java#617 · 3 comments ·
-
Difficulty 2/5 1-3 hours Newbie friendliness 55/100
tensorflow/java#615 · 1 comment ·
-
Difficulty 5/5 Over a week Newbie friendliness 25/100
tensorflow/java#614 · 1 comment ·
Similar issues
-
Difficulty 2/5 1-3 hours Newbie friendliness 82/100
infinispan/infinispan#18150 ·
-
area/frontend
Difficulty 2/5 1-3 hours Newbie friendliness 65/100
-
Difficulty 2/5 1-3 hours Newbie friendliness 84/100
-
untriaged
Difficulty 2/5 1-3 hours Newbie friendliness 82/100
opensearch-project/k-NN#3597 ·
-
bug
Difficulty 2/5 1-3 hours Newbie friendliness 88/100