DEV Community

Artificial Neural Network

Artificial Neural Network (ANN) – A Beginner's Guide with Python

Category: Machine Learning | Deep Learning | Data Science

1. Introduction

Artificial Intelligence allows computers to perform tasks that normally require human-like intelligence.

Artificial Neural Networks (ANNs) are machine learning models inspired, in a simplified way, by networks of connected neurons. They learn patterns from examples and are widely used in modern Artificial Intelligence applications.

2. What is an Artificial Neural Network?

An Artificial Neural Network is a computational model made up of connected processing units called neurons.

These neurons are arranged in layers. Each connection has a weight, and neurons use activation functions to transform their inputs.

ANNs can learn relationships and patterns from data to perform prediction and classification tasks.

3. Architecture and Working of ANN

A basic Artificial Neural Network contains three main types of layers.

A. Input Layer

Receives the input data and passes it to the network.

B. Hidden Layer

Processes the information using weights, biases and activation functions.

C. Output Layer

Produces the final prediction or classification result.

ANN Architecture

Input Layer → Hidden Layer(s) → Output Layer

Mathematical Formula

a = f(w₁x₁ + w₂x₂ + … + b)

Where:

  • a: Output of the neuron.
  • x: Input values.
  • w: Weights.
  • b: Bias.
  • f: Activation function.

How Does ANN Work?

  1. Input data enters the network.
  2. Neurons calculate weighted sums.
  3. Activation functions transform the values.
  4. The output layer generates a prediction.
  5. The model compares the prediction with the correct answer.
  6. Backpropagation and optimisation update the weights during training.

4. Common Activation Functions

A. ReLU

ReLU is commonly used in hidden layers. It returns zero for negative inputs and the input itself for positive inputs.

B. Sigmoid

Sigmoid converts values into a range between 0 and 1.

C. Softmax

Softmax converts output scores into class probabilities for multi-class classification.

5. Python Implementation

We can implement an Artificial Neural Network using Python and Scikit-learn.

In this example, we use the Iris flower dataset to classify flowers into three categories.

Step 1: Install Required Libraries

pip install numpy scikit-learn
Enter fullscreen mode Exit fullscreen mode

Step 2: Python Program

from sklearn.datasets import load_iris
from sklearn.model_selection import train_test_split
from sklearn.neural_network import MLPClassifier
from sklearn.metrics import accuracy_score
from sklearn.preprocessing import StandardScaler
from sklearn.pipeline import make_pipeline

# Load Iris dataset
iris = load_iris()

# Split the dataset
X_train, X_test, y_train, y_test = train_test_split(
    iris.data,
    iris.target,
    test_size=0.2,
    random_state=42,
    stratify=iris.target
)

# Create ANN model
model = make_pipeline(
    StandardScaler(),
    MLPClassifier(
        hidden_layer_sizes=(10,),
        max_iter=2000,
        random_state=42
    )
)

# Train the model
model.fit(X_train, y_train)

# Make predictions
predictions = model.predict(X_test)

# Calculate accuracy
accuracy = accuracy_score(y_test, predictions)

print("Test accuracy:", round(accuracy * 100, 2), "%")
Enter fullscreen mode Exit fullscreen mode

6. Expected Output

The program displays the test accuracy as a percentage.

Test accuracy: (model-generated percentage) %
Enter fullscreen mode Exit fullscreen mode

Output Explanation:

The model trains using the training dataset and predicts flower categories from the test dataset.

The accuracy indicates the percentage of test samples classified correctly. The exact value may vary depending on software versions and training settings.

7. Real-Life Applications of ANN

Artificial Neural Networks are used in:

  1. Image recognition.
  2. Speech recognition.
  3. Recommendation systems.
  4. Demand forecasting.
  5. Text classification.
  6. Pattern detection in scientific data.

8. Advantages of ANN

  • Can learn complex non-linear patterns.
  • Supports different types of input data.
  • Can be used for classification and regression.
  • Useful in many Artificial Intelligence applications.

9. Limitations of ANN

  • May require substantial training data.
  • Can require significant computing resources.
  • Can be difficult to interpret.
  • Requires careful model tuning.
  • May overfit without proper validation and regularisation.

10. Conclusion

Artificial Neural Networks are an important foundation of modern Deep Learning.

They learn patterns from data using interconnected neurons, weights, biases and activation functions.

Understanding ANN architecture and training provides a strong foundation for studying advanced deep learning models such as CNNs and RNNs.

11. References and Further Reading

  1. Scikit-learn Documentation
  2. TensorFlow Learning Resources
  3. GeeksforGeeks – Computer Science

Top comments (0)