TF急切模式:如果可能,从磁盘加载完整的模型

时间:2018-09-24 17:43:41

标签: python tensorflow

更新:“ AttributeError:'AdamOptimizer'对象没有属性'name'”可能是此问题的替代标题。如果这可以解决,那么整个事情可能会正常进行。

有一个没有模型保存代码的新jupyter笔记本。训练好的模型已经使用另一个笔记本保存了。在这里使用了虹膜数据集,并且对模型都进行了训练:

#This might be the way:
#https://www.tensorflow.org/guide/eager#object_based_saving
#https://stackoverflow.com/questions/47852516/tensorflow-eager-mode-how-to-restore-a-model-from-a-checkpoint
savePrefix = "/tmp/iris"
root = tfe.Checkpoint(optimizer=optimizer, model=model, optimizer_step=global_step)
restorePrefix = root.save(savePrefix)
# '/tmp/iris-1'
print(restorePrefix)
print(root)
print("The model was saved to {}\nRestore the model using {}".format(savePrefix, restorePrefix))
# Try loading this in a different notebook to prove it worked.

>/tmp/iris-1
><tensorflow.contrib.eager.python.checkpointable_utils.Checkpoint object at 0x7fb308799e80>
>The model was saved to /tmp/iris
>Restore the model using /tmp/iris-1

我从上面获取了输出路径,然后尝试使用tf.contrib.eager代码将模型加载到新笔记本中,但失败了:

s = tfe.Saver([optimizer, model, global_step])
s.restore(file_prefix="/tmp/iris-1")

>NameError: name 'optimizer' is not defined

当模型加载笔记本中的代码没有保存模型并且没有模型时,用tf.contrib.eager api(不是会话api)加载以前开发的模型的实际工作用例代码是什么?模型的内存部分已经像优化器,图形定义和global_state一样?

TF文档始终选择演示毫无意义的示例,以加载我们已经在内存中存储的模型。我无法确定“使用TF.contrib.eager API,您是否必须在笔记本中拥有显式的模型创建代码,优化器创建代码和全局步骤代码,因为TFE无法从磁盘加载这些东西”或“它是一个新的API”并且缺少某些功能”,或“您还必须同时使用会话和图形编码api以及tf.contrib.eager api”或“仅使用实际上与命令性代码一起使用的Microsoft cntk,他们并没有忘记API的关键部分” 。

如果您知道什么,谢谢。如果进行推测,请说明。

我想这是下面的一些超集和子集组合,如果它能起作用的话。 (从有关该主题的SO帖子中删除。)

tfe.restore_variables_on_create 
tf.train.import_meta_graph() 
new_saver.restore()
tf.train.latest_checkpoint()
tfe.save_network_checkpoint()
tfe.restore_network_checkpoint()

更新-我添加了代码,首先手动将模型,优化器等重新创建到内存中,然后运行restore()代码。错误-优化器没有名称。但是,奇怪的是,根据文档,它实际上确实具有name属性:

#OK I'll just try to use code to create the model artifacts first. 
# I'd rather load it all from disk but maybe at least it might work.

optimizer = tf.train.AdamOptimizer() #ga
global_step = tf.train.get_or_create_global_step()  # what is a global_step?
model = tf.keras.Sequential([
  tf.keras.layers.Dense(10, activation=tf.nn.relu, input_shape=(4,)),  # input shape required
  tf.keras.layers.Dense(10, activation=tf.nn.relu, kernel_initializer='glorot_uniform'),
  tf.keras.layers.Dense(3)
])
s = tfe.Saver([optimizer, model, global_step])
s.restore(file_prefix="/tmp/iris-1")

INFO:tensorflow:从/ tmp / iris-1恢复参数


AttributeError:“ AdamOptimizer”对象没有属性“名称”

文档说:

__init__(
    learning_rate=0.001,
    beta1=0.9,
    beta2=0.999,
    epsilon=1e-08,
    use_locking=False,
    name='Adam'
)

肯定看起来亚当优化器已经有了名称!那很好笑。那是什么问题呢?

也许TF错误实际上是说无法恢复优化器以及模型和global_state变量。那么从检查点还原什么-特别是在保存和相应还原中将包含哪些变量?谢谢,如果您什么都不知道。

0 个答案:

没有答案
相关问题