# Config for layers and neurons for MLP classifier

**URL:** <https://discuss.ray.io/t/config-for-layers-and-neurons-for-mlp-classifier/1231>\
**Category:** Ray Tune\
**Created:** [March 11, 2021, 9:36pm UTC](https://discuss.ray.io/t/config-for-layers-and-neurons-for-mlp-classifier/1231 "2021-03-11T21:36:28Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![tkmamidi](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.ray.io/tkmamidi/32/588_2.png) [@tkmamidi](https://discuss.ray.io/u/tkmamidi)\
**Post date:** [March 11, 2021, 9:36pm UTC](https://discuss.ray.io/t/config-for-layers-and-neurons-for-mlp-classifier/1231/1 "2021-03-11T21:36:28Z")

</div>

Can someone help me with a config snippet on how I can custom create a search space for layers and neurons in Neural network. So far I have this,

`"hidden_layer_sizes": tune.sample_from(lambda list: [list.append(tune.randint(1, 100)) for i in range(tune.randint(1, 50))]),`

Thanks in advance!

---

<div class="post-metadata">

**Author:** ![Calvin](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.ray.io/calvin/32/1130_2.png) [@Calvin](https://discuss.ray.io/u/Calvin)\
**Post date:** [June 13, 2021, 2:42pm UTC](https://discuss.ray.io/t/config-for-layers-and-neurons-for-mlp-classifier/1231/2 "2021-06-13T14:42:34Z")

</div>

I am running into the same problem…

I tried to define the total number of Neuron to use and then split them using a custom integer partitioning function for layer definition, and then pass it into the network using a **list of numbers** :

```auto
config = {"lr": tune.loguniform(1e-4, 1e-1),
          "batch_size": tune.choice([2, 4, 8, 16]),
          "h_total": tune.choice([10, 20, 30]),
          "h_branch": tune.sample_from(lambda spec: [[N_FEATURES] + # Append Input Layer as Number of Features
                                                      split_sampling(num_ele = h_seg_ele, 
                                                                     n_min = min_neuron_per_layer,
                                                                     out_dim = None) for h_seg_ele in spec.config.h_total ]),
         }

```

But this doesn’t seem to work, it gives me error when I try to refer to it in the model:

```auto
model = Net(N_SEGMENT, config["h_branch"]).to(device)

```

it returns error:

```auto
----> 9 self.hidden.append(NN_Branch(Layers[s_id]))
     10 

TypeError: 'Function' object is not subscriptable

```

It seems like Ray Tune does not allow the parameters to be multidimensional… but that’s the only way to do it when you have a flexible combination of layer and neurons. Could someone shed some lights onto this?

---

<div class="post-metadata">

**Author:** ![Calvin](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.ray.io/calvin/32/1130_2.png) [@Calvin](https://discuss.ray.io/u/Calvin)\
**Post date:** [June 14, 2021, 10:07am UTC](https://discuss.ray.io/t/config-for-layers-and-neurons-for-mlp-classifier/1231/3 "2021-06-14T10:07:53Z")

</div>

(Update)  
I tried to get around the problem by creating a list of the NN architecture, and use a list of indices as the config parameters for Ray Tune. But it also failed…

```auto
config = {"lr": tune.loguniform(1e-4, 1e-1),
          "batch_size": tune.choice([2, 4, 8, 16]),
          "h_branch": tune.grid_search([1, 2, 3]),
         }
model = Net(N_SEGMENT, architecture_list, config["h_branch"])

```

Then it shows me KeyError…

`> KeyError: "None of [Index(['grid_search'], dtype='object')] are in the [index]"`

Anybody can give me some help on this…?
