aimhubio / aim

Aim 💫 — An easy-to-use & supercharged open-source experiment tracker.
https://aimstack.io
Apache License 2.0
5.2k stars 319 forks source link

Metric values are returned out of order #2631

Open mrzv opened 1 year ago

mrzv commented 1 year ago

🐛 Bug

metric.values.values_list() returns values not in the order they were logged. Same true for values_numpy(), sparse_list(), sparse_numpy(). It's not clear how to sort them in the correct order.

To reproduce

losses = [m for m in run.metrics() if m.name == "loss"][0].values.values_list()

Expected behavior

losses appear in the order they were added.

Environment

mrzv commented 1 year ago

I found a way to fix this, but it seems counter-intuitive. For anyone looking for the same:

indices = np.argsort(metric.timestamps.values_numpy())
losses = losses[indices]
gorarakelyan commented 1 year ago

@mrzv thanks for reporting! This is super weird, will look into it.

YodaEmbedding commented 1 year ago

I found a way to fix this, but it seems counter-intuitive. [...]

I ended up using a similar workaround for out-of-order metric data when I encountered this a few months ago. It works by doing a reverse lookup on a known monotonically increasing metric such as timestamps (as you indicated). In my case, I log an "epoch" metric, which is just the value of the current epoch whenever I log my epoch metrics, and is strictly monotonically increasing, e.g. 1, 2, 3, 4, .... Here's a minimal workaround example:

# Workaround via reverse lookup.
def get_ordered_metric_values(metric, context, monotonic_metric="epoch"):
    _, unordered_metric_values = run.get_metric(metric, context).values.sparse_numpy()
    _, monotonic_metric_values = run.get_metric(monotonic_metric, context).values.sparse_numpy()
    idx = np.argsort(monotonic_metric_values)
    return unordered_metric_values[idx]