我正在使用 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],
...,
[ …Run Code Online (Sandbox Code Playgroud)