`device`参数应使用`torch.device`设置或传入字符串作为参数

我的数据迭代器目前在CPU上运行,因为device=0参数已被弃用。但我需要它与模型的其余部分一起在GPU上运行。

这是我的代码:

pad_idx = TGT.vocab.stoi["<blank>"]model = make_model(len(SRC.vocab), len(TGT.vocab), N=6)model = model.to(device)criterion = LabelSmoothing(size=len(TGT.vocab), padding_idx=pad_idx, smoothing=0.1)criterion = criterion.to(device)BATCH_SIZE = 12000train_iter = MyIterator(train, device, batch_size=BATCH_SIZE,                        repeat=False, sort_key=lambda x: (len(x.src), len(x.trg)),                        batch_size_fn=batch_size_fn, train=True)valid_iter = MyIterator(val, device, batch_size=BATCH_SIZE,                        repeat=False, sort_key=lambda x: (len(x.src), len(x.trg)),                        batch_size_fn=batch_size_fn, train=False)#model_par = nn.DataParallel(model, device_ids=devices)

以上代码引发了以下错误:

The `device` argument should be set by using `torch.device` or passing a string as an argument. This behavior will be deprecated soon and currently defaults to cpu.The `device` argument should be set by using `torch.device` or passing a string as an argument. This behavior will be deprecated soon and currently defaults to cpu.

我尝试将'cuda'作为参数传入,而不是device=0,但收到了以下错误:

<ipython-input-50-da3b1f7ed907> in <module>()    10     train_iter = MyIterator(train, 'cuda', batch_size=BATCH_SIZE,    11                             repeat=False, sort_key=lambda x: (len(x.src), len(x.trg)),---> 12                             batch_size_fn=batch_size_fn, train=True)    13     valid_iter = MyIterator(val, 'cuda', batch_size=BATCH_SIZE,    14                             repeat=False, sort_key=lambda x: (len(x.src), len(x.trg)),TypeError: __init__() got multiple values for argument 'batch_size'

我也尝试将device作为参数传入。设备定义为device = torch.device('cuda:0' if torch.cuda.is_available() else 'cpu')

但收到了与上面相同的错误。

任何建议都将不胜感激,谢谢。


回答:

pad_idx = TGT.vocab.stoi["<blank>"]model = make_model(len(SRC.vocab), len(TGT.vocab), N=6)model = model.to(device)criterion = LabelSmoothing(size=len(TGT.vocab), padding_idx=pad_idx, smoothing=0.1)criterion = criterion.to(device)BATCH_SIZE = 12000train_iter = MyIterator(train, batch_size=BATCH_SIZE, device = torch.device('cuda'),                        repeat=False, sort_key=lambda x: (len(x.src), len(x.trg)),                        batch_size_fn=batch_size_fn, train=True)valid_iter = MyIterator(val, batch_size=BATCH_SIZE, device = torch.device('cuda'),                        repeat=False, sort_key=lambda x: (len(x.src), len(x.trg)),                        batch_size_fn=batch_size_fn, train=False)

经过多次尝试和错误后,我设法将device设置为device = torch.device('cuda'),而不是device=0

Related Posts

如何从数据集中移除EXIF数据?

我在尝试从数据集中的图像中移除EXIF数据(这些数据将…

用于Python中的“智能点”游戏的遗传算法不工作

过去几天我一直在尝试实现所谓的“智能点”游戏。我第一次…

哪个R平方得分更有帮助?

data.drop(‘Movie Title’, ax…

使用线性回归预测GRE分数对录取率的影响

我正在学习线性回归,并尝试在Jupyter笔记本中用P…

使用mlrMBO贝叶斯优化进行SVM超参数调优时出现错误

我试图针对一个分类任务优化SVM,这个方法在许多其他模…

Keras模型的二元交叉熵准确率未发生变化

我在网上看到了很多关于这个问题的提问,但没有找到明确的…

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注