-
Notifications
You must be signed in to change notification settings - Fork 0
/
train_mhn.py
188 lines (150 loc) · 5.08 KB
/
train_mhn.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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
# Adapted from training notebook in https://github.com/bhoov/eqx-hamux
import os
import random
import equinox as eqx
import jax
import jax.numpy as jnp
import jax.random as jr
import jax.tree_util as jtu
import numpy as np
import optax
import torch
from tqdm.auto import tqdm
import hamux as hmx
from config import (
TRAIN_WIDTH,
TRAIN_HEIGHT,
NUM_EPOCHS,
DIGITS_TO_TEST,
DEFAULT_SEED,
NOISE_TYPE,
NUM_NOISE_VARIATIONS_TO_TRAIN,
NOISE_LEVEL_TO_TRAIN,
)
from mdl_mhn import ModernHN, plot_prediction_and_gold, get_golden_mhn
from utils import get_correct_and_incorrect_num_memories, get_train_data
# If GPU is available, use it
os.environ["CUDA_VISIBLE_DEVICES"] = "0"
os.environ["XLA_PYTHON_CLIENT_MEM_FRACTION"] = "0.5" # Defaults to 0.9 * TOTAL_MEM
# Train Functions
class DenseSynapseHid(eqx.Module):
W: jax.Array
def __init__(self, key, d1: int, d2: int):
super().__init__()
self.W = jax.random.normal(key, (d1, d2)) * 0.02 + 0.2
@property
def nW(self):
nc = jnp.sqrt(jnp.sum(self.W**2, axis=0, keepdims=True))
return self.W / nc
def __call__(self, g1):
"""Compute the energy of the synapse"""
x2 = g1 @ self.nW
beta = 1e1
return -1 / beta * jax.nn.logsumexp(beta * x2, axis=-1)
def lossf(ham, xs, key, nsteps=1, alpha=1.0):
"""Given a noisy initial image, descend the energy and try to reconstruct the original image at the end of the dynamics.
Works best with fewer steps due to the vanishing gradient problem"""
img = xs["input"]
xs["input"] = img + jr.normal(key, img.shape) * 0.3
gs = ham.activations(xs)
for i in range(nsteps):
# Construct noisy image to final prediction
evalue, egrad = ham.dEdg(gs, xs, return_energy=True)
xs = jtu.tree_map(lambda x, dEdg: x - alpha * dEdg, xs, egrad)
gs = ham.activations(xs)
# 1step prediction means gradient == image
img_final = gs["input"]
loss = ((img_final - img) ** 2).mean()
logs = {
"loss": loss,
}
return loss, logs
@eqx.filter_jit
def step(img, ham, opt_state, key, opt):
xs = ham.init_states(bs=img.shape[0])
xs["input"] = img
(loss, logs), grads = eqx.filter_value_and_grad(lossf, has_aux=True)(ham, xs, key)
updates, opt_state = opt.update(grads, opt_state, ham)
newparams = optax.apply_updates(eqx.filter(ham, eqx.is_array), updates)
ham = eqx.combine(newparams, ham)
return ham, opt_state, logs
def train(
seed: int,
train_jax: jax.Array,
num_memories_to_train: int,
n_epochs: int = NUM_EPOCHS,
train_width: int = TRAIN_WIDTH,
train_height: int = TRAIN_HEIGHT,
):
# Settings seeds
random.seed(seed)
np.random.seed(seed)
torch.manual_seed(seed)
key = jax.random.PRNGKey(seed)
neurons = {
"input": hmx.Neurons(hmx.lagr_spherical_norm, (train_width * train_height,)),
}
synapses = {
"s1": DenseSynapseHid(key, train_width * train_height, num_memories_to_train),
}
connections = [(["input"], "s1")]
ham = hmx.HAM(neurons, synapses, connections)
xs = ham.init_states()
gs = ham.activations(xs)
opt = optax.adam(4e-2)
pbar = tqdm(range(n_epochs), total=n_epochs)
img = train_jax[:]
batch_size = 1
ham = ham.vectorize()
opt_state = opt.init(eqx.filter(ham, eqx.is_array))
noise_rng = jr.PRNGKey(100)
batch_rng = jr.PRNGKey(10)
for e in pbar:
batch_key, batch_rng = jr.split(batch_rng)
idxs = jr.permutation(batch_key, jnp.arange(img.shape[0]))
i = 0
while i < img.shape[0]:
noise_key, noise_rng = jr.split(noise_rng)
batch = img[idxs[i : i + batch_size]]
ham, opt_state, logs = step(batch, ham, opt_state, noise_key, opt)
i = i + batch_size
pbar.set_description(
f'epoch = {e + 1:03d}/{n_epochs:03d}, loss = {logs["loss"].item():2.6f}'
)
return ham
def ham_to_mhn(ham) -> ModernHN:
# Convert ham weights to torch.Tensor
weights = torch.from_numpy(np.array(ham.synapses["s1"].nW.T))
return ModernHN(weights)
def main():
train_data = get_train_data(
NOISE_TYPE, DIGITS_TO_TEST, NUM_NOISE_VARIATIONS_TO_TRAIN, NOISE_LEVEL_TO_TRAIN
)
train_jax = jnp.array(train_data)
golden_mhn = get_golden_mhn(DIGITS_TO_TEST)
(
correct_num_memories,
incorrect_num_memories,
) = get_correct_and_incorrect_num_memories(
DIGITS_TO_TEST, NUM_NOISE_VARIATIONS_TO_TRAIN
)
correct_num_memories_best_mhn = train(DEFAULT_SEED, train_jax, correct_num_memories)
plot_prediction_and_gold(
ham_to_mhn(correct_num_memories_best_mhn),
train_data,
should_show=True,
should_save=False,
golden_mhn=golden_mhn,
)
incorrect_num_memories_best_mhn = train(
DEFAULT_SEED, train_jax, incorrect_num_memories
)
plot_prediction_and_gold(
ham_to_mhn(incorrect_num_memories_best_mhn),
train_data,
should_show=True,
should_save=False,
golden_mhn=golden_mhn,
)
if __name__ == "__main__":
main()