Python – AttributeError: 'numpy.ndarray' object has no attribute 'to'

Issue

I now have the updated code as follows:

    # Hyperparameters
random_seed = 123
learning_rate = 0.01
num_epochs = 10
batch_size = 128

device = torch.device("cuda:1" if torch.cuda.is_available() else "cpu")

for epoch in range(num_epochs):
model = resnet34.train()
for batch_idx, (features, targets) in enumerate(train_generator):

    features = features.to(device)
    targets = targets.to(device)
        
    ### FORWARD AND BACK PROP
    logits = model(features)
    cost = torch.nn.functional.cross_entropy(logits, targets)
    optimizer.zero_grad()
    
    cost.backward()
    
    ### UPDATE MODEL PARAMETERS
    optimizer.step()
    
    ### LOGGING
    if not batch_idx % 50:
        print ('Epoch: %03d/%03d | Batch %03d/%03d | Cost: %.4f' 
               %(epoch+1, num_epochs, batch_idx, 
                 len(datagen)//batch_size, cost))

model = model.eval() # eval mode to prevent upd. batchnorm params during inference
with torch.set_grad_enabled(False): # save memory during inference
    print('Epoch: %03d/%03d training accuracy: %.2f%%' % (
          epoch+1, num_epochs, 
          compute_accuracy(model, train_generator)))

When having only one image, the code runs fine. But, when I add another image or more, I get the following:

features = features.to(device)
targets = targets.to(device)
AttributeError: 'numpy.ndarray' object has no attribute 'to'

Solution

It would be nice to see your train_generator code for clarity, but it does not seem to be a torch DataLoader. In this case, you should probably convert your arrays to tensors manually. There are several ways to do so:

  • torch.from_numpy(numpy_array) – for numpy arrays;
  • torch.as_tensor(list) – for common lists and tuples;
  • torch.tensor(array) should also work but the above ways will avoid copying the data when possible.

Answered By – dx2-66

This Answer collected from stackoverflow, is licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0

Leave a Reply

(*) Required, Your email will not be published