Challenge - 5 Problems
Weights Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
What is the output of this TensorFlow code snippet?
Consider the following code that creates a simple model, saves only its weights, then reloads them. What will be printed?
TensorFlow
import tensorflow as tf import numpy as np model = tf.keras.Sequential([ tf.keras.layers.Dense(2, input_shape=(3,), activation='relu') ]) model.compile(optimizer='adam', loss='mse') # Train on dummy data x = np.array([[1, 2, 3]]) y = np.array([[1, 0]]) model.fit(x, y, epochs=1, verbose=0) # Save weights model.save_weights('weights_only.ckpt') # Create new model instance new_model = tf.keras.Sequential([ tf.keras.layers.Dense(2, input_shape=(3,), activation='relu') ]) # Load weights new_model.load_weights('weights_only.ckpt') # Predict with new model pred = new_model.predict(x) print(pred)
Attempts:
2 left
💡 Hint
Weights saved and loaded correctly should produce similar predictions.
✗ Incorrect
The weights are saved and loaded correctly, so the new model produces similar non-zero predictions after loading weights.
❓ Model Choice
intermediate1:30remaining
Which model architecture is compatible for loading saved weights?
You saved weights from a model with layers: Dense(4, input_shape=(5,)), Dense(2). Which new model can load these weights without error?
Attempts:
2 left
💡 Hint
The architecture must match exactly to load weights.
✗ Incorrect
Weights can only be loaded if the new model has the exact same layer sizes and input shapes as the original model.
❓ Hyperparameter
advanced1:30remaining
What happens if you save weights before training?
You create a model and immediately save its weights before any training. What will the saved weights represent?
Attempts:
2 left
💡 Hint
Model weights start with initial random values before training.
✗ Incorrect
Before training, model weights are initialized randomly (or as per the initializer), so saving weights at this point saves these initial values.
🔧 Debug
advanced2:00remaining
Why does loading weights fail with this error?
You saved weights from a model with layers: Dense(3, input_shape=(2,)), Dense(1). Now you try to load weights into a model with layers: Dense(3, input_shape=(3,)), Dense(1). The error is: ValueError: Layer weight shape mismatch. What caused this?
Attempts:
2 left
💡 Hint
Weights depend on layer input and output sizes.
✗ Incorrect
Weights shape depends on input and output dimensions. Changing input shape changes weight shapes, causing mismatch errors.
🧠 Conceptual
expert2:30remaining
Why save only weights instead of the full model?
Which is the main advantage of saving only the weights of a TensorFlow model instead of the entire model?
Attempts:
2 left
💡 Hint
Weights alone do not store model architecture or optimizer info.
✗ Incorrect
Saving only weights produces smaller files and faster save/load, but requires the same model architecture to reload weights correctly.