Skip to content
This repository was archived by the owner on Jul 7, 2023. It is now read-only.

add a metrics for calculating pearson correlation coefficient #1274

Merged
merged 8 commits into from
Dec 5, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions tensor2tensor/utils/metrics.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ class Metrics(object):
APPROX_BLEU = "approx_bleu_score"
RMSE = "rmse"
LOG_POISSON = "log_poisson"
PEARSON = "pearson"
R2 = "r_squared"
ROUGE_2_F = "rouge_2_fscore"
ROUGE_L_F = "rouge_L_fscore"
Expand Down Expand Up @@ -741,6 +742,20 @@ def from_characters(raw, lookup_):
return distance / reference_length, reference_length


def pearson_correlation_coefficient(predictions, labels, weights_fn=None):
"""Calculate pearson correlation coefficient.

Args:
predictions: The raw predictions.
labels: The actual labels.
weights_fn: Weighting function.

Returns:
The pearson correlation coefficient.
"""
_, pearson = tf.contrib.metrics.streaming_pearson_correlation(predictions, labels)
return pearson, tf.constant(1.0)

# Metrics are functions that take predictions and labels and return
# a tensor of metrics and a tensor of weights.
# If the function has "features" as an argument, it will receive the whole
Expand All @@ -756,6 +771,7 @@ def from_characters(raw, lookup_):
Metrics.APPROX_BLEU: bleu_hook.bleu_score,
Metrics.RMSE: padded_rmse,
Metrics.LOG_POISSON: padded_log_poisson,
Metrics.PEARSON: pearson_correlation_coefficient,
Metrics.R2: padded_variance_explained,
Metrics.ROUGE_2_F: rouge.rouge_2_fscore,
Metrics.ROUGE_L_F: rouge.rouge_l_fscore,
Expand Down
15 changes: 15 additions & 0 deletions tensor2tensor/utils/metrics_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -319,6 +319,21 @@ def testMultilabelMatch3(self):
actual = session.run(a)
self.assertAlmostEqual(actual, expected, places=6)

def testPearsonCorrelationCoefficient(self):
predictions = np.random.rand(12, 1)
targets = np.random.rand(12, 1)

expected = np.corrcoef(np.squeeze(predictions), np.squeeze(targets))[0][1]
with self.test_session() as session:
pearson, _ = metrics.pearson_correlation_coefficient(
tf.constant(predictions, dtype=tf.float32),
tf.constant(targets, dtype=tf.float32))
session.run(tf.global_variables_initializer())
session.run(tf.local_variables_initializer())
actual = session.run(pearson)
print(actual)
print(expected)
self.assertAlmostEqual(actual, expected)

if __name__ == '__main__':
tf.test.main()