# \[Tune Class API + Pytorch\] Custom metrics are not properly passed to ExperimentAnalysis and Tensorboard

**URL:** <https://discuss.ray.io/t/tune-class-api-pytorch-custom-metrics-are-not-properly-passed-to-experimentanalysis-and-tensorboard/275>\
**Category:** Ray Tune\
**Created:** [December 17, 2020, 11:03am UTC](https://discuss.ray.io/t/tune-class-api-pytorch-custom-metrics-are-not-properly-passed-to-experimentanalysis-and-tensorboard/275 "2020-12-17T11:03:41Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![lena-schwert](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.ray.io/lena-schwert/32/170_2.png) [@lena-schwert](https://discuss.ray.io/u/lena-schwert)\
**Post date:** [December 17, 2020, 11:03am UTC](https://discuss.ray.io/t/tune-class-api-pytorch-custom-metrics-are-not-properly-passed-to-experimentanalysis-and-tensorboard/275/1 "2020-12-17T11:03:41Z")

</div>

I am using `step()`to return my losses like this:

> ```
> return {'train_epoch_loss': train_epoch_loss.detach().numpy(),
> 'validate_epoch_loss': validate_epoch_loss.detach().numpy()}
> 
> ```

While the experiment runs, both metrics appear in the CLIReporter together with the standard metrics such as the hyperparameters or `time_this_iter_s`.

Once the trials terminate, both metrics also appear in `progress.csv` and `result.json`, however they do **not** appear in “Scalars” or “Hparams” in Tensorboard (only in “Distributions”) and calling `analysis.get_best_config(metric = 'validate_epoch_loss', mode = 'min', scope = 'last'))` only returns:

`WARNING experiment_analysis.py:557 -- Could not find best trial. Did you pass the correct metric parameter?`

Any idea how to resolve this?

Ray version: 1.0.1.post1  
PyTorch version: 1.7.0

---

<div class="post-metadata">

**Author:** ![smorad](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.ray.io/smorad/32/272_2.png) [@smorad](https://discuss.ray.io/u/smorad)\
**Post date:** [February 15, 2021, 5:02pm UTC](https://discuss.ray.io/t/tune-class-api-pytorch-custom-metrics-are-not-properly-passed-to-experimentanalysis-and-tensorboard/275/2 "2021-02-15T17:02:10Z")

</div>

Possibly related, in my `Torchv2CustomModel` subclass, `metrics` does not appear to work:

```auto
class MyModel(Torchv2CustomModel):
  bork = 1

def metrics(self):
  return {'bork': self.bork}

```

Yet there is no `bork` in tensorboard.

---

<div class="post-metadata">

**Author:** ![lena-schwert](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.ray.io/lena-schwert/32/170_2.png) [@lena-schwert](https://discuss.ray.io/u/lena-schwert)\
**Post date:** [March 29, 2021, 10:08am UTC](https://discuss.ray.io/t/tune-class-api-pytorch-custom-metrics-are-not-properly-passed-to-experimentanalysis-and-tensorboard/275/3 "2021-03-29T10:08:23Z")

</div>

I found my mistake, it was due to the type of my variable `validate_epoch_loss`. It was a Numpy array and not “just a number” which is apparently internally difficult for ray. Once I accessed the array entry with `myarray.item()`, ray’s functions accept it as a metric and correctly identify the best config.
