What are artificial neural networks?
Artificial neural networks (ANNs) are computer models inspired by the design and working of the human brain. They are a subset of machine learning algorithms used for various tasks, including classification, regression, pattern recognition, and decision-making.
Components of an ANN
An ANN consists of the following elements:
- An input layer: The first layer, which accepts input data and transmits it to the layers below.
- One or more hidden layers: Layers that process data via interconnected neurons between the input and output layers.
- An output layer: The layer at the end of the network that generates predictions or outputs.
- Neurons: The fundamental processing units take in inputs, apply weights and biases, and then, through an activation function, produce an output.
- Weights: Parameters that control the intensity of connectivity between neurons and thus the direction of information flow.
- Biases: Additional neuronal characteristics that adjust the network’s behavior and change the activation function.
- Activation function: A non-linear function applied to the weighted sum of inputs in each neuron, introducing non-linearity to the network.
- Forward propagation: The method of creating predictions by transferring input data from the input layer to the output layer through the network.
- Backpropagation: The process of computing gradients of the error with respect to weights and biases to adjust them during training.
- Loss function: A function that gauges the performance of the network by calculating the difference between predicted results and actual labels.
Training the model
Training an ANN entails modifying the weights and biases using a learning algorithm and a labeled dataset. The goal of the network is to minimize the gap between its actual and predicted output (i.e., the error). Gradient descent or its derivatives are commonly used in this technique.
Python with TensorFlow/Keras ANN example
In this example, we’ll build a basic
-
First, the required libraries will be imported.
import numpy as np import tensorflow as tf from tensorflow import keras from tensorflow.keras import layers -
Let’s generate a synthetic dataset
Xthat consists of two features (X1 and X2) and the labelsyfor binary classification.np.random.seed(55) X = np.random.rand(1200, 2) y = (X[:, 0] + X[:, 1] > 1).astype(float) -
Create training and testing sets from the dataset.
from sklearn.model_selection import train_test_split X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.25, random_state=55) -
Create the neural network model.
model = keras.Sequential([ layers.Dense(64, activation='relu', input_shape=(2,)), layers.Dense(1, activation='sigmoid') ]) -
Now the model will be compiled.
model.compile(optimizer='adam', loss='binary_crossentropy', metrics=['accuracy']) -
Utilise the training data to train the model.
history = model.fit(X_train, y_train, epochs=25, batch_size=32, validation_split=0.2) -
Using the test data, evaluate the model.
loss, accuracy = model.evaluate(X_test, y_test) print(f'Test loss: {loss:.4f}, Test accuracy: {accuracy:.4f}') -
Now we can make predictions with the help of the trained model.
predictions = model.predict(X_test)
We’ve built a simple ANN for a binary classification problem using Python and TensorFlow/Keras. Note that this is a simplistic example and that we may need to perform data preprocessing, adjust hyperparameters, and manage more complex architectures for better performance in real-world situations.
Try it out
The output of the code snippets can be observed below after clicking the “Run” button:
import numpy as npimport tensorflow as tffrom tensorflow import kerasfrom tensorflow.keras import layers# Generate synthetic datanp.random.seed(55)X = np.random.rand(1200, 2)y = (X[:, 0] + X[:, 1] > 1).astype(float)from sklearn.model_selection import train_test_splitX_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.25, random_state=55)model = keras.Sequential([layers.Dense(64, activation='relu', input_shape=(2,)),layers.Dense(1, activation='sigmoid')])model.compile(optimizer='adam', loss='binary_crossentropy', metrics=['accuracy'])history = model.fit(X_train, y_train, epochs=25, batch_size=32, validation_split=0.2)loss, accuracy = model.evaluate(X_test, y_test)print(f'Test loss: {loss:.4f}, Test accuracy: {accuracy:.4f}')predictions = model.predict(X_test)
Code explanation
-
Lines7–9: The code generates a synthetic dataset
Xwith 1200 samples and two features, and a target arrayycontaining binary labels based on the condition whether the sum of the two features inXis greater than 1. -
Line 13: It then splits the dataset
Xand corresponding labelsyinto training and testing sets usingtrain_test_split()function from scikit-learn library, with 25% of the data reserved (X_test,y_test) for testing and 75% of the data (X_train,y_train) reserved for training. Therandom_stateis an optional parameter that allows you to set a seed for the random number generator. Providing a specificrandom_stateensures that the data split will be the same each time you run the code, which is helpful for reproducibility. In this case,random_state=55sets the random seed to55. -
Lines 15–18: The code defines a simple neural network model using Keras with two dense layers. The first layer has 64 neurons and uses
reluactivation, while the second layer has one neuron with asigmoidactivation function, suitable for binary classification with two input features. -
Line 20: The model is compiled using the
compile()function. It takes different parameters:adamas theoptimizer,binary_crossentropyas thelossfunction, andaccuracyis used as the evaluation metric. -
Line 22: The model is trained using the
fit()function on the training data (X_train,y_train) for 25 epochs with abatch_sizeof 32, and 20% of the training data is used for validation during training. -
Lines 24–25: After training, the model’s performance is evaluated using the
evaluate()function on the test set (X_test,y_test), and the testlossandaccuracyare printed. -
Line 27: Finally, the model is used to make predictions on the test set
X_testusing thepredict()function, and the predictions are stored in thepredictionsvariable.
Unlock your potential: Neural network series, all in one place!
To continue your exploration of Neural network, check out our series of Answers below:
What are artificial neural networks?
Learn how artificial neural networks (ANNs), inspired by the human brain, perform tasks like classification and prediction through interconnected layers and neurons.Why do we use neural networks?
Learn how neural networks offer high approximation and representational power, enabling valuable data utilization and excelling in tasks like automated image classification.Training of a neural network using pytorch
Learn how artificial neural networks mimic brain functions to process data, and how PyTorch simplifies building and training them using layers, weights, loss functions, and backpropagation.How neural language models work in ChatGPT
Learn how ChatGPT uses transformer architecture with a focus on the decoder, leveraging vast data and attention mechanisms to generate coherent responses.Benefits and Limitations of Neural Machine Translation in ChatGPT
Learn how ChatGPT's neural machine translation offers efficient, accurate language translations, while acknowledging its limitations due to its novelty.What are Graph Neural Networks?
Learn how Graph Neural Networks (GNNs) handle non-Euclidean data using graphs, excelling in clustering, visualization, prediction, NLP, molecule structures, cybersecurity, and social network analysis.What is a neural network-based approach for graph embeddings?
Learn how graph embeddings use neural networks like GCNs to represent graph data as vectors, enabling efficient analysis and tasks like node classification and link prediction.How to avoid overfitting in neural network
Learn how to use cross-validation, regularization, dropout, early stopping, and data augmentation to effectively avoid overfitting in machine learning models.How to Do Back Propagation in a Neural Network
Learn how to calculate gradients using backpropagation to update neural network parameters and improve learning from data actions.PyTorch cheatsheet: Neural network layers
PyTorch provides diverse neural network layers, enabling the design and training of complex models for tasks like image classification, sequence modeling, and reinforcement learning.
Free Resources