gpt4 book ai didi

python - 如何在pytorch中将字符串列表转换为张量?

转载 作者:行者123 更新时间:2023-12-03 17:23:35 34 4
gpt4 key购买 nike

我正在使用 phytorch 制作和预测几个模型。
因为内存问题,我把张量列表做成了数据框,保存为Excel。
之后,我尝试通过加载存储在Excel中的数据来预测模型,但是当我调用Excel时,张量列表变成了一个str列表。
如何将此 str 列表更改回张量列表?
我将引用部分代码,原始张量。


def BERT_reasoning(tokens_tensor, segments_tensors):
model.eval()
predictions=[]
for i in range(len(tokens_tensor)):
if torch.cuda.is_available():
tokens_tensor[i] = tokens_tensor[i].to('cuda')
segments_tensors[i] = segments_tensors[i].to('cuda')
model.to('cuda')
with torch.no_grad():
outputs = model(tokens_tensor[i], token_type_ids=segments_tensors[i])
predictions.append(outputs[0])
torch.cuda.empty_cache()
return(predictions)


predictions=[0 for i in range(len(target))]
for i in tqdm(range(len(target))):
predictions[0]=BERT_reasoning(tokens_tensor[i],segments_tensors[i])
globals()['df_pred_{}'.format(i)]=pd.DataFrame(predictions[0])
del predictions[0]
excel_name='prediction_{}.xlsx'.format(i)
globals()['df_pred_{}'.format(i)].to_excel(excel_name)
del globals()['df_pred_{}'.format(i)]
torch.cuda.empty_cache()



Result :
orginal tensor -
tensor([[[ -7.2395, -7.2337, -7.2301, ..., -6.6463, -6.5081, -4.4686],
[ -8.1057, -8.1946, -8.0791, ..., -8.4518, -7.6345, -5.3930],
[-10.7883, -10.6919, -10.5438, ..., -9.9607, -10.0536, -6.8828],
...,
[ -9.0698, -9.3295, -8.9949, ..., -6.1696, -7.4357, -7.4828],
[ -6.3161, -6.4182, -6.5455, ..., -5.5366, -5.7362, -2.2207],
[-12.0209, -11.9511, -12.0039, ..., -11.8723, -9.6545, -8.2306]]],
device='cuda:0')

changed str
"tensor([[[ -7.2395, -7.2337, -7.2301, ..., -6.6463, -6.5081, -4.4686],\n [ -8.1057, -8.1946, -8.0791, ..., -8.4518, -7.6345, -5.3930],\n [-10.7883, -10.6919, -10.5438, ..., -9.9607, -10.0536, -6.8828],\n ...,\n [ -9.0698, -9.3295, -8.9949, ..., -6.1696, -7.4357, -7.4828],\n [ -6.3161, -6.4182, -6.5455, ..., -5.5366, -5.7362, -2.2207],\n [-12.0209, -11.9511, -12.0039, ..., -11.8723, -9.6545, -8.2306]]],\n device='cuda:0')"

最佳答案

您可以使用内置的 eval函数从字符串中获取张量。请注意,您的张量不应包含省略号(即“...”),因为张量不会被明确定义。所有值都应出现在您希望恢复的字符串中(否则,无法确定它们应该是什么)。
例子:

t = eval("tensor([[1,2,3],[4,5,6]], device='cuda:0')")

关于python - 如何在pytorch中将字符串列表转换为张量?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63937472/

34 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com