forked from xand-stapleton/ainstein
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrun.py
More file actions
executable file
·148 lines (125 loc) · 4.92 KB
/
run.py
File metadata and controls
executable file
·148 lines (125 loc) · 4.92 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
import tensorflow as tf
tfk = tf.keras
tfk.backend.set_floatx("float64")
import numpy as np
import yaml
import wandb
from helper_functions import argument_parser, wandb_helper
from network.ball import BallNetwork
from sampling.ball import BallSample, CubeSample
# Main body function for performing the metric training
def main(hyperparameters_file, runtime_args, wandb_id=None):
###########################################################################
### Training & Logging set-up ###
# Load the hyperparameters YAML file
with open(hyperparameters_file, "r") as file:
args = yaml.safe_load(file)
for arg, arg_val in args.items():
if arg in runtime_args:
args[arg] = runtime_args[arg]
# Check if restoring WandB
if wandb_id is not None:
args, x_train = wandb_helper.restore_wandb(args, wandb_id)
else:
# Initialise the training and val batches
x_train, x_val = None, None
# Check and set seeds for reproducibility
rng = np.random.default_rng()
# ...for NumPy
if args["np_seed"] is None:
args["np_seed"] = int(rng.integers(2**32 - 2))
# ...for TensorFlow
if args["tf_seed"] is None:
args["tf_seed"] = int(rng.integers(2**32 - 2))
# Make sure the config things are ints
args["np_seed"] = int(args["np_seed"])
args["tf_seed"] = int(args["tf_seed"])
np.random.seed(args["np_seed"])
tf.random.set_seed(args["tf_seed"])
tfk.utils.set_random_seed(args["tf_seed"])
# Print some random characters to check seed applied correctly
print("TF random key: ", tf.random.uniform(shape=[6]))
print("NP random key: ", np.random.randint(1, np.iinfo(np.int32).max, size=6))
# Start a WeightsandBiases session, and allow resuming from checkpoint
wandb.init(
project="Ainstein_ball",
entity="logml",
config=args,
id=wandb_id,
resume="allow",
)
# Allow WandB to control the hyperparameters for sweeps (amounts to
# over-writing the hyperparameters file with new values).
hp = wandb.config
# Add run identifiers for saving tracability
run_name = wandb.run.name or ""
run_id = wandb.run.id or 42
hp["run_identifiers"] = (run_name, run_id)
###########################################################################
### Data set-up ###
# Create training and validation samples
if wandb_id is None:
# Ball patch sampling
if hp["ball"]:
train_sample = BallSample(
hp.num_samples,
dimension=hp.dim,
patch_width=hp["patch_width"],
density_power=hp["density_power"],
)
if hp["validate"]:
val_sample = BallSample(
hp.num_val_samples,
dimension=hp.dim,
patch_width=hp["patch_width"],
density_power=hp["density_power"],
)
# Cube patch sampling (full functionality unlikely entirely compatible at present)
else:
assert hp["n_patches"] == 1, (
"Cube sampling only suitable for local geometries where don't need the ball structure for patching (set n_patches = 1)"
)
train_sample = CubeSample(
hp.num_samples,
dimension=hp.dim,
width=hp["patch_width"],
density_power=hp["density_power"],
)
if hp["validate"]:
val_sample = CubeSample(
hp.num_val_samples,
dimension=hp.dim,
width=hp["patch_width"],
density_power=hp["density_power"],
)
# If wandb_id is not None
else:
train_sample = x_train
if hp["validate"]:
val_sample = x_val
# Convert to tf objects
train_sample_tf = tf.convert_to_tensor(train_sample, dtype=tf.dtypes.float64)
val_sample_tf = None
if hp["validate"]:
val_sample_tf = tf.convert_to_tensor(val_sample, dtype=tf.dtypes.float64)
###########################################################################
### Run ML ###
# Instantiate the network
network = BallNetwork(hp=hp, print_losses=hp.print_losses)
# Train!
loss_hist = network.train(
x_train=train_sample_tf, validate=hp["validate"], x_val=val_sample_tf
)
# Close the WandB session
wandb.finish()
return loss_hist, train_sample_tf, val_sample_tf
###############################################################################
if __name__ == "__main__":
# Extract the runtime args
args = argument_parser.get_args()
# Extract any specified WandB id passed to the run
wandb_id = args.wandb_id
# Create a dict of the training arguments
runtime_args = argument_parser.prune_none_args(args)
# Perform the training
lh, train_data, val_data = main(args.hyperparams, runtime_args, wandb_id)