Cross Entropy Loss Dimensions

FahadAslamCS: <p>I am facing a problem with my own data dimensions. I am completely new to PyTorch I will be grateful if one can help me here.<br>
I am getting error of  “Target (10)(this no changes between my class name) is out of bounds”</p>
<p>I have 6 classes denoted by 0, 5,20,40, 2.5, 10<br>
I do not know how to handle it.</p>
<pre><code class="lang-auto">np_target = pd.read_csv('naphData.csv').astype('int')
np_data = pd.read_csv('naphTarget.csv')
X_train, X_test, y_train, y_test = train_test_split(np_data, np_target, test_size=0.2)

train_dataset = torch.tensor(X_train.values)
train_dataset = train_dataset.float()
train_target = torch.tensor(y_train.values)

test_dataset = torch.tensor(X_test.values)
test_dataset = test_dataset.float()
test_target = torch.tensor(y_test.values)

# Define a Neural Network class

class Net(nn.Module):
    
    # Constructor
    def __init__(self, D_in, H, D_out):
        super(Net, self).__init__()
        self.linear1 = nn.Linear(D_in, H)
        self.linear2 = nn.Linear(H, D_out)

    # Prediction    
    def forward(self, x):
        x = torch.sigmoid(self.linear1(x))  
        x = self.linear2(x)
        return x

train_data = []
for i in range(len(train_dataset)):
   train_data.append([train_dataset[i], train_target[i]])

trainloader = torch.utils.data.DataLoader(train_data, shuffle=True, batch_size=10)

i1, l1 = next(iter(trainloader))
print(i1.shape)

test_data = []
for i in range(len(test_dataset)):
   test_data.append([test_dataset[i], test_target[i]])

validationloader = torch.utils.data.DataLoader(test_data, shuffle=True, batch_size=10)

i1, l1 = next(iter(validationloader))
print(i1.shape)

# Create the model with 100 neurons

input_dim = 1 * 160  #160 features in each sample
hidden_dim = 100
output_dim = 6  #number of class

model = Net(input_dim, hidden_dim, output_dim)

# Create criterion function
criterion = nn.CrossEntropyLoss()

def train(model, criterion, train_loader, validation_loader, optimizer, epochs):
    i = 0
    useful_stuff = {'training_loss': [],'validation_accuracy': []}  
    for epoch in range(epochs):
        for i, (x, y) in enumerate(train_loader) : 
            optimizer.zero_grad()
            z = model(x.view(-1, 1 * 160))
            y = y.squeeze(1)
          
            loss = criterion(z, y) #getting error here
            loss.backward()
            optimizer.step()
             #loss for every iteration
            useful_stuff['training_loss'].append(loss.data.item())
        correct = 0
        for x, y in validation_loader:
            #validation 
            z = model(x.view(-1, 1 * 160))
            _, label = torch.max(z, 1)
            correct += (label == y).sum().item()
        accuracy = 100 * (correct / len(validation_dataset))
        useful_stuff['validation_accuracy'].append(accuracy)
    return useful_stuff
learning_rate = 0.01
optimizer = torch.optim.SGD(model.parameters(), lr=learning_rate)

# Train the model

training_results = train(model, criterion, trainloader, validationloader, optimizer, epochs=100)

</code></pre>

KFrank: <p>Hello Fahad!</p>
<aside class="quote no-group" data-username="FahadAslamCS" data-post="1" data-topic="77324" data-full="true">
<div class="title">
<div class="quote-controls"></div>
<img loading="lazy" alt="" width="24" height="24" src="https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/fahadaslamcs/48/22964_2.png" class="avatar"> FahadAslamCS:</div>
<blockquote>
<p>I have 6 classes denoted by 0, 5,20,40, 2.5, 10</p>
</blockquote>
</aside>
<p>When you use <code>CrossEntropyLoss</code>, your <code>target</code> <code>y</code> that you pass<br>
in to <code>criterion</code> must be integer class labels that take on values<br>
running from <code>0</code> to <code>nClass - 1</code> (in your case, <code>(0, 1, 2, 3, 4, 5)</code>).</p>
<p>If your class labels are originally (0, 5, 20, 40, 2.5, 10), you will<br>
have to remap them before passing them to <code>criterion</code>.</p>
<p>Just to be clear, the <code>shape</code> of <code>y</code> should simply be <code>[nBatch]</code>.</p>
<p>Best.</p>
<p>K. Frank</p>