當前位置: 首頁>>編程示例 >>用法及示例精選 >>正文


Python tf.io.decode_json_example用法及代碼示例

將 JSON-encoded 示例記錄轉換為二進製協議緩衝區字符串。

用法

tf.io.decode_json_example(
    json_examples, name=None
)

參數

  • json_examples 包含 json-serialized tf.Example protos 的字符串張量。
  • name 操作的名稱。

返回

  • 包含 binary-serialized tf.Example protos 的字符串張量。

拋出

注意:這是不是一個通用的 JSON 解析操作。

此操作將 JSON-serialized tf.train.Example(可能使用 json_format.MessageToJson 創建,遵循標準 JSON 映射)轉換為 binary-serialized tf.train.Example(相當於 Example.SerializeToString()),適合使用 tf. io.parse_example。

這是一個tf.train.Example 原型:

example = tf.train.Example(
  features=tf.train.Features(
      feature={
          "a":tf.train.Feature(
              int64_list=tf.train.Int64List(
                  value=[1, 1, 3]))}))

在這裏它被轉換為 JSON:

from google.protobuf import json_format
example_json = json_format.MessageToJson(example)
print(example_json)
{
  "features":{
    "feature":{
      "a":{
        "int64List":{
          "value":[
            "1",
            "1",
            "3"
          ]
        }
      }
    }
  }
}

此操作將上述 json 字符串轉換為二進製 proto:

example_binary = tf.io.decode_json_example(example_json)
example_binary.numpy()
b'\n\x0f\n\r\n\x01a\x12\x08\x1a\x06\x08\x01\x08\x01\x08\x03'

OP 適用於 andy 形狀的弦張量:

tf.io.decode_json_example([
    [example_json, example_json],
    [example_json, example_json]]).shape.as_list()
[2, 2]

由此產生的 binary-string 等效於 Example.SerializeToString() ,並且可以使用 tf.io.parse_example 和相關函數轉換為張量:

tf.io.parse_example(
  serialized=[example_binary.numpy(),
             example.SerializeToString()],
  features = {'a':tf.io.FixedLenFeature(shape=[3], dtype=tf.int64)})
{'a':<tf.Tensor:shape=(2, 3), dtype=int64, numpy=
 array([[1, 1, 3],
        [1, 1, 3]])>}

相關用法


注:本文由純淨天空篩選整理自tensorflow.org大神的英文原創作品 tf.io.decode_json_example。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。