Understand how to implement Neural Network using TensorFlow with a basic example.

Neural networks are machine learning models inspired by biological neural networks, consisting of interconnected nodes that can learn to perform tasks by analyzing data through adjusting connection weights between nodes during training, applicable to various domains like image recognition, natural language processing, and predictive modeling.

For quick data ingestion, we will import the **load_digits** sample dataset from the sklearn package.

`from sklearn.datasets import load_digits`

data = load_digits()

To learn more about how the dataset looks view this article:

Data Scaling is a data preprocessing step for numerical features. There is a way of data scaling, where the minimum of feature is made equal to zero and the maximum of feature equal to one. **MinMaxScaler** shrinks the data within the given range, usually of 0 to 1.

`from sklearn.preprocessing import MinMaxScaler`

scaler = MinMaxScaler()

X = scaler.fit_transform(digits.data)

y = digits.target

Various ML models do not work with categorical data and to fit this data into the machine learning model it needs to be converted into numerical data. We use the **OneHotEncoder** function for encoding categorical and numerical variables into binary vectors.

`from sklearn.preprocessing import OneHotEncoder`

encoder = OneHotEncoder(sparse=True)

y_encoded = encoder.fit_transform(y.reshape(-1,1))

To split the dataset into train and test sets we will use the **train_test_split **function within the sklearn model. To replicate results of my code, please use the random_state as 42.

`from sklearn.model_selection import train_test_split`

X_train, X_test, y_train, y_test = train_test_split(X, y_encoded, test_size=0.2,

random_state=42)

## 3.1: Define model

Within the keras model, we have the Sequencial function which helps us define the ‘sequence’ of flow of the model. Firstly, we define the hidden layer with its activation function. Secondly, we create an output layer with another activation function.

`import tensorflow as tf`

import numpy as npmodel = tf.keras.models.Sequential([

tf.keras.layers.Dense(64, activation='relu', input_shape=(X_train.shape[1],)),

tf.keras.layers.Dense(len(np.unique(digits.target)), activation='softmax')

])

## 3.2: Compile model

To compile the model that we built, we will have to add the optimizer, the loss function and the required eveluation metrics.

`model.compile(optimizer='adam',`

loss='categorical_crossentropy',

metrics=['accuracy'])

## 3.3: Train Model

Furthermore, we will train the model with the available dataset. We will use the test data to validate the model fitting, which will accordingly build the best available model as per the mentioned epochs.

`history = model.fit(X_train, y_train, epochs=1000, `

validation_data=(X_test, y_test), verbose=2)

## 3.4: Evaluate Model

After our model has been compiled and trained, we will need to evaluate its results by checking its accuracy.

`test_loss, test_accuracy = model.evaluate(X_test, y_test, verbose=2)`

print(f"Test accuracy: {test_accuracy:.2%}")

To understand the basics of neural networks, which includes how it works, what is forward and backward propogation, please refer the the below article:

In the realm of machine learning, numerous models and algorithms exist to tackle intricate problems. However, Neural Networks have emerged as one of the most powerful methods. These intricate systems comprise interconnected nodes capable of learning and executing tasks through data analysis. During training, the connection weights between nodes undergo adjustments, enabling the network to learn and adapt. This process of weight adjustment allows Neural Networks to discern patterns and relationships within data, equipping them to solve highly complex problems with remarkable accuracy and efficiency. Their ability to continuously learn and improve makes Neural Networks a preeminent choice for tackling challenging tasks across various domains.

## Be the first to comment