AttributeError: 'BatchNorm1d' object has no attribute 'fit'

username: Annika10
cooked: <p>Hi,</p>
<p>it’s my first pytorch model and I tried to make it quite similar to this tutorial: <a href="https://pytorch.org/tutorials/beginner/nn_tutorial.html#" class="inline-onebox" rel="noopener nofollow ugc">What is torch.nn really? — PyTorch Tutorials 1.8.1+cu102 documentation</a></p>
<p>But I’m always receiving the following error and I don’t know why and can’t find it by googling:</p>
<pre><code class="lang-auto">Traceback (most recent call last):
  File ".../train_model.py", line 160, in &lt;module&gt;
    fit(model=model, loss_func=loss_func, optimizer=optimizer, epochs=epochs, trainloader=trainloader, validloader=validloader)
  File ".../train_model.py", line 97, in fit
    model.train()
  File ".../venv3.8/lib/python3.8/site-packages/torch/nn/modules/module.py", line 1449, in train
    module.fit(mode)
  File ".../venv3.8/lib/python3.8/site-packages/torch/nn/modules/module.py", line 947, in __getattr__
    raise AttributeError("'{}' object has no attribute '{}'".format(
AttributeError: 'BatchNorm1d' object has no attribute 'fit'
</code></pre>
<p>My model creation looks like this:</p>
<pre><code class="lang-auto">def create_model():
    model = nn.Sequential(
        nn.BatchNorm1d(num_features=1),
    )
    return model
</code></pre>
<p>The training is defined like this:</p>
<pre><code class="lang-auto">def fit(model, loss_func, optimizer, epochs, trainloader, validloader):
    for epoch in range(epochs):  # loop over the dataset multiple times
        model.train()
        for xb, yb in trainloader:
            pred = model(xb)
            loss = loss_func(pred, yb)
            loss.backward()
            optimizer.step()
            # zero the parameter gradients
            optimizer.zero_grad()
        model.eval()
        with torch.no_grad():
            valid_loss = sum(loss_func(model(xb), yb) for xb, yb in validloader)
        print(epoch, valid_loss / len(validloader))
</code></pre>
<p>And I call the model creation and training like this:</p>
<pre><code class="lang-auto">if __name__ == "__main__":
    batch_size = 32
    epochs = 3

    x_train, y_train, x_val, y_val, x_test, y_test = load_data("processed_data.pickle")
    trainloader = get_Dataloader(x_train, y_train, batch_size=batch_size)
    validloader = get_Dataloader(x_val, y_val, batch_size=batch_size * 2)

    loss_func = F.cross_entropy
    model = create_model()
    learning_rate = 0.001
    optimizer = torch.optim.Adamax(model.parameters(), lr=learning_rate)

    fit(model=model, loss_func=loss_func, optimizer=optimizer, epochs=epochs, trainloader=trainloader, validloader=validloader)
</code></pre>
<p>I also tried it by using a model with one convolutional layer, and also with multiple other layers together (how the model should really look like in the end and not just the batchnorm) but it results in the same error. Hope you can help me!</p>

username: ptrblck
cooked: <p>The <code>.fit()</code> method is not a default method from <code>nn.Module</code> and is defined in the tutorial (which will start the training).</p>

username: Annika10
cooked: <p>Many thanks for your answer <a class="mention" href="/u/ptrblck">@ptrblck</a> but it was just a problem with my virtual environment.</p>