def build_init(self):
"""Builds the initialization sub-graph.
The default implementation creates an initialization op that initializes all variables,
locals for initialization, and another for all non-traininable variables and tables for local
initialization.
Initialization is run when the graph is first created, before training. Local initialization is
performed after a previously trained model is loaded.
Returns:
A tuple containing the init op and local init op to use to initialize the graph.
"""
init_op = tf.variables_initializer(tf.global_variables(), name='init')
# For some reason not all local variables are in the local variables collection, but some are in
# the global variables collection (such as those setup by reader ops).
# So in addition to initializing local variables in the local_init_op, we also initialize the
# set of variables in the global variables, that are not trainable.
# Just to add to the mix, tables are neither, and so must be explicitly included as well.
# All of these will be initialized after restoring from a checkpoint.
variables = tf.global_variables()
for trainable in tf.trainable_variables():
variables.remove(trainable)
local_init_op = tf.group(tf.variables_initializer(variables),
tf.variables_initializer(tf.local_variables()),
tf.tables_initializer(),
name='local_init_op')
# Add the local initialization op to the main op collection, which is looked up at model loading
# time, and is automatically invoked after it has been loaded.
tf.add_to_collection('saved_model_main_op', local_init_op)
return init_op, local_init_op
评论列表
文章目录