|
a |
|
b/man/f1_wrapper.Rd |
|
|
1 |
% Generated by roxygen2: do not edit by hand |
|
|
2 |
% Please edit documentation in R/metrics.R |
|
|
3 |
\name{f1_wrapper} |
|
|
4 |
\alias{f1_wrapper} |
|
|
5 |
\title{F1 metric} |
|
|
6 |
\usage{ |
|
|
7 |
f1_wrapper(num_targets = 2, loss = "binary_crossentropy") |
|
|
8 |
} |
|
|
9 |
\arguments{ |
|
|
10 |
\item{num_targets}{Size of model output.} |
|
|
11 |
|
|
|
12 |
\item{loss}{Loss function of model.} |
|
|
13 |
} |
|
|
14 |
\value{ |
|
|
15 |
A keras metric. |
|
|
16 |
} |
|
|
17 |
\description{ |
|
|
18 |
Compute F1 metric. If loss is \code{"categorical_crossentropy"}, number of targets must be 2. If |
|
|
19 |
loss is \code{"binary_crossentropy"} and number of targets > 1, will flatten \code{y_true} and \code{y_pred} matrices |
|
|
20 |
to a single vector (rather than computing separate F1 scores for each class). |
|
|
21 |
} |
|
|
22 |
\examples{ |
|
|
23 |
\dontshow{if (reticulate::py_module_available("tensorflow")) (if (getRversion() >= "3.4") withAutoprint else force)(\{ # examplesIf} |
|
|
24 |
|
|
|
25 |
y_true <- c(1,0,0,1,1,0,1,0,0) |
|
|
26 |
y_pred <- c(0.9,0.05,0.05,0.9,0.05,0.05,0.9,0.05,0.05) |
|
|
27 |
\donttest{ |
|
|
28 |
library(keras) |
|
|
29 |
f1_metric <- f1_wrapper(3L, "binary_crossentropy") |
|
|
30 |
f1_metric$update_state(y_true, y_pred) |
|
|
31 |
f1_metric$result() |
|
|
32 |
|
|
|
33 |
|
|
|
34 |
# add metric to a model |
|
|
35 |
|
|
|
36 |
num_targets <- 1 |
|
|
37 |
model <- create_model_lstm_cnn(maxlen = 20, |
|
|
38 |
layer_lstm = 8, |
|
|
39 |
bal_acc = FALSE, |
|
|
40 |
last_layer_activation = "sigmoid", |
|
|
41 |
loss_fn = "binary_crossentropy", |
|
|
42 |
layer_dense = c(8, num_targets)) |
|
|
43 |
|
|
|
44 |
f1_metric <- f1_wrapper(num_targets, loss = model$loss) |
|
|
45 |
model \%>\% keras::compile(loss = model$loss, |
|
|
46 |
optimizer = model$optimizer, |
|
|
47 |
metrics = c(model$metrics, f1_metric)) |
|
|
48 |
} |
|
|
49 |
\dontshow{\}) # examplesIf} |
|
|
50 |
} |