繁体   English   中英

如何将完整的字符串 output 转换为火炬张量

[英]How to convert a full string output into torch tensor

Having defined an API based on a transformer model which I send request to, I got as output an object of class string supposed to represent a torch tensor.

import requests
import json
import torch

response = requests.post("http://127.0.0.1:5000/predict", json="This is a test.")
data = response.json()
print(data)

tensor([[-5.5169e-01,  5.1988e-01, -1.4731e-01,  4.6096e-02, -5.5753e-02,
         -6.9530e-01,  7.3424e-01,  3.0014e-01,  4.0528e-01, -1.7587e-01,
          1.2586e-01,  5.6712e-01, -4.0757e-01,  1.5796e-01,  9.4700e-01,
          6.2967e-01,  3.1027e-01,  1.0169e-02,  3.1380e-02,  1.2585e-01,
          2.3633e-01,  6.0813e-01, -1.0548e+00, -1.8581e-01, -5.9870e-02,
          1.2952e-01, -3.8818e-01,  2.3425e-01]])

我检查了requests模块文档,但找不到任何方法将这个完整的字符串转换为火炬张量?

您可以使用eval来评估字符串中包含的代码:

from torch import tensor

my_tensor = eval(data)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM