# Adding custom metrics to policy

**URL:** <https://discuss.ray.io/t/adding-custom-metrics-to-policy/2319>\
**Category:** RLlib\
**Created:** [May 28, 2021, 8:26am UTC](https://discuss.ray.io/t/adding-custom-metrics-to-policy/2319 "2021-05-28T08:26:08Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![rfali](https://avatars.discourse-cdn.com/v4/letter/r/d6d6ee/32.png) [@rfali](https://discuss.ray.io/u/rfali)\
**Post date:** [May 28, 2021, 8:26am UTC](https://discuss.ray.io/t/adding-custom-metrics-to-policy/2319/1 "2021-05-28T08:26:09Z")

</div>

Hi,

I am writing a custom policy and want to add some metrics like [this](https://github.com/ray-project/ray/blob/33a69135cbb5cbeda9ce80873859b7a86681bcd2/rllib/agents/dqn/dqn_tf_policy.py#L107).

```python
self.stats = {
                "mean_q": tf.reduce_mean(q_t_selected)
}

```

The problem is, if it is an int or a float value, for example

```python
some_value = 10
some_list = [0, 0.5, 0.99]
self.stats = {
                "some_value ": some_value 
                "some_list ": some_list 
}

```

I get an error like this

```auto
File "/home/abc/miniconda3/envs/env_hdqn/lib/python3.8/site-packages/tensorflow/python/framework/ops.py", line 3814, in _as_graph_element_locked
raise TypeError("Can not convert a %s into a %s." %
 TypeError: Can not convert a float into a Tensor or Operation.`

File "/home/abc/miniconda3/envs/env_hdqn/lib/python3.8/site-packages/tensorflow/python/client/session.py", line 309, in __init__
raise TypeError('Fetch argument %r has invalid type %r, '
TypeError: Fetch argument 0.0 has invalid type <class 'float'>, must be a string or Tensor. (Can not convert a float into a Tensor or Operation.)

```

I have been successful at adding a Tensor to this dict, but not an `int` or `float`.

I have looked at the [custom\_metrics\_and\_callbacks](https://github.com/ray-project/ray/blob/master/rllib/examples/custom_metrics_and_callbacks.py) and this post [here](https://discuss.ray.io/t/entropy-of-policy-networks-output/1163/2) but this seems a little complicated for something I want. Is there a simpler way? Why is adding a value to the dict throwing this error?

Thanks you.

---

<div class="post-metadata">

**Author:** ![rfali](https://avatars.discourse-cdn.com/v4/letter/r/d6d6ee/32.png) [@rfali](https://discuss.ray.io/u/rfali)\
**Post date:** [June 1, 2021, 5:14am UTC](https://discuss.ray.io/t/adding-custom-metrics-to-policy/2319/2 "2021-06-01T05:14:32Z")

</div>

I was able to solve this issue by casting the custom metric into its appropriate type. So e.g.

```python
some_value = 10
some_string = 'abc'
some_list = [0, 0.5, 0.99]
self.stats = {
                "some_value ": some_value, 
                "some_list ": some_list,
                "some_string ": some_string,
}

```

I first tried using this method as the error mentioned it must be a **string** or Tensor.

```python
"some_value ": tf.strings.as_string(some_value)

```

which at least solved the issue but the resulting stats were byte encoded strings e.g. `b'some_value'`  
`'[b''0.000000'' b''0.500000'' b''0.99'']'`

Eventually, I saw in another rllib policy that values are being casted, so I did as below

```python
some_value = 10
some_string = 'abc'
some_list = [0, 0.5, 0.99]
self.stats = {
                "some_value ": tf.cast(some_value, tf.int64),
                "some_list ": tf.cast(some_list, tf.float64),,
                "some_string ": tf.cast(some_string, tf.string),,
}

```

which gave me the stats I was looking for.
