Topview Logo
  • Create viral videos with
    GPT-4o + Ads library
    Use GPT-4o to edit video empowered by Youtube & Tiktok & Facebook ads library. Turns your links or media assets into viral videos in one click.
    Try it free
    gpt video

    How to create your FIRST NEURAL NETWORK with TensorFlow!

    blog thumbnail

    How to Create Your FIRST NEURAL NETWORK with TensorFlow!

    Creating your first neural network with TensorFlow is an exciting journey. In this article, we'll guide you through the process step-by-step. By the end, you'll know how to import TensorFlow, load and prepare data, create and compile a model, and finally train and evaluate it. Let's dive in!

    Step 1: Import TensorFlow

    First, you need to import TensorFlow. This can be done simply by using the following code:

    import tensorflow as tf
    

    Step 2: Load the MNIST Dataset

    TensorFlow comes with several built-in datasets. Here, we'll use the MNIST dataset, which consists of handwritten digits. We need to load the data and split it into training and test sets:

    mnist = tf.keras.datasets.mnist
    (x_train, y_train), (x_test, y_test) = mnist.load_data()
    

    Step 3: Normalize the Data

    It is important to normalize the data to be between 0 and 1 for faster convergence:

    x_train, x_test = x_train / 255.0, x_test / 255.0
    

    Step 4: Build the Neural Network

    We'll use the Sequential API from TensorFlow to build our neural network. This allows us to stack layers in a straightforward manner:

    model = tf.keras.models.Sequential([
        tf.keras.layers.Flatten(input_shape=(28, 28)),  # Flatten input
        tf.keras.layers.Dense(128, activation='relu'), # Dense layer with ReLU activation
        tf.keras.layers.Dense(10, activation='softmax') # Output layer with 10 classes
    ])
    

    Step 5: Compile the Model

    Next, we compile the model by specifying the optimizer, loss function, and metrics:

    model.compile(optimizer='adam',
                  loss='sparse_categorical_crossentropy',
                  metrics=['accuracy'])
    

    Step 6: Train the Model

    We then train the model using the training data and specify the number of epochs:

    model.fit(x_train, y_train, epochs=5)
    

    Step 7: Evaluate the Model

    Finally, we evaluate the model to see how it performs on the test data:

    model.evaluate(x_test, y_test)
    

    Results

    After running the code, we obtained an accuracy of 97%. Not bad for our first neural network!

    Keyword

    • TensorFlow
    • Neural Network
    • MNIST Dataset
    • Normalization
    • Sequential API
    • Compile Model
    • Train Model
    • Evaluate Model

    FAQ

    Q: What is the purpose of normalizing the data? A: Normalizing the data to be between 0 and 1 helps in faster convergence during training.

    Q: What is the MNIST dataset? A: The MNIST dataset is a collection of 70,000 handwritten digits used for training image processing systems.

    Q: Why do we use the Sequential API? A: The Sequential API allows for an easy and straightforward way to build deep learning models by stacking layers.

    Q: What is the role of the Flatten layer in the neural network? A: The Flatten layer transforms the input data into a 1D array, making it suitable for the Dense layer.

    Q: Which optimizer and loss function are used in this example? A: We use the 'adam' optimizer and 'sparse_categorical_crossentropy' loss function.

    Q: How do we measure the performance of the model? A: The performance of the model is measured using the accuracy metric during the evaluation phase.

    One more thing

    In addition to the incredible tools mentioned above, for those looking to elevate their video creation process even further, Topview.ai stands out as a revolutionary online AI video editor.

    TopView.ai provides two powerful tools to help you make ads video in one click.

    Materials to Video: you can upload your raw footage or pictures, TopView.ai will edit video based on media you uploaded for you.

    Link to Video: you can paste an E-Commerce product link, TopView.ai will generate a video for you.

    You may also like