Home > Software engineering >  Int object is not iterable when looping through a dataset
Int object is not iterable when looping through a dataset

Time:06-09

I am trying to extract data in batches from a dataset to train a model Here is part of the code

#defining the loss,optimizer and training function for the neural network
def train_network(model,optimizer,loss_function,num_epochs,batch_size,x_train,y_train):
    #start model training
    model.train()
    loss_for_every_epoch=nn.ModuleList()
    for epoch in range(num_epochs):
        train_loss=0.0
        for i in range(0,x_train.shape[0],batch_size):
            #extract train batch from x and y
            input_data=x_train[i:min(x_train.shape[0]),i batch_size]
            labels=y_train[i:min(y_train.shape[0]),i batch_size]
            #set gradients to zero before beginning optimization
            optimizer.zero_grad()
            #forwad pass 
            output_data=model(input_data)
            #calculate loss
            loss=loss_function(output_data,labels)
            #backpropagate
            loss.backward()
            #update weights
            optimizer.step()
            train_loss =loss.item()*batch_size
        print("Epoch: {} - Loss:{:.4f}".format(epoch 1,train_loss )) 
        loss_for_every_epoch.extend([train_loss])
    #predict
    y_test_prediction=model(x_test)
    a=np.where(y_test_prediction>0.5,1,0)
    return loss_for_every_epoch

#create an object of the class
model=neuralnetwork()
#define the loss function
loss_function = nn.BCELoss()#binary cross entropy loss function
#define optimizer
adam_optimizer=torch.optim.Adam(params=model.parameters(),lr=0.001)
#define epochs and batch size
number_of_epochs=100
batch_size=16
#Calling the function for training and pass model, optimizer, loss and related paramters
adam_loss=train_network(model,adam_optimizer,loss_function,number_of_epochs,batch_size,x_train,y_train)

But i get the following error

---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
g:\My Drive\CODE\pythondatascience\simpleneuralnetwork.ipynb Cell 7' in <cell line: 11>()
      9 batch_size=16
     10 #Calling the function for training and pass model, optimizer, loss and related paramters
---> 11 adam_loss=train_network(model,adam_optimizer,loss_function,number_of_epochs,batch_size,x_train,y_train)

g:\My Drive\CODE\pythondatascience\simpleneuralnetwork.ipynb Cell 5' in train_network(model, optimizer, loss_function, num_epochs, batch_size, x_train, y_train)
      7 train_loss=0.0
      8 for i in range(0,4000,batch_size):
      9     #extract train batch from x and y
---> 10     input_data=x_train[i:min(x_train.shape[0]),i batch_size]
     11     labels=y_train[i:min(y_train.shape[0]),i batch_size]
     12     #set gradients to zero before beginning optimization

TypeError: 'int' object is not iterable

What could be the cause of the error cause thse source that am using to write the programmed did it in the exact same way

In addition to that can someone explain to me specifically what this line means

input_data=x_train[i:min(x_train.shape[0]),i batch_size]

x_train is a dataset

CodePudding user response:

input_data=... is taking a slice of your data to use it as an input to your training algorithm.

Although the comparison statement is invalid (x_train.shape should return a tuple of ints, you shouldn't be able to do min(x_train.shape[0]) without comparing it to something else. My guess is that you should have input_data=x_train[i:min(x_train.shape[0],i batch_size)]. You have the same issue for y_train as well.

  • Related