我无法在 GPU 而不是 CPU 上运行我的代码。
我正在尝试运行 FinBert 代码,该代码将文本的每个句子分类为正面、负面或中性。但是,这段代码需要很长时间才能在我的 CPU 上运行。因此,我试图在我的 GPU 上运行它,但到目前为止我还不知道该怎么做。
这是我要在 GPU 上执行的代码:
# first I am importing the necessary packages
import pandas as pd
from transformers import BertTokenizer, BertForSequenceClassification
from transformers import pipeline
import pandas as pd
import nltk
# then I load the pre-trained FinBert Model
finbert = BertForSequenceClassification.from_pretrained('yiyanghkust/finbert-
tone',num_labels=3)
tokenizer = BertTokenizer.from_pretrained('yiyanghkust/finbert-tone')
# build model and pipeline
nlp = pipeline("sentiment-analysis", model=finbert, tokenizer=tokenizer)
然后我运行以下 for 循环:
for i in range(0,len(data)):
print(i)
# access the text i from the data set
temp = data.iloc[i,1]
# tokenize the text to get the sentences of the text
sentences = nltk.sent_tokenize(temp)
# apply FinBert-Model onto the sentences and save results in the variable "results"
results = nlp(sentences)
# get the filename of call i from clean_data
# set j to 0 for the next for-loop
j = 0
# reset positive, neutral, and negative after each iteration over the second for-loop back to 0 to not inflate the count
positive = 0
neutral = 0
negative = 0
# Create for loop to check for each sentence within the text whether FinBert classifies this sentence
# as positive, neutral or negative
for j in range (0,len(results)):
label = results[j]["label"]
if label == "Positive":
positive = positive + 1
elif label == "Neutral":
neutral = neutral + 1
else:
negative = negative + 1
# Calculate the sentiment scores
percentage_positive = positive / len(results)
percentage_negative = negative / len(results)
net_score = percentage_positive - percentage_negative
# save the results in a DataFrame previously created
results.iloc[i,1] = percentage_positive
results.iloc[i,2] = percentage_negative
results.iloc[i,3] = net_score
我有 NVIDIA GeForce MX130。
如果我运行以下代码,它说有可用的 GPU:
import tensorflow as tf
print("Num GPUs Available: ", len(tf.config.list_physical_devices('GPU')))
output: Num GPUs Available: 1
这段代码打印出来:
tf.config.list_logical_devices('GPU')
output: [LogicalDevice(name='/device:GPU:0', device_type='GPU')]
我尝试以多种方式在 GPU 上运行代码,包括这个:
tf.debugging.set_log_device_placement(True)
# Place ops on the GPU
with tf.device('/GPU:0'):
-> my code here
并且运行以下代码也显示我的 GPU 可用:
import torch
use_cuda = torch.cuda.is_available()
print(use_cuda)
output: True
if use_cuda:
print('__CUDNN VERSION:', torch.backends.cudnn.version())
print('__Number CUDA Devices:', torch.cuda.device_count())
print('__CUDA Device Name:',torch.cuda.get_device_name(0))
print('__CUDA Device Total Memory
[GB]:',torch.cuda.get_device_properties(0).total_memory/1e9)
output:
[GB]:',torch.cuda.get_device_properties(0).total_memory/1e9)
__CUDNN VERSION: 8302
__Number CUDA Devices: 1
__CUDA Device Name: GeForce MX130
__CUDA Device Total Memory [GB]: 4.294967296
但我仍然无法在 GPU 上运行代码。 我错过了什么?
最好的, 凯尔
在PyTorch中,用户必须自己手动将模型、数据等移动到cuda。由于您的模型是 PyTorch,因此您需要将模型和数据都移动到
"cuda"
。别担心,你正朝着正确的方向前进:
finbert = BertForSequenceClassification.from_pretrained('yiyanghkust/finbert- tone',num_labels=3)
finbert.to("cuda")
...
nlp = pipeline("sentiment-analysis", model=finbert, tokenizer=tokenizer, device=0)
如果它仍然引发
RuntimeError
但具有相同的消息,您也可以添加带有 to.("cuda")
的行。