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'
train_generatorwas defined?.to(device)on it, which is not possible with numpy arrays, because they aren't pytorch tensors. I hope that was clear.