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

Legolas: <p>Hi,<br>
I’m getting this error, but can’t find a way to solve it. I hope someone can help.</p>
<p><div class="lightbox-wrapper"><a class="lightbox" href="https://discuss.pytorch.org/uploads/default/original/3X/e/f/efe96d940332ddf3ea14bb206f7a28e2e28ebd44.jpeg" data-download-href="https://discuss.pytorch.org/uploads/default/efe96d940332ddf3ea14bb206f7a28e2e28ebd44" title="pytorch forum tensor problems3"><img src="https://discuss.pytorch.org/uploads/default/optimized/3X/e/f/efe96d940332ddf3ea14bb206f7a28e2e28ebd44_2_690x222.jpeg" alt="pytorch forum tensor problems3" data-base62-sha1="yemeqwlmgU7JCxkDF8dPozCwYEA" width="690" height="222" srcset="https://discuss.pytorch.org/uploads/default/optimized/3X/e/f/efe96d940332ddf3ea14bb206f7a28e2e28ebd44_2_690x222.jpeg, https://discuss.pytorch.org/uploads/default/original/3X/e/f/efe96d940332ddf3ea14bb206f7a28e2e28ebd44.jpeg 1.5x, https://discuss.pytorch.org/uploads/default/original/3X/e/f/efe96d940332ddf3ea14bb206f7a28e2e28ebd44.jpeg 2x" data-dominant-color="FAFAF9"><div class="meta"><svg class="fa d-icon d-icon-far-image svg-icon" aria-hidden="true"><use href="#far-image"></use></svg><span class="filename">pytorch forum tensor problems3</span><span class="informations">874×282 27.5 KB</span><svg class="fa d-icon d-icon-discourse-expand svg-icon" aria-hidden="true"><use href="#discourse-expand"></use></svg></div></a></div></p>
<p><div class="lightbox-wrapper"><a class="lightbox" href="https://discuss.pytorch.org/uploads/default/original/3X/f/d/fdddbc2d8a30c955dc5d62dce7a1c9fa33cf3b87.jpeg" data-download-href="https://discuss.pytorch.org/uploads/default/fdddbc2d8a30c955dc5d62dce7a1c9fa33cf3b87" title="pytorch forum tensor problems2"><img src="https://discuss.pytorch.org/uploads/default/optimized/3X/f/d/fdddbc2d8a30c955dc5d62dce7a1c9fa33cf3b87_2_461x499.jpeg" alt="pytorch forum tensor problems2" data-base62-sha1="AdNRDU7j1mLmtfNhbXP36ZcXqwn" width="461" height="499" srcset="https://discuss.pytorch.org/uploads/default/optimized/3X/f/d/fdddbc2d8a30c955dc5d62dce7a1c9fa33cf3b87_2_461x499.jpeg, https://discuss.pytorch.org/uploads/default/optimized/3X/f/d/fdddbc2d8a30c955dc5d62dce7a1c9fa33cf3b87_2_691x748.jpeg 1.5x, https://discuss.pytorch.org/uploads/default/original/3X/f/d/fdddbc2d8a30c955dc5d62dce7a1c9fa33cf3b87.jpeg 2x" data-dominant-color="F5F6F5"><div class="meta"><svg class="fa d-icon d-icon-far-image svg-icon" aria-hidden="true"><use href="#far-image"></use></svg><span class="filename">pytorch forum tensor problems2</span><span class="informations">770×835 93.8 KB</span><svg class="fa d-icon d-icon-discourse-expand svg-icon" aria-hidden="true"><use href="#discourse-expand"></use></svg></div></a></div></p>
<p>Thank you!</p>

ptrblck: <p>Could you post a code snippet to reproduce this issue, please?<br>
You can post code by wrapping it into three backticks ```, which makes debugging easier. <img src="https://discuss.pytorch.org/images/emoji/apple/wink.png?v=9" title=":wink:" class="emoji" alt=":wink:"></p>
<p>Based on the error message, if seems you are passing a Python type into the <code>to()</code> method, but it’s hard to help any further without seeing the real code.</p>

Legolas: <p>Hi,<br>
thank you for your time.<br>
Actually, I’m trying to repeat the results from this NLP tutorial: <a href="https://github.com/keitakurita/practical-torchtext/blob/master/Lesson%201%20intro%20to%20torchtext%20with%20text%20classification.ipynb" rel="nofollow noopener">https://github.com/keitakurita/practical-torchtext/blob/master/Lesson%201%20intro%20to%20torchtext%20with%20text%20classification.ipynb</a>.<br>
I can repeat result on CPU with small changes (loss.data[0] --&gt; loss.data) in training part.<br>
Of course, CPU training executes for ages and I wanna put it on GPU.<br>
After I make the following changes I get the error:</p>
<pre><code class="lang-auto">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.
)
</code></pre>
<pre><code class="lang-auto">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
</code></pre>
<pre><code class="lang-auto">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)
</code></pre>
<pre><code class="lang-auto">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)
</code></pre>
<pre><code class="lang-auto">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
</code></pre>
<pre><code class="lang-auto">
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))
</code></pre>
<pre><code class="lang-auto">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)
</code></pre>

ptrblck: <p>I guess this line of code raises the error:</p>
<pre><code class="lang-python">test_iter = Iterator(tst, batch_size=64, device=torch.device, sort=False, sort_within_batch=False, repeat=False)
</code></pre>
<p>since you are passing <code>torch.device</code> as the object type to the <code>device</code> argument, while you should pass an instance (<code>torch.device('cuda')</code>) or a <code>str</code> to it.</p>

Legolas: <p>Thank you…It works now <img src="https://discuss.pytorch.org/images/emoji/apple/slight_smile.png?v=9" title=":slight_smile:" class="emoji" alt=":slight_smile:"></p>