Diff of /man/f1_wrapper.Rd [000000] .. [409433]

Switch to side-by-side view

--- a
+++ b/man/f1_wrapper.Rd
@@ -0,0 +1,50 @@
+% Generated by roxygen2: do not edit by hand
+% Please edit documentation in R/metrics.R
+\name{f1_wrapper}
+\alias{f1_wrapper}
+\title{F1 metric}
+\usage{
+f1_wrapper(num_targets = 2, loss = "binary_crossentropy")
+}
+\arguments{
+\item{num_targets}{Size of model output.}
+
+\item{loss}{Loss function of model.}
+}
+\value{
+A keras metric.
+}
+\description{
+Compute F1 metric. If loss is \code{"categorical_crossentropy"}, number of targets must be 2. If
+loss is \code{"binary_crossentropy"} and number of targets > 1, will flatten \code{y_true} and \code{y_pred} matrices
+to a single vector (rather than computing separate F1 scores for each class).
+}
+\examples{
+\dontshow{if (reticulate::py_module_available("tensorflow")) (if (getRversion() >= "3.4") withAutoprint else force)(\{ # examplesIf}
+
+y_true <- c(1,0,0,1,1,0,1,0,0)  
+y_pred <-  c(0.9,0.05,0.05,0.9,0.05,0.05,0.9,0.05,0.05) 
+\donttest{
+library(keras)
+f1_metric <- f1_wrapper(3L, "binary_crossentropy")
+f1_metric$update_state(y_true, y_pred)
+f1_metric$result()  
+
+
+# add metric to a model
+
+num_targets <- 1
+model <- create_model_lstm_cnn(maxlen = 20,
+                               layer_lstm = 8,
+                               bal_acc = FALSE,
+                               last_layer_activation = "sigmoid",
+                               loss_fn = "binary_crossentropy",
+                               layer_dense = c(8, num_targets))
+
+f1_metric <- f1_wrapper(num_targets, loss = model$loss)
+model \%>\% keras::compile(loss = model$loss, 
+                         optimizer = model$optimizer,
+                         metrics = c(model$metrics, f1_metric))
+}                    
+\dontshow{\}) # examplesIf}
+}