TypeError: tensor(): argument 'device' must be torch.device, not type

Hi,
I’m getting this error, but can’t find a way to solve it. I hope someone can help.

Thank you!

Could you post a code snippet to reproduce this issue, please?
You can post code by wrapping it into three backticks ```, which makes debugging easier. :wink:

Based on the error message, if seems you are passing a Python type into the to() method, but it’s hard to help any further without seeing the real code.

Hi,
thank you for your time.
Actually, I’m trying to repeat the results from this NLP tutorial: https://github.com/keitakurita/practical-torchtext/blob/master/Lesson%201%20intro%20to%20torchtext%20with%20text%20classification.ipynb.
I can repeat result on CPU with small changes (loss.data[0] --> loss.data) in training part.
Of course, CPU training executes for ages and I wanna put it on GPU.
After I make the following changes I get the error:

torch.backends.cudnn.deterministic = True
device = torch.device(“cuda:0”)

from torchtext.data import Iterator, BucketIterator
train_iter, val_iter = BucketIterator.splits(
(trn, vld), # we pass in the datasets we want the iterator to draw data from
batch_sizes=(64, 64),
device = torch.device(“cuda”), #also tried just torch.device
#device=-1,
#sort_key=lambda x: len(x.post),
sort_within_batch=False,
repeat=False # we pass repeat=False because we want to wrap this Iterator layer.
)
batch = next(train_iter. **iter** ()); batch

test_iter = Iterator(tst, batch_size=64, device=torch.device, sort=False, sort_within_batch=False, repeat=False)

class BatchWrapper:
def  **init** (self, dl, x_var, y_vars):
self.dl, self.x_var, self.y_vars = dl, x_var, y_vars # we pass in the list of attributes for x and y
def __iter__(self):
    for batch in self.dl:
        x = getattr(batch, self.x_var) # we assume only one input in this wrapper

        if self.y_vars is not None: # we will concatenate y into a single tensor
            y = torch.cat([getattr(batch, feat).unsqueeze(1) for feat in self.y_vars], dim=1).float()
        else:
            y = torch.zeros((1))

        yield (x, y)

def __len__(self):
    return len(self.dl)
class SimpleBiLSTMBaseline(nn.Module):
def  **init** (self, hidden_dim, emb_dim=300,
spatial_dropout=0.05, recurrent_dropout=0.1, num_linear=1):
super(). **init** () # don’t forget to call this!
self.embedding = nn.Embedding(len(TEXT.vocab), emb_dim)
self.encoder = nn.LSTM(emb_dim, hidden_dim, num_layers=1, dropout=recurrent_dropout)
self.linear_layers = []
for _ in range(num_linear - 1):
self.linear_layers.append(nn.Linear(hidden_dim, hidden_dim))
self.linear_layers = nn.ModuleList(self.linear_layers)
self.predictor = nn.Linear(hidden_dim, 16)
def forward(self, seq):
    hdn, _ = self.encoder(self.embedding(seq))
    feature = hdn[-1, :, :]
    for layer in self.linear_layers:
        feature = layer(feature)
    preds = self.predictor(feature)
    return preds

em_sz = 100
nh = 500
nl = 3
model = SimpleBiLSTMBaseline(nh, emb_dim=em_sz); model

model.cuda()
import tqdm
opt = optim.Adam(model.parameters(), lr=1e-2)
loss_func = nn.BCEWithLogitsLoss()
epochs = 2
%%time
for epoch in range(1, epochs + 1):
running_loss = 0.0
running_corrects = 0
model.train() # turn on training mode
for x, y in tqdm.tqdm(train_dl): # thanks to our wrapper, we can intuitively iterate over our data!
opt.zero_grad()
    preds = model(x)
    loss = loss_func(preds, y)
    loss.backward()
    opt.step()
    running_loss += loss.data * x.size(1)
epoch_loss = running_loss / len(trn)
# calculate the validation loss for this epoch
val_loss = 0.0
model.eval() # turn on evaluation mode
for x, y in valid_dl:
    preds = model(x)
    loss = loss_func(preds, y)
    val_loss += loss.data * x.size(1) 
val_loss /= len(vld)
print('Epoch: {}, Training Loss: {:.4f}, Validation Loss: {:.4f}'.format(epoch, epoch_loss, val_loss))
test_preds = []
for x, y in tqdm.tqdm(test_dl):
preds = model(x)  # if you’re data is on the GPU, you need to move the data back to the cpu
preds = preds.data.cpu().numpy()
#preds = preds.data.numpy() # the actual outputs of the model are logits, so we need to pass these values to the sigmoid function
preds = 1 / (1 + np.exp(-preds))
test_preds.append(preds)
#test_preds = np.hstack(test_preds)
test_preds = np.vstack(test_preds)

I guess this line of code raises the error:

test_iter = Iterator(tst, batch_size=64, device=torch.device, sort=False, sort_within_batch=False, repeat=False)

since you are passing torch.device as the object type to the device argument, while you should pass an instance (torch.device('cuda')) or a str to it.

Thank you…It works now :slight_smile: