Introduction
Training helps the model learn by changing its weights to make better guesses. This makes the model improve over time.
Jump into concepts and practice - no test required
Training helps the model learn by changing its weights to make better guesses. This makes the model improve over time.
model.compile(optimizer='adam', loss='sparse_categorical_crossentropy', metrics=['accuracy']) model.fit(training_data, training_labels, epochs=5)
compile() sets how the model learns and measures errors.
fit() runs the training to update weights using data.
model.compile(optimizer='sgd', loss='mean_squared_error') model.fit(x_train, y_train, epochs=10)
model.compile(optimizer='adam', loss='sparse_categorical_crossentropy', metrics=['accuracy']) model.fit(x_train, y_train, epochs=3)
This program creates a small model, trains it on simple data, and shows how weights change after training.
import tensorflow as tf from tensorflow.keras import layers, models # Create simple model model = models.Sequential([ layers.Dense(5, activation='relu', input_shape=(3,)), layers.Dense(2, activation='softmax') ]) # Compile model with optimizer and loss model.compile(optimizer='adam', loss='sparse_categorical_crossentropy', metrics=['accuracy']) # Sample training data (4 samples, 3 features each) x_train = tf.constant([[1.0, 2.0, 3.0], [4.0, 5.0, 6.0], [7.0, 8.0, 9.0], [10.0, 11.0, 12.0]]) # Labels for 2 classes y_train = tf.constant([0, 1, 0, 1]) # Train model for 5 epochs history = model.fit(x_train, y_train, epochs=5, verbose=2) # Show final weights of first layer weights = model.layers[0].get_weights()[0] print('First layer weights after training:') print(weights)
Training changes weights step by step to reduce errors.
Optimizer decides how weights update during training.
Loss measures how far predictions are from true answers.
Training updates model weights to improve predictions.
Weights start random and get better by learning from data.
Optimizers and loss guide how weights change during training.
apply_gradients directly updates weights using gradients.compile sets training config, fit runs training loop, and tf.Variable creates variables but does not update weights.import tensorflow as tf
model = tf.keras.Sequential([tf.keras.layers.Dense(1)])
optimizer = tf.keras.optimizers.SGD(learning_rate=0.1)
x = tf.constant([[1.0]])
y = tf.constant([[2.0]])
with tf.GradientTape() as tape:
prediction = model(x)
loss = tf.reduce_mean((y - prediction) ** 2)
grads = tape.gradient(loss, model.trainable_variables)
optimizer.apply_gradients(zip(grads, model.trainable_variables))
print(loss.numpy())import tensorflow as tf
model = tf.keras.Sequential([tf.keras.layers.Dense(1)])
optimizer = tf.keras.optimizers.Adam(learning_rate=0.01)
x = tf.constant([[1.0]])
y = tf.constant([[2.0]])
with tf.GradientTape() as tape:
prediction = model(x)
loss = tf.reduce_mean((y - prediction) ** 2)
grads = tape.gradient(loss, model.trainable_variables)
# Missing apply_gradients call here
print(model.trainable_variables[0].numpy())apply_gradients, so weights stay the same.