tensorflow - 如何将功能转换为 Python 对象?
问题描述
我有一些Example
s 以二进制格式存储在文件中,我正在阅读如下:
for fp in shard_filepaths:
record_iterator = tf.python_io.tf_record_iterator(path=fp)
for record in record_iterator:
example = tf.train.Example.FromString(record)
有没有办法将其映射example
到 Pythondict
或object
?
我知道我可以做类似的事情
data = json.loads(MessageToJson(tf.train.Example.FromString(record)))
但这会产生相当复杂和不方便的输出。
我在这里有什么选择?
此外,这是to_example(dictionary)
生成从 adict
到a 的记录的方法(参见 tensor2tensor ) example
:
def to_example(dictionary):
"""Helper: build tf.Example from (string -> int/float/str list) dictionary."""
features = {}
for (k, v) in six.iteritems(dictionary):
if not v:
raise ValueError('Empty generated field: %s' % str((k, v)))
if isinstance(v[0], six.integer_types):
features[k] = tf.train.Feature(int64_list=tf.train.Int64List(value=v))
elif isinstance(v[0], float):
features[k] = tf.train.Feature(float_list=tf.train.FloatList(value=v))
elif isinstance(v[0], six.string_types):
if not six.PY2: # Convert in python 3.
v = [bytes(x, 'utf-8') for x in v]
features[k] = tf.train.Feature(bytes_list=tf.train.BytesList(value=v))
elif isinstance(v[0], bytes):
features[k] = tf.train.Feature(bytes_list=tf.train.BytesList(value=v))
else:
raise ValueError('Value for %s is not a recognized type; v: %s type: %s' % (k, str(v[0]), str(type(v[0]))))
return tf.train.Example(features=tf.train.Features(feature=features))
解决方案
据我所知,这就是您要查找的内容,您需要修改变量名,但这应该可以完成工作。
reader = tf.TFRecordReader()
filename_queue = tf.train.string_input_producer(['filename.tfrecord'])
_, serialized_example = reader.read(filename_queue)
# Define features
read_features = {
'Feature 1': tf.FixedLenFeature([], dtype=tf.int64),
'Feature 2 tf.VarLenFeature(dtype=tf.string),
'Feature 3: tf.VarLenFeature(dtype=tf.float32),
'Feature 4: tf.FixedLenFeature([], dtype=tf.string),
'Feature 5: tf.FixedLenFeature([], dtype=tf.float32),
'Feature 6: tf.FixedLenFeature([], dtype=tf.float32)}
# Extract features from serialized data
read_data = tf.parse_single_example(serialized=serialized_example,
features=read_features)
read_data 将是一个列表输出。
推荐阅读
- qt - QCharts 裁剪为矩形并使用水平滚动
- entity-framework - 在 asp.net core 3.0 中找不到 _CookieConsentPartial.cshtml
- laravel - Laravel:中间件检查用户是否被软删除
- excel - Microsoft Excel 最新更新(版本 1902 Build 11328.20512)破坏了 Querytables vba。代码在以前的版本中工作
- hibernate - JPA/Hibernate:合并 Composite Key 的属性创建另一行而不是更新现有的
- arrays - 尝试保留此原始数组在此处失败
- kotlin - kotlin中接口引用如何访问类方法和变量
- java - 如何打开现有的 android 应用程序并使用颤振向其发送数据
- postgresql - Postgres 在 Schema 中使用 Hstore 给出运算符不存在的错误
- maven - 在“更新 Maven 项目”期间发生内部错误。java.lang.ExceptionInInitializerError