Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Question] Setting up a classifier with params #94

Open
jpbarddal opened this issue Nov 5, 2024 · 1 comment
Open

[Question] Setting up a classifier with params #94

jpbarddal opened this issue Nov 5, 2024 · 1 comment

Comments

@jpbarddal
Copy link

jpbarddal commented Nov 5, 2024

Hi,

I am interested in deep-river. Is the project still being incremented?
Also, I am attempting to pass a module with specific params. A possible MWE would be as follows, however, it seems the Classifier class requires as input a non-instantiated nn.Module.
Am I missing something?

MLP code:

class MLP(nn.Module):

    def __init__(self,
                 input_dim: int = 512,
                 hidden_dim: int = 256,
                 output_dim: int = 2):
        super(MLP, self).__init__()
        self.input_dim = input_dim
        self.hidden_dim = hidden_dim
        self.output_dim = output_dim

        self.linear = nn.Linear(self.input_dim, self.hidden_dim)
        self.activation = nn.ReLU()
        self.linear_2 = nn.Linear(self.hidden_dim, self.output_dim)

    def forward(self, x):
        out = self.linear(x)
        out = self.activation(out)
        out = self.linear_2(out)
        return out

Pipeline construction:

    model = MLP(input_dim=dataset.n_features - 1,
                hidden_dim=(dataset.n_features - 1) // 2,
                output_dim=2)
    model_pipeline = compose.Pipeline(
        preprocessing.StandardScaler(),
        classification.Classifier(module=model,
                                  loss_fn='binary_cross_entropy',
                                  optimizer_fn='adam',
                                  )
    )
@kulbachcedric
Copy link
Collaborator

Hi @jpbarddal,
this project is still being incremented!
You are right the classifier requires a non-instantiated nn.Module.
The reason for that is that in data streams the number of classes to predict or the number of features are might not be known at the beginning of the data stream. For that reason we initialize the model within the first event of the data stream.

However, to pass specific params to the Module, you can still do that by adding it to the Classifiers arguments:

    class MLP(nn.Module):

    def __init__(self,
                 n_features: int = 512,
                 hidden_dim: int = 256,
                 output_dim: int = 2):
        super(MLP, self).__init__()
        self.input_dim = n_features
        self.hidden_dim = hidden_dim
        self.output_dim = output_dim

        self.linear = nn.Linear(self.input_dim, self.hidden_dim)
        self.activation = nn.ReLU()
        self.linear_2 = nn.Linear(self.hidden_dim, self.output_dim)

    def forward(self, x):
        out = self.linear(x)
        out = self.activation(out)
        out = self.linear_2(out)
        return out


    model_pipeline = compose.Pipeline(
        preprocessing.StandardScaler(),
        classification.Classifier(module=MLP,
                                  loss_fn='binary_cross_entropy',
                                  optimizer_fn='adam',
                                  hidden_dim=(dataset.n_features - 1) // 2,
                                  output_dim=2
                                  )
    )

Note, that I removed the input_dim argument and added the n_features field as this variable is set with the first event of the data stream.

Kind regards
Cedric

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants