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

Hi,

it’s my first pytorch model and I tried to make it quite similar to this tutorial: What is torch.nn really? — PyTorch Tutorials 1.8.1+cu102 documentation

But I’m always receiving the following error and I don’t know why and can’t find it by googling:

Traceback (most recent call last):
  File ".../train_model.py", line 160, in <module>
    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'

My model creation looks like this:

def create_model():
    model = nn.Sequential(
        nn.BatchNorm1d(num_features=1),
    )
    return model

The training is defined like this:

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))

And I call the model creation and training like this:

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)

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!

The .fit() method is not a default method from nn.Module and is defined in the tutorial (which will start the training).

Many thanks for your answer @ptrblck but it was just a problem with my virtual environment.