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) for index,row in df.iterrows()))
File "/asyn_emd.py", line 61, in count
print(words,row['course_name'],cos(i, 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!