# Using different modules for a condition

**URL:** <https://discuss.pytorch.org/t/using-different-modules-for-a-condition/52267>\
**Category:** Uncategorized\
**Created:** [August 1, 2019, 5:02am UTC](https://discuss.pytorch.org/t/using-different-modules-for-a-condition/52267 "2019-08-01T05:02:46Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Eric\_Taw1](https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/eric_taw1/32/14656_2.png) [@Eric\_Taw1](https://discuss.pytorch.org/u/Eric_Taw1)\
**Post date:** [August 1, 2019, 5:02am UTC](https://discuss.pytorch.org/t/using-different-modules-for-a-condition/52267/1 "2019-08-01T05:02:47Z")

</div>

I have a bunch of values I’d like to predict, and each value belongs to a category that has its own distribution of values. Instead of creating a new model per category, I’d like to incorporate weight sharing, then have the model apply a different fully connected layer based on the category. What’s the most efficient way to implement this?

---

<div class="post-metadata">

**Author:** ![ptrblck](https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/ptrblck/32/1823_2.png) [@ptrblck](https://discuss.pytorch.org/u/ptrblck)\
**Post date:** [August 2, 2019, 8:59pm UTC](https://discuss.pytorch.org/t/using-different-modules-for-a-condition/52267/2 "2019-08-02T20:59:31Z")

</div>

Could you explain your use case a bit?  
How would you select the category? Do you have this information for each sample, i.e. training and test samples?

---

<div class="post-metadata">

**Author:** ![Eric\_Taw1](https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/eric_taw1/32/14656_2.png) [@Eric\_Taw1](https://discuss.pytorch.org/u/Eric_Taw1)\
**Post date:** [August 6, 2019, 2:22am UTC](https://discuss.pytorch.org/t/using-different-modules-for-a-condition/52267/3 "2019-08-06T02:22:03Z")

</div>

Sure. For context, this has to do with the CHAMPS competition on Kaggle.

I have a tensor of shape NxF, where N is the number of nodes in a graph and F is the length of features. Per graph, I have a set of attributes that I am regressing against, the scalar coupling constant. Each attribute involves a pair of nodes, and I am given the type of coupling for each attribute I am trying to predict. Ideally, I’d like to have a model that does convolutions on the entire graph, then use different fully connected networks per coupling type. The only weight sharing across coupling types is the convolution/pooling part.

Currently, I am doing this by concatenating every combination of 2 nodes possible, forming an NxNxF tensor, then passing that through a fully connected network that outputs 8 values, one for every coupling type (now NxNx8). Then, since I have the indices of each pair of nodes to train on, I can index that tensor with `torch.take` and get the right values. However, I’m wondering if there’s a more efficient way to do this. Doing a pairwise concatenation of every node is very memory intensive.

Do you have any suggestions? Thanks!
