8000 minigo/oneoffs/swa.py at eval_server · gms2009/minigo · GitHub
[go: up one dir, main page]

Skip to content
{"props":{"color_mode":"dark","logged_in":false,"marketing_page":false,"home_path":"/","login_path":"/login?return_to=https%3A%2F%2FGitHub.com%2Fgms2009%2Fminigo%2Fblob%2Feval_server%2Foneoffs%2Fswa.py","signup_path":"/signup?ref_cta=Sign+up\u0026ref_loc=header+logged+out\u0026ref_page=%2F%3Cuser-name%3E%2F%3Crepo-name%3E%2Fblob%2Fshow\u0026source=header-repo\u0026source_repo=gms2009%2Fminigo","signup_enabled":true,"is_signup_controller":false,"show_search_and_nav":true,"hide_search":false,"private_mode_enabled":false,"should_use_dotcom_links":true,"auth_hydro_click":"{\"event_type\":\"authentication.click\",\"payload\":{\"location_in_page\":\"site header menu\",\"repository_id\":null,\"auth_type\":\"SIGN_UP\",\"originating_url\":\"https://GitHub.com/gms2009/minigo/blob/eval_server/oneoffs/swa.py\",\"user_id\":null}}","auth_hydro_click_hmac":"2890bf72091ce09b6813f0c78f40a96e8e582104c0de68e400b4c69cd2cc5a03"}}

Latest commit

 

History

History
103 lines (82 loc) · 3.07 KB

swa.py

File metadata and controls

103 lines (82 loc) · 3.07 KB
# Copyright 2018 Google LLC
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# Example usage:
# export BOARD_SIZE=19
# python3 oneoffs/swa.py --bucket_name="minigo-pub/v7-19x19" --data_dir="data/swa"
# python main.py freeze-graph data/swa/swa-2
import sys
sys.path.insert(0, '.')
import os
import tensorflow as tf
from absl import flags
from tensorflow.python.framework import meta_graph
import dual_net
from rl_loop import fsdb
from utils import ensure_dir_exists
flags.DEFINE_string("data_dir", "data/swa/", "Where to save swa models")
flags.DEFINE_integer("count", 2, "number of top models to aggregate")
FLAGS = flags.FLAGS
def swa():
path_base = fsdb.models_dir()
model_names = [
"000393-lincoln",
"000390-indus",
"000404-hannibal",
"000447-hawke",
"000426-grief",
"000431-lion",
"000428-invincible",
"000303-olympus",
"000291-superb",
"000454-victorious",
]
model_names = model_names[:FLAGS.count]
model_paths = [os.path.join(path_base, m) for m in model_names]
# construct the graph
features, labels = dual_net.get_inference_input()
dual_net.model_fn(features, labels, tf.estimator.ModeKeys.PREDICT, FLAGS.flag_values_dict())
# restore all saved weights
meta_graph_def = meta_graph.read_meta_graph_file(model_paths[0] + '.meta')
stored_var_names = set(
[n.name for n in meta_graph_def.graph_def.node if n.op == 'VariableV2'])
var_list = [v for v in tf.global_variables()
if v.op.name in stored_var_names]
var_list.sort(key=lambda v: v.op.name)
print(stored_var_names)
print(len(stored_var_names), len(var_list))
sessions = [tf.Session() for _ in model_paths]
saver = tf.train.Saver()
for sess, model_path in zip(sessions, model_paths):
saver.restore(sess, model_path)
# Load all VariableV2s for each model.
values = [sess.run(var_list) for sess in sessions]
# Iterate over all variables average values from all models.
all_assign = []
for var, vals in zip(var_list, zip(*values)):
print("{}x {}".format(len(vals), var))
if var.name == "global_step:0":
avg = vals[0]
for val in vals:
avg = tf.maximum(avg, val)
else:
avg = tf.add_n(vals) / len(vals)
continue
all_assign.append(tf.assign(var, avg))
# Run all asign ops on an existing model (which has other ops and graph).
sess = sessions[0]
sess.run(all_assign)
# Export a new saved model.
ensure_dir_exists(FLAGS.data_dir)
dest_path = os.path.join(FLAGS.data_dir, "swa-" + str(FLAGS.count))
saver.save(sess, dest_path)
if __name__ == '__main__':
remaining_argv = flags.FLAGS(sys.argv, known_only=True)
swa()
0