-
tensorflow
中加载模型时和图中已定义的张量名重复时,会将以前保存的模型的tensor_name
也就是张量名后面加上_1
。实验如下图:-
以前保存的模型,模型中有a、b、c三个张量和一个val变量。
- a的
tensor
为:Tensor("Const:0", shape=(), dtype=int32)
,其中tensor_name
为Const:0
- b的
tensor
为:Tensor("Const_1:0", shape=(), dtype=int32)
,其中tensor_name
为Const_1:0
- c的
tensor
为:Tensor("Add:0", shape=(), dtype=int32)
,其中tensor_name
为Add:0
a = tf.constant(102) b = tf.constant(2) c = tf.add(a, b) val = tf.Variable(1.3) init_op = tf.global_variables_initializer() with tf.Session() as sess: sess.run(init_op) saver = tf.train.Saver() saver.save(sess, "./model2/test")
- a的
-
再次加载模型时:图中已经定义了a、b、c三个张量,以前图中也有a、b、c三个张量,看最终的效果:
a = tf.constant(11) b = tf.constant(4) c = tf.add(a, b) val = tf.constant(5) with tf.Session() as sess: print(a) print(b) print(val) print(sess.run([a, b, val])) saver = tf.train.import_meta_graph("./model2/test.meta") saver.restore(sess, "./model2/test") # 加载以前模型当中的a b c三个张量,其中c的张量名字已经由Add:0变成了Add_1:0 graph = tf.get_default_graph() c = graph.get_tensor_by_name("Add_1:0") print(a) print(b) print(c) print(sess.run([a, b, c]))
-
tensorflow中加载模型时和图中已定义的张量名重复怎么办?
最新推荐文章于 2024-04-26 23:48:56 发布