当变量形状改变时从检查点恢复 [英] Restoring from checkpoint when variable shape changes
问题描述
我无法恢复包含改变形状的变量的检查点模型.例如这个简单的模型:
I'm unable to restore checkpointed models that include variables that change shape. For example with this simple model:
var = tf.get_variable(initializer=tf.constant_initializer([0]), shape=[1], trainable=False, name='var')
op = tf.assign(var, [1, 2], validate_shape=False)
saver = tf.train.Saver(reshape=False)
如果我运行 op
然后保存模型,当我尝试恢复它时,我会收到以下错误:
if I run op
and then save the model, when I try to restore it I get the following error:
Assign requires shapes of both tensors to match. lhs shape= [1] rhs shape= [2]
[[Node: save/Assign = Assign[T=DT_FLOAT, use_locking=true, validate_shape=true, _device="/job:localhost/replica:0/task:0/cpu:0"](var, save/restore_slice)]]
这似乎与不断变化的形状和 Saver
试图验证形状有关.如果我在构建 Saver
时将 reshape
设置为 True
,根据文档应该可以解决这个问题,我反而得到这个错误:>
which seems to have to do with the changing shape and Saver
trying to validate shape. If I set reshape
to True
when constructing the Saver
, which according to the docs should solve this problem, I instead get this error:
Input to reshape is a tensor with 2 values, but the requested shape has 1
[[Node: save/Reshape = Reshape[T=DT_FLOAT, _device="/job:localhost/replica:0/task:0/cpu:0"](save/restore_slice, save/Reshape/shape)]]
我倾向于认为这是一个错误.
I'm inclined to think that this is a bug.
推荐答案
Saver
的 reshape 选项仅在形状具有相同的元素总数时才有效.例如,它可以让您从形状为 []
的数据中加载形状为 [1]
的变量,或形状为 [15, 7]<的变量/code> 来自形状为
[5, 21]
的数据.如果形状以这种方式不兼容,则您必须构建一个新图.
The reshape option to Saver
only works if the shapes have the same total number of elements. For example, it will let you load a variable with shape [1]
from data with shape []
, or a variable with shape [15, 7]
from data with shape [5, 21]
. If the shapes aren't compatible in this way, you'll have to build a new graph.
这篇关于当变量形状改变时从检查点恢复的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!