package geniusweb.bagga.dicehaggler; import java.io.BufferedReader; import java.io.BufferedWriter; import java.io.File; import java.io.FileReader; import java.io.FileWriter; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.io.LineNumberReader; import java.util.stream.Collectors; import org.datavec.api.records.reader.RecordReader; import org.datavec.api.records.reader.impl.csv.CSVRecordReader; import org.datavec.api.split.FileSplit; import org.deeplearning4j.datasets.datavec.RecordReaderDataSetIterator; import org.nd4j.linalg.api.ndarray.INDArray; import org.nd4j.linalg.dataset.DataSet; import org.nd4j.linalg.dataset.api.preprocessor.DataNormalization; import org.nd4j.linalg.dataset.api.preprocessor.NormalizerStandardize; import org.nd4j.linalg.factory.Nd4j; import org.nd4j.linalg.io.ResourceUtils; public class Dl4jUtils { public DataNormalization getTrainedNormalization(String fileName) throws IOException, InterruptedException { final int numLinesToSkip = 1; final char delimiter = ','; //final File excelFile = new File(fileName); // ClassLoader classLoader = getClass().getClassLoader(); // //InputStream resourceFile = classLoader.getResourceAsStream(fileName); // //Properties config = new Properties(); // //config.load(resourceFile); // //String resourceFileName = config.getProperty("name"); // //String resourceFileName = classLoader.getResource(fileName).getFile(); // String resourceFileName; // try (InputStream inputStream = getClass().getResourceAsStream("/"+fileName); // BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream))) { // resourceFileName = reader.lines() // .collect(Collectors.joining(System.lineSeparator())); // } // final File excelFile; //// if(resourceFileName == null) { //// System.out.println("inside the if consition:"); //// excelFile = ResourceUtils.getFile(fileName); //// } //// else //// { //// System.out.println("inside else condition"); // excelFile = new File(resourceFileName); //// } String completeFile; InputStream aa = Thread.currentThread().getContextClassLoader().getResourceAsStream(fileName); BufferedReader br = new BufferedReader(new InputStreamReader(aa)); completeFile = br.lines().collect(Collectors.joining(System.lineSeparator())); // final File excelFile; // // excelFile = new File(completeFile); File excelFile = File.createTempFile("tempfile", ".csv"); BufferedWriter bw = new BufferedWriter(new FileWriter(excelFile)); bw.write(completeFile); bw.close(); final RecordReader recordReader = new CSVRecordReader(numLinesToSkip, delimiter); recordReader.initialize(new FileSplit(excelFile)); System.out.println(recordReader.getLabels()); final int batchSize = getRowCountFromExcel(excelFile); final RecordReaderDataSetIterator iterator = new RecordReaderDataSetIterator(recordReader, batchSize - 1); iterator.setCollectMetaData(true); // Instruct the iterator to collect metadata, and store // it in the DataSet objects final DataSet allData = iterator.next(); final DataNormalization normalizer = new NormalizerStandardize(); normalizer.fit(allData); // Collect the statistics (mean/stdev) from the training data. This // does not modify the input data // Apply normalization to the test data. This is using statistics calculated from the // *training* set return normalizer; // allData.shuffle(123) } private int getRowCountFromExcel(File file) throws IOException { int linenumber = 0; if (file.exists()) { final FileReader fr = new FileReader(file); final LineNumberReader lnr = new LineNumberReader(fr); while (lnr.readLine() != null) { linenumber++; } // System.out.println("Total number of lines : " + linenumber); lnr.close(); } else { System.out.println("File does not exist!"); } return linenumber; } public static INDArray getInput(double time, int totalNumberOfPossibleBids, int numberOfIssues, int givenNumberOfbids, double recentlyReceivedBidUtility, double opponentBestBidUtility, double averageOpponentUtility, double standardDevUtility) { final INDArray features = Nd4j.zeros(8); features.putScalar(new int[] { 0 }, time); features.putScalar(new int[] { 1 }, totalNumberOfPossibleBids); features.putScalar(new int[] { 2 }, numberOfIssues); features.putScalar(new int[] { 3 }, givenNumberOfbids); features.putScalar(new int[] { 4 }, recentlyReceivedBidUtility); features.putScalar(new int[] { 5 }, opponentBestBidUtility); features.putScalar(new int[] { 6 }, averageOpponentUtility); features.putScalar(new int[] { 7 }, standardDevUtility); return features; } }