Base Metric Class

tflearn.metrics.Metric (name=None)

Metric class is meant to be used by TFLearn models class. It can be first initialized with desired parameters, and a model class will build it later using the given network output and targets.

Attributes

  • tensor: Tensor. The metric tensor.

Methods

build (predictions, targets, inputs)

Build metric method, with common arguments to all Metrics.

Arguments
  • prediction: Tensor. The network to perform prediction.
  • targets: Tensor. The targets (labels).
  • inputs: Tensor. The input data.

get_tensor (self)

Get the metric tensor.

Returns

The metric Tensor.


Accuracy

tflearn.metrics.Accuracy (name=None)

Computes the model accuracy. The target predictions are assumed to be logits.

If the predictions tensor is 1D (ie shape [?], or [?, 1]), then the labels are assumed to be binary (cast as float32), and accuracy is computed based on the average number of equal binary outcomes, thresholding predictions on logits > 0.

Otherwise, accuracy is computed based on categorical outcomes, and assumes the inputs (both the model predictions and the labels) are one-hot encoded. tf.argmax is used to obtain categorical predictions, for equality comparison.

Examples

# To be used with TFLearn estimators
acc = Accuracy()
regression = regression(net, metric=acc)

Arguments

  • name: The name to display.

Top-k

tflearn.metrics.Top_k (k=1, name=None)

Computes Top-k mean accuracy (whether the targets are in the top 'K' predictions).

Examples

# To be used with TFLearn estimators
top5 = Top_k(k=5)
regression = regression(net, metric=top5)

Arguments

  • k: int. Number of top elements to look at for computing precision.
  • name: The name to display.

Standard Error

tflearn.metrics.R2 (name=None)

Computes coefficient of determination. Useful to evaluate a linear regression.

Examples

# To be used with TFLearn estimators
r2 = R2()
regression = regression(net, metric=r2)

Arguments

  • name: The name to display.

Weighted Standard Error

tflearn.metrics.WeightedR2 (name=None)

Computes coefficient of determination. Useful to evaluate a linear regression.

Examples

# To be used with TFLearn estimators
weighted_r2 = WeightedR2()
regression = regression(net, metric=weighted_r2)

Arguments

  • name: The name to display.

accuracy_op

tflearn.metrics.accuracy_op (predictions, targets)

An op that calculates mean accuracy, assuming predictiosn are targets are both one-hot encoded.

Examples

input_data = placeholder(shape=[None, 784])
y_pred = my_network(input_data) # Apply some ops
y_true = placeholder(shape=[None, 10]) # Labels
acc_op = accuracy_op(y_pred, y_true)

# Calculate accuracy by feeding data X and labels Y
accuracy = sess.run(acc_op, feed_dict={input_data: X, y_true: Y})

Arguments

  • predictions: Tensor.
  • targets: Tensor.

Returns

Float. The mean accuracy.


binary_accuracy_op

tflearn.metrics.binary_accuracy_op (predictions, targets)

An op that calculates mean accuracy, assuming predictions are logits, and targets are binary encoded (and represented as int32).

Examples

input_data = placeholder(shape=[None, 784])
y_pred = my_network(input_data) # Apply some ops
y_true = placeholder(shape=[None, 10]) # Labels
acc_op = binary_accuracy_op(y_pred, y_true)

# Calculate accuracy by feeding data X and labels Y
binary_accuracy = sess.run(acc_op, feed_dict={input_data: X, y_true: Y})

Arguments

  • predictions: Tensor of float type.
  • targets: Tensor of float type.

Returns

Float. The mean accuracy.


top_k_op

tflearn.metrics.top_k_op (predictions, targets, k=1)

An op that calculates top-k mean accuracy.

Examples

input_data = placeholder(shape=[None, 784])
y_pred = my_network(input_data) # Apply some ops
y_true = placeholder(shape=[None, 10]) # Labels
top3_op = top_k_op(y_pred, y_true, 3)

# Calculate Top-3 accuracy by feeding data X and labels Y
top3_accuracy = sess.run(top3_op, feed_dict={input_data: X, y_true: Y})

Arguments

  • predictions: Tensor.
  • targets: Tensor.
  • k: int. Number of top elements to look at for computing precision.

Returns

Float. The top-k mean accuracy.


r2_op

tflearn.metrics.r2_op (predictions, targets)

An op that calculates the standard error.

Examples

input_data = placeholder(shape=[None, 784])
y_pred = my_network(input_data) # Apply some ops
y_true = placeholder(shape=[None, 10]) # Labels
stderr_op = r2_op(y_pred, y_true)

# Calculate standard error by feeding data X and labels Y
std_error = sess.run(stderr_op, feed_dict={input_data: X, y_true: Y})

Arguments

  • predictions: Tensor.
  • targets: Tensor.

Returns

Float. The standard error.


weighted_r2_op

tflearn.metrics.weighted_r2_op (predictions, targets, inputs)

An op that calculates the standard error.

Examples

input_data = placeholder(shape=[None, 784])
y_pred = my_network(input_data) # Apply some ops
y_true = placeholder(shape=[None, 10]) # Labels
stderr_op = weighted_r2_op(y_pred, y_true, input_data)

# Calculate standard error by feeding data X and labels Y
std_error = sess.run(stderr_op, feed_dict={input_data: X, y_true: Y})

Arguments

  • predictions: Tensor.
  • targets: Tensor.
  • inputs: Tensor.

Returns

Float. The standard error.