nn.ModuleList vs nn.Parameter[] on Global-Local Model

a_garciadecorral: <p>Hi,</p>
<p>I’m trying to create an architecture to model multiple time series at the same time. I want to generate a simple Global-Local Model.</p>
<p>Let’s assume for simplicity that I have two time series TS_1 and TS_2 and we assume that both can be modeled as:</p>
<p>y = trend + seasonality + noise</p>
<p>I want to use the same parameters to model seasonality for both TS_1 and TS_2, but I want the parameters defining TS_1 and TS_2 to be different, so:</p>
<p>y_i = trend_global + seasonality_i + noise</p>
<p>Again for simplification, imaging the trend is made of 3 parameters and the seasonality from 6 parameters, which I then combine with really easy Math Operations to create the trend and seasonality components.</p>
<p>I define the trend parameters as nn.Parameter[dim=[3]] (and, as I mentioned, then I do some math ops to create the trend component)</p>
<p>For the seasonality parameters I also need to define the parameters, and I tried three different approaches</p>
<ul>
<li>Method 1: nn.ParameterDict(TS_1: nn.Parameter[6],TS_2: nn.Parameter[6])</li>
<li>Method 2: ModuleList( nn.Parameter[6], nn.Parameter[6])</li>
<li>Method 3: nn.Parameter[2, 6]</li>
</ul>
<p>For a particular batch of data I need to identify each sample’s TS.  To do that, in the DataLoader we will have the tuple (data_inputs, metadata). So, in the forward method,  we need to identify the local seasonality parameters, to manipulate the input data consistently.  For each method we get the seasonality parameters of the batch in the following way:</p>
<ul>
<li>Method 1: I apply a vectorised function, using the metadata as key over the nn.ParameterDictionary across all samples of the batch. Then I torch.stack the output.</li>
<li>Method 2: I apply a vectorised function, using the metadata as index over the nn.ModuleList across all samples of the batch. Then I torch.stack the output.</li>
<li>Method 3: Using hot_encoding I can use torch.multiply and by doing array multiplication I get the parameters of each batch.</li>
</ul>
<p>Method 1 and 2 have a computation disadvantage. The computation time is slower because the Autograd Graph is as wide as the batch size? (see picture attached(*)). I guess this is because how torch.stack behaves…?</p>
<p>Method 3 has a design disadvantage. If instead of nn.Parameter[6] (+ simple Math Ops manipulations) , I want to model seasonality with a more complex class like nn.Linear… I have to do complex manipulations to behave as nn.Linear…. And becomes unfeasible at some point.</p>
<p>My question is if you can think of any solution which brings the best out of each method?<br>
I guess the bottleneck is having to use a vectorised function on the forward method… I guess what I was looking for is a MathOperation from pytorch which allows doing something like:</p>
<p>“tensor = torch.indexation(Modulelist, tensor_indexes)”</p>
<p>Thanks a lot for your time, and please let me know if you have any questions <img src="https://discuss.pytorch.org/images/emoji/apple/slight_smile.png?v=12" title=":slight_smile:" class="emoji" alt=":slight_smile:" loading="lazy" width="20" height="20"></p>
<p>(*) in the file  attached we are actually modeling trend and seasonality globally and AutoRegressive component locally, for 8 Time Series.<br>
<div class="lightbox-wrapper"><a class="lightbox" href="https://discuss.pytorch.org/uploads/default/original/3X/d/a/da1398bb9c37df8f41b4b18d5ca124a9cc8681f8.jpeg" data-download-href="https://discuss.pytorch.org/uploads/default/da1398bb9c37df8f41b4b18d5ca124a9cc8681f8" title="fig"><img src="https://discuss.pytorch.org/uploads/default/optimized/3X/d/a/da1398bb9c37df8f41b4b18d5ca124a9cc8681f8_2_690x93.jpeg" alt="fig" data-base62-sha1="v7c4ARLC3E2N0UJfctkv8Jcnrja" width="690" height="93" srcset="https://discuss.pytorch.org/uploads/default/optimized/3X/d/a/da1398bb9c37df8f41b4b18d5ca124a9cc8681f8_2_690x93.jpeg, https://discuss.pytorch.org/uploads/default/optimized/3X/d/a/da1398bb9c37df8f41b4b18d5ca124a9cc8681f8_2_1035x139.jpeg 1.5x, https://discuss.pytorch.org/uploads/default/optimized/3X/d/a/da1398bb9c37df8f41b4b18d5ca124a9cc8681f8_2_1380x186.jpeg 2x" data-dominant-color="F5F5F5"><div class="meta"><svg class="fa d-icon d-icon-far-image svg-icon" aria-hidden="true"><use href="#far-image"></use></svg><span class="filename">fig</span><span class="informations">1920×261 32.1 KB</span><svg class="fa d-icon d-icon-discourse-expand svg-icon" aria-hidden="true"><use href="#discourse-expand"></use></svg></div></a></div></p>