我正在尝试使用tensorflow修改现有代码的图形(添加没有可训练变量的ops)并加载预训练模型进行微调。虽然在修改后简单地加载模型没有给出任何错误,但我很困惑为什么tensorflow仍然可以将变量与数据匹配。如果我添加一些带有可训练变量的操作或修改原始变量的名称会怎么样?
我正在处理的代码太长,所以我把伪代码放在这里。
最初
# build graph
w = tf.Variable(..., name='weight')
b = tf.Variable(..., name='bias')
x = tf.placeholder(..., name='input')
y = W * x + b
label = tf.placeholder(..., name='label')
loss = (y-label) * (y-label)
# normal training and saving
...
我修改了程序并加载了模型
# build graph
w = tf.Variable(..., name='weight')
b = tf.Variable(..., name='bias')
x = tf.placeholder(..., name='input')
y = W * x + b
label = tf.placeholder(..., name='label')
label = label * 0.5 # add an op
loss = (y-label) * (y-label)
# load model
saver.restore(sess, 'path/to/model')
效果很好。如果我更改变量的名称或添加新变量
# build graph
w = tf.Variable(..., name='weight_2') # changed name
b = tf.Variable(..., name='bias')
scalar = tf.Variable(..., name='scalar') # add variable
x = tf.placeholder(..., name='input')
y = scalar * W * x + b
label = tf.placeholder(..., name='label')
label = label * 0.5 # add an op
loss = (y-label) * (y-label)
# load model
saver.restore(sess, 'path/to/model')
它还能用吗?