/** * The feature to add, and the example/row number * * @param feature the feature vector to add * @param example the number of the example to append to */ @Override public void addFeatureVector(INDArray feature, int example) { getFeatures().putRow(example, feature); }
/** * Initializes this data transform fetcher from the passed in datasets * * @param examples the examples to use */ protected void initializeCurrFromList(List<DataSet> examples) { if (examples.isEmpty()) log.warn("Warning: empty dataset from the fetcher"); INDArray inputs = createInputMatrix(examples.size()); INDArray labels = createOutputMatrix(examples.size()); for (int i = 0; i < examples.size(); i++) { inputs.putRow(i, examples.get(i).getFeatureMatrix()); labels.putRow(i, examples.get(i).getLabels()); } curr = new DataSet(inputs, labels); }
/** * Read line via input streams * * @param filePath the input stream ndarray * @param split the split separator * @return the read txt method */ public static INDArray readNumpy(InputStream filePath, String split) throws IOException { BufferedReader reader = new BufferedReader(new InputStreamReader(filePath)); String line; List<float[]> data2 = new ArrayList<>(); int numColumns = -1; INDArray ret; while ((line = reader.readLine()) != null) { String[] data = line.trim().split(split); if (numColumns < 0) { numColumns = data.length; } else assert data.length == numColumns : "Data has inconsistent number of columns"; data2.add(readSplit(data)); } ret = Nd4j.create(data2.size(), numColumns); for (int i = 0; i < data2.size(); i++) ret.putRow(i, Nd4j.create(Nd4j.createBuffer(data2.get(i)))); return ret; }
/** * Get whole rows from the passed indices. * * @param rindices */ @Override public IComplexNDArray getRows(int[] rindices) { INDArray rows = Nd4j.create(rindices.length, columns()); for (int i = 0; i < rindices.length; i++) { rows.putRow(i, getRow(rindices[i])); } return (IComplexNDArray) rows; }
/** * Creates an out come vector from the specified inputs * * @param index the index of the label * @param numOutcomes the number of possible outcomes * @return a binary label matrix used for supervised learning */ public static INDArray toOutcomeMatrix(int[] index, long numOutcomes) { INDArray ret = Nd4j.create(index.length, numOutcomes); for (int i = 0; i < ret.rows(); i++) { int[] nums = new int[(int) numOutcomes]; nums[index[i]] = 1; ret.putRow(i, NDArrayUtil.toNDArray(nums)); } return ret; }
/** * Sets the outcome of a particular example * * @param example the example to transform * @param label the label of the outcome */ @Override public void setOutcome(int example, int label) { if (example > numExamples()) throw new IllegalArgumentException("No example at " + example); if (label > numOutcomes() || label < 0) throw new IllegalArgumentException("Illegal label"); INDArray outcome = FeatureUtil.toOutcomeVector(label, numOutcomes()); getLabels().putRow(example, outcome); }
/** * This method produces concatenated array, that consist from tensors, fetched from source array, against some dimension and specified indexes * * @param source source tensor * @param sourceDimension dimension of source tensor * @param indexes indexes from source array * @return */ @Override public INDArray pullRows(INDArray source, int sourceDimension, int[] indexes, char order) { long vectorLength = source.shape()[sourceDimension]; INDArray ret = Nd4j.createUninitialized(new long[] {indexes.length, vectorLength}, order); for (int cnt = 0; cnt < indexes.length; cnt++) { ret.putRow(cnt, source.tensorAlongDimension((int) indexes[cnt], sourceDimension)); } return ret; }
/** * Rotate a matrix 90 degrees * * @param toRotate the matrix to rotate * @return the rotated matrix */ @Override public void rot90(INDArray toRotate) { if (!toRotate.isMatrix()) throw new IllegalArgumentException("Only rotating matrices"); INDArray start = toRotate.transpose(); for (int i = 0; i < start.rows(); i++) start.putRow(i, reverse(start.getRow(i))); }
out.putRow(i, in.getRow(list.get(i)));
/** * Divides each row by its max * * @param toScale the matrix to divide by its row maxes */ public static void scaleByMax(INDArray toScale) { INDArray scale = toScale.max(1); for (int i = 0; i < toScale.rows(); i++) { double scaleBy = scale.getDouble(i); toScale.putRow(i, toScale.getRow(i).divi(scaleBy)); } }
throw new IllegalStateException("Label not found on row " + i); INDArray newRow = FeatureUtil.toOutcomeVector(i2, labels.length); newLabelMatrix.putRow(i, newRow);
public static INDArray tailor4d2d(@NonNull INDArray data) { long instances = data.size(0); long channels = data.size(1); long height = data.size(2); long width = data.size(3); INDArray in2d = Nd4j.create(channels, height * width * instances); long tads = data.tensorssAlongDimension(3, 2, 0); for (int i = 0; i < tads; i++) { INDArray thisTAD = data.tensorAlongDimension(i, 3, 2, 0); in2d.putRow(i, Nd4j.toFlattened(thisTAD)); } return in2d.transposei(); }
/** * Sample a dataset * * @param numSamples the number of samples to getFromOrigin * @param rng the rng to use * @param withReplacement whether to allow duplicates (only tracked by example row number) * @return the sample dataset */ @Override public DataSet sample(int numSamples, org.nd4j.linalg.api.rng.Random rng, boolean withReplacement) { INDArray examples = Nd4j.create(numSamples, getFeatures().columns()); INDArray outcomes = Nd4j.create(numSamples, numOutcomes()); Set<Integer> added = new HashSet<>(); for (int i = 0; i < numSamples; i++) { int picked = rng.nextInt(numExamples()); if (!withReplacement) while (added.contains(picked)) picked = rng.nextInt(numExamples()); examples.putRow(i, get(picked).getFeatures()); outcomes.putRow(i, get(picked).getLabels()); } return new DataSet(examples, outcomes); }
/** * Get whole rows from the passed indices. * * @param rindices */ @Override public INDArray getRows(int[] rindices) { Nd4j.getCompressor().autoDecompress(this); if (!isMatrix() && !isVector()) throw new IllegalArgumentException("Unable to get columns from a non matrix or vector"); if (isVector()) return Nd4j.pullRows(this, 1, rindices); else { INDArray ret = Nd4j.create(rindices.length, columns()); for (int i = 0; i < rindices.length; i++) ret.putRow(i, getRow(rindices[i])); return ret; } }
private static void addRow(INDArray ret, int row, String[] line) { double[] vector = new double[4]; for (int i = 0; i < 4; i++) vector[i] = Double.parseDouble(line[i]); ret.putRow(row, Nd4j.create(vector)); } }
private static void addRow(INDArray ret, int row, String[] line) { double[] vector = new double[4]; for (int i = 0; i < 4; i++) vector[i] = Double.parseDouble(line[i]); ret.putRow(row, Nd4j.create(vector)); } }
/** * Divides each row by its max * * @param toScale the matrix to divide by its row maxes */ public static void scaleByMax(INDArray toScale) { INDArray scale = toScale.max(1); for (int i = 0; i < toScale.rows(); i++) { double scaleBy = scale.getDouble(i); toScale.putRow(i, toScale.getRow(i).divi(scaleBy)); } }
public static INDArray tailor4d2d(@NonNull INDArray data) { int instances = data.size(0); int channels = data.size(1); int height = data.size(2); int width = data.size(3); INDArray in2d = Nd4j.create(channels, height * width * instances); int tads = data.tensorssAlongDimension(3, 2, 0); for (int i = 0; i < tads; i++) { INDArray thisTAD = data.tensorAlongDimension(i, 3, 2, 0); in2d.putRow(i, Nd4j.toFlattened(thisTAD)); } return in2d.transposei(); }