# Fog-Computing-Based Heartbeat Detection and Arrhythmia Classification Using Machine Learning
## 4 Implementation Details
### Arrhythmia-Classification Module
We measure the performance of the classifiers using the following measures: per-class precision, per-class recall, per-class F1 Score, average precision, average recall, and average F1 Score, which are defined as follows:  
\[Precision_{c}=\frac{TP_{c}}{TP_{c}+FP_{c}}\]  
\[Recall_{c}=\frac{TP_{c}}{TP_{c}+FN_{c}}\]  
\[F1\ Score_{c}=2\cdot\frac{Precision_{c}\cdot Recall_{c}}{Precision_{c}+ Recall_{c}}\]  
\[Average\ precision=\sum\
olimits_{c=0}^{3}\frac{TP_{c}}{TP_{c}+FP_{c}}\]  
\[Average\ recall=\sum\
olimits_{c=0}^{3}\frac{TP_{c}}{TP_{c}+FN_{c}}\]  
\[Average\ F1\ Score=\sum\
olimits_{c=0}^{3}2\cdot\frac{Precision_{c}\cdot Recall _{c}}{Precision_{c}+Recall_{c}},\]  
where \(c\) is the class index, and \(TP_{c}\), \(FP_{c}\), \(TN_{c}\), and \(FN_{c}\) stand for true positive, false positive, true negative, and false negative outcomes, respectively, for class \(c\). In accordance with the AAMI recommendations, we calculated two sets of performance measurements. It is worth noting that the AAMI-recommended calculations do not reward or penalize a classifier for the classification of ventricular fusion (F) or unknown beats (Q), such as VEBs. The validation stage is devoted to the search of the best combination of hyperparameters, that is, the one that results in a model with the best performance on the validation set. We define as the best combination the one with the best average F1 score, as it is the measure that takes into account both the precision and the recall of the result for each class. The hyperparameters that have been fine-tuned are the window size, the number of channels, the timesteps, the number of LSTM layers, the number of fully connected layers, the number of neurons for each layer, the batch Size, the dropout probability, and the hidden-units activation function. We trained the model using the Adam optimization algorithm, an extension to stochastic gradient descent, using the categorical cross entropy as the loss function, defined as: \(-\sum_{c=1}^{M}y_{o,c}\ln(p_{o,c})\), where \(M\) is the number of classes, \(y_{o,c}\) is a binary indicator (0 or 1) if class label \(c\) is the correct classification for observation \(o\), and \(p_{o,c}\) is the predicted probability observation that \(o\) is of class \(c\). The value of the _number of epochs_ parameter varied on the various executions. We employed an _early stopping criterion_: the model stops training when the value of the loss function does not decrease for ten epochs in the validation step. When this happens, we restore the weights of the network corresponding to the lowest loss value. Because of the elevated number of candidate combinations, it was not feasible to perform an exhaustive grid search. For this reason, we tuned each parameter in isolation, maintaining the other ones fixed. When we chose the value for a given parameter, we kept it for all the subsequent runs. First, we examined the effect of the window size. We define a heartbeat as a fixed number of samples around an \(R\) peak location. There is not an appropriate value for the number of samples agreed on in the literature, so we examine different values. Figure 5(a) depicts the achieved performance for different window sizes. The best value in terms of average F1 Score on the validation dataset is 200 samples. The second parameter that we examined was the number of timesteps considered in the feature vector, that is, the number of previous beats included for making the prediction for the current beat. Figure 5(b) depicts the evaluation of the performance of the classifier for different number of timesteps included. The results indicate that the best value is 2. The next parameter we examined was the number of LSTM layers that we connect in a sequential manner in the classifier model. We also tested whether the inclusion of standard fully connected layers after the LSTMs improves the results. We also examined the use of dropout regularization layers between the fully connected layers. The dropout probabilities considered are zero (no dropout layers), 0.1, 0.3, 0.5, and 0.8.