Restoring Trainable Variables from Saved Model
还没有人认领这个 Issue。
- 主要语言
- Java
- 星标
- 928
- 派生
- 227
- PR 合并指标
- 30 天内没有已合并 PR
描述
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
贡献指南
从这里开始
- 先读完整个 Issue,再读项目的贡献指南。
- 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
- Fork 仓库,在一个分支上完成修改。
- 提交 Pull Request,并在描述里引用这个 Issue 编号。
调研方向
从 SavedModelBundle.load、model.function("serving_default") 和 model.metaGraphDef() 中展示的 Java 加载路径开始。检查 ConcreteFunction 和 SignatureDef 如何公开保存的模型,然后确定可训练变量的值是否在其中表示。完成的标准是记录或启用一种受 Java 支持的方式,以从保存的模型中获取这些值。
由索引模型根据 Issue 内容生成。
评估
- 技术栈
- java, tensorflow
- 领域
- machine-learning
- Issue 类型
- 功能
- 难度
- 4/5
- 预计耗时
- 3-5 天
- 活跃度
- 停滞
- 描述清晰度
- 需要澄清
- 新手友好度
- 25/100