gpt4 book ai didi

java - 使用java在apche_beam中写入tfrecords

转载 作者:行者123 更新时间:2023-12-01 17:23:32 25 4
gpt4 key购买 nike

如何用java编写下面的代码?如果我有java中的记录/字典列表,我如何编写beam代码将它们写入tfrecords中,其中tf.train.Examples被序列化。有很多使用 python 实现这一点的示例,下面是 python 中的一个示例,我如何在 java 中编写相同的逻辑?

import tensorflow as tf
import apache_beam as beam
from apache_beam.runners.interactive import interactive_runner
from apache_beam.coders import ProtoCoder

class Foo(beam.DoFn):
def process(self, element, *args, **kwargs):
import tensorflow as tf

foo = element.get('foo')
bar = element.get('bar')

feature = {
"foo":
tf.train.Feature(bytes_list=tf.train.BytesList(value=[foo.encode('utf-8')])),
"bar":
tf.train.Feature(bytes_list=tf.train.BytesList(value=[bar.encode('utf-8')]))
}
example_proto = tf.train.Example(features=tf.train.Features(feature=feature))
yield example_proto

p = beam.Pipeline(runner=interactive_runner.InteractiveRunner())

records = p | "Create records" >> beam.Create([{'foo': 'abc', 'bar': 'pqr'} for _ in range(10)])
tf_examples = records | "Convert to tf examples" >> beam.ParDo(Foo())
tf_examples | "Dump Records" >> beam.io.WriteToTFRecord(file_path_prefix="./output/data-",
coder=ProtoCoder(tf.train.Example()),
file_name_suffix='.tfrecord', num_shards=2)

p.run()

最佳答案

我已经用java尝试过这个,但仍然遇到一些问题,新问题的链接在这里 Write tfrecords from beam pipeline? .

关于java - 使用java在apche_beam中写入tfrecords,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61247661/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com