TransformProcess 在使用 DataSetIterator 时转换数据

TransformProcess transform data while using DataSetIterator

我有一个包含数字和名义属性的 CSV 数据集。我为列出标称属性的所有可能值的数据集定义了 Schema。之后,我创建了 TransformProcess,使用 CategoricalToOneHotTransform 将标称值转换为数值。我如何在 RecordReaderDataSetIterator 上使用此 TransformProcess 来为我的神经网络做准备?

        Schema schema = new Schema.Builder()
        .addColumnInteger("age")
        .addColumnCategorical("workclass", "Private", "Self-emp-not-inc", "Self-emp-inc", "Federal-gov", "Local-gov", "State-gov", "Without-pay", "Never-worked")
        .addColumnInteger("fnlwgt")
        .addColumnCategorical("education", "Bachelors", "Some-college", "11th", "HS-grad", "Prof-school", "Assoc-acdm", "Assoc-voc", "9th", "7th-8th", "12th", "Masters", "1st-4th", "10th", "Doctorate", "5th-6th", "Preschool")
        .addColumnInteger("education-num")
        .addColumnCategorical("marital-status", "Married-civ-spouse", "Divorced", "Never-married", "Separated", "Widowed", "Married-spouse-absent", "Married-AF-spouse")
        .addColumnCategorical("occupation", "Tech-support", "Craft-repair", "Other-service", "Sales", "Exec-managerial", "Prof-specialty", "Handlers-cleaners", "Machine-op-inspct", "Adm-clerical", "Farming-fishing", "Transport-moving", "Priv-house-serv", "Protective-serv", "Armed-Forces")
        .addColumnCategorical("relationship", "Wife", "Own-child", "Husband", "Not-in-family", "Other-relative", "Unmarried")
        .addColumnCategorical("race", "White", "Asian-Pac-Islander", "Amer-Indian-Eskimo", "Other", "Black")
        .addColumnCategorical("sex", "Female", "Male")
        .addColumnInteger("capital-gain")
        .addColumnInteger("capital-loss")
        .addColumnInteger("hours-per-week")
        .addColumnCategorical("native-country", "United-States", "Cambodia", "England", "Puerto-Rico", "Canada", "Germany", "Outlying-US(Guam-USVI-etc)", "India", "Japan", "Greece", "South", "China", "Cuba", "Iran", "Honduras", "Philippines", "Italy", "Poland", "Jamaica", "Vietnam", "Mexico", "Portugal", "Ireland", "France", "Dominican-Republic", "Laos", "Ecuador", "Taiwan", "Haiti", "Columbia", "Hungary", "Guatemala", "Nicaragua", "Scotland", "Thailand", "Yugoslavia", "El-Salvador", "Trinadad&Tobago", "Peru", "Hong", "Holand-Netherlands")
        .addColumnCategorical("class", ">50K", "<=50K")
        .build();

    TransformProcess tp = new TransformProcess.Builder(schema)
        .transform(new CategoricalToOneHotTransform("workclass"))
        .transform(new CategoricalToOneHotTransform("education"))
        .transform(new CategoricalToOneHotTransform("marital-status"))
        .transform(new CategoricalToOneHotTransform("occupation"))
        .transform(new CategoricalToOneHotTransform("relationship"))
        .transform(new CategoricalToOneHotTransform("race"))
        .transform(new CategoricalToOneHotTransform("sex"))
        .transform(new CategoricalToOneHotTransform("native-country"))
        .transform(new CategoricalToIntegerTransform("class"))
        .build();

    Schema outputSchema = tp.getFinalSchema();

    int numLinesToSkip = 0;
    String delimiter = ",";
    CSVRecordReader recordReader = new CSVRecordReader(numLinesToSkip, delimiter);
    recordReader.initialize(new FileSplit(Paths.get("..\adult.data").toFile()));


    int labelIndex = outputSchema.getColumnNames().size() - 1;
    int numClasses = 2;
    int batchSize = 2000;

    RecordReaderDataSetIterator iterator = new RecordReaderDataSetIterator(recordReader, batchSize, labelIndex, numClasses);

    DataSet allData = iterator.next();
    allData.shuffle();
    SplitTestAndTrain testAndTrain = allData.splitTestAndTrain(0.65);

看看:https://github.com/deeplearning4j/DataVec/blob/master/datavec-api/src/main/java/org/datavec/api/records/reader/impl/transform/TransformProcessRecordReader.java

RecordReaderDataSetItertor 接收记录 reader 并处理向量化过程。这会包装一条记录 reader 并输出一个转换后的记录,然后将其馈送到记录readerdatasetiterator.