estimate F1 and other metrics in tensorflow in R

Hello,

I'm trying to set a DL algorithm in R using tensorflow, but I don't know if the computation of F1 and other metrics are correct. This is the code:

set.seed(123)  # Set seed for reproducibility
test$Test <- as.factor(test$Test)
test$Test <- as.numeric(test$Test) - 1  # Convert to 0 and 1 for binary classification
# Define the features and labels
features <- c("F1", "F2", "F3", "duration")
labels <- "Test"
sample <- sample.int(n = nrow(test), size = floor(.7 * nrow(test)), replace = FALSE)
train_data <- test[sample, ]
test_data <- test[-sample, ]
x_train <- as.matrix(train_data[, features])
y_train <- train_data[, labels]
x_test <- as.matrix(test_data[, features])
y_test <- test_data[, labels]
# Build a new model for training on 70% of data
model <- keras_model_sequential() %>%
layer_dense(units = 16, activation = 'relu', input_shape = c(length(features))) %>%
layer_dense(units = 16, activation = 'relu') %>%
layer_dense(units = 1, activation = 'sigmoid')
# Define custom F1 score metric function
f1_score_metric <- function(y_true, y_pred) {
    # Compute true positives (TP), false positives (FP), and false negatives (FN)
    TP <- sum(y_pred * y_true)
    FP <- sum(y_pred * (1 - y_true))  # y_true = 0 where there are false positives
    FN <- sum((1 - y_pred) * y_true)  # y_pred = 0 where there are false negatives
    # Compute precision and recall
    precision <- TP / (TP + FP)
    recall <- TP / (TP + FN)
    # Compute F1 score
    f1_score <- 2 * precision * recall / (precision + recall)
    return(f1_score)
}
# Compile the model
model %>% compile(loss = 'binary_crossentropy', optimizer = optimizer_adam(), metrics = c("accuracy", "precision", "recall", "AUC", f1_score_metric))
# Train the model
history <- model %>% fit(x_train, y_train, epochs = 30, batch_size = 10, validation_split = 0.2,  verbose = 1)
# Evaluate the model on the test data
score <- model %>% evaluate(x_test, y_test)
cat('Test loss:', score$loss, '\n')
cat('Test accuracy:', score$accuracy, '\n')
cat('Test precision:', score$precision, '\n')
cat('Test recall:', score$recall, '\n')
cat('Test F1 score:', f1_score_metric(y_test, model %>% predict(x_test)), '\n')
cat('Test AUC:', score$AUC, '\n')
}

Your help is greatly appreciated!

George