# Huggingface transformer sequence classification

**URL:** <https://discuss.huggingface.co/t/huggingface-transformer-sequence-classification/16104>\
**Category:** 🤗Transformers\
**Created:** [March 26, 2022, 2:52am UTC](https://discuss.huggingface.co/t/huggingface-transformer-sequence-classification/16104 "2022-03-26T02:52:29Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![nitempe](https://avatars.discourse-cdn.com/v4/letter/n/54ee81/32.png) [@nitempe](https://discuss.huggingface.co/u/nitempe)\
**Post date:** [March 26, 2022, 2:52am UTC](https://discuss.huggingface.co/t/huggingface-transformer-sequence-classification/16104/1 "2022-03-26T02:52:29Z")

</div>

I am fine tuning a transformer and I would like to use GPUs

[This](https://github.com/huggingface/notebooks/blob/main/examples/text_classification.ipynb) page shows how we to do the same

```
trainer = Trainer(
    model=model,
    args=training_args,
    compute_metrics=compute_metrics,
    train_dataset=train_df_tuning_dataset_tokenized,
    eval_dataset=val_dataset_tokenized
)

```

Do I need to modify above part like below? I am adding `.to(device)` to ensure data is on GPU

```
device = 'cuda' if torch.cuda.is_available() else 'cpu'
#device = 'cpu'
device

trainer = Trainer(
    model=model,
    args=training_args,
    compute_metrics=compute_metrics,
    train_dataset=train_df_tuning_dataset_tokenized.to(device),
    eval_dataset=val_dataset_tokenized.to(device)
)

```

---

<div class="post-metadata">

**Author:** ![bengul](https://avatars.discourse-cdn.com/v4/letter/b/838e76/32.png) [@bengul](https://discuss.huggingface.co/u/bengul)\
**Post date:** [March 26, 2022, 3:19am UTC](https://discuss.huggingface.co/t/huggingface-transformer-sequence-classification/16104/2 "2022-03-26T03:19:09Z")

</div>

I am not 100% sure, but I think you need to send the dataset and the model to device before you call the Trainer.  
Something like this might work,

```auto
import torch
device = torch.device('cuda') if torch.cuda.is_available() else torch.device('cpu')

model.to(device)
train_df_tuning_dataset_tokenized.to(device)
val_dataset_tokenized.to(device)

trainer = Trainer(
    model=model,
    args=training_args,
    compute_metrics=compute_metrics,
    train_dataset=train_df_tuning_dataset_tokenized,
    eval_dataset=val_dataset_tokenized
)

```

---

<div class="post-metadata">

**Author:** ![nitempe](https://avatars.discourse-cdn.com/v4/letter/n/54ee81/32.png) [@nitempe](https://discuss.huggingface.co/u/nitempe)\
**Post date:** [March 26, 2022, 3:32am UTC](https://discuss.huggingface.co/t/huggingface-transformer-sequence-classification/16104/3 "2022-03-26T03:32:14Z")

</div>

lets hope we get a confirmation. Thanks for pointing out that model needs to be on gpu too

---

<div class="post-metadata">

**Author:** ![nielsr](https://sea2.discourse-cdn.com/hellohellohello/user_avatar/discuss.huggingface.co/nielsr/32/39617_2.png) [@nielsr](https://discuss.huggingface.co/u/nielsr)\
**Post date:** [March 26, 2022, 12:08pm UTC](https://discuss.huggingface.co/t/huggingface-transformer-sequence-classification/16104/4 "2022-03-26T12:08:42Z")

</div>

Hi,

You shouldn’t actually have any device placements, the Trainer handles this for you. The Trainer has an attribute called `place_model_on_device` which is set to `True` by default.

The Trainer will also handle device placement of your data.
