-1

I used loss_val.item() instead of loss_val.data[0] in my code because new version of python do not support that but as you can see here but I receive Error for gathering Loss in list

" 'float' object is not iterable"

do you know what should I do ?

num_epochs = 10 losses = [] for epoch in range(num_epochs): for i, (inputs, targets) in enumerate(train_dl): inputs = to_var(inputs) # convert to variable targets = to_var(targets)

    # forwad pass
    optimizer.zero_grad()
    outputs = model(inputs) 

    # loss
    loss_val = criterion(outputs, targets) 
    **losses+= loss_val.item()**

    # backward pass
    loss_val.backward() 

    # update parameters
    optimizer.step()  
    #W=W-Ir*dW
    # report
    if (i + 1) % 50 == 0:
        print('Epoch [%2d/%2d], Step [%3d/%3d], Loss: %.4f'% (epoch + 1, num_epochs, i + 1, len(train_ds) // batch_size, loss_val.item())) 
ELI
  • 45
  • 7
  • please send a minimal example (something that we can run). As well as the stack-trace you're getting, – avloss Apr 15 '20 at 09:40

1 Answers1

0

I think I should tell something more: if you look at my code

1) losses= [] is a list then I try to add loss in every iteration to that for draw chart :

2)loss_val = criterion(outputs, targets)

3)losses+= loss_val.item()

but I recieve this error that " float' object is not iterable"

this error comes in 3 lines

ELI
  • 45
  • 7