|
| 1 | +import java.nio.file.Files; |
| 2 | +import java.nio.file.Paths; |
| 3 | +import java.util.Arrays; |
| 4 | +import java.util.Random; |
| 5 | +import org.tensorflow.Graph; |
| 6 | +import org.tensorflow.Session; |
| 7 | +import org.tensorflow.Tensor; |
| 8 | +import org.tensorflow.Tensors; |
| 9 | + |
| 10 | +public class Train { |
| 11 | + |
| 12 | + public static void main(String[] args) throws Exception { |
| 13 | + if (args.length != 2) { |
| 14 | + System.err.println("Require two arguments: <graph_def_filename> <directory_for_checkpoints>"); |
| 15 | + System.exit(1); |
| 16 | + } |
| 17 | + final byte[] graphDef = Files.readAllBytes(Paths.get(args[0])); |
| 18 | + final String checkpointDir = args[1]; |
| 19 | + final boolean checkpointExists = Files.exists(Paths.get(checkpointDir)); |
| 20 | + |
| 21 | + // These names of tensors/operations in the graph (string arguments to feed(), fetch(), and |
| 22 | + // addTarget()) would have been printed out by model.py |
| 23 | + try (Graph graph = new Graph(); |
| 24 | + Session sess = new Session(graph); |
| 25 | + Tensor<String> checkpointPrefix = |
| 26 | + Tensors.create(Paths.get(checkpointDir, "checkpoint").toString())) { |
| 27 | + graph.importGraphDef(graphDef); |
| 28 | + |
| 29 | + // Initialize or restore. |
| 30 | + if (checkpointExists) { |
| 31 | + System.out.println("Restoring variables from checkpoint"); |
| 32 | + sess.runner().feed("save/Const", checkpointPrefix).addTarget("save/restore_all").run(); |
| 33 | + } else { |
| 34 | + System.out.println("Initializing variables"); |
| 35 | + sess.runner().addTarget("init").run(); |
| 36 | + } |
| 37 | + |
| 38 | + System.out.println("Generating initial predictions"); |
| 39 | + printPredictionsOnTestSet(sess); |
| 40 | + |
| 41 | + System.out.println("Training for a few steps"); |
| 42 | + final int BATCH_SIZE = 10; |
| 43 | + float inputs[][][] = new float[BATCH_SIZE][1][1]; |
| 44 | + float targets[][][] = new float[BATCH_SIZE][1][1]; |
| 45 | + for (int i = 0; i < 200; ++i) { |
| 46 | + fillNextBatchForTraining(inputs, targets); |
| 47 | + try (Tensor<Float> inputBatch = Tensors.create(inputs); |
| 48 | + Tensor<Float> targetBatch = Tensors.create(targets)) { |
| 49 | + sess.runner() |
| 50 | + .feed("input", inputBatch) |
| 51 | + .feed("target", targetBatch) |
| 52 | + .addTarget("train") |
| 53 | + .run(); |
| 54 | + } |
| 55 | + } |
| 56 | + |
| 57 | + System.out.println("Updated predictions"); |
| 58 | + printPredictionsOnTestSet(sess); |
| 59 | + |
| 60 | + System.out.println("Saving checkpoint"); |
| 61 | + sess.runner().feed("save/Const", checkpointPrefix).addTarget("save/control_dependency").run(); |
| 62 | + } |
| 63 | + } |
| 64 | + |
| 65 | + public static void printPredictionsOnTestSet(Session sess) { |
| 66 | + final float[][][] inputBatch = new float[][][] {{{1.0f}}, {{2.0f}}, {{3.0f}}}; |
| 67 | + try (Tensor<Float> input = Tensors.create(inputBatch); |
| 68 | + Tensor<Float> output = |
| 69 | + sess.runner().feed("input", input).fetch("output").run().get(0).expect(Float.class)) { |
| 70 | + final long shape[] = output.shape(); |
| 71 | + final int batchSize = (int) shape[0]; |
| 72 | + final int rows = (int) shape[1]; |
| 73 | + final int cols = (int) shape[2]; |
| 74 | + float[][][] predictions = output.copyTo(new float[batchSize][rows][cols]); |
| 75 | + for (int i = 0; i < batchSize; ++i) { |
| 76 | + System.out.print("\t x = "); |
| 77 | + System.out.print(Arrays.deepToString(inputBatch[i])); |
| 78 | + System.out.print(", predicted y = "); |
| 79 | + System.out.println(Arrays.deepToString(predictions[i])); |
| 80 | + } |
| 81 | + } |
| 82 | + } |
| 83 | + |
| 84 | + public static void fillNextBatchForTraining(float[][][] inputs, float[][][] targets) { |
| 85 | + final Random r = new Random(); |
| 86 | + for (int i = 0; i < inputs.length; ++i) { |
| 87 | + inputs[i][0][0] = r.nextFloat(); |
| 88 | + targets[i][0][0] = inputs[i][0][0] * 3.0f + 2.0f; |
| 89 | + } |
| 90 | + } |
| 91 | +} |
0 commit comments