Introduction
Prediction helps us see what the model thinks about new data. Evaluation tells us how good the model is at making those guesses.
Jump into concepts and practice - no test required
predictions = model.predict(new_data) evaluation = model.evaluate(test_data, test_labels)
predictions = model.predict(X_test)
loss, accuracy = model.evaluate(X_test, y_test)
predictions = model.predict(new_samples[:5])import tensorflow as tf from tensorflow.keras import layers, models import numpy as np # Create simple data: inputs and labels X_train = np.array([[0], [1], [2], [3], [4], [5]], dtype=float) y_train = np.array([0, 1, 2, 3, 4, 5], dtype=float) X_test = np.array([[6], [7], [8]], dtype=float) y_test = np.array([6, 7, 8], dtype=float) # Build a simple model model = models.Sequential([ layers.Dense(1, input_shape=(1,)) ]) # Compile model with optimizer and loss model.compile(optimizer='sgd', loss='mean_squared_error', metrics=['mean_absolute_error']) # Train model model.fit(X_train, y_train, epochs=100, verbose=0) # Predict on test data predictions = model.predict(X_test) # Evaluate model on test data loss, mae = model.evaluate(X_test, y_test, verbose=0) # Print results print('Predictions:', predictions.flatten()) print(f'Test Loss: {loss:.4f}') print(f'Test MAE: {mae:.4f}')
model.predict() function do in TensorFlow?model.predict()model.fit(), saving uses model.save(), and deleting is manual memory management, none of which are predict().X_test and y_test?model.evaluate() to measure performance on test data.model.predict() makes predictions, model.fit() trains, and model.score() is not a TensorFlow method.import tensorflow as tf import numpy as np model = tf.keras.Sequential([ tf.keras.layers.Dense(1, input_shape=(1,)) ]) model.compile(optimizer='sgd', loss='mse') X = np.array([1, 2, 3, 4], dtype=float) y = np.array([2, 4, 6, 8], dtype=float) model.fit(X, y, epochs=10, verbose=0) predictions = model.predict(np.array([5.0])) print(predictions)
model.evaluate(X_test, y_test) but get a ValueError about mismatched shapes. What is the most likely cause?X_test1, y_test1 and X_test2, y_test2. Which approach correctly compares their accuracy using TensorFlow?model.evaluate() returns loss and metrics on test data without training, ideal for comparing performance.