Documentation
¶
Overview ¶
Package metrics holds a library of metrics and defines
Index ¶
- Constants
- func BatchSize(data *Node) *Node
- func BinaryAccuracyGraph(_ *model.Scope, labels, predictions []*Node) *Node
- func BinaryLogitsAccuracyGraph(_ *model.Scope, labels, logits []*Node) *Node
- func SparseCategoricalAccuracyGraph(_ *model.Scope, labels, logits []*Node) *Node
- type BaseMetricGraph
- type Interface
- func NewBaseMetric(name, shortName, metricType string, metricFn BaseMetricGraph, ...) Interface
- func NewExponentialMovingAverageMetric(name, shortName, metricType string, metricFn BaseMetricGraph, ...) Interface
- func NewMovingAverageBinaryAccuracy(name, shortName string, newExampleWeight float64) Interface
- func NewMovingAverageBinaryLogitsAccuracy(name, shortName string, newExampleWeight float64) Interface
- func NewMovingAverageSparseCategoricalAccuracy(name, shortName string, newExampleWeight float64) Interface
- type MeanMetric
- func (m *MeanMetric) MetricType() string
- func (m *MeanMetric) Name() string
- func (m *MeanMetric) PrettyPrint(value *tensors.Tensor) string
- func (m *MeanMetric) Reset(scope *model.Scope)
- func (m *MeanMetric) ScopeName() string
- func (m *MeanMetric) ShortName() string
- func (m *MeanMetric) UpdateGraph(scope *model.Scope, labels, predictions []*Node) (metric *Node)
- func (m *MeanMetric) WithDynamicBatch(dynamicBatch bool) *MeanMetric
- type PrettyPrintFn
- type StreamingMedianMetric
- func (m *StreamingMedianMetric) MetricType() string
- func (m *StreamingMedianMetric) Name() string
- func (m *StreamingMedianMetric) PrettyPrint(value *tensors.Tensor) string
- func (m *StreamingMedianMetric) ReadGo() *tensors.Tensor
- func (m *StreamingMedianMetric) Reset(scope *model.Scope)
- func (m *StreamingMedianMetric) ScopeName() string
- func (m *StreamingMedianMetric) ShortName() string
- func (m *StreamingMedianMetric) UpdateGo(results *tensors.Tensor)
- func (m *StreamingMedianMetric) UpdateGraph(scope *model.Scope, labels, predictions []*Node) (metric *Node)
- func (m *StreamingMedianMetric) WithSampleSize(n int) *StreamingMedianMetric
- type UpdateGo
Constants ¶
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 ¶
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 ¶
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 ¶
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 ¶
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 ¶
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) PrettyPrint ¶
func (*MeanMetric) Reset ¶
func (m *MeanMetric) Reset(scope *model.Scope)
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 ¶
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) PrettyPrint ¶
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) 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).