【发布时间】:2023-01-28 06:49:00
【问题描述】:
我有一个像这样的 python 字典:
{'class_name': 'InputLayer',
'config': {'batch_input_shape': (None, 32),
'dtype': 'float32',
'sparse': False,
'ragged': False,
'name': 'input_5'}}
当我尝试使用 json_format 方法将其转换为 protobuf 消息时,它会将 config.batch_input_shape 32 的 int 数据类型更改为 float 32.0。
转换使用的代码(layer_config就是上面的dict):
import json
from google.protobuf import json_format
from google.protobuf import struct_pb2 as struct
json_format.Parse(json.dumps(layer_config), struct.Struct())
有什么方法可以避免从 int 到 float 的这种类型转换?
我还尝试使用 update 方法进行转换,如下所示:
s = Struct()
s.update(layer_config)
但随后它也会转换类型。
【问题讨论】:
标签: python json protocol-buffers