-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathnuts_cmn_eval_uci.py
162 lines (134 loc) · 5.42 KB
/
nuts_cmn_eval_uci.py
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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
# Copyright 2024 VERSES AI, Inc.
#
# Licensed under the VERSES Academic Research License (the “License”);
# you may not use this file except in compliance with the license.
#
# You may obtain a copy of the License at
#
# https://github.com/VersesTech/cavi-cmn/blob/main/LICENSE.txt
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
import jax
from jax import random as jr
from benchmarks import create_uci_dataloader, fit_cmn_nuts
import os
import argparse
def parse_args():
parser = argparse.ArgumentParser("Two Layer Conditional Mixture Network-NUTS")
parser.add_argument("--seed", default=1234, type=int)
## data config
parser.add_argument(
"--data",
default="rice",
type=str,
choices=[
"rice",
"waveform",
"breast_cancer",
"statlog",
"banknote",
"hcv",
"connectionist_bench",
"iris",
],
)
parser.add_argument("--train_size", default=500, type=int)
parser.add_argument("--test_size", default=100, type=int)
parser.add_argument("--max_train_size", default=500, type=int)
# dimension of discrete latents in the Conditional Mixture layer
parser.add_argument("--n_components", default=20, type=int)
# logging config
parser.add_argument(
"--log_metrics",
"-logme",
action="store_true",
help="Include this flag if you want to additionally log the metrics of the model training",
)
# MCMC kernel config
parser.add_argument("--n_warmup", "-n_w", default=800, type=int)
parser.add_argument("--n_nuts_samples", "-nns", default=64, type=int)
# n_chains is treated like n_models in other scripts -- metrics are averaged over parallel chains
parser.add_argument("--n_chains", "-n_c", default=16, type=int)
# type of prior: either gamma or wishart
parser.add_argument(
"--prior_type", "-pt", default="gamma", type=str, choices=["gamma", "wishart"]
)
# beta scale
parser.add_argument("--scale_beta", "-sb", default=5.0, type=float)
# floating-point precision config
parser.add_argument(
"--precision", default="float32", choices=["float32", "float64"], type=str
)
args = parser.parse_args()
args.batch_size = args.train_size
return args
if __name__ == "__main__":
args = parse_args()
# Update precision based on the command line argument
if args.precision == "float64":
jax.config.update("jax_enable_x64", True)
elif args.precision == "float32":
jax.config.update("jax_default_matmul_precision", "float32")
key = jr.PRNGKey(args.seed)
data_key, model_key = jr.split(key)
(train_dataloader, test_dataloader), stats = create_uci_dataloader(
data_key,
args.data,
args.train_size,
args.test_size,
max_train_size=args.max_train_size,
not_split=False,
)
x_dim = stats["x_dim"] # number of input features (regressor dimension)
# number of classes in the output (regressand dimension)
y_dim = n_classes = stats["n_classes"]
num_layers = 1
hidden_dim = n_classes - 1
print(f"Dataset={args.data}: train_size={args.train_size}, n_classes={n_classes}")
print(
f"Two Layer NUTS-CMN: components={args.n_components}, hidden_dim={hidden_dim}, n_warmup={args.n_warmup}, n_samples={args.n_nuts_samples}, n_chains={args.n_chains}, floating point dtype: {int(args.precision[-2:])}"
)
# number of hidden units (continuous latents) in the single hidden (Conditional Mixture) layer of the model
hidden_dims = [hidden_dim] * num_layers
# number of components (discrete latents) in the single hidden (Conditional Mixture) layer of the model
n_components = [args.n_components] * num_layers
if not os.path.exists("./logging/"):
os.makedirs("./logging/")
exp_name = f"{args.data}-nuts-cmn-layers={num_layers}-n_components={args.n_components}-hidden_dims={hidden_dim}-train_size={args.train_size}-n_classes={n_classes}"
x_train, y_train = next(iter(train_dataloader))
x_test, y_test = next(iter(test_dataloader))
train_acc, test_acc, lpd, ece, _ = fit_cmn_nuts(
model_key,
x_train=x_train,
y_train=y_train,
x_test=x_test,
y_test=y_test,
num_classes=n_classes,
layer_dims=hidden_dims,
num_components=n_components,
prior_type=args.prior_type,
scale_beta=args.scale_beta,
num_warmup=args.n_warmup,
num_nuts_samples=args.n_nuts_samples,
num_chains=args.n_chains,
prob_type="stick-breaking",
grid=None,
)
print(
f"Average train / test accuracy: {train_acc.mean():.3f} / {test_acc.mean():.3f}, LPD: {lpd.mean():.3f}, ECE: {ece.mean():.3f}"
)
if args.log_metrics:
fout = open(
"./logging/" + exp_name + f"-metrics" + ".txt",
mode="a+",
)
for model_i in range(args.n_chains):
print(
f"model={model_i+1}, train_accuracy={train_acc[model_i]:.3f}, test_accuracy={test_acc[model_i]:.3f}, lpd={lpd[model_i]:.3f}, ece={ece[model_i]:.3f}",
file=fout,
)
fout.close()