Spark,Scala,保存tensorflow-TfRecord到HDFS,示例

Spark,Scala,保存tensorflow-TfRecord到HDFS,示例,第1张

Spark,Scala,保存tensorflow-TfRecord到HDFS,示例
import org.tensorflow.example.Features
import org.tensorflow.example.Feature
import org.tensorflow.example.Example
import org.tensorflow.example.FloatList 
import org.tensorflow.example.Int64List
import org.apache.hadoop.io.{BytesWritable, NullWritable}
import org.tensorflow.hadoop.io.TFRecordFileOutputFormat


def processLongArrayFeature(data: java.lang.Iterable[_ <: java.lang.Long]): Feature = {
    val int_list = Int64List.newBuilder.addAllValue(data).build
    Feature.newBuilder.setInt64List(int_list).build
  }

def processFloatArrayFeature(data: java.lang.Iterable[java.lang.Float]): Feature = {
    val float_list = FloatList.newBuilder.addAllValue(data).build
    Feature.newBuilder.setFloatList(float_list).build
  }

feature_DF = feature_DF.rdd.map(row => {
//...
val long_array = new util.ArrayList[java.lang.Long]()
val float_array = new util.ArrayList[java.lang.Float]()

val feature_builder: Features.Builder = Features.newBuilder()

feature_builder
        .putFeature("float_array", processFloatArrayFeature(float_array))
        .putFeature("long_array", processLongArrayFeature(long_array))

val result = Example.newBuilder.setFeatures(feature_builder.build).build.toByteArray

(new BytesWritable(result), null)
 }).coalesce(100)
 
feature_DF.asInstanceOf[RDD[(BytesWritable, Null)]]
      .saveAsNewAPIHadoopFile(the_path, classOf[BytesWritable], classOf[NullWritable], classOf[TFRecordFileOutputFormat], sparkContext.hadoopConfiguration)

欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/zaji/5705367.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2022-12-17
下一篇2022-12-17

发表评论

登录后才能评论

评论列表(0条)

    保存