Forked from qubvel/checkpoints_weights_averaging.py
Created
November 19, 2019 10:38
-
-
Save max-kuk/463163cee2530e87a9109781feacc581 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import torch | |
from collections import OrderedDict | |
from typing import List | |
checkpoints_weights_paths: List[str] = ... # sorted in descending order by score | |
model: torch.nn.Module = ... | |
def average_weights(state_dicts: List[dict]): | |
everage_dict = OrderedDict() | |
for k in state_dicts[0].keys(): | |
everage_dict[k] = sum([state_dict[k] for state_dict in state_dicts]) / len(state_dicts) | |
return everage_dict | |
all_weights = [torch.load(path) for path in checkpoints_weights_paths] | |
best_score = 0 | |
best_weights = [] | |
for w in all_weights: | |
current_weights = best_weights + [w] | |
average_dict = average_weights(current_weights) | |
model.load_state_dict(average_dict) | |
score = evaluate_model(model, ...) | |
if score > best_score: | |
best_score = score | |
best_weights.append(w) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment