我已经按照建议的by Google's tensor2tensor repository遵循了colab笔记本的翻译教程
在导出模型并将其上传到Google的AI平台引擎进行在线预测后,我在向模型发出请求时遇到了问题。
我认为翻译模型的输入是源文本的张量。但是我收到一个错误,TypeError: Object of type 'EagerTensor' is not JSON serializable
def encode(input_str, output_str=None):
"""Input str to features dict, ready for inference"""
inputs = encoders["inputs"].encode(input_str) + [1] # add EOS id
batch_inputs = tf.reshape(inputs, [1, -1, 1]) # Make it 3D.
return {"inputs": batch_inputs}
enfr_problem = problems.problem(PROBLEM)
encoders = enfr_problem.feature_encoders(DATA_DIR)
encoded_inputs = encode("Some text")
model_output = predict_json('project_name','model_name', encoded_inputs,'version_1')["outputs"]我试着将张量转换为numpy,但仍然没有成功。有人能给我指个方向吗?
发布于 2020-11-18 16:57:28
问题是,当您这样做时,TensorFlow会返回一个EagerTensor:
inputs = encoders["inputs"].encode(input_str) + [1] # add EOS id
batch_inputs = tf.reshape(inputs, [1, -1, 1])并且不能将EagerTensor转换为JSON。不幸的是,3D numpy数组也不能转换为JSON。但是numpy数组可以很容易地转换为列表。举个例子:
import json
import numpy as np
import tensorflow as tf
a = np.array([1, 2, 3])
b = np.array([1, 2, 3])
c = tf.multiply(a, b)
print(c) # -> <tf.Tensor: shape=(3,), dtype=int64, numpy=array([1, 4, 9])>
print(c.numpy()) # -> array([1, 4, 9])
print(c.numpy().tolist()) # -> [1, 4, 9]
with open("example.json", "w") as f:
json.dump(c, f) # TypeError: Object of type EagerTensor is not JSON serializable
json.dump(c.numpy(), f) # TypeError: Object of type ndarray is not JSON serializable
json.dump(c.numpy().tolist(), f) # works!我无法提供与您的情况完全相同的示例,因为您的代码片段不够完整。但
return {"inputs": batch_inputs.numpy().tolist()}应该能胜任这项工作。
发布于 2021-02-13 08:47:11
如果您想要将dict中的张量数据保存到JSON文件中,一个简单的解决方案是递归地进入您的字典,并使用正确的函数将您的数据转换为Json中可序列化的数据(例如,如果只是为了保存字符串,则为字符串)。我相信tensorflow一定有办法将你的数据保存为pickle文件,如果这是你真正想做的(即保存你的数据)。
以下代码用于递归地将dict中的内容转换为字符串,但您应该能够根据您的用例轻松地修改和数值化、jsonify等代码。我的用例是以人类可读的格式保存数据(不仅仅是torch.save):
#%%
def _to_json_dict_with_strings(dictionary):
"""
Convert dict to dict with leafs only being strings. So it recursively makes keys to strings
if they are not dictionaries.
Use case:
- saving dictionary of tensors (convert the tensors to strins!)
- saving arguments from script (e.g. argparse) for it to be pretty
e.g.
"""
if type(dictionary) != dict:
return str(dictionary)
d = {k: _to_json_dict_with_strings(v) for k, v in dictionary.items()}
return d
def to_json(dic):
import types
import argparse
if type(dic) is dict:
dic = dict(dic)
else:
dic = dic.__dict__
return _to_json_dict_with_strings(dic)
def save_to_json_pretty(dic, path, mode='w', indent=4, sort_keys=True):
import json
with open(path, mode) as f:
json.dump(to_json(dic), f, indent=indent, sort_keys=sort_keys)
def my_pprint(dic):
"""
@param dic:
@return:
Note: this is not the same as pprint.
"""
import json
# make all keys strings recursively with their naitve str function
dic = to_json(dic)
# pretty print
pretty_dic = json.dumps(dic, indent=4, sort_keys=True)
print(pretty_dic)
# print(json.dumps(dic, indent=4, sort_keys=True))
# return pretty_dic
import torch
# import json # results in non serializabe errors for torch.Tensors
from pprint import pprint
dic = {'x': torch.randn(1, 3), 'rec': {'y': torch.randn(1, 3)}}
my_pprint(dic)
pprint(dic)输出:
{
"rec": {
"y": "tensor([[-0.3137, 0.3138, 1.2894]])"
},
"x": "tensor([[-1.5909, 0.0516, -1.5445]])"
}
{'rec': {'y': tensor([[-0.3137, 0.3138, 1.2894]])},
'x': tensor([[-1.5909, 0.0516, -1.5445]])}相关链接:
https://discuss.pytorch.org/t/typeerror-tensor-is-not-json-serializable/36065/3或How to prettyprint a JSON file?和https://github.com/fossasia/visdom/issues/554。
https://stackoverflow.com/questions/58220123
复制相似问题