# Tune.choice for list of lists

**URL:** <https://discuss.ray.io/t/tune-choice-for-list-of-lists/3024>\
**Category:** Ray Tune\
**Created:** [July 26, 2021, 6:49pm UTC](https://discuss.ray.io/t/tune-choice-for-list-of-lists/3024 "2021-07-26T18:49:06Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Stuart\_Siegel](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.ray.io/stuart_siegel/32/1331_2.png) [@Stuart\_Siegel](https://discuss.ray.io/u/Stuart_Siegel)\
**Post date:** [July 26, 2021, 6:49pm UTC](https://discuss.ray.io/t/tune-choice-for-list-of-lists/3024/1 "2021-07-26T18:49:06Z")

</div>

As of v1.3 the following fails with the below error. Can anyone suggest a workaround? Thank you.

```auto
In [1]: from ray import tune
In [2]: c = tune.choice([[1,2],[3,4]])
In [3]: c.sample()
---------------------------------------------------------------------------
ValueError Traceback (most recent call last)
<ipython-input-3-b421c6722185> in <module>
----> 1 c.sample()

~/miniconda3/envs/fctk/lib/python3.7/site-packages/ray/tune/sample.py in sample(self, spec, size)                                                                             
     45 def sample(self, spec=None, size=1):
     46 sampler = self.get_sampler()
---> 47 return sampler.sample(self, spec=spec, size=size)
     48 
     49 def is_grid(self):

~/miniconda3/envs/fctk/lib/python3.7/site-packages/ray/tune/sample.py in sample(self, domain, spec, size)                                                                     
    295 size: int = 1):
    296 
--> 297 items = np.random.choice(domain.categories, size=size).tolist()
    298 return items if len(items) > 1 else domain.cast(items[0])
    299 

mtrand.pyx in numpy.random.mtrand.RandomState.choice()

ValueError: a must be 1-dimensional

```

---

<div class="post-metadata">

**Author:** ![rliaw](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.ray.io/rliaw/32/24_2.png) [@rliaw](https://discuss.ray.io/u/rliaw)\
**Post date:** [July 27, 2021, 6:26pm UTC](https://discuss.ray.io/t/tune-choice-for-list-of-lists/3024/2 "2021-07-27T18:26:29Z")

</div>

> [@Stuart\_Siegel](#):
>
> `c = tune.choice([[1,2],[3,4]])`

One workaround is just to use an index instead:

```auto
mapping = {"a": [1, 2], "b": [3, 4]}
tune.choice(["a", "b"])

```

---

<div class="post-metadata">

**Author:** ![rliaw](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.ray.io/rliaw/32/24_2.png) [@rliaw](https://discuss.ray.io/u/rliaw)\
**Post date:** [July 27, 2021, 6:26pm UTC](https://discuss.ray.io/t/tune-choice-for-list-of-lists/3024/3 "2021-07-27T18:26:52Z")

</div>

@Stuart_Siegel we should try to fix this though! would you be willing to help open an issue on Github for me?

---

<div class="post-metadata">

**Author:** ![Stuart\_Siegel](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.ray.io/stuart_siegel/32/1331_2.png) [@Stuart\_Siegel](https://discuss.ray.io/u/Stuart_Siegel)\
**Post date:** [July 27, 2021, 6:49pm UTC](https://discuss.ray.io/t/tune-choice-for-list-of-lists/3024/4 "2021-07-27T18:49:54Z")

</div>

Thanks @rliaw .

I was just about to post that I found a workaround by defining my own sampler using the older random.choice method that was used in ray\<v1.3.

For reference for others who might be interested:

```auto
class LegacyCategoricalSampler(Uniform):
    def sample(self, domain: "Categorical", spec, size: int = 1):
        items = random.choice(domain.categories)
        return items if len(items) > 1 else domain.cast(items[0])

c = tune.choice([[1,2],[3,4]])
c.set_sampler(class LegacyCategoricalSampler(), allow_override=True)
c.sample() # works as before (returns either [1,2] or [3.4]

```
