我正在尝试将使用Tensorflow训练的模型的权重加载到新的Keras模型中。
当我尝试评估变量以获取权重时,我得到以下信息:
with sess.as_default():
# import graph
saver = tf.train.import_meta_graph(meta_path)
# load weights for graph
saver.restore(sess, meta_path[:-5])
# get all global variables (including model variables)
vars_global = tf.global_variables()
# get their name and value and put them into dictionary
model_vars = {}
for var in vars_global:
try:
model_vars[var.name] = var.eval()
except Exception as e:
print("For var={}, an exception occurred".format(var.name))
print(e)
for key in sorted(model_vars.keys()):
print(key, model_vars[key].shape)
global_step:0 ()
model/conv_1/biases:0 (128,)
model/conv_1/weights:0 (8, 500, 1, 128)
model/conv_2/biases:0 (128,)
model/conv_2/weights:0 (8, 500, 1, 128)
model/embed/embedding_vars:0 (257, 8)
model/fc_1/biases:0 (128,)
model/fc_1/weights:0 (128, 128)
model/fc_2/biases:0 (1,)
model/fc_2/weights:0 (128, 1)
model/model/conv_1/biases/Momentum:0 (128,)
model/model/conv_1/weights/Momentum:0 (8, 500, 1, 128)
model/model/conv_2/biases/Momentum:0 (128,)
model/model/conv_2/weights/Momentum:0 (8, 500, 1, 128)
model/model/embed/embedding_vars/Momentum:0 (257, 8)
model/model/fc_1/biases/Momentum:0 (128,)
model/model/fc_1/weights/Momentum:0 (128, 128)
model/model/fc_2/biases/Momentum:0 (1,)
model/model/fc_2/weights/Momentum:0 (128, 1)
我应该在新模型的各层中使用哪两组不同的权重?还是应该以某种方式将它们结合起来?
我尝试过分别加载两者,但没有得到任何有意义的结果。