Lecture

Creating a Simple Number Prediction Machine Learning Model

Machine Learning is a technology that uses data to learn patterns and make predictions; it's one of the most widely used methods for implementing AI.

Deep Learning is a branch of machine learning that uses Artificial Neural Networks to learn complex patterns.

Artificial neural networks are programs inspired by the human brain. They are composed of various layers that have learned large-scale data, and each layer consists of neurons similar to a human brain.

Detailed lessons on machine learning and deep learning will be covered in future classes. In this session, we will create a simple machine learning model using one of the most popular machine learning libraries, TensorFlow.

Don't worry if you do not fully understand the code yet! We will dive deeper into what Python code for AI models means in future lessons. For now, focus on running the code to experience it. 😊


How is an AI model created?

The process of creating an AI model consists of three major steps:

  1. Data Preparation: Prepare the data that the model will learn from.

  2. Model Definition: Set up the structure of the neural network.

  3. Model Training: Train the model with the data and verify the results.


Creating a Simple AI Model with TensorFlow

The following code creates an AI model that predicts a number’s output using the formula y = x² + 1.

Now, let's follow along with TensorFlow step by step to create a simple number prediction model.


Importing TensorFlow and NumPy Libraries

Use Python's import keyword to bring in the tensorflow and numpy libraries.

Import Libraries
import tensorflow as tf import numpy as np

You can use tf for tensorflow within the code, and np for numpy.


1. Data Preparation

X represents inputs for training the AI model, and Y represents the desired outputs.

The model will learn the relationship between the inputs and outputs.

Data Preparation
# Creating training dataset (y = x^2 + 1), x_train: inputs x_train = np.array([1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0, 10.0], dtype=np.float32) # y_train: outputs y_train = np.array([2.0, 5.0, 10.0, 17.0, 26.0, 37.0, 50.0, 65.0, 82.0, 100.0], dtype=np.float32)

2. Model Definition

Use TensorFlow and Keras to define an AI model.

Keras is a library that makes it easier to create machine learning models.

Much like following a recipe, Keras enables you to build a machine learning model using simplified code instructions.

Model Definition
model = tf.keras.Sequential([ # Input layer with 50 neurons, using ReLU as activation function tf.keras.layers.Dense(50, activation='relu', input_shape=[1]), # Hidden layer with 50 neurons, using ReLU as activation function tf.keras.layers.Dense(50, activation='relu'), # Output layer with 1 neuron tf.keras.layers.Dense(1) ])

Sequential is a way to design an artificial neural network by layering layers (input → hidden → output) sequentially.

Dense refers to a Fully Connected Layer (FC Layer). This means that every neuron is connected to every neuron in the next layer.


3. Model Compilation

Below is the step where we configure how our AI model will learn.

Model Compilation
model.compile(optimizer=tf.keras.optimizers.Adam(learning_rate=0.01), loss='mean_squared_error')

When training the AI model with data, two things must be set:

  • How to calculate the error: mean_squared_error computes the loss using mean squared error. A loss function calculates the difference between the predicted value and the actual value.

  • Optimization method: Adam is one of the optimization methods that adjusts weights to reduce the loss function’s value.


4. Model Training

The code below trains the defined model over 200 epochs, meaning the entire dataset is used 200 times during training.

Model Training
history = model.fit(x_train, y_train, epochs=200, verbose=1)

The fit function trains the model using the x_train and y_train data.

epochs is a parameter that sets how many times the entire dataset will be repeated for training.


5. Checking Results

Finally, we use the trained model for predictions and check the results.

Prediction Test
x_test = np.array([2.0, 5.0, 8.0], dtype=np.float32) predictions = model.predict(x_test, verbose=1)

The predict function calculates predictions using the model for the given input values.

Afterward, use the print function to display the predicted values.


We have now created a simple AI model to predict outputs based on input numbers using Python code.

As mentioned earlier, AI is essentially a function that learns from data and performs predictions.

Training an AI model involves refining its internal function to improve prediction accuracy.

To achieve stronger performance, it is important to use high-quality training data and carefully design the model architecture.

In the next lesson, we will use the sklearn library to create a simple spam mail classification model.

Lecture

AI Tutor

Design

Upload

Notes

Favorites

Help