philipperemy / n-beats

Keras/Pytorch implementation of N-BEATS: Neural basis expansion analysis for interpretable time series forecasting.
MIT License
855 stars 163 forks source link

GENERIC_BLOCK usage generating TypeError #43

Closed jroakes closed 3 years ago

jroakes commented 3 years ago

Using Google Colab and nbeats-pytorch==1.3.1, when setting up the NBeatNet model with the following parameters:

        net = NBeatsNet(
            stack_types=[NBeatsNet.GENERIC_BLOCK, NBeatsNet.GENERIC_BLOCK],
            forecast_length=60,
            thetas_dims=[7, 8],
            nb_blocks_per_stack=3,
            backcast_length=60,
            hidden_layer_units=128,
            share_weights_in_stack=False,
            device=torch.device("cuda")
        )

I am receiving the following error:

/usr/local/lib/python3.6/dist-packages/nbeats_pytorch/model.py in __init__(self, device, stack_types, nb_blocks_per_stack, forecast_length, backcast_length, thetas_dims, share_weights_in_stack, hidden_layer_units, nb_harmonics)
     34         print(f'| N-Beats')
     35         for stack_id in range(len(self.stack_types)):
---> 36             self.stacks.append(self.create_stack(stack_id))
     37         self.parameters = nn.ParameterList(self.parameters)
     38         self.to(self.device)

/usr/local/lib/python3.6/dist-packages/nbeats_pytorch/model.py in create_stack(self, stack_id)
     48             else:
     49                 block = block_init(self.hidden_layer_units, self.thetas_dim[stack_id],
---> 50                                    self.device, self.backcast_length, self.forecast_length, self.nb_harmonics)
     51                 self.parameters.extend(block.parameters())
     52             print(f'     | -- {block}')

TypeError: __init__() takes from 4 to 6 positional arguments but 7 were given

If I use NBeatsNet.TREND_BLOCK and NBeatsNet.SEASONALITY_BLOCK the stacks build with no issue, but NBeatsNet.GENERIC_BLOCK fails.

The issue appears to be that in the create_stack method, you are including self.nb_harmonics as a parameter:

                block = block_init(self.hidden_layer_units, self.thetas_dim[stack_id],
                                   self.device, self.backcast_length, self.forecast_length, self.nb_harmonics)

yet in the initalization for the GENERIC_BLOCK, this parameter is omitted:

class GenericBlock(Block):

    def __init__(self, units, thetas_dim, device, backcast_length=10, forecast_length=5):
jroakes commented 3 years ago

I should add that it looks like the issue only exists in the Pypi library and is corrected in this Github Repo.

pnmartinez commented 3 years ago

Can confirm what @jroakes says: the PIP library is what's wrong, not the code included in the repo.

The detailed workaround is to uninstall it from pip:

pip uninstall nbeats-pytorch

And then copy paste the nbeats_pytorch folder included in the repo on the same directory your code is.

philipperemy commented 3 years ago

I updated the code and pushed a version 1.3.3 for both pytorch and keras that reflects what is on the Github. The issue should be solved now ;)