AI models selected and shared by research scholars,
now available to you completely free of charge.
+35
AI Models Hitting Globally
22M
Open-Source Dataset with base
## Import necessary libraries
import tensorflow as tf
from tensorflow.keras import
layers, models
from tensorflow.keras.datasets import mnist
from tensorflow.keras.utils import to_categorical
# Load and preprocess data
def load_and_preprocess_data():
"""Load and preprocess the MNIST dataset."""
(x_train, y_train), (x_test, y_test) =
mnist.load_data()
# Normalize the images to [0, 1] range
x_train = x_train.astype('float32') /
255.0
x_test = x_test.astype('float32') / 255.0
# Reshape data to include channel dimension
x_train = x_train.reshape(-1, 28, 28, 1)
x_test = x_test.reshape(-1, 28, 28, 1)
# One-hot encode the labels
y_train = to_categorical(y_train, 10)
y_test = to_categorical(y_test, 10)
return (x_train, y_train), (x_test, y_test)