TypeError: __init__() got multiple values for argument 'batch_size'

I get this error. I don’t understand how to solve this error. How can solve this error.
‘’’
X_train, X_test, y_age_train, y_age_test, y_gender_train, y_gender_test = train_test_split(X, label_age, label_gender, test_size=0.2)
y_age_train = np.array(y_age_train)
y_gender_train = np.array(y_gender_train)
y_age_test = np.array(y_age_test)
y_gender_test = np.array(y_gender_test)

batch_size = 100
train_dataloader = torch.utils.data.DataLoader(X_train_tensor, y_age_train_tensor, y_gender_train_tensor, batch_size=batch_size, shuffle=True)
test_dataloader = torch.utils.data.DataLoader(X_test_tensor, y_age_test_tensor, y_gender_test_tensor, batch_size=batch_size, shuffle=True)
‘’’
Please help me to solve this error. Thank you in advance.

The DataLoader class expects input arguments as dataset, batch_size, ... as seen in the docs. Since you are passing each tensor separately to it, the y_age_train_tensor will be used as the batch_size and will thus create the issue. Wrap your tensors in a TensorDataset, pass it to the DataLoader, and it should work.

Thank you so much. It works perfectly.

When I have trained and tested my model I get this error:
TypeError: only integer scalar arrays can be converted to a scalar index.
I checked on many websites and also on NumPy array how to solve this type of error. But I could not get how to implement it in my model.

‘’’
X_train, X_test, y_age_train, y_age_test, y_gender_train, y_gender_test = train_test_split(X, label_age, label_gender, test_size=0.2)

y_age_train = np.array(y_age_train)
y_gender_train = np.array(y_gender_train)
y_age_test = np.array(y_age_test)
y_gender_test = np.array(y_gender_test)

<class ‘numpy.ndarray’>
X_train[0]
array([[[108],
[108],
[108],
…,
[112],
[108],
[103]],

   [[108],
    [108],
    [108],
    ...,
    [110],
    [107],
    [102]],

   [[109],
    [109],
    [109],
    ...,
    [107],
    [103],
    [ 98]],

   ...,

   [[119],
    [120],
    [120],
    ...,
    [ 51],
    [ 53],
    [ 54]],

   [[119],
    [120],
    [120],
    ...,
    [ 54],
    [ 55],
    [ 56]],

   [[119],
    [120],
    [120],
    ...,
    [ 53],
    [ 54],
    [ 57]]], dtype=uint8)

print("y_age_train_data: ",y_age_train)
print("y_gender_train_data: ",y_gender_train)
print("y_age_test_data: ",y_age_test)
print("y_gender_test_data: ",y_gender_test)

y_age_train_data: [35 38 27 … 80 40 38]
y_gender_train_data: [0 0 0 … 0 0 1]
y_age_test_data: [12 29 45 … 40 38 36]
y_gender_test_data: [0 0 0 … 0 0 1]
‘’’
‘’’
def train_model(model, criterion1, criterion2, optimizer, n_epochs=25):
“”“returns trained model”“”
# initialize tracker for minimum validation loss
valid_loss_min = np.Inf
for epoch in range(1, n_epochs):
train_loss = 0.0
valid_loss = 0.0
# train the model #
model.train()

    for batch_idx, sample_batched in enumerate(train_dataloader):
        # importing data and moving to GPU
        image, label1, label2 = sample_batched[images].to(device),\
                                 sample_batched[y_age_train].to(device),\
                                  sample_batched[y_gender_train].to(device)
                                             
        # zero the parameter gradients
        optimizer.zero_grad()
        output=model(image)
        label1_hat=output['label1']
        label2_hat=output['label2']
        #label3_hat=output['label3']         
        # calculate loss
        loss1=criterion1(label1_hat, label1.squeeze().type(torch.LongTensor))
        loss2=criterion2(label2_hat, label2.squeeze().type(torch.LongTensor))
        #loss3=criterion1(label3_hat, label3.squeeze().type(torch.LongTensor))     
        loss=loss1+loss2
        # back prop
        loss.backward()
        # grad
        optimizer.step()
        train_loss = train_loss + ((1 / (batch_idx + 1)) * (loss.data - train_loss))
        if batch_idx % 50 == 0:
            print('Epoch %d, Batch %d loss: %.6f' %
              (epoch, batch_idx + 1, train_loss))
    # validate the model #
    model.eval()

    for batch_idx, sample_batched in enumerate(test_dataloader):
        image, label1, label2 = sample_batched[X_test].to(device),\
                                         sample_batched[y_age_test].to(device),\
                                          sample_batched[y_gender_test].to(device),\
                                           #sample_batched['label_race'].to(device)  
        output = model(image)
        output=model(image)
        label1_hat=output['label1']
        label2_hat=output['label2']
        #label3_hat=output['label3']               
        # calculate loss
        loss1=criterion1(label1_hat, label1.squeeze().type(torch.LongTensor))
        loss2=criterion2(label2_hat, label2.squeeze().type(torch.LongTensor))
        #loss3=criterion1(label3_hat, label3.squeeze().type(torch.LongTensor))  
        loss=loss1+loss2
        valid_loss = valid_loss + ((1 / (batch_idx + 1)) * (loss.data - valid_loss))
    
    # print training/validation statistics 
    print('Epoch: {} \tTraining Loss: {:.6f} \tValidation Loss: {:.6f}'.format(
        epoch, train_loss, valid_loss))
    
    ## TODO: save the model if validation loss has decreased
    if valid_loss < valid_loss_min:
        torch.save(model, 'model.pt')
        print('Validation loss decreased ({:.6f} --> {:.6f}).  Saving model ...'.format(
        valid_loss_min,
        valid_loss))
        valid_loss_min = valid_loss
# return trained model
return model

‘’’
‘’’
model_conv = train_model(net, criterion_multioutput, criterion_binary, optimizer)
‘’’
I’m not sure how to include it in my model. Please tell me how to properly incorporate it into my model.
Thank you in advace

I don’t know which line of code raises the error, but numpy would raise it if you are trying to provide a numpy array as a dimension/axis argument as seen here:

a = np.random.randn(10)
a.sum(axis=0) # works

a.sum(axis=np.random.randint(0, 2, (1,)))
# TypeError: only integer scalar arrays can be converted to a scalar index

np.concatenate((a, a)) # works
np.concatenate(a, a)
# TypeError: only integer scalar arrays can be converted to a scalar index

‘’’
def train_model(model, criterion1, criterion2, optimizer, n_epochs=25):
“”“returns trained model”“”
# initialize tracker for minimum validation loss
valid_loss_min = np.Inf
for epoch in range(1, n_epochs):
train_loss = 0.0
valid_loss = 0.0
# train the model #
model.train()

    for batch_idx, sample_batched in enumerate(train_dataloader):
        # importing data and moving to GPU
        image, label1, label2 = sample_batched[X_train].to(device),\   <------ for this it raises the error
                                 sample_batched[y_age_train].to(device),\
                                  sample_batched[y_gender_train].to(device)
                                             
        # zero the parameter gradients
        optimizer.zero_grad()
        output=model(image)
        label1_hat=output['label1']
        label2_hat=output['label2']
        #label3_hat=output['label3']         
        # calculate loss
        loss1=criterion1(label1_hat, label1.squeeze().type(torch.LongTensor))
        loss2=criterion2(label2_hat, label2.squeeze().type(torch.LongTensor))
        #loss3=criterion1(label3_hat, label3.squeeze().type(torch.LongTensor))     
        loss=loss1+loss2
        # back prop
        loss.backward()
        # grad
        optimizer.step()
        train_loss = train_loss + ((1 / (batch_idx + 1)) * (loss.data - train_loss))
        if batch_idx % 50 == 0:
            print('Epoch %d, Batch %d loss: %.6f' %
              (epoch, batch_idx + 1, train_loss))
    # validate the model #
    model.eval()

    for batch_idx, sample_batched in enumerate(test_dataloader):
        image, label1, label2 = sample_batched[X_test].to(device),\
                                         sample_batched[y_age_test].to(device),\
                                          sample_batched[y_gender_test].to(device),\
                                           #sample_batched['label_race'].to(device)  
        output = model(image)
        output=model(image)
        label1_hat=output['label1']
        label2_hat=output['label2']
        #label3_hat=output['label3']               
        # calculate loss
        loss1=criterion1(label1_hat, label1.squeeze().type(torch.LongTensor))
        loss2=criterion2(label2_hat, label2.squeeze().type(torch.LongTensor))
        #loss3=criterion1(label3_hat, label3.squeeze().type(torch.LongTensor))  
        loss=loss1+loss2
        valid_loss = valid_loss + ((1 / (batch_idx + 1)) * (loss.data - valid_loss))
    
    # print training/validation statistics 
    print('Epoch: {} \tTraining Loss: {:.6f} \tValidation Loss: {:.6f}'.format(
        epoch, train_loss, valid_loss))
    
    ## TODO: save the model if validation loss has decreased
    if valid_loss < valid_loss_min:
        torch.save(model, 'model.pt')
        print('Validation loss decreased ({:.6f} --> {:.6f}).  Saving model ...'.format(
        valid_loss_min,
        valid_loss))
        valid_loss_min = valid_loss
# return trained model
return model

model_conv = train_model(net, criterion_multioutput, criterion_binary, optimizer)
‘’’
it raises the error for

TypeError Traceback (most recent call last)
Input In [43], in <cell line: 1>()
----> 1 model_conv = train_model(net, criterion_multioutput, criterion_binary, optimizer)

Input In [42], in train_model(model, criterion1, criterion2, optimizer, n_epochs)
9 model.train()
11 for batch_idx, sample_batched in enumerate(train_dataloader):
12 # importing data and moving to GPU
—> 13 image, label1, label2 = sample_batched[X_train].to(device),
14 sample_batched[y_age_train].to(device),
15 sample_batched[y_gender_train].to(device)
17 # zero the parameter gradients
18 optimizer.zero_grad()

TypeError: only integer scalar arrays can be converted to a scalar index

I read NumPy also. But I can’t figure out how to implement it in my model.
Please help me how to should I implement it in my model.
Thank you in advance.

The DataLoader will already return a batch of samples while it seems you are trying to index this batch with the original dataset stored in a numpy array.
Check the object type of sample_batches which should be a list containing tensors.