Model Pipeline - Loading and inference
This pipeline shows how a saved TensorFlow model is loaded and used to make predictions on new data. It starts with input data, loads the trained model, and outputs predictions.
This pipeline shows how a saved TensorFlow model is loaded and used to make predictions on new data. It starts with input data, loads the trained model, and outputs predictions.
Loss
1.0 |****
0.8 |****
0.6 |****
0.4 |****
0.2 |****
0.0 +----
1 2 3 4 5 Epochs| Epoch | Loss ↓ | Accuracy ↑ | Observation |
|---|---|---|---|
| 1 | 0.85 | 0.60 | Model starts learning with moderate accuracy |
| 2 | 0.60 | 0.75 | Loss decreases and accuracy improves |
| 3 | 0.45 | 0.82 | Model continues to improve |
| 4 | 0.35 | 0.88 | Good convergence observed |
| 5 | 0.30 | 0.90 | Training stabilizes with high accuracy |
tf.keras.models.load_model() in TensorFlow?tf.keras.models.load_model() is used to load a model that was saved earlier, including its architecture, weights, and optimizer state.model on input data new_data?predict() is used.fit() trains the model, evaluate() tests performance, and compile() sets up the model.predictions?
import tensorflow as tf import numpy as np model = tf.keras.Sequential([ tf.keras.layers.Dense(3, input_shape=(4,)) ]) model.compile(optimizer='adam', loss='mse') input_data = np.array([[1, 2, 3, 4], [5, 6, 7, 8]]) predictions = model.predict(input_data)
model = tf.keras.models.load_model('my_model') but get this error when calling model.predict(new_data): ValueError: Error when checking input: expected dense_input to have shape (4,) but got array with shape (3,). What is the likely cause?sample = [1, 2, 3, 4, 5]. Which code snippet correctly prepares the input and gets the prediction?input_data = np.array(sample)
prediction = model.predict(input_data) gives shape (5,), not 2D; input_data = np.array(sample).reshape(5, 1)
prediction = model.predict(input_data) reshapes to (5,1) which is wrong; input_data = np.array(sample).reshape(1, 1, 5)
prediction = model.predict(input_data) reshapes to (1,1,5) which is 3D and incorrect.Pass the test and this pattern moves unknown