enzymeretrieval / dev /install_bert.py
da24b002athreyaganesh's picture
v5: v4 + fine-tuned rxnfp reaction BERT
177457f verified
Raw History Blame Contribute Delete
2.3 kB
"""Install a fine-tuned reaction BERT into weights/ and switch meta.json to the v5 blend.
Run after dev/train_bert.py has finished on the full training data:
python dev/install_bert.py dev/runs/b_full/bert.npz
Refuses, and changes nothing, unless bert.npz exists and its EC vocabulary is
the one weights/model.npz was trained on, so a half-finished or matched-split
run cannot end up in the image.
"""
import argparse
import json
import shutil
import sys
from pathlib import Path
import numpy as np
ROOT = Path(__file__).resolve().parents[1]
# Tuned on the matched split with v4's networks: 52.99% against v4's 50.91%.
V5 = dict(alpha=0.9, alpha_far=0.9, gate=0.7, beta=1.0, reverse_tta=False, bert_weight=0.4, bert_deadline=420.0)
def main():
parser = argparse.ArgumentParser(description=__doc__, formatter_class=argparse.RawDescriptionHelpFormatter)
parser.add_argument('bert', help='bert.npz written by dev/train_bert.py')
parser.add_argument('--weights', default=str(ROOT / 'weights'))
args = parser.parse_args()
source, weights = Path(args.bert), Path(args.weights)
train = (f' python dev/train_bert.py --train ../data/train.parquet --val none --out {source.parent}\n'
f'which takes about an hour and ends with "wrote {source}"')
if not source.exists():
sys.exit(f'error: {source} does not exist. Train it first:\n{train}')
meta = json.loads((weights / 'meta.json').read_text())
blob = np.load(source)
if 'ec_vocab' not in blob.files:
sys.exit(f'error: {source} has no EC vocabulary, so it predates the full-data run. Retrain it:\n{train}')
ec_vocab = bytes(blob['ec_vocab']).decode().split('\n')
if ec_vocab != meta['vocab']:
sys.exit(f'error: {source} predicts {len(ec_vocab)} ECs but {weights}/model.npz has {len(meta["vocab"])}; '
'train it on ../data/train.parquet, the data model.npz was trained on')
shutil.copyfile(source, weights / 'bert.npz')
meta.update(V5)
(weights / 'meta.json').write_text(json.dumps(meta, indent=1))
print(f'copied {source} to {weights}/bert.npz ({source.stat().st_size / 1e6:.1f} MB)')
print(f'set in {weights}/meta.json: ' + ', '.join(f'{key} {value}' for key, value in V5.items()))
if __name__ == '__main__':
main()