如何使用 TensorFlow 在 Returnn 中加载经过训练的网络的权重

How to load the weights of a trained network in Returnn with TensorFlow

当尝试将保存的权重加载到训练有素的多个时期时 使用以下代码返回网络:

import tensorflow as tf
from returnn.Config import Config
from returnn.TFNetwork import TFNetwork

for i in range(1,11):
    modelFilePath = path/to/model/ + 'network.' + '%03d' % (i,)

    returnnConfig = Config()
    returnnConfig.load_file(path/to/configFile)
    returnnTfNetwork = TFNetwork(config=path/to/configFile, train_flag=False, eval_flag=True)

    returnnTfNetwork.construct_from_dict(returnnConfig.typed_value('network'))

    with tf.Session() as sess:
        returnnTfNetwork.load_params_from_file(modelFilePath, sess)

我收到以下错误:

Variables to restore which are not in checkpoint:
global_step_1

Variables in checkpoint which are not needed for restore:
global_step

Probably we can restore these:
(None)

Error, some entry is missing in the checkpoint

问题是你在循环中每次都重新创建TFNetwork,并且每次都会为全局步骤创建一个新变量,必须调用不同的变量,因为每个变量都必须有一个唯一的名字.

你可以在循环中做这样的事情:

tf.reset_default_graph()