Artificial Neural Network in Scala - part 1

Alexey Novakov published on

15 min, 2863 words

Categories: math

Deep Learning is a group of machine learning methods which are based on artificial neural networks. Some of the deep learning architectures are deep neural networks. Deep neural network is an artificial neural network (ANN further) with multiple layers between the input and output layers. There are different types of neural networks, but they always have neurons, synapses, weights, biases and functions.

Scala is a full-stack multi-paradigm programming language. Scala is famous for its innovations in JVM eco-system and ambitious language syntax features leaving all other JVM-based languages for years behind. Scala is also popular language thanks to Apache Spark, Kafka and Flink projects which are mainly implemented in it.


This tutorial is divided into 2 articles.

  1. In this article we will go through the theory of ANN implementation. I will guide you through the basic calculus such as linear algebra and a little bit of differential calculus, which you need to know to implement neural network training and optimization algorithms.

  2. In the second aritcle we are going to implement ANN from scratch in Scala. You can jump into second part directly, if you are familiar with the theory. But first look at the ANN Jargon table, if you decided to switch to second part.

ANN Jargon

I assume you are a bit familiar with Machine Learning or Deep Learning. Nevertheless, below table will be useful to match Deep Learning terminology with further Scala implementation. Some of the variable names in Scala code will be directly based on the Deep learning name definitions, so that it is important to know why some variable is named as z and another one as w.

Code Symbol / TypeDescriptionEncoded as
xinput data for each neuron2-dimensional tensor, i.e. matrix
y , actualtarget data we know in advance from the training dataset1-d tensor, i.e. vector
yHat , predictedoutput of the neural network during the training or single prediction1-d tensor
wlayer weight, a.k.a model parameters2-d tensor
blayer bias, part of the layer parameters1-d tensor
zlayer activation, calculated as
x * w + b
2-d tensor
factivation function to activate neuron. Specific implementation: sigmoid, reluScala function
alayer activity, calculated as f(z)2-d tensor
errorit is result of yHat - y1-d tensor
lossFuncloss function to calculate error rate on training/validation (example: mean squared error)Scala function
epochsnumber of iterations to train ANNinteger > 0
accuracy% of correct predictions on train or test data setsdouble number, between 0 and 1
learningRatenumeric parameter used in weights updatedouble number, usually between 0.01 and 0.1


Deep Learning is all about tensor calculus. Tensor in computer science is a generic abstraction for N-dimensional array. In our ANN implementation, we are going to use scalar numbers which are encoded as 0-dimension tensor, vector - encoded as 1-d tensor and finally matrix - encoded as 2-d tensor. Vector and matrix are the most used building blocks for Deep Learning calculus. All tensor shapes we will use are going to be rectangular - every element is the same size along each axis.


We will take _Churn Modelling dataset which predicts whether a customer is going to leave the bank or not. This dataset is traveling across many tutorials at the Internet, so that you can find a couple different code implementations among dozens of articles using the same data, so do I.


The last column is y, i.e. the value we want our ANN to predict between 0 and 1. Rest of the columns are x data, also known as features. We drop first 3 columns and the last column y before we use them as x matrix. Remaining columns:


Network Topology

Above picture describes a network we are going to implement.

  • N is a number of features, i.e. remaining columns from the input dataset.

  • Each layer is fully connected with next layer. I draw each layer by skipping middle neurons to get smaller visual overhead.

  • Between each layer we have weights which form linear equations via dot product x * w + bias.

  • Each layer has its own activation functions. We use ReLU (Rectified Linear Unit) and Sigmoid functions.

Forward Propagation

Basic idea of neural network implementation is to leverage math principals for matrix and vector multiplication. As neural network is going to be fully connected from layer to layer, we can represent learning and optimization algorithms as following:


Single training example

It is one data record:

  1. x: 12 input columns as 1 x 12 matrix
  2. w1: 12 x 6 matrix + 6 x 1 matrix for biases
  3. w2: 6 x 6 matrix + 6 x 1 matrix for biases
  4. w3: 6 x 1 matrix + 1 x 1 matrix for biases
  5. yHat: scalar number

An amount of hidden layers and neurons are parameters to be tuned by an expert, i.e. externally to the main algorithm. We set 2 hidden layers with 6 neurons each. Our last layer is single neuron that produces final prediction, which we will treat as yes or no to answer customer churn question.

Multiple training examples

Mini-batch approach, i.e. multiple training examples at once. Batch size to be tuned externally as well. Let's take 16 as batch size:

  1. x: 16 x 12 matrix
  2. w1: 12 x 6 matrix + 6 x 1 matrix for biases
  3. w2: 6 x 6 matrix + 6 x 1 matrix for biases
  4. w3: 6 x 1 matrix + 1 x 1 matrix for biases
  5. yHat: 16 x 1 matrix

As you can see, our matrices are equal in rows at input and output layers. That means we can input any number of rows through the neural network at once when we do training or prediction.

Math of the Forward propagation

When we train neural network, we use input data and parameters on all hidden layers to reach output layer, so that we get the prediction value(s). First part of the ANN implementation that calculates predictions, i.e. yHat is called _forward propagation.

Linear algebra helps us to feed data into the network and get the result using matrix multiplication principals. That makes the entire training and single prediction quite generic, so that we can easily program that in any programming language.

In a nutshell, our 12 x 6 x 6 x 1 network will form the following expressions for every training batch:

First Hidden Layer

Above picture shows activations for the neurons of the first hidden layer. In fact, next layers are calculated in similar way.

  • X is a matrix where each row is a data sample. Every column is a particular feature/column from the initial dataset. Usually, input column is scaled/encoded (see second article).

  • We use dot product operation for X and W. Resulting matrix is used to add biases using element-wise addition. b1 will be added to each element of the first row of that resulting matrix, then b2 to the second row and so on.

Second Hidden Layer

The x of the second layer is an a we calculated on the previous layer. We will call it a1.

a1 (16 x 6) * w2 (6 x 6) + b2 (6 x 1) = z2 (16 x 6)

f(z2) = a2 (16 x 6)

Above pseudo-code shows matrix dimensions in the parenthesis.

Output Layer

Input data is a2. Here we get out prediction at the end:

a2 (16 x 6) * w3 (6 x 1) + b3 (1 x 1) = z3 (16 x 1)

f(z3) = a3 (16 x 1)

a3 a.k.a yHat represents a prediction for each data sample in the batch. Prediction values are probabilities between 0 and 1.

If you are confused with above explanation, I recommend to check great video series on Deep Learning: But what is a Neural Network? | Deep learning, chapter 1.

Batch Tracing

In order to see what is going on with the state of neural network, let's feed one single batch into it.

There are different strategies for initial weights and biases initialisation. In our implementation we will follow:

  • weight matrices are initialised using uniform-random
  • bias vectors are initialised with zeros

Later they will be updated via optimization algorithm.

x - input

Below matrix is our x, which is our input data from the training or test set. The values it contains is a result of data preparation step, which I will explain in details further:

sizes: 16x12, Tensor2D[Float]:

w1 - between input and 1st hidden layers


sizes: 12x6, Tensor2D[Float]:


sizes: 6, Tensor1D[Float]:

w2 - between 1st and 2nd hidden layers


sizes: 6x6, Tensor2D[Float]:


sizes: 6, Tensor1D[Float]:

w3 - between 2nd hidden and output layers


sizes: 6x1, Tensor2D[Float]:


sizes: 1, Tensor1D[Float]:

yHat - Output layer

Predictions for the input batch:

sizes: 16, Tensor1D[Float]:

Above trace is for the first training batch at the very first epoch. You should not try understand these digits in weights, biases and outputs matrices. They are going to change their values a lot after running training loop 100 times (epochs) with N batches each.

Backward Propagation

Initial weights and biases are not going to give us right equations to predict our y. Even if we propagate the entire dataset through the network. Obviously, someone needs to update these parameters based on some feedback. This feedback is calculated via loss function. In the science literature, loss function is also called as cost function. We are going to use loss and cost here as synonymous.

There are different loss functions in Deep Learning we can use. We will go with binary-cross-entropy as we predict binary value. Our loss value will show how good we are updating the network weights at specific training epoch. However, updates will be done using Gradient Descent optimization algorithm.

Gradient Descent optimization

Model prediction is calculated through the forward propagation of the input data sample(s). If weights and biases are trained well, then our predictions will be accurate as well. In order to say whether our model performs well, we check loss value on every training cycle as well as accuracy metric. Accuracy is number of correct predictions divided by total number of data samples during the training or validation.

You might ask yourself - how can I train model parameters well? In other words how to update them in right direction, so that they give accurate predictions?

Here we meet Gradient Descent.

As per Wikipedia:

Gradient descent is a first-order iterative optimization algorithm for finding a local minimum of a differentiable function

Gradient descent is a way to minimise an objective loss function for neural network parameters. In other words, it helps to find best parameters by minimising loss function. Smaller loss, better overall network accuracy.

Using Gradient Descent, we are finding proper change rate on every training cycle. So that we can update our weights and biases to get the best model performance.

How exactly the Gradient Descent algorithm calculates that change?

There are thousands of articles explaining that visually. In short, we are trying to find steepest descent of the derivative function. Using small coefficient learningRate we subtract the gradient value from the initial parameter. That allows us to find best parameter and minimise the loss function. One of the good visual explanation for linear regression problem with gradient descent is here. It also works well for multiple linear regression like in our case, we got 12 features, so 12 independent variables.

Coming back to our back-propagation implementation. f'(x)is a derivative function of the layer's activation function. We use derivative function to update all weights except the last one, i.e. w1 and w2, not w3. To update w3, we use delta based on the error, i.e. predicted - actual. Layers w1 and w2 are using relu as activation function. Derivative of the relu function is following:

if (x < 0) 0 
else 1

Such derivative function is applied element-wise to z matrix in the step #6 of the back propagation part (see second article for Scala reference implementation).

Math of the Backward propagation

Back-propagation part of ANN is a bit more complicated than forward propagation part. In Gradient Descent Optimization algorithm, we calculate derivatives to calculate yHat rate of change with respect to last z, in our case it is z3.

High-level steps of the Gradient Descent algorithm

  1. Calculate error based on actual y and on predicted yHat. It will be called delta further and in the code.
delta = (yHat - y) multiply f'(z)

result is a 16 x 1 matrix

    - "f`" is a derivative to activation function on the current layer.   
    - "z" is current layer activation.
    - "multiply" is Hadamard product, a.k.a. element-wise multiplication. 
      Note: it is not the same as dot product.
  1. Iterate list of weights matrices from end to start, i.e. backwards. Let's take "i" as layer index
  2. Calculate partial derivative as:
 partialDerivativeN = xi.T * delta

     - "T" is a matrix transpose operation,
     - "*" is dot product operation.    
  1. Update weights of i layer via:
wi = wi - learningRate * partialDerivative_i 

  - "learningRate" is a scalar number. 
  - "partialDerivative_i" is a matrix, so "*" is dot product here as well.
  1. Update bias of i layer via:
bi = bi - sum(delta)
  1. Pass updated weight wi and delta to previous layer, i.e. to wi - 1.

  2. Now starting wi - 1 weight update, we calculate delta differently.

delta = (previousDelta * previousW) multiply f'(z)

   - "previousDelta" is delta from previous step, i.e. 
      it is from layer next to the right, because we go backward. 
   - "previousW" - is a weight matrix from the previous step as well.

If i > 0, then:

decrement i via i = i -1 and then repeat steps from 2 to 5.


we finished back-propagation for a specific batch or a single data sample (in case of stochastic gradient descent).

Back-propagation tracing

w3 - between 2nd hidden and output layers

Delta (error):

sizes: 16, Tensor1D[Float]:

x transpose:

sizes: 6x16, Tensor2D[Float]:

partialDerivative3 = x.T * (delta multiply f`(z)):

sizes: 6x1, Tensor2D[Float]:

updated Weight = w - learningRate * partialDerivative3 :

sizes: 6x1, Tensor2D[Float]:

updated Bias = b - learningRate * sum(delta) :

sizes: 1, Tensor1D[Float]:

w2 - between 1st and 2nd hidden layers

current delta:

delta (16x6) = (previous delta * previous w) multiply f`(z)

current derivative:

partialDerivative2 (6 x 6) = x.T (6 x 16) * delta (16 x 6)

updated Weight:

w2 (6x6) = w2 (6x6) - learningRate * partialDerivative2 (6 x 6)

updated Bias:

b2 (6 x 1) = b2 (6 x 1) - learningRate * sum(delta)

w1 - between input and 1st hidden layers

current delta:

delta (16x6) = (previous delta * previous w) multiply f`(z)

current derivative:

partialDerivative1 (12 x 6) = x.T (12 x 16) * delta (16 x 6)

updated Weight:

w1 (12x6) = w2 (12x6) - learningRate * partialDerivative1 (12 x 6)

updated Bias:

b1 (6 x 1) = b1 (6 x 1) - learningRate * sum(delta)

Wrapping up

We have finished with theoretical part and ready to continue with the second article ANN in Scala: implementation. In case you have not gotten how the ANN is actually working in theory, then I encourage you to search for other articles and videos. It is important to understand this part before looking at actual implementation in code.