TypeError: 'TensorDataset' object is not callable

Hello,

l get the following error

    train(train_loader, model, criterion, optimizer, epoch)
TypeError: 'TensorDataset' object is not callable

when l run

    for epoch in range(args.start_epoch, args.epochs):
        if args.distributed:
            train_sampler.set_epoch(epoch)
        adjust_learning_rate(optimizer, epoch)

        # train for one epoch
        train(train_loader, model, criterion, optimizer, epoch)

the errors come from train_loader in train()

which is defined as follow :

 train_loader = torch.utils.data.DataLoader(
        train, batch_size=args.batch_size, shuffle=(train_sampler is None),
        num_workers=args.workers, pin_memory=True, sampler=train_sampler)

and the input train to train_loader is built as follow :

import torch.utils.data as data_utils
 data_train = torch.from_numpy(data_train)
labels_train=torch.from_numpy(labels_train)
train = data_utils.TensorDataset(data_train.float(), labels_train)

What’s wrong with my code ?

Thank you

I think the problem is that the python variable train refers to two things in your code.
Your dataset object here train = data_utils.TensorDataset(data_train.float(), labels_train) and also a function that you use to perform training given the way you try to use it.

3 Likes

Thanks. Solved by renaming data train variable