#ANN#chitresh vardhan verma#Deep Learning#Gradient descent#Keras#kgptalkie#Machine Learning#Optimizers#Tensorflow

Building Your First ANN with TensorFlow 2.0

Build your first ANN with TensorFlow 2.0 and Keras. Covers activation functions, optimizers, backpropagation, and binary classification on tabular data.

May 21, 2026 at 2:15 PM4 min readFollowFollow (Hindi)

Topics You Will Master

ANN architecture: neurons, layers, weights, and biases
Activation functions: ReLU, sigmoid, and softmax
Optimizers: SGD, Adam, and learning rate scheduling
Backpropagation and gradient descent mechanics
Binary classification evaluation with accuracy metrics
Best For

Beginners with basic Python knowledge ready to build their first neural network.

Expected Outcome

A working ANN binary classifier built and trained in TensorFlow 2.0.

Deep learning with Tensorflow

Artificial Neural Networks (ANNs) learn patterns from data by propagating activations through layers of weighted neurons, then adjusting weights via backpropagation to minimize a loss function. This tutorial builds a binary classification ANN in TensorFlow 2.0 and Keras, covering activation functions, optimizers, and evaluation metrics.

Installing libraries

PYTHON
# pip install tensorflow==2.0.0-rc0
# pip install tensorflow-gpu==2.0.0-rc0
PYTHON
import tensorflow as tf
from tensorflow import keras
from tensorflow.keras import Sequential
from tensorflow.keras.layers import Flatten, Dense
PYTHON
print(tf.__version__)
OUTPUT
2.2.0

Importing necessary libraries

PYTHON
import numpy as np
import pandas as pd
from sklearn.model_selection import train_test_split
PYTHON
dataset = pd.read_csv('Customer_Churn_Modelling.csv')
PYTHON
dataset.head()
OUTPUT
RowNumberCustomerIdSurnameCreditScoreGeographyGenderAgeTenureBalanceNumOfProductsHasCrCardIsActiveMemberEstimatedSalaryExited
0115634602Hargrave619FranceFemale4220.00111101348.881
1215647311Hill608SpainFemale41183807.86101112542.580
2315619304Onio502FranceFemale428159660.80310113931.571
3415701354Boni699FranceFemale3910.0020093826.630
4515737888Mitchell850SpainFemale432125510.8211179084.100
PYTHON
X = dataset.drop(labels=['CustomerId', 'Surname', 'RowNumber', 'Exited'], axis = 1)
y = dataset['Exited']
PYTHON
X.head()
OUTPUT
CreditScoreGeographyGenderAgeTenureBalanceNumOfProductsHasCrCardIsActiveMemberEstimatedSalary
0619FranceFemale4220.00111101348.88
1608SpainFemale41183807.86101112542.58
2502FranceFemale428159660.80310113931.57
3699FranceFemale3910.0020093826.63
4850SpainFemale432125510.8211179084.10
PYTHON
y.head()
OUTPUT
0    1
1    0
2    1
3    0
4    0
Name: Exited, dtype: int64

Using label encoder we are converting categorical features to numerical features

PYTHON
from sklearn.preprocessing import LabelEncoder
PYTHON
label1 = LabelEncoder()
X['Geography'] = label1.fit_transform(X['Geography'])
label = LabelEncoder()
X['Gender'] = label.fit_transform(X['Gender'])
X.head()
OUTPUT
CreditScoreGeographyGenderAgeTenureBalanceNumOfProductsHasCrCardIsActiveMemberEstimatedSalary
0619004220.00111101348.88
16082041183807.86101112542.58
250200428159660.80310113931.57
3699003910.0020093826.63
485020432125510.8211179084.10
PYTHON
X = pd.get_dummies(X, drop_first=True, columns=['Geography'])
X.head()
OUTPUT
CreditScoreGenderAgeTenureBalanceNumOfProductsHasCrCardIsActiveMemberEstimatedSalaryGeography_1Geography_2
061904220.00111101348.8800
1608041183807.86101112542.5801
25020428159660.80310113931.5700
369903910.0020093826.6300
48500432125510.8211179084.1001
  • Here using standardscaler we are scaling our data, we are scaling such that the mean is 0 and variance is 1 for data
PYTHON
from sklearn.preprocessing import StandardScaler
PYTHON
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size = 0.2, random_state = 0, stratify = y)
PYTHON
scaler = StandardScaler()
X_train = scaler.fit_transform(X_train)
X_test = scaler.transform(X_test)
PYTHON
X_train
OUTPUT
array([[-1.24021723, -1.09665089,  0.77986083, ...,  1.64099027,
        -0.57812007, -0.57504086],
       [ 0.75974873,  0.91186722, -0.27382717, ..., -1.55587522,
         1.72974448, -0.57504086],
       [-1.72725557, -1.09665089, -0.9443559 , ...,  1.1038111 ,
        -0.57812007, -0.57504086],
       ...,
       [-0.51484098,  0.91186722,  0.87565065, ..., -1.01507508,
         1.72974448, -0.57504086],
       [ 0.73902369, -1.09665089, -0.36961699, ..., -1.47887193,
        -0.57812007, -0.57504086],
       [ 0.95663657,  0.91186722, -1.32751517, ...,  0.50945854,
        -0.57812007,  1.73900686]])

Build ANN

  • Here bwe are building ANN model.
  • First we add input layer of shape of input that is 11 in this case.
  • There is only one hidden layers whose shape is 128.
  • Shape of Output layer is only 1 since we have only one output.
PYTHON
model = Sequential()
model.add(Dense(X.shape[1], activation='relu', input_dim = X.shape[1]))
model.add(Dense(128, activation='relu'))
model.add(Dense(1, activation = 'sigmoid'))
PYTHON
X.shape[1]
OUTPUT
11
  • Here we are compiling our model. we have selected Adam optimizer. loss is binary crossentropy and metric is accuracy
PYTHON
model.compile(optimizer='adam', loss = 'binary_crossentropy', metrics=['accuracy'])
  • Here we are fitting model on training dataset . we have given bacth size of 10 and eopchs are 10
PYTHON
model.fit(X_train, y_train.to_numpy(), batch_size = 10, epochs = 10, verbose = 1)
OUTPUT
Epoch 1/10
800/800 [==============================] - 1s 2ms/step - loss: 0.4516 - accuracy: 0.8116
Epoch 2/10
800/800 [==============================] - 1s 1ms/step - loss: 0.3948 - accuracy: 0.8372
Epoch 3/10
800/800 [==============================] - 1s 1ms/step - loss: 0.3597 - accuracy: 0.8543
Epoch 4/10
800/800 [==============================] - 1s 2ms/step - loss: 0.3475 - accuracy: 0.8576
Epoch 5/10
800/800 [==============================] - 1s 1ms/step - loss: 0.3426 - accuracy: 0.8611
Epoch 6/10
800/800 [==============================] - 1s 1ms/step - loss: 0.3389 - accuracy: 0.8619
Epoch 7/10
800/800 [==============================] - 1s 1ms/step - loss: 0.3366 - accuracy: 0.8625
Epoch 8/10
800/800 [==============================] - 1s 1ms/step - loss: 0.3350 - accuracy: 0.8629
Epoch 9/10
800/800 [==============================] - 1s 2ms/step - loss: 0.3333 - accuracy: 0.8635
Epoch 10/10
800/800 [==============================] - 1s 1ms/step - loss: 0.3311 - accuracy: 0.8634
  • Using model.predict we predict output values for our input data.
PYTHON
y_pred = model.predict_classes(X_test)
PYTHON
y_pred
OUTPUT
array([[0],
       [0],
       [0],
       ...,
       [0],
       [1],
       [0]])
PYTHON
y_test
OUTPUT
1344    1
8167    0
4747    0
5004    1
3124    1
       ..
9107    0
8249    0
8337    0
6279    1
412     0
Name: Exited, Length: 2000, dtype: int64
PYTHON
model.evaluate(X_test, y_test.to_numpy())
OUTPUT
63/63 [==============================] - 0s 2ms/step - loss: 0.3489 - accuracy: 0.8520

[0.34891313314437866, 0.8519999980926514]
PYTHON
from sklearn.metrics import confusion_matrix, accuracy_score

Confusion matrix

PYTHON
confusion_matrix(y_test, y_pred)
OUTPUT
array([[1546,   47],
       [ 249,  158]], dtype=int64)
PYTHON
accuracy_score(y_test, y_pred)
OUTPUT
0.852

Conclusion

In this tutorial you built a three-layer ANN in TensorFlow 2.0 to predict customer churn from the Bank Customer Churn dataset. After encoding categorical features with LabelEncoder and get_dummies, and standardizing with StandardScaler, the model trained for 10 epochs and reached 85.2% test accuracy — confirmed by both model.evaluate() and the confusion_matrix.

Key takeaways:

  • Categorical features like geography and gender must be encoded before feeding into a neural network — LabelEncoder plus one-hot encoding via get_dummies is the standard preprocessing pipeline.
  • A simple three-layer ANN (input → 128-unit hidden → sigmoid output) is a strong baseline for binary classification before moving to deeper or convolutional architectures.
  • The confusion matrix reveals that the model identifies most non-churners correctly but misclassifies 249 churners — class imbalance handling would improve recall on the minority class.

Next steps:

Find this tutorial useful?

Subscribe to our YouTube channels for more practical production walk-throughs.

Discussion & Comments