Short form for constructing a MeasureTorch.
See the Custom Learning Problems article for how to create and use such measures.
Arguments
- id
(
character(1))
The id of the measure.- fun
(
function())
The scoring function. It receives whichever of the argumentstruth,response,prob,se,lazy_tensor,prediction,task,learner,train_setandweightsit declares, and must return a single number. Asking for anything else is an error. An argument that the prediction does not have –weightson a task without aweights_measurecolumn, orprobon a response-only prediction – is not passed at all, so a default declared for it is what the function sees.weightsare also withheld when the measure's$use_weightsis set to"ignore", so give the argument a default if the measure should score with and without weights, and none if it cannot do without them – asking for weights that do not exist is an error, not a score.- minimize
(
logical(1))
Whether a smaller score is better.NA(default) means the direction is unknown.- range
(
numeric(2))
The range of possible scores.- predict_type
(
character(1))
The predict type the measure requires:"response"(default),"prob","se"or"lazy_tensor". A measure asking for one it did not declare here still receives it, if the prediction has it.- properties
(
character())
Properties of the measure, seeMeasure. The"requires_task","requires_learner","requires_train_set"and"weights"properties are added automatically whenfundeclares the corresponding argument."requires_model"is not: alearnerargument only says that the learner object is needed, andmlr3hands that over even when the model was not stored – after aresample()withstore_models = FALSE,learner$networkis thenNULLand a measure reading it scores whatever an empty model gives. Passproperties = "requires_model"yourself whenever the measure reaches for the trained network, so thatmlr3refuses to score instead.- label
(
character(1))
The label of the measure.- obs_loss
(
function()orNULL)
The per-observation loss. Declared likefun, except thattrain_setis not available here, and if specified adds the"obs_loss"property. It must return one number per observation: a multi-target loss reduces over the targets (rowMeans()), not over the observations (mean()), and returning a single number is an error rather than a column of that number repeated.
Examples
m = msr_torch("hamming", function(truth, response) mean(as.matrix(truth) != response))
m$properties
#> character(0)
# with a per-observation loss
m = msr_torch("mse", function(truth, response) mean((truth - response)^2),
obs_loss = function(truth, response) (truth - response)^2)
m$properties
#> [1] "obs_loss"