metric

package
v0.28.21 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Oct 31, 2026 License: Apache-2.0 Imports: 15 Imported by: 2 modules, 13 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Overview

Package metrics holds a library of metrics and defines

Index

Constants

View Source
const (
	// LossMetricType is the type of loss metrics.
	// Used to aggregate metrics of the same  type in the same plot.
	LossMetricType = "loss"

	// AccuracyMetricType is the type of loss metrics.
	// Used to aggregate metrics of the same  type in the same plot.
	AccuracyMetricType = "accuracy"

	// Scope used to store metrics helper variables (e.g.: running averages).
	Scope = "metrics"
)

Variables

This section is empty.

Functions

func BatchSize

func BatchSize(data *Node) *Node

BatchSize returns the batch size (assumed first dimension) of the data node, casting it to the same dtype as data.

func BinaryAccuracyGraph

func BinaryAccuracyGraph(_ *model.Scope, labels, predictions []*Node) *Node

BinaryAccuracyGraph can be used in combination with New*Metric functions to build metrics for binary accuracy. It assumes predictions are probabilities, that labels are `{0, 1}`, and those predictions and labels have the same shape and dtype.

func BinaryLogitsAccuracyGraph

func BinaryLogitsAccuracyGraph(_ *model.Scope, labels, logits []*Node) *Node

BinaryLogitsAccuracyGraph can be used in combination with New*Metric functions to build metrics for binary accuracy for logit. Notice 0s are considered a miss. It assumes predictions are logits, that labels are {0, 1} and that predictions and labels have the same size and dtype. The shape may be different (e.g.: `[batch_size, 1]` and `[batch_size]`), they will be reshaped to the logits shape before the accuracy is calculated.

labels is converted to predictions dtype, and it's expected to convert to 1.0 (for true) or 0.0 for false. So booleans should work, as an int type that is 0 or 1.

func SparseCategoricalAccuracyGraph

func SparseCategoricalAccuracyGraph(_ *model.Scope, labels, logits []*Node) *Node

SparseCategoricalAccuracyGraph returns the accuracy -- fraction of times argmax(logits) is the true label. It works for both probabilities or logits. Ties are considered misses. Labels is expected to be some integer type. And the returned dtype is the same as logits.

Weights and mask can be given in the `labels` slice, following the labels themselves and they will be accounted for.

Types

type BaseMetricGraph

type BaseMetricGraph func(scope *model.Scope, labels, predictions []*Node) *Node

BaseMetricGraph is a graph building function of any metric that can be calculated stateless, without the need for any model. It should return a scalar, the mean for the given batch.

type Interface

type Interface interface {
	// Name of the metric.
	Name() string

	// ShortName is a shortened version of the name (preferably a few characters) to display in progress bars or
	// similar UIs.
	ShortName() string

	// ScopeName used to store state: a combination of name and something unique.
	ScopeName() string

	// MetricType is a key for metrics that share the same quantity or semantics. Eg.:
	// "Moving-Average-Accuracy" and "Batch-Accuracy" would both have the same
	// "accuracy" metric type, and for instance, can be displayed on the same plot, sharing
	// the Y-axis.
	MetricType() string

	// UpdateGraph builds a graph that takes as input the predictions (or logits) and labels and
	// outputs the resulting metric (a scalar).
	//
	// Weights and masks are by convention given as extra `*Node` values in the labels slice,
	// when supported by the metrics.
	UpdateGraph(scope *model.Scope, labels, predictions []*Node) (metric *Node)

	// PrettyPrint is used to pretty-print a metric value, usually in a short form.
	PrettyPrint(value *tensors.Tensor) string

	// Reset metrics internal counters when starting a new evaluation.
	// Notice this may be called before UpdateGraph, and the metric should handle this without errors.
	Reset(scope *model.Scope)
}

Interface for a Metric.

func NewBaseMetric

func NewBaseMetric(name, shortName, metricType string, metricFn BaseMetricGraph, pPrintFn PrettyPrintFn) Interface

NewBaseMetric creates a stateless metric from any BaseMetricGraph function, it will return the metric calculated solely on the last batch. pPrintFn can be left as nil, and a default will be used.

func NewExponentialMovingAverageMetric

func NewExponentialMovingAverageMetric(
	name, shortName, metricType string,
	metricFn BaseMetricGraph,
	pPrintFn PrettyPrintFn,
	newExampleWeight float64,
) Interface

NewExponentialMovingAverageMetric creates a metric from any BaseMetricGraph function. It takes new examples with the given weight (newExampleWeight), and decays the reset to 1-newExampleWeight.

A typical value of newExampleWeight is 0.01, the smaller the value, the slower the moving average moves. pPrintFn can be left as nil, and a default will be used.

This doesn't have a set prior, it will start being a normal average until there are enough terms, and it becomes an exponential moving average.

func NewMovingAverageBinaryAccuracy

func NewMovingAverageBinaryAccuracy(name, shortName string, newExampleWeight float64) Interface

NewMovingAverageBinaryAccuracy returns a new binary accuracy metric with the given names. A typical value of newExampleWeight is 0.01, the smaller the value, the slower the moving average moves.

func NewMovingAverageBinaryLogitsAccuracy

func NewMovingAverageBinaryLogitsAccuracy(name, shortName string, newExampleWeight float64) Interface

NewMovingAverageBinaryLogitsAccuracy returns a new binary accuracy metric with the given names. A typical value of newExampleWeight is 0.01, the smaller the value, the slower the moving average moves.

func NewMovingAverageSparseCategoricalAccuracy

func NewMovingAverageSparseCategoricalAccuracy(name, shortName string, newExampleWeight float64) Interface

NewMovingAverageSparseCategoricalAccuracy returns a new sparse categorical accuracy metric with the given names. The accuracy is defined as the fraction of times argmax(logits) is the true label. It works for both probabilities or logits. Ties are considered misses. Labels is expected to be some integer type. And the returned dtype is the same as logits. A typical value of newExampleWeight is 0.01, the smaller the value, the slower the moving average moves.

type MeanMetric

type MeanMetric struct {
	// contains filtered or unexported fields
}

MeanMetric implements a metric that keeps the mean of a metric.

func NewMeanBinaryAccuracy

func NewMeanBinaryAccuracy(name, shortName string) *MeanMetric

NewMeanBinaryAccuracy returns a new binary accuracy metric with the given names.

func NewMeanBinaryLogitsAccuracy

func NewMeanBinaryLogitsAccuracy(name, shortName string) *MeanMetric

NewMeanBinaryLogitsAccuracy returns a new binary accuracy metric with the given names.

func NewMeanMetric

func NewMeanMetric(
	name, shortName, metricType string,
	metricFn BaseMetricGraph,
	prettyPrintFn PrettyPrintFn,
) *MeanMetric

NewMeanMetric creates a metric from any BaseMetricGraph function.

It assumes the batch size (to weight the mean with each new result) is given by the first dimension of the labels' node. If you want all batches to count the same, sed WithDynamicBatch(false).

`prettyPrintFn` can be left as nil, and a default will be used.

func NewSparseCategoricalAccuracy

func NewSparseCategoricalAccuracy(name, shortName string) *MeanMetric

NewSparseCategoricalAccuracy returns a new sparse categorical accuracy metric with the given names. The accuracy is defined as the fraction of times argmax(logits) is the true label. It works for both probabilities or logits. Ties are considered misses. Labels is expected to be some integer type. And the returned dtype is the same as logits.

func (*MeanMetric) MetricType

func (m *MeanMetric) MetricType() string

func (*MeanMetric) Name

func (m *MeanMetric) Name() string

func (*MeanMetric) PrettyPrint

func (m *MeanMetric) PrettyPrint(value *tensors.Tensor) string

func (*MeanMetric) Reset

func (m *MeanMetric) Reset(scope *model.Scope)

func (*MeanMetric) ScopeName

func (m *MeanMetric) ScopeName() string

func (*MeanMetric) ShortName

func (m *MeanMetric) ShortName() string

func (*MeanMetric) UpdateGraph

func (m *MeanMetric) UpdateGraph(scope *model.Scope, labels, predictions []*Node) (metric *Node)

func (*MeanMetric) WithDynamicBatch

func (m *MeanMetric) WithDynamicBatch(dynamicBatch bool) *MeanMetric

WithDynamicBatch sets whether the mean should attempt to weight each batch by its size, defined as the dimension of the first axis. Default is true.

If set to false, each batch counts as 1, independent of its shape.

type PrettyPrintFn

type PrettyPrintFn func(value *tensors.Tensor) string

PrettyPrintFn is a function to convert a metric value to a string.

type StreamingMedianMetric

type StreamingMedianMetric struct {
	// contains filtered or unexported fields
}

StreamingMedianMetric implements a metric that keeps an approximate median of a metric from a streaming input.

func NewMedianMetric

func NewMedianMetric(
	name, shortName, metricType string,
	metricFn BaseMetricGraph,
	prettyPrintFn PrettyPrintFn,
) *StreamingMedianMetric

NewMedianMetric creates a streaming median metric from any BaseMetricGraph function.

BaseMetricGraph must return a scalar value (one value is consumed at a time). If you are processing batches at a time (and not a batch of size 1), this will return a median of the batch means. This may be a reasonable approximation, but something to be mindful.

`prettyPrintFn` can be left as nil, and a default will be used.

func (*StreamingMedianMetric) MetricType

func (m *StreamingMedianMetric) MetricType() string

func (*StreamingMedianMetric) Name

func (m *StreamingMedianMetric) Name() string

func (*StreamingMedianMetric) PrettyPrint

func (m *StreamingMedianMetric) PrettyPrint(value *tensors.Tensor) string

func (*StreamingMedianMetric) ReadGo

func (m *StreamingMedianMetric) ReadGo() *tensors.Tensor

func (*StreamingMedianMetric) Reset

func (m *StreamingMedianMetric) Reset(scope *model.Scope)

Reset will delete all related variables to the streaming median: they will be recreated again at the start of an update.

func (*StreamingMedianMetric) ScopeName

func (m *StreamingMedianMetric) ScopeName() string

func (*StreamingMedianMetric) ShortName

func (m *StreamingMedianMetric) ShortName() string

func (*StreamingMedianMetric) UpdateGo

func (m *StreamingMedianMetric) UpdateGo(results *tensors.Tensor)

func (*StreamingMedianMetric) UpdateGraph

func (m *StreamingMedianMetric) UpdateGraph(scope *model.Scope, labels, predictions []*Node) (metric *Node)

func (*StreamingMedianMetric) WithSampleSize

func (m *StreamingMedianMetric) WithSampleSize(n int) *StreamingMedianMetric

WithSampleSize configures the default number of random samples to keep to estimate the median.

type UpdateGo

type UpdateGo interface {
	Interface

	// UpdateGo is called for each batch with the resulting metric returned by UpdateGraph()
	UpdateGo(value *tensors.Tensor)

	// ReadGo can be called whenever one wants the current value of the target metric.
	// Typically it is called after UpdateGo is called for all batches of the dataset.
	ReadGo() *tensors.Tensor
}

UpdateGo interface can be implemented by metrics that prefer to update their values during evaluation of a dataset in Go (as opposed to using a computation graph).

These may be easier for calculating things like running quantiles where it's simpler to handle one element at a time, as opposed to process them as vectors (tensors).

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL