|
| 1 | +/* |
| 2 | + * Licensed to the Apache Software Foundation (ASF) under one |
| 3 | + * or more contributor license agreements. See the NOTICE file |
| 4 | + * distributed with this work for additional information |
| 5 | + * regarding copyright ownership. The ASF licenses this file |
| 6 | + * to you under the Apache License, Version 2.0 (the |
| 7 | + * "License"); you may not use this file except in compliance |
| 8 | + * with the License. You may obtain a copy of the License at |
| 9 | + * |
| 10 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | + * |
| 12 | + * Unless required by applicable law or agreed to in writing, software |
| 13 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 14 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 15 | + * See the License for the specific language governing permissions and |
| 16 | + * limitations under the License. |
| 17 | + */ |
| 18 | + |
| 19 | +package org.apache.wayang.tests; |
| 20 | + |
| 21 | +import org.apache.wayang.api.*; |
| 22 | +import org.apache.wayang.basic.model.DLModel; |
| 23 | +import org.apache.wayang.basic.model.op.*; |
| 24 | +import org.apache.wayang.basic.model.op.nn.CrossEntropyLoss; |
| 25 | +import org.apache.wayang.basic.model.op.nn.Linear; |
| 26 | +import org.apache.wayang.basic.model.op.nn.Sigmoid; |
| 27 | +import org.apache.wayang.basic.model.optimizer.Adam; |
| 28 | +import org.apache.wayang.basic.model.optimizer.Optimizer; |
| 29 | +import org.apache.wayang.basic.operators.DLTrainingOperator; |
| 30 | +import org.apache.wayang.core.api.WayangContext; |
| 31 | +import org.apache.wayang.core.util.Tuple; |
| 32 | +import org.apache.wayang.java.Java; |
| 33 | +import org.apache.wayang.tensorflow.Tensorflow; |
| 34 | +import org.junit.Test; |
| 35 | + |
| 36 | +import java.net.URI; |
| 37 | +import java.net.URISyntaxException; |
| 38 | +import java.util.ArrayList; |
| 39 | +import java.util.List; |
| 40 | +import java.util.Map; |
| 41 | +import java.util.Random; |
| 42 | + |
| 43 | +/** |
| 44 | + * Test the Tensorflow integration with Wayang. |
| 45 | + * Note: this test fails on M1 Macs because of Tensorflow-Java incompatibility. |
| 46 | + */ |
| 47 | +public class TensorflowIrisScalaLikeApiIT { |
| 48 | + |
| 49 | + public static URI TRAIN_PATH = createUri("/iris_train.csv"); |
| 50 | + public static URI TEST_PATH = createUri("/iris_test.csv"); |
| 51 | + |
| 52 | + public static Map<String, Integer> LABEL_MAP = Map.of( |
| 53 | + "Iris-setosa", 0, |
| 54 | + "Iris-versicolor", 1, |
| 55 | + "Iris-virginica", 2 |
| 56 | + ); |
| 57 | + |
| 58 | + @Test |
| 59 | + public void test() { |
| 60 | + WayangContext wayangContext = new WayangContext() |
| 61 | + .with(Java.basicPlugin()) |
| 62 | + .with(Tensorflow.plugin()); |
| 63 | + |
| 64 | + JavaPlanBuilder plan = new JavaPlanBuilder(wayangContext); |
| 65 | + |
| 66 | + final Tuple<DataQuantaBuilder<?, float[]>, DataQuantaBuilder<?, Integer>> trainSource = |
| 67 | + fileOperation(plan, TRAIN_PATH, true); |
| 68 | + final Tuple<DataQuantaBuilder<?, float[]>, DataQuantaBuilder<?, Integer>> testSource = |
| 69 | + fileOperation(plan, TEST_PATH, false); |
| 70 | + |
| 71 | + /* training features */ |
| 72 | + DataQuantaBuilder<?, float[]> trainXSource = trainSource.field0; |
| 73 | + |
| 74 | + /* training labels */ |
| 75 | + DataQuantaBuilder<?, Integer> trainYSource = trainSource.field1; |
| 76 | + |
| 77 | + /* test features */ |
| 78 | + DataQuantaBuilder<?, float[]> testXSource = testSource.field0; |
| 79 | + |
| 80 | + /* test labels */ |
| 81 | + DataQuantaBuilder<?, Integer> testYSource = testSource.field1; |
| 82 | + |
| 83 | + /* model */ |
| 84 | + Op l1 = new Linear(4, 32, true); |
| 85 | + Op s1 = new Sigmoid(); |
| 86 | + Op l2 = new Linear(32, 3, true); |
| 87 | + s1.with(l1.with(new Input(Input.Type.FEATURES))); |
| 88 | + l2.with(s1); |
| 89 | + |
| 90 | + DLModel model = new DLModel(l2); |
| 91 | + |
| 92 | + /* training options */ |
| 93 | + // 1. loss function |
| 94 | + Op criterion = new CrossEntropyLoss(3); |
| 95 | + criterion.with( |
| 96 | + new Input(Input.Type.PREDICTED, Op.DType.FLOAT32), |
| 97 | + new Input(Input.Type.LABEL, Op.DType.INT32) |
| 98 | + ); |
| 99 | + |
| 100 | + // 2. accuracy calculation function |
| 101 | + Op acc = new Mean(0); |
| 102 | + acc.with(new Cast(Op.DType.FLOAT32).with(new Eq().with( |
| 103 | + new ArgMax(1).with(new Input(Input.Type.PREDICTED, Op.DType.FLOAT32)), |
| 104 | + new Input(Input.Type.LABEL, Op.DType.INT32) |
| 105 | + ))); |
| 106 | + |
| 107 | + // 3. optimizer with learning rate |
| 108 | + Optimizer optimizer = new Adam(0.1f); |
| 109 | + |
| 110 | + // 4. batch size |
| 111 | + int batchSize = 45; |
| 112 | + |
| 113 | + // 5. epoch |
| 114 | + int epoch = 10; |
| 115 | + |
| 116 | + DLTrainingOperator.Option option = new DLTrainingOperator.Option(criterion, optimizer, batchSize, epoch); |
| 117 | + option.setAccuracyCalculation(acc); |
| 118 | + |
| 119 | + /* training operator */ |
| 120 | + DLTrainingDataQuantaBuilder<float[], Integer> trainingOperator = |
| 121 | + trainXSource.dlTraining(trainYSource, model, option); |
| 122 | + |
| 123 | + /* predict operator */ |
| 124 | + PredictDataQuantaBuilder<float[], float[]> predictOperator = |
| 125 | + trainingOperator.predict(testXSource, float[].class); |
| 126 | + |
| 127 | + /* map to label */ |
| 128 | + MapDataQuantaBuilder<float[], Integer> mapOperator = predictOperator.map(array -> { |
| 129 | + int maxIdx = 0; |
| 130 | + float maxVal = array[0]; |
| 131 | + for (int i = 1; i < array.length; i++) { |
| 132 | + if (array[i] > maxVal) { |
| 133 | + maxIdx = i; |
| 134 | + maxVal = array[i]; |
| 135 | + } |
| 136 | + } |
| 137 | + return maxIdx; |
| 138 | + }); |
| 139 | + |
| 140 | + /* sink */ |
| 141 | + List<Integer> predicted = new ArrayList<>(mapOperator.collect()); |
| 142 | + // fixme: Currently, wayang's scala-like api only supports a single collect, |
| 143 | + // so it is not possible to collect multiple result lists in a single plan. |
| 144 | +// List<Integer> groundTruth = new ArrayList<>(testYSource.collect()); |
| 145 | + |
| 146 | + System.out.println("predicted: " + predicted); |
| 147 | +// System.out.println("ground truth: " + groundTruth); |
| 148 | + |
| 149 | +// float success = 0; |
| 150 | +// for (int i = 0; i < predicted.size(); i++) { |
| 151 | +// if (predicted.get(i).equals(groundTruth.get(i))) { |
| 152 | +// success += 1; |
| 153 | +// } |
| 154 | +// } |
| 155 | +// System.out.println("test accuracy: " + success / predicted.size()); |
| 156 | + } |
| 157 | + |
| 158 | + public static Tuple<DataQuantaBuilder<?, float[]>, DataQuantaBuilder<?, Integer>> |
| 159 | + fileOperation(JavaPlanBuilder plan, URI uri, boolean random) { |
| 160 | + DataQuantaBuilder<?, String> textFileSource = plan.readTextFile(uri.toString()); |
| 161 | + |
| 162 | + if (random) { |
| 163 | + Random r = new Random(); |
| 164 | + textFileSource = textFileSource.sort(e -> r.nextInt()); |
| 165 | + } |
| 166 | + |
| 167 | + MapDataQuantaBuilder<String, Tuple<float[], Integer>> mapXY = textFileSource.map(line -> { |
| 168 | + String[] parts = line.split(","); |
| 169 | + float[] x = new float[parts.length - 1]; |
| 170 | + for (int i = 0; i < x.length; i++) { |
| 171 | + x[i] = Float.parseFloat(parts[i]); |
| 172 | + } |
| 173 | + int y = LABEL_MAP.get(parts[parts.length - 1]); |
| 174 | + return new Tuple<>(x, y); |
| 175 | + }); |
| 176 | + |
| 177 | + MapDataQuantaBuilder<Tuple<float[], Integer>, float[]> mapX = mapXY.map(tuple -> tuple.field0); |
| 178 | + MapDataQuantaBuilder<Tuple<float[], Integer>, Integer> mapY = mapXY.map(tuple -> tuple.field1); |
| 179 | + |
| 180 | + return new Tuple<>(mapX, mapY); |
| 181 | + } |
| 182 | + |
| 183 | + public static URI createUri(String resourcePath) { |
| 184 | + try { |
| 185 | + return TensorflowIrisScalaLikeApiIT.class.getResource(resourcePath).toURI(); |
| 186 | + } catch (URISyntaxException e) { |
| 187 | + throw new IllegalArgumentException("Illegal URI.", e); |
| 188 | + } |
| 189 | + } |
| 190 | +} |
0 commit comments