1. 程式人生 > >tensorflow 僅初始化指定的variables及未初始化的variables

tensorflow 僅初始化指定的variables及未初始化的variables

1. 已知variables v0,v1,v2,對特定的variables進行初始化可使用以下方法:

initialize_op = tf.variables_initializer([v0,v1,v2])
sess.run(initialize_op)

 2. restore graph 進行了fine_tuning, 但仍保留restored weights,僅對新的未被初始化的tensor進行初始化,可使用以下方式:

unitialized_vars = []
for var in tf.global_variables():
  try:
    sess.run(var)
  except tf.errors.FailedPreconditionError:
    uninitialized_vars.append(var)

initialize_op = tf.variables_initializer(uninitialized_vars)
sess.run(initialize_op)