如何解决RuntimeError:预期所有张量都在同一设备上,但发现至少两个设备cuda:0和cpu
import asyncio
import torch
import os
import pandas as pd
from flair.data import Sentence
from flair.embeddings import FlairEmbeddings,DocumentPoolEmbeddings,WordEmbeddings
device = torch.device("cpu")
print(device)
# first,declare how you want to embed
embeddings = DocumentPoolEmbeddings(
[WordEmbeddings('glove'),FlairEmbeddings('news-forward'),FlairEmbeddings('news-backward')])
path = os.getcwd()
df=pd.read_pickle(path+'/embedding_all_courses_2.pkl')
query_emd=[]
cos = torch.nn.CosineSimilarity(dim=0,eps=1e-6)
query= Sentence("some text")
embeddings.embed([query])
query_emd.append(query.embedding)
async def count(index,row):
for i in query_emd:
print(words,row['course_name'],cos(i,row['embedding']))
print(index)
async def main():
await asyncio.gather(*(count(index,row) for index,row in df.iterrows()))
if __name__ == "__main__":
import time
s = time.perf_counter()
asyncio.run(main())
elapsed = time.perf_counter() - s
print(f"{__file__} executed in {elapsed:0.2f} seconds.")
尝试在asyncio包中运行pytorch余弦相似度以获取并行结果。使用天赋模型嵌入文本。需要将文本与巨大的数据框进行比较,并获得最相似的文本,结果响应速度应该非常快。您能否也建议一种替代方法?我还需要仅在CPU内存上而不是在GPU Cuda系统上运行此代码
错误:
raceback (most recent call last):
File "asyn_emd.py",line 74,in <module>
asyncio.run(main())
File "/home/linuxbrew/.linuxbrew/Cellar/python/3.7.5/lib/python3.7/asyncio/runners.py",line 43,in run
return loop.run_until_complete(main)
File "/home/linuxbrew/.linuxbrew/Cellar/python/3.7.5/lib/python3.7/asyncio/base_events.py",line 579,in run_until_complete
return future.result()
File "/asyn_emd.py",line 68,in main
await asyncio.gather(*(count(index,row in df.iterrows()))
File "/asyn_emd.py",line 61,in count
print(words,row['embedding']))
File "/home/karthickaravindan/.virtualenvs/test/lib/python3.7/site-packages/torch/nn/modules/module.py",line 722,in _call_impl
result = self.forward(*input,**kwargs)
File "/home/karthickaravindan/.virtualenvs/test/lib/python3.7/site-packages/torch/nn/modules/distance.py",line 75,in forward
return F.cosine_similarity(x1,x2,self.dim,self.eps)
RuntimeError: Expected all tensors to be on the same device,but found at least two devices,cuda:0 and cpu!
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。