-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain_v1.py
47 lines (32 loc) · 1.23 KB
/
main_v1.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
import jax
import jax.numpy as jnp
import optax
from networks.resnet import ResNet18
def create_train_state(rng, learning_rate, input_shape):
global_client_net, server_net = ResNet18()
params = global_client_net.init(rng, jnp.zeros(input_shape))
tx = optax.adam(learning_rate)
return train_state.TrainState.create(
apply_fn=model.apply, params=params, tx=tx)
@jax.jit
def train_step(state, batch):
x, y = batch
loss_grad_fn = jax.value_and_grad(loss_fn, argnums=0)
loss, grads = loss_grad_fn(state.params, x, y, state.apply_fn)
state = state.apply_gradients(grads=grads)
return state, loss
def train_model(state, train_ds, num_epochs, batch_size):
for epoch in range(num_epochs):
rng = jax.random.PRNGKey(epoch)
train_ds = jax.random.permutation(rng, train_ds)
batches = jnp.array_split(train_ds, len(train_ds) // batch_size)
epoch_loss = 0.0
for batch in batches:
state, loss = train_step(state, batch)
epoch_loss += loss
avg_loss = epoch_loss / len(batches)
print(f"Epoch {epoch+1}, Loss: {avg_loss:.4f}")
return state
if __name__ == "__main__":
client_net, server_net = ResNet18()
print(client_net)