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: d/d | Batch d/d | 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: d/d 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'
CodePudding user response:
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.