Im trying to do an image classifier, but im having a problem with the data cast on the GPU.
JavaScript
x
49
49
1
def train(train_loader, net, epoch):
2
3
# Training mode
4
net.train()
5
6
start = time.time()
7
8
epoch_loss = []
9
pred_list, label_list = [], []
10
11
for batch in train_loader:
12
13
#Batch cast on the GPU
14
input, label = batch
15
input.to(args['device'])
16
label.to(args['device'])
17
18
#Forward
19
ypred = net(input)
20
loss = criterion(ypred, label)
21
epoch_loss.append(loss.cpu().data)
22
23
_, pred = torch.max(ypred, axis=1)
24
pred_list.append(pred.cpu().numpy())
25
label_list.append(label.cpu().numpy())
26
27
#Backward
28
optimizer.zero_grad()
29
loss.backward()
30
optimizer.step()
31
32
epoch_loss = np.asarray(epoch_loss)
33
pred_list = np.asarray(pred_list).ravel()
34
label_list = np.asarray(label_list).ravel()
35
36
acc = accuracy_score(pred_list, label_list)
37
38
end = time.time()
39
print('#################### Train ####################')
40
print('Epoch %d, Loss: %.4f +/- %.4f, Acc: %.2f, Time: %.2f' % (epoch, epoch_loss.mean(),
41
epoch_loss.std(), acc*100, end-start))
42
43
return epoch_loss.mean()
44
45
46
for epoch in range(args['epoch_num']):
47
train(train_loader, net, epoch)
48
break #Testing
49
Model already is in cuda, but i get error that says
JavaScript
1
2
1
Input type is torch.FloatTensor and not torch.cuda.FloatTensor
2
Whats the problem with input.to(args['device'])
?
Advertisement
Answer
UPDATE: According to the OP, an aditional data.to(device)
before the train loop caused this issue.
you are probably getting a string like
0
orcuda
from args[‘device’]; you should do this:JavaScript112121'cpu') #pass your args['device'] ``` so then use `device` to move the
2model to GPU: ``` model.to(device) ```
3
4then call the model with:
5
6``` for batch,(data,label) in enumerate(train_loader):
7
8#Batch cast on the GPU
9data.to(device =device)
10label.to(device =device)
11
12