-
Notifications
You must be signed in to change notification settings - Fork 0
/
mnist.py
101 lines (76 loc) · 2.44 KB
/
mnist.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
"""
MNIST DNN classifier
"""
import json
import jax.numpy as np
import wandb
from sklearn.datasets import fetch_openml
from sklearn.model_selection import train_test_split
from sklearn.preprocessing import LabelBinarizer, StandardScaler
from sklearn.utils import check_random_state
from tqdm.autonotebook import tqdm
from lorax.metrics import accuracy
from lorax.train import Experiment, wandb_notes
config = {
"experiment_name": "mnist",
"model_config": {
"kind": "MLP",
"output_dim": 10,
"input_dim": 784,
"hidden_sizes": [1024, 1024],
"activation": "relu",
"dropout_keep": None,
},
"random_seed": 42,
"loss": "cross_entropy",
"regularization": None,
"optimizer": "adam",
"learning_rate": 0.01,
"batch_size": 16,
"global_step": 2500,
"log_every": 100,
}
wandb.init(project="colin_net_mnist", config=config, save_code=True)
config = wandb.config
experiment = Experiment.from_flattened(config)
# Create Input Data and True Labels
# Load data from https://www.openml.org/d/554
X, y = fetch_openml("mnist_784", version=1, return_X_y=True)
random_state = check_random_state(experiment.random_seed)
permutation = random_state.permutation(X.shape[0])
X = X[permutation]
y = y[permutation]
X = X.reshape((X.shape[0], -1))
X_train, X_test, y_train, y_test = train_test_split(
X,
y,
)
scaler = StandardScaler()
X_train = scaler.fit_transform(X_train)
X_test = scaler.transform(X_test)
encoder = LabelBinarizer()
y_train = encoder.fit_transform(y_train)
y_test = encoder.transform(y_test)
print(json.dumps(experiment.dict(), indent=4))
update_generator = experiment.train(
X_train, y_train, X_test, y_test, iterator_type="batch_iterator"
)
bar = tqdm(total=experiment.global_step)
for update_state in update_generator:
if update_state.step == 1:
markdown = f"{update_state.model.json()}"
wandb_notes(markdown)
if update_state.step % experiment.log_every == 0:
bar.set_description(f"loss:{update_state.loss:.5f}")
bar.update()
final_model = update_state.model
# Display Predictions
final_model = final_model.to_eval()
test_iterator = experiment.create_iterator("batch_iterator", X_test, y_test)
prob_list = []
for batch in test_iterator:
probs = final_model.predict_proba(batch.inputs)
prob_list.append(probs)
probabilties = np.hstack(prob_list)
accuracy_score = float(accuracy(y_test, probabilties))
print("Accuracy: ", accuracy_score)