keras custom training loop

Solutions on MaxInterview for keras custom training loop by the best coders in the world

showing results for - "keras custom training loop"
Lia
08 Feb 2016
1from tensorflow import keras
2from tensorflow.keras import layers
3
4model = keras.Sequential()
5model.add(layers.Dense(64, kernel_initializer='uniform', input_shape=(10,)))
6model.add(layers.Activation('softmax'))
7
8opt = keras.optimizers.Adam(learning_rate=0.01)
9model.compile(loss='categorical_crossentropy', optimizer=opt)
10
11# Instantiate an optimizer.
12optimizer = tf.keras.optimizers.Adam()
13
14# Iterate over the batches of a dataset.
15for x, y in dataset:
16    # Open a GradientTape.
17    with tf.GradientTape() as tape:
18        # Forward pass.
19        logits = model(x)
20        # Loss value for this batch.
21        loss_value = loss_fn(y, logits)
22
23    # Get gradients of loss wrt the weights.
24    gradients = tape.gradient(loss_value, model.trainable_weights)
25
26    # Update the weights of the model.
27    optimizer.apply_gradients(zip(gradients, model.trainable_weights))
28