Introduction to Keras

Keras - Chapter 1

Keras Tutorial

We appreciate your patience while we actively develop and enhance our content for a better experience.

Sample Code
import numpy as np
from tensorflow.keras.datasets import mnist
from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import Flatten, Dense
from tensorflow.keras.optimizers import Adam
from tensorflow.keras.losses import SparseCategoricalCrossentropy
from tensorflow.keras.metrics import Accuracy

def main():
    print("This program demonstrates how to use Keras to create a neural network to classify handwritten digits from the MNIST dataset.")

    # Load the MNIST dataset
    (X_train, y_train), (X_test, y_test) = mnist.load_data()

    # Normalize the data
    X_train, X_test = X_train / 255.0, X_test / 255.0

    # Create a simple neural network model
    model = Sequential([
        Flatten(input_shape=(28, 28)),
        Dense(128, activation='relu'),
        Dense(10, activation='softmax')
    ])

    # Compile the model
    model.compile(optimizer=Adam(),
                  loss=SparseCategoricalCrossentropy(),
                  metrics=[Accuracy()])

    # Train the model
    model.fit(X_train, y_train, epochs=5)

    # Evaluate the model on the test dataset
    _, accuracy = model.evaluate(X_test, y_test)
    print(f"Accuracy of the neural network on the test set: {accuracy * 100:.2f}%")

if __name__ == "__main__":
    main()