def demonstrate_loading_two_instances_of_model1():
print("="*60 + " Demonstrate loading weights from model1 into two instances of model1 in scopeA and scopeB")
tf.reset_default_graph()
with tf.variable_scope("scopeA") as scope:
m1a = Model1()
print ("-" * 40 + " Trying to load model1 weights: should fail")
try:
m1a.model.load("model1.tfl", weights_only=True)
except Exception as err:
print ("Loading failed, with error as expected, because variables are in scopeA")
print ("error: %s" % str(err))
print ("-" * 40)
print ("=" * 60 + " Trying to load model1 weights: should succeed")
m1a.model.load("model1.tfl", scope_for_restore="scopeA", verbose=True, weights_only=True)
with tf.variable_scope("scopeB") as scope:
m1b = Model1()
m1b.model.load("model1.tfl", scope_for_restore="scopeB", verbose=True, weights_only=True)
print ("="*60 + " Successfully restored weights to two instances of model1, in different scopes")
评论列表
文章目录