# Leveraging Artificial Intelligence in Blockchain-Based E-Health for Safer Decision Making Framework
## 4 Implementation
### ML Detection Implementation
#### 4.5.3 MLP Decision Function  
MLP has an input layer of 9-dimensions, and two hidden layers of 5 and 2 dimensions, and since it is solving a binary classification problem, it has a 1-dimension output layer.  
The decision function of MLP concludes a series of addition and multiplication to classify an input. In this calculation, each hidden neuron's value equals the linear summation of all previous layer's neurons' values multiplied by their coefficients, or the weights between the neuron's layer and the last layer. An additional value of intercept, or bias, is added to this summation:  
\[h_{i}^{(1)}=\phi(\sum_{j}x_{j}w_{i,j}+b_{i}^{(1)})\]  
\[h_{i}^{(2)}=\phi(\sum_{j}h_{j}^{(1)}w_{i,j}+b_{i}^{(2)}) \tag{6}\]  
\[y_{i}=\phi(\sum_{j}h_{j}^{(2)}w_{i,j}+b_{i}^{(3)})\]  
Equation (6) calculates MLP decision function where \(h_{i}^{n}\) is the neuron \(i\) value in the \(n\)th layer. This implementation includes two layers and a final output layer with one neuron, \(y_{i}\), which gives the final summation value. \(\phi()\) is the nonlinear activation function that calculates the neuron's value by a weighted sum, where \(x_{j}\) is the input features vector, \(h_{i}^{n}\) is the neurons' values at layer \(i-1\), \(w_{i,j}\) is the weight, and \(b_{i}^{n}\) is the intercept of neuron \(i\) at the \(n\)th layer.  
\[f(x)=max(0,x) \tag{7}\]  
This MLP implementation follows a Rectifier Linear Unit activation function (reLu), which, as illustrated by Equation (7), returns the max between 0 and the weighted sum, \(x\), of a neuron.  
Algorithm 8 shows the steps in calculating the Equation (6) decision function of this paper's implementation of MLP by using a three-level loop. It is worth noting that the current development of smart contracts did not allow for multi-loop applications. For this reason, the inner loops were applied and called separate functions inside the contract.  
Eventually, the MLP decision function returns a decimal value of 0 or >0. If the output is greater than zero, the classification label is 1; otherwise, it is 0.  
The number of iterations in each loop is related to the number of neurons in each layer of MLP, and Algorithm 8 was tailored according to this paper's MLP implementation; a different implementation should follow different specifications accordingly.  
```
1:Input:\(x\) integer input vector,
2:\(w\) weight matrices,
3:\(b\) biases array
4:Output:\(v\) decimal number
5:procedureMLPDecFun
6:\(z\leftarrow\) ScaleData(\(x\)) \(\triangleright\) ScaleData procedure
7:init\(A[]\) as Array
8:init\(B[]\) as Array
9:for\(i\gets 0\) to \(i\gets 2\)do
10:if\(i\gets 1\)then
11:for\(j\gets 0\) to \(j\gets 4\)do
12:\(xSum\gets 0\)
13:for\(k\gets 0\) to \(k\gets 8\)do
14:\(xw\gets z[k]*w[i][k][j]\)
15:\(xSum\gets xSum+xw\)
16:\(a\gets xSum+b[i][j]\)
17:\(a\leftarrow\) max(0,\(a\))
18:push\(a\) to \(A[]\)
19:if\(i\gets 1\)then
20:for\(j\gets 0\) to \(j\gets 1\)do
21:\(aSum\gets 0\)
22:for\(k\gets 0\) to \(k\gets 4\)do
23:\(aSum\gets aSum+aw\)
24:\(b\gets aSum+b[i][j]\)
25:\(b\leftarrow\) max(0,\(b\))
26:push\(b\) to \(B[]\)
27:if\(i\gets 2\)then
28:\(bSum\gets 0\)
29:for\(k\gets 0\) to \(k\gets 1\)do
30:\(b\gets B[k]*w[i][k]\)
31:\(bSum\gets bSum+bw\)
32:\(y\gets bSum+b[i]\)
33:\(y\leftarrow\) max(0,\(y\)) \(\triangleright\) last reLU function
34:return\(y\)
```  
**Algorithm 8** MLP decision function