Training the ANN for Banknote Classification

Below are the steps for implementing ANN model for classifying counterfeit notes.

1. Importing Necessary Libraries

Python
import csv
import tensorflow as tf
from sklearn.model_selection import train_test_split

2. Reading Data from CSV:

  • Importing the CSV module and opens the “banknotes.csv” file.
  • It creates a CSV reader object and skips the header row using `next(reader)`.
  • It iterates over each row in the CSV file.
  • For each row, it extracts the features (the first four elements, converted to floats) and the label (the fifth element, converted to 1 if it’s “0”, else 0).
  • It appends a dictionary containing the evidence and label to the `data` list.
Python
# Read data in from file
with open("banknotes.csv") as f:
    reader = csv.reader(f)
    next(reader)

    data = []
    for row in reader:
        data.append({
            "evidence": [float(cell) for cell in row[:4]],
            "label": 1 if row[4] == "0" else 0
        })

3. Splitting Data into Training and Testing Sets

  • Importing the `train_test_split` function from scikit-learn.
  • It extracts the features (`evidence`) and labels (`labels`) from the `data` list of dictionaries.
  • It splits the data into training and testing sets using the `train_test_split` function. Here, 60% of the data is used for training (`test_size=0.4`).
Python
evidence = [row["evidence"] for row in data]
labels = [row["label"] for row in data]
X_training, X_testing, y_training, y_testing = train_test_split(
    evidence, labels, test_size=0.4
)

3. Creating the Neural Network Model

  • Initializes a sequential model using `tf.keras.models.Sequential()`.
  • It adds a hidden layer with 8 units and ReLU activation using `model.add(tf.keras.layers.Dense(8, input_shape=(4,), activation=”relu”))`. The `input_shape=(4,)` specifies the shape of the input data (four features).
  • It adds an output layer with 1 unit and sigmoid activation using `model.add(tf.keras.layers.Dense(1, activation=”sigmoid”))`. Sigmoid activation is used for binary classification problems to produce probabilities between 0 and 1.

tf.keras.layers.Dense(8, input_shape=(4,), activation=”relu”):

  • tf.keras.layers.Dense(8, …): This creates a dense (fully connected) layer with 8 neurons (units). The number 8 represents the dimensionality of the output space, meaning there will be 8 neurons in this layer.
  • input_shape=(4,): This specifies the shape of the input data that this layer will receive. The input shape is set to `(4,)`, indicating that each input sample has 4 features. This is necessary only for the first layer of the model. Subsequent layers can infer their input shapes automatically.
  • activation=”relu”: This specifies the activation function for the neurons in this layer. “relu” stands for Rectified Linear Unit, which is a commonly used activation function in neural networks. It introduces non-linearity to the model and helps the network learn complex patterns in the data by allowing it to model non-linear relationships between features.
Python
model = tf.keras.models.Sequential()

model.add(tf.keras.layers.Dense(8, input_shape=(4,), activation="relu"))
model.add(tf.keras.layers.Dense(1, activation="sigmoid"))

4. Compiling the Model:

  • This part of the code compiles the model using `model.compile()`.
  • It specifies the optimizer as “adam”, the loss function as “binary_crossentropy”, and the metric to monitor as “accuracy”.

optimizer=”adam”, loss=”binary_crossentropy”, metrics=[“accuracy”]:

  • “adam” optimizer: Combines AdaGrad and RMSProp, using momentum and adaptive learning rates for efficient training of deep neural networks.
  • “binary_crossentropy” loss: Ideal for binary classification tasks, it measures the difference between true and predicted distributions using cross-entropy.
  • “accuracy” metric: Measures the proportion of correctly classified samples during training and testing. It helps monitor the model’s performance.
Python
model.compile(
    optimizer="adam",
    loss="binary_crossentropy",
    metrics=["accuracy"]
)

5. Training and Evaluating the Model

  • Training the model on the training data (`X_training` and `y_training`) using `model.fit()`.
  • It specifies `epochs=20`, meaning the training will run for 20 iterations over the entire dataset.
Python
model.fit(X_training, y_training, epochs=20)
model.evaluate(X_testing, y_testing, verbose=2)

7. Saving the Model:

Python
model.save("banknotes_model.h5")
  • model.save(): This method is provided by the Keras library and is used to save the entire model (including architecture, weights, and optimizer state) to a file.
  • “banknotes_model.h5”: This is the filename specified for the saved model. It can be any valid filename with the extension “.h5”, which is a common convention for HDF5 files used to store neural network models.

After running this line of code, a file named “banknotes_model.h5” will be created in the current directory containing all the necessary information to recreate and use the trained neural network model in the future. You can load this saved model later using tf.keras.models.load_model(“banknotes_model.h5”).

8. Running Python file:

Running `python banknotes.py` will execute the Python script named "banknotes.py".

Output:

an excerpt of terminal screen after running the python file

Securing Transactions: Banknote Classification with Neural Networks

Banknote classification is a critical task in financial systems, ensuring the validity and integrity of monetary transactions. With advancements in machine learning, particularly deep learning, the accuracy and efficiency of banknote classification systems have significantly improved. In this article, we’ll learn about the basic implementation of a Artificial Neural Networks using TensorFlow and Keras. We’ll add a dense hidden layer to the model. Artificial Neural Networks will be trained on a dataset and binary classification will be performed to predict the output class.

Table of Content

  • Understanding Artificial Neural Networks (ANNs)
  • Implementing Neural Network Model for Banknote Classification
  • Training the ANN for Banknote Classification

Similar Reads

Understanding Artificial Neural Networks (ANNs)

Artificial Neural Networks are not a new technology but the recent attainments in the field of Artificial Intelligence has made it important to learn ANN’s implementation and uses....

Implementing Neural Network Model for Banknote Classification

The ANN will be trained on a data set of Banknotes, and Binary Classification will be performed on the inputs. The Banknotes Data set will consists of data about the bank notes and classes denoting whether a banknote is genuine or counterfeit....

Training the ANN for Banknote Classification

Below are the steps for implementing ANN model for classifying counterfeit notes....

Conclusion

In this article, we demonstrated how to implement an Artificial Neural Network for banknote classification using TensorFlow and Keras. We covered the steps from importing necessary libraries, reading and preprocessing the data, creating and compiling the model, to training and evaluating the model. This approach can be extended to other binary classification problems, leveraging the power of deep learning to achieve high accuracy and efficiency....

Contact Us