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

Niraja: <p>I get this error. I don’t understand how to solve this error. How can solve this error.<br>
‘’’<br>
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)<br>
y_age_train = np.array(y_age_train)<br>
y_gender_train = np.array(y_gender_train)<br>
y_age_test = np.array(y_age_test)<br>
y_gender_test = np.array(y_gender_test)</p>
<p>batch_size = 100<br>
train_dataloader = torch.utils.data.DataLoader(X_train_tensor, y_age_train_tensor, y_gender_train_tensor, batch_size=batch_size, shuffle=True)<br>
test_dataloader = torch.utils.data.DataLoader(X_test_tensor, y_age_test_tensor, y_gender_test_tensor, batch_size=batch_size, shuffle=True)<br>
‘’’<br>
Please help me to solve this error. Thank you in advance.</p>

ptrblck: <p>The <code>DataLoader</code> class expects input arguments as <code>dataset, batch_size, ...</code> as seen in <a href="https://pytorch.org/docs/stable/data.html#torch.utils.data.DataLoader">the docs</a>. Since you are passing each tensor separately to it, the <code>y_age_train_tensor</code> will be used as the <code>batch_size</code> and will thus create the issue. Wrap your tensors in a <code>TensorDataset</code>, pass it to the <code>DataLoader</code>, and it should work.</p>

Niraja: <p>Thank you so much. It works perfectly.</p>

Niraja: <p>When I have trained and tested my model I get this error:<br>
TypeError: only integer scalar arrays can be converted to a scalar index.<br>
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.</p>
<p>‘’’<br>
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)</p>
<p>y_age_train = np.array(y_age_train)<br>
y_gender_train = np.array(y_gender_train)<br>
y_age_test = np.array(y_age_test)<br>
y_gender_test = np.array(y_gender_test)</p>
<blockquote>
<p>&lt;class ‘numpy.ndarray’&gt;<br>
X_train[0]<br>
array([[[108],<br>
[108],<br>
[108],<br>
…,<br>
[112],<br>
[108],<br>
[103]],</p>
</blockquote>
<pre><code>   [[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)
</code></pre>
<p>print("y_age_train_data: ",y_age_train)<br>
print("y_gender_train_data: ",y_gender_train)<br>
print("y_age_test_data: ",y_age_test)<br>
print("y_gender_test_data: ",y_gender_test)</p>
<blockquote>
<p>y_age_train_data:  [35 38 27 … 80 40 38]<br>
y_gender_train_data:  [0 0 0 … 0 0 1]<br>
y_age_test_data:  [12 29 45 … 40 38 36]<br>
y_gender_test_data:  [0 0 0 … 0 0 1]<br>
‘’’<br>
‘’’<br>
def train_model(model, criterion1, criterion2, optimizer, n_epochs=25):<br>
“”“returns trained model”“”<br>
# initialize tracker for minimum validation loss<br>
valid_loss_min = np.Inf<br>
for epoch in range(1, n_epochs):<br>
train_loss = 0.0<br>
valid_loss = 0.0<br>
# train the model #<br>
model.train()</p>
</blockquote>
<pre><code>    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 &lt; valid_loss_min:
        torch.save(model, 'model.pt')
        print('Validation loss decreased ({:.6f} --&gt; {:.6f}).  Saving model ...'.format(
        valid_loss_min,
        valid_loss))
        valid_loss_min = valid_loss
# return trained model
return model
</code></pre>
<p>‘’’<br>
‘’’<br>
model_conv = train_model(net, criterion_multioutput, criterion_binary, optimizer)<br>
‘’’<br>
I’m not sure how to include it in my model. Please tell me how to properly incorporate it into my model.<br>
Thank you in advace</p>

ptrblck: <p>I don’t know which line of code raises the error, but <code>numpy</code> would raise it if you are trying to provide a numpy array as a dimension/axis argument as seen here:</p>
<pre><code class="lang-python">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
</code></pre>

Niraja: <p>‘’’<br>
def train_model(model, criterion1, criterion2, optimizer, n_epochs=25):<br>
“”“returns trained model”“”<br>
# initialize tracker for minimum validation loss<br>
valid_loss_min = np.Inf<br>
for epoch in range(1, n_epochs):<br>
train_loss = 0.0<br>
valid_loss = 0.0<br>
# train the model #<br>
model.train()</p>
<pre><code>    for batch_idx, sample_batched in enumerate(train_dataloader):
        # importing data and moving to GPU
        image, label1, label2 = sample_batched[X_train].to(device),\   &lt;------ 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 &lt; valid_loss_min:
        torch.save(model, 'model.pt')
        print('Validation loss decreased ({:.6f} --&gt; {:.6f}).  Saving model ...'.format(
        valid_loss_min,
        valid_loss))
        valid_loss_min = valid_loss
# return trained model
return model
</code></pre>
<h2>
<a name="model_conv-train_modelnet-criterion_multioutput-criterion_binary-optimizer-it-raises-the-error-for-1" class="anchor" href="#model_conv-train_modelnet-criterion_multioutput-criterion_binary-optimizer-it-raises-the-error-for-1"></a>model_conv = train_model(net, criterion_multioutput, criterion_binary, optimizer)<br>
‘’’<br>
it raises the error for</h2>
<p>TypeError                                 Traceback (most recent call last)<br>
Input In [43], in &lt;cell line: 1&gt;()<br>
----&gt; 1 model_conv = train_model(net, criterion_multioutput, criterion_binary, optimizer)</p>
<p>Input In [42], in train_model(model, criterion1, criterion2, optimizer, n_epochs)<br>
9 model.train()<br>
11 for batch_idx, sample_batched in enumerate(train_dataloader):<br>
12     # importing data and moving to GPU<br>
—&gt; 13     image, label1, label2 = sample_batched[X_train].to(device),<br>
14                              sample_batched[y_age_train].to(device),<br>
15                               sample_batched[y_gender_train].to(device)<br>
17     # zero the parameter gradients<br>
18     optimizer.zero_grad()</p>
<p>TypeError: only integer scalar arrays can be converted to a scalar index</p>
<p>I read NumPy also. But I can’t figure out how to implement it in my model.<br>
Please help me how to should I implement it in my model.<br>
Thank you in advance.</p>

ptrblck: <p>The <code>DataLoader</code> 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.<br>
Check the object type of <code>sample_batches</code> which should be a <code>list</code> containing tensors.</p>

sambutle: <aside class="quote no-group" data-username="Niraja" data-post="6" data-topic="170018">
<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/niraja/48/61924_2.png" class="avatar"> Niraja:</div>
<blockquote>
<p>TypeError: only integer scalar arrays can be converted to a scalar index</p>
</blockquote>
</aside>
<p>This error message suggests that a function or operation is expecting an integer <a href="https://net-informations.com/q/py/scalar.html" rel="noopener nofollow ugc">scalar index</a> (single integer) as an index, but is instead receiving an array or a non-integer value. This can occur when attempting to index or slice an array with a non-integer or non-scalar value. To resolve this error, ensure that the index being used is a single integer and not an array or non-integer value. If working with arrays, consider using integer indexing or slicing methods to access specific elements or subsets of the array.</p>