- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在运行以下代码。如果我尝试仅在 CPU 上运行它,它运行良好,但需要太多时间来训练。所以我想到将运行时改为GPU并进行了适当的更改。现在卡住了。
import torch
from models.bert_attention_model import AttentionModel
from models.bert_cnn_model import BERTCNNSentiment
import sys
if sys.argv[1].lower() =="hinglish":
data_path = "../data/hinglish/"
elif sys.argv[1].lower() == "spanglish":
data_path = "../data/spanglish/"
else:
print("Format: %s %s" %(argv[0], argv[1]))
train_name = "train.txt"
test_name = "test.txt"
model_save_names = ["../checkpoint/cnn_model.txt", "../checkpoint/attention_model.txt"]
import random
import numpy as np
SEED = 1234
random.seed(SEED)
np.random.seed(SEED)
torch.manual_seed(SEED)
torch.backends.cudnn.deterministic = True
from transformers import BertTokenizer, AutoTokenizer, XLMRobertaTokenizer
tokenizer = XLMRobertaTokenizer.from_pretrained("xlm-roberta-base")
print('XLM Roberta Tokenizer Loaded...')
init_token_idx = tokenizer.cls_token_id
eos_token_idx = tokenizer.sep_token_id
pad_token_idx = tokenizer.pad_token_id
unk_token_idx = tokenizer.unk_token_id
max_input_length = 150
print("Max input length: %d" %(max_input_length))
def tokenize_and_cut(sentence):
tokens = tokenizer.tokenize(sentence)
tokens = tokens[:max_input_length-2]
return tokens
from torchtext import data
UID = data.Field(sequential=False, use_vocab=False, pad_token=None)
TEXT = data.Field(batch_first = True,
use_vocab = False,
tokenize = tokenize_and_cut,
preprocessing = tokenizer.convert_tokens_to_ids,
init_token = init_token_idx,
eos_token = eos_token_idx,
pad_token = pad_token_idx,
unk_token = unk_token_idx)
LABEL = data.LabelField()
from torchtext import datasets
fields = [('uid',UID),('text', TEXT),('label', LABEL)]
train_data, test_data = data.TabularDataset.splits(
path = data_path,
train = train_name,
test = test_name,
format = 'tsv',
fields = fields,
skip_header = True)
train_data, valid_data = train_data.split(random_state = random.seed(SEED))
print('Data loading complete')
print(f"Number of training examples: {len(train_data)}")
print(f"Number of validation examples: {len(valid_data)}")
print(f"Number of test examples: {len(test_data)}")
tokens = tokenizer.convert_ids_to_tokens(vars(train_data.examples[0])['text'])
LABEL.build_vocab(train_data, valid_data)
print(LABEL.vocab.stoi)
BATCH_SIZE = 128
device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
print("Device in use:",device)
train_iterator, valid_iterator, test_iterator = data.BucketIterator.splits(
(train_data, valid_data, test_data),
sort_key=lambda x: len(x.text),
batch_size = BATCH_SIZE,
device = device)
print('Iterators created')
print('Downloading XLM Roberta model...')
from transformers import XLMRobertaModel
bert = XLMRobertaModel.from_pretrained('xlm-roberta-base')
import torch.nn as nn
import torch.nn.functional as F
from torch.autograd import Variable
print('XLM Roberta model downloaded')
OUTPUT_DIM = 3
DROPOUT = 0.3
N_FILTERS = 100
FILTER_SIZES = [2,3,4]
HIDDEN_DIM = 100
model_names = ["CNN_Model", "Attention_Model"]
models = [ BERTCNNSentiment(bert, OUTPUT_DIM, DROPOUT, N_FILTERS, FILTER_SIZES),
AttentionModel(bert, BATCH_SIZE, OUTPUT_DIM, HIDDEN_DIM, 50000, 768) ]
def count_parameters(model):
return sum(p.numel() for p in model.parameters() if p.requires_grad)
for i in range(2):
print(f'The {models[i]} has {count_parameters(models[i]):,} trainable parameters')
for i in range(2):
print("Parameters for " + f'{model_names[i]}')
for name, param in models[i].named_parameters():
if param.requires_grad:
print(name)
import torch.optim as optim
from sklearn.metrics import confusion_matrix
def clip_gradient(model, clip_value):
params = list(filter(lambda p: p.grad is not None, model.parameters()))
for p in params:
p.grad.data.clamp_(-clip_value, clip_value)
optimizers = [optim.Adam(models[0].parameters()), optim.Adam(models[1].parameters())]
criterion = nn.CrossEntropyLoss()
nll_loss = nn.NLLLoss()
log_softmax = nn.LogSoftmax()
for i in range(2):
models[i] = models[i].to(device)
criterion = criterion.to(device)
nll_loss = nll_loss.to(device)
log_softmax = log_softmax.to(device)
from sklearn.metrics import f1_score
def categorical_accuracy(preds, y):
count0,count1,count2 = torch.zeros(1),torch.zeros(1),torch.zeros(1)
count0 = torch.zeros(1).to(device)
count1 = torch.zeros(1).to(device)
count2 = torch.zeros(1).to(device)
total0,total1,total2 = torch.FloatTensor(1),torch.FloatTensor(1),torch.FloatTensor(1)
max_preds = preds.argmax(dim = 1, keepdim = True) # get the index of the max probability
correct = max_preds.squeeze(1).eq(y)
predictions = max_preds.squeeze(1)
true_correct = [0,0,0]
for j,i in enumerate(y.cpu().numpy()):
true_correct[y.cpu().numpy()[j]]+=1
if i==0:
count0+=correct[j]
total0+=1
elif i==1:
count1+=correct[j]
total1+=1
elif i==2:
count2+=correct[j]
else:
total2+=1
metric=torch.FloatTensor([count0/true_correct[0],count1/true_correct[1],count2/true_correct[2],f1_score(y.cpu().numpy(),predictions.cpu().numpy(),average='macro')])
return correct.sum() / torch.FloatTensor([y.shape[0]]),metric,confusion_matrix(y.cpu().numpy(),max_preds.cpu().numpy())
def train(model, iterator, optimizer, criterion, i):
epoch_loss = 0
epoch_acc = 0
model.train()
for batch in iterator:
optimizer.zero_grad()
if (i == 0):
predictions = model(batch.text).squeeze(1)
else:
predictions = model(batch.text, batch_size = len(batch)).squeeze(1)
loss = criterion(predictions, batch.label)
acc,_,_ = categorical_accuracy(predictions, batch.label)
loss.backward()
clip_gradient(model, 1e-1)
optimizer.step()
epoch_loss += loss.item()
epoch_acc += acc.item()
return epoch_loss / len(iterator), epoch_acc / len(iterator)
def evaluate(model, iterator, criterion, i):
epoch_loss = 0
epoch_acc = 0
epoch_all_acc = torch.FloatTensor([0,0,0,0])
confusion_mat = torch.zeros((3,3))
confusion_mat_temp = torch.zeros((3,3))
model.eval()
with torch.no_grad():
for batch in iterator:
if (i == 0):
predictions = model(batch.text).squeeze(1)
else:
predictions = model(batch.text,batch_size=len(batch)).squeeze(1)
loss = criterion(predictions, batch.label)
acc,all_acc,confusion_mat_temp = categorical_accuracy(predictions, batch.label)
epoch_loss += loss.item()
epoch_acc += acc.item()
epoch_all_acc += all_acc
confusion_mat+=confusion_mat_temp
return epoch_loss / len(iterator), epoch_acc / len(iterator),epoch_all_acc/len(iterator),confusion_mat
import time
def epoch_time(start_time, end_time):
elapsed_time = end_time - start_time
elapsed_mins = int(elapsed_time / 60)
elapsed_secs = int(elapsed_time - (elapsed_mins * 60))
return elapsed_mins, elapsed_secs
N_EPOCHS = 40
best_f1 = [-1, -1]
for epoch in range(N_EPOCHS):
for i in range(2):
start_time = time.time()
train_loss, train_acc = train(models[i], train_iterator, optimizers[i], criterion, i)
valid_loss, valid_acc,tot,conf = evaluate(models[i], valid_iterator, criterion, i)
f1 = tot[3]
end_time = time.time()
epoch_mins, epoch_secs = epoch_time(start_time, end_time)
if f1 > best_f1[i]:
best_f1[i] = f1
path = model_save_names[i]
print(path)
torch.save(models[i].state_dict(), path)
print(f'Epoch: {epoch+1:02} | Epoch Time: {epoch_mins}m {epoch_secs}s')
print(f'\tTrain Loss: {train_loss:.3f} | Train Acc: {train_acc*100:.2f}%')
print(f'\t Val. Loss: {valid_loss:.3f} | Val. Acc: {valid_acc*100:.2f}%')
print(tot)
print(conf)
for i in range(2):
path = model_save_names[i]
models[i].load_state_dict(torch.load(path))
def ensemble_evaluate(models, iterator, criterion):
epoch_loss = 0
epoch_acc = 0
epoch_all_acc = torch.FloatTensor([0,0,0,0])
models[0].eval()
models[1].eval()
confusion_mat = torch.zeros((3,3))
confusion_mat_temp = torch.zeros((3,3))
with torch.no_grad():
for batch in iterator:
predictions0 = models[0](batch.text).squeeze(1)
predictions1 = models[1](batch.text, batch_size=len(batch)).squeeze(1)
predictions = F.softmax(predictions0, dim=1) * F.softmax(predictions1, dim=1)
loss = criterion(predictions, batch.label)
acc,all_acc,confusion_mat_temp = categorical_accuracy(predictions, batch.label)
epoch_loss += loss.item()
epoch_acc += acc.item()
epoch_all_acc += all_acc
confusion_mat += confusion_mat_temp
print(confusion_mat)
return epoch_loss / len(iterator), epoch_acc / len(iterator),epoch_all_acc/len(iterator)
def ensemble_write_to_file(models, test_iterator):
label_dict = {'0':'negative', '1':'neutral', '2':'positive'}
file = open("answer.txt", "w")
file.write('Uid,Sentiment\n')
count = 0
for batch in test_iterator:
predictions0 = models[0](batch.text).squeeze(1)
predictions1 = models[1](batch.text, batch_size=len(batch)).squeeze(1)
predictions = F.softmax(predictions0, dim=1) * F.softmax(predictions1, dim=1)
max_preds = predictions.argmax(dim = 1, keepdim = True).detach().cpu().numpy()
for i,row in enumerate(batch.uid.cpu().numpy()):
count += 1
label_number = max_preds[i][0]
label_number_str = list(LABEL.vocab.stoi.keys())[list(LABEL.vocab.stoi.values()).index(label_number)]
predicted_label_name = label_dict[label_number_str]
if count != len(test_data):
file.write('%s,%s\n'%(row,predicted_label_name))
else:
file.write('%s,%s'%(row,predicted_label_name))
file.close()
valid_loss, valid_acc, tot = ensemble_evaluate(models, test_iterator, criterion)
print(f'\t Val. Loss: {valid_loss:.3f} | Val. Acc: {valid_acc*100:.2f}%')
print(tot)
这是我得到的输出。我只显示了调试所需的输出
Traceback (most recent call last):
File "main.py", line 268, in <module>
train_loss, train_acc = train(models[i], train_iterator, optimizers[i], criterion, i)
File "main.py", line 212, in train
acc,_,_ = categorical_accuracy(predictions, batch.label)
File "main.py", line 184, in categorical_accuracy
count1+=correct[j]
RuntimeError: Expected all tensors to be on the same device, but found at least two devices, cuda:0 and cpu!
有人可以告诉我在哪里进行适当的更改,以便代码可以在 Google Colab 的 GPU 上运行吗?
编辑_1:非常感谢您提供的帮助,它实际上解决了我的问题,但现在出现了类似的新问题。请注意,我已更新代码以包含您提到的更改
Traceback (most recent call last):
File "main.py", line 271, in <module>
train_loss, train_acc = train(models[i], train_iterator, optimizers[i], criterion, i)
File "main.py", line 215, in train
acc,_,_ = categorical_accuracy(predictions, batch.label)
File "main.py", line 194, in categorical_accuracy
return correct.sum() / torch.FloatTensor([y.shape[0]]),metric,confusion_matrix(y.cpu().numpy(),max_preds.cpu().numpy())
RuntimeError: Expected all tensors to be on the same device, but found at least two devices, cuda:0 and cpu!
最佳答案
问题正如错误所述,Pytorch 希望所有操作都在同一设备中完成,但您添加的两个张量位于不同的位置。
您需要将 .to(device)
添加到这些变量
count0,count1,count2 = torch.zeros(1),torch.zeros(1),torch.zeros(1)
喜欢
count0 = torch.zeros(1).to(device)
count1 = torch.zeros(1).to(device)
count2 = torch.zeros(1).to(device)
但以防万一,如果出现 NameError: name 'device' is not Defined
,您可以只使用 y
或 pred
的设备就像
device = y.device
count0 = torch.zeros(1).to(device)
count1 = torch.zeros(1).to(device)
count2 = torch.zeros(1).to(device)
关于python - 卡在这个错误 "RuntimeError: Expected all tensors to be on the same device, but found at least two devices, cuda:0 and cpu",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/67251758/
就目前而言,这个问题不适合我们的问答形式。我们希望答案得到事实、引用资料或专业知识的支持,但这个问题可能会引发辩论、争论、投票或扩展讨论。如果您觉得这个问题可以改进并可能重新打开,visit the
我是脚本新手。如何编写 Expect 脚本以通过 ssh 连接到设备并提示用户输入密码?我们使用 pin + RSA token 代码作为密码,因此我无法存储密码。 #!/usr/bin/expect
我编写了以下代码并尝试执行它。但我在执行 do {”时遇到“无效的命令名称“do”” 代码: #!/usr/bin/expect set val 0; set input 5; do { pu
我已经查看了 Expect 联机帮助页并用 Google 搜索了它,但我还没有找到 expect 的 -r 是什么。我看到这个选项以前是这样用的 expect -r "\r\n\r\n" 在 expe
我的 shebang 看起来像这样: #!/usr/bin/expect -d 当我从命令行运行脚本时,它会提供我想要的内容。 但是,我通过 crontab 运行这个脚本。是否可以将调试开关保持打开状
我是 Expect 脚本的新手。 我在 Linux 机器上为 ssh 编写了一个 Expect 脚本,在那里我在 ssh 到不同的 Linux 机器时遇到了问题。下面我复制了脚本。 !/usr/loc
Scene 1, Layer 'script', Frame 1, Line 9 1084: Syntax error: expecting identifier before this. Sc
我正在运行调试命令以将命令的输出记录到文件中。我尝试了 log_file 命令,但它没有记录输出。我的代码如下: log_file -a gdb.txt send "~/debugulator.sh
我希望 expect_user 有一个无限的(或非常大的)超时和 expect 的默认超时。有没有办法设置不同的超时?或者我是否只需要在每次更改用途之前手动执行此操作? 最佳答案 expect 和ex
我正在学习 iOS 编程(我来自 Android),我正在寻找更容易获取字符串的方法。有了这个建议,我定义了下一个宏并在一些代码片段中使用它: #define STRING_BASE @"InfoPl
你好我是 rspec 的新手,我想弄清楚将 block 传递给 expect{} 和只使用 expect() 之间的区别 这是一个简单的例子 require "rails_helper" RSpec.
我正在尝试为 React JS 运行单元测试 - 使用 jest/enzyme。 目前测试失败。不太清楚为什么,也许我没有正确调用 expect(wrapper.find)。这是我测试的一部分: F
例如,现在我有一个“root.exp”期望脚本如下: spawn ssh user@ip expect "Password:" send "password" 然后,我要发送到这个ssh服务器的exp
您好,我是 Expect 脚本编写的新手,我一直在尝试使用以下方法将 IP 地址获取到变量中: set timeout -1 spawn $env(SHELL) match_max 100000 se
expect.anything() 不适用于 expect.toBe(),但适用于 expect.toEqual() test("this will pass", () => { expect("
我有一个如下所示的简单脚本,从命令行读取 2 个数字并将它们加在一起: $cat runexp.sh #!/bin/bash echo "read 1st number" read n1 echo "
当 Linux 机器的 $IP 登录后询问密码时,下面的 expect 脚本工作正常 但在某些情况下,某些Linux机器不需要ssh密码(我们可以不用密码登录), 所以我需要更改我的期望脚本以支持没有
我正在尝试使用 expect 远程登录服务器并更改用户密码。该应用程序要求,如果您要更改的密码包含特殊字符,则将其引用。问题是,还需要引用 expect send 语句,当我尝试将两者结合起来时,脚本
下面这个简单的 expect 脚本的目标是获取远程机器上的 hostname 名称 有时期望脚本无法执行到 $IP_ADDRESS 的 ssh(因为远程机器不活动等) 所以在这种情况下,expect
我试图创建一个宏来替换, first: Some(first.as_ref().parse::().expect("Could not parse 'first'")) 我在其他模块(如 Clap w
我是一名优秀的程序员,十分优秀!