简体   繁体   English

翻译 Model 预测:TypeError: Object of type 'EagerTensor' is not JSON serializable

[英]Translation Model Predictions: TypeError: Object of type 'EagerTensor' is not JSON serializable

I have followed the translation colab notebook tutorial as suggested by Google's tensor2tensor repository我按照Google 的 tensor2tensor 存储库的建议遵循了翻译 colab 笔记本教程

After exporting the model and uploading it to Google's AI Platform engine for online prediction, I am having trouble making requests to the model.在导出 model 并将其上传到 Google 的 AI Platform 引擎进行在线预测后,我无法向 model 发出请求。

I believe the input to the translation model is a tensor of the source text.我相信翻译 model 的输入是源文本的张量。 But I am receiving an error that TypeError: Object of type 'EagerTensor' is not JSON serializable但我收到一个错误, 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"]

I've tried converting the tensor to numpy but still no luck.我尝试将张量转换为 numpy 但仍然没有运气。 Could someone point me in the right direction?有人能指出我正确的方向吗?

The problem is that TensorFlow returns an EagerTensor when you do:问题是 TensorFlow 在您执行以下操作时会返回 EagerTensor:

inputs = encoders["inputs"].encode(input_str) + [1]  # add EOS id
batch_inputs = tf.reshape(inputs, [1, -1, 1])

And EagerTensor cannot be converted to JSON.并且 EagerTensor 无法转换为 JSON。 Unfortunately an 3D numpy array cannot be converted to JSON too.不幸的是,3D numpy 数组也无法转换为 JSON。 But numpy arrays can be converted to lists easily.但是 numpy arrays 可以轻松转换为列表。 An example:一个例子:

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!

I cannot provide an example for your exact case because your code snippet is not complete enough.我无法为您的确切情况提供示例,因为您的代码片段不够完整。 But

return {"inputs": batch_inputs.numpy().tolist()}

should do the job.应该做的工作。

If you want to save your tensor data in a dict to a JSON file one simple solution is to recursively go into your dictionary and use the right function to convert your data into something serializable in Json (eg a string if it's just for saving the string). If you want to save your tensor data in a dict to a JSON file one simple solution is to recursively go into your dictionary and use the right function to convert your data into something serializable in Json (eg a string if it's just for saving the string )。 I am sure tensorflow must have a way to save your data as pickle files if that's what you really want to do (ie save your data).我确信 tensorflow 必须有办法将您的数据保存为泡菜文件,如果这是您真正想要做的(即保存您的数据)。

The following code works for converting the things inside your dict recursively into a string but you should be able to modify and numpify, jsonify, etc the code easily depending your use case.以下代码用于将 dict 中的内容递归地转换为字符串,但您应该能够根据您的用例轻松修改和 numify、jsonify 等代码。 My use case was saving the data in a format that was human readable (and not just torch.save ):我的用例是以人类可读的格式保存数据(而不仅仅是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)

output: output:

{
    "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]])}

Related links:相关链接:

https://discuss.pytorch.org/t/typeerror-tensor-is-not-json-serializable/36065/3 or How to prettyprint a JSON file? https://discuss.pytorch.org/t/typeerror-tensor-is-not-json-serializable/36065/3如何美化 Z0ECD11C1D7A287401D148A23BBD 文件? and https://github.com/fossasia/visdom/issues/554 .https://github.com/fossasia/visdom/issues/554

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

相关问题 “TypeError”类型的对象不是 JSON 可序列化的 - Object of type 'TypeError' is not JSON serializable TypeError:TypeError 类型的对象不是 JSON 可序列化的 Python - TypeError: Object of type TypeError is not JSON serializable Python DRF:TypeError: <Model: Model object> 不可序列化为JSON - DRF: TypeError: <Model: Model object> is not JSON serializable 类型错误:TextIOWrapper 类型的对象不是 JSON 可序列化的 - TypeError: Object of type TextIOWrapper is not JSON serializable Python TypeError:“ ndarray”类型的对象不可JSON序列化 - Python TypeError: Object of type 'ndarray' is not JSON serializable 类型错误:WorkItem 类型的对象不是 JSON 可序列化的 - TypeError: Object of type WorkItem is not JSON serializable TypeError:“时间”类型的对象不可JSON序列化 - TypeError: Object of type 'time' is not JSON serializable TypeError:类型X的对象不可JSON序列化 - TypeError: Object of type X is not JSON serializable TypeError:“ Redditor”类型的对象不可JSON序列化 - TypeError: Object of type 'Redditor' is not JSON serializable TypeError:IntegrityError类型的对象不可JSON序列化 - TypeError: Object of type IntegrityError is not JSON serializable
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM