ImportError: cannot import name 'AdultDataset' from 'dataset'

0

Please I am working on AdultDataset for a classification task

I found out: from dataset import AdultDataset

is giving the error below:

ImportError: cannot import name ‘AdultDataset’ from ‘dataset’

Import Relevant Libraries

import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
from sklearn.preprocessing import LabelEncoder
from sklearn.model_selection import train_test_split

import torch
import torch.nn as nn

import torch.optim as optim
from torch.autograd import Variable
from torch.utils.data import DataLoader


from dataset import AdultDataset

So when I am trying to create a 3 layer Feed Forward Neural network using pytorch that takes as input the a dataset entries and classifies if the individuals gains more or less than 50K (i.e., the fnlwgt label)

starting with the code below I get error

train_dataset = AdultDataset(X_train, y_train)
test_dataset = AdultDataset(X_test, y_test)

NameError Traceback (most recent call last) in 1 # Using norm_D01 2 ----> 3 train_dataset = AdultDataset(X_train, y_train) 4 test_dataset = AdultDataset(X_test, y_test) 5

NameError: name 'AdultDataset' is not defined

Hi,

What is dataset here? I think it is not related to PyTorch as you dataset seems like a custom module where AdultDataset should be in it.

And the error is saying that AdultDataset does not exist in dataset.

Bests

1 Like

Hey,

As @Nikronic said above, it seems that dataset is a custom module. So, make sure that you have a file called dataset.py within the same directory as your Python script because if it’s not in the same directory Python won’t be able to find it. (Unless you specifically change your working directory, or just copy the file into the same directory as your Python script).

And, within the file dataset.py make sure there’s a class called AdultDataset inside that file. That’s what you’re looking for with the line from dataset import AdultDataset

If this dataset is a file you’ve installed via pip or conda make sure that your Python environment is set to use the correct environment!

Kind regards,