Java Code Examples for org.nd4j.linalg.api.ndarray.INDArray#isMatrix()
The following examples show how to use
org.nd4j.linalg.api.ndarray.INDArray#isMatrix() .
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: RegressionMetrics.java From konduit-serving with Apache License 2.0 | 6 votes |
private void handleNdArray(INDArray output) { if(output.isVector()) { for(int i = 0; i < output.length(); i++) { statCounters.get(i).add(output.getDouble(i)); } } else if(output.isMatrix() && output.length() > 1) { for(int i = 0; i < output.rows(); i++) { for(int j = 0; j < output.columns(); j++) { statCounters.get(i).add(output.getDouble(i,j)); } } } else if(output.isScalar()) { statCounters.get(0).add(output.sumNumber().doubleValue()); } else { throw new IllegalArgumentException("Only vectors and matrices supported right now"); } }
Example 2
Source File: SparseCOOGemvParameters.java From nd4j with Apache License 2.0 | 5 votes |
public SparseCOOGemvParameters(INDArray a, INDArray x, INDArray y) { this.a = a; this.x = x; this.y = y; if (a.isMatrix() && a.getFormat() == SparseFormat.COO) { BaseSparseNDArrayCOO coo = (BaseSparseNDArrayCOO) a; val = coo.getIncludedValues(); nnz = coo.nnz(); // FIXME: int cast m = (int) coo.rows(); setIndexes(coo, false); } }
Example 3
Source File: BaseNDArrayFactory.java From nd4j with Apache License 2.0 | 5 votes |
/** * 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))); }
Example 4
Source File: BaseNDArrayFactory.java From deeplearning4j with Apache License 2.0 | 5 votes |
/** * 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))); }
Example 5
Source File: MLLibUtil.java From deeplearning4j with Apache License 2.0 | 5 votes |
/** * Convert an ndarray to a matrix. * Note that the matrix will be con * @param arr the array * @return an mllib vector */ public static Matrix toMatrix(INDArray arr) { if (!arr.isMatrix()) { throw new IllegalArgumentException("passed in array must be a matrix"); } // if arr is a view - we have to dup anyway if (arr.isView()) { return Matrices.dense(arr.rows(), arr.columns(), arr.dup('f').data().asDouble()); } else // if not a view - we must ensure data is F ordered return Matrices.dense(arr.rows(), arr.columns(), arr.ordering() == 'f' ? arr.data().asDouble() : arr.dup('f').data().asDouble()); }
Example 6
Source File: GemvParameters.java From nd4j with Apache License 2.0 | 4 votes |
public GemvParameters(INDArray a, INDArray x, INDArray y) { a = copyIfNecessary(a); x = copyIfNecessaryVector(x); this.a = a; this.x = x; this.y = y; if (a.columns() > Integer.MAX_VALUE || a.rows() > Integer.MAX_VALUE) throw new ND4JArraySizeException(); if (x.columns() > Integer.MAX_VALUE || x.rows() > Integer.MAX_VALUE) throw new ND4JArraySizeException(); if (a.ordering() == 'f' && a.isMatrix()) { this.m = (int) a.rows(); this.n = (int) a.columns(); this.lda = (int) a.rows(); } else if (a.ordering() == 'c' && a.isMatrix()) { this.m = (int) a.columns(); this.n = (int) a.rows(); this.lda = (int) a.columns(); aOrdering = 'T'; } else { this.m = (int) a.rows(); this.n = (int) a.columns(); this.lda = (int) a.size(0); } if (x.rank() == 1) { incx = 1; } else if (x.isColumnVector()) { incx = x.stride(0); } else { incx = x.stride(1); } this.incy = y.elementWiseStride(); if (x instanceof IComplexNDArray) this.incx /= 2; if (y instanceof IComplexNDArray) this.incy /= 2; }
Example 7
Source File: GemvParameters.java From deeplearning4j with Apache License 2.0 | 4 votes |
public GemvParameters(INDArray a, INDArray x, INDArray y) { a = copyIfNecessary(a); x = copyIfNecessaryVector(x); this.a = a; this.x = x; this.y = y; if (a.columns() > Integer.MAX_VALUE || a.rows() > Integer.MAX_VALUE) throw new ND4JArraySizeException(); if (x.columns() > Integer.MAX_VALUE || x.rows() > Integer.MAX_VALUE) throw new ND4JArraySizeException(); if (a.ordering() == 'f' && a.isMatrix()) { this.m = (int) a.rows(); this.n = (int) a.columns(); this.lda = (int) a.rows(); } else if (a.ordering() == 'c' && a.isMatrix()) { this.m = (int) a.columns(); this.n = (int) a.rows(); this.lda = (int) a.columns(); aOrdering = 'T'; } else { this.m = (int) a.rows(); this.n = (int) a.columns(); this.lda = (int) a.size(0); } if (x.rank() == 1) { incx = 1; } else if (x.isColumnVector()) { incx = x.stride(0); } else { incx = x.stride(1); } this.incy = y.elementWiseStride(); }
Example 8
Source File: ParagraphVectorsTest.java From deeplearning4j with Apache License 2.0 | 4 votes |
/** * This test is not indicative. * there's no need in this test within travis, use it manually only for problems detection * * @throws Exception */ @Test @Ignore public void testParagraphVectorsReducedLabels1() throws Exception { val tempDir = testDir.newFolder(); ClassPathResource resource = new ClassPathResource("/labeled"); resource.copyDirectory(tempDir); LabelAwareIterator iter = new FileLabelAwareIterator.Builder().addSourceFolder(tempDir).build(); TokenizerFactory t = new DefaultTokenizerFactory(); /** * Please note: text corpus is REALLY small, and some kind of "results" could be received with HIGH epochs number, like 30. * But there's no reason to keep at that high */ ParagraphVectors vec = new ParagraphVectors.Builder().minWordFrequency(1).epochs(3).layerSize(100) .stopWords(new ArrayList<String>()).windowSize(5).iterate(iter).tokenizerFactory(t).build(); vec.fit(); //WordVectorSerializer.writeWordVectors(vec, "vectors.txt"); INDArray w1 = vec.lookupTable().vector("I"); INDArray w2 = vec.lookupTable().vector("am"); INDArray w3 = vec.lookupTable().vector("sad."); INDArray words = Nd4j.create(3, vec.lookupTable().layerSize()); words.putRow(0, w1); words.putRow(1, w2); words.putRow(2, w3); INDArray mean = words.isMatrix() ? words.mean(0) : words; log.info("Mean" + Arrays.toString(mean.dup().data().asDouble())); log.info("Array" + Arrays.toString(vec.lookupTable().vector("negative").dup().data().asDouble())); double simN = Transforms.cosineSim(mean, vec.lookupTable().vector("negative")); log.info("Similarity negative: " + simN); double simP = Transforms.cosineSim(mean, vec.lookupTable().vector("neutral")); log.info("Similarity neutral: " + simP); double simV = Transforms.cosineSim(mean, vec.lookupTable().vector("positive")); log.info("Similarity positive: " + simV); }
Example 9
Source File: NDArrayIndex.java From nd4j with Apache License 2.0 | 3 votes |
/** * Create from a matrix. The rows are the indices * The columns are the individual element in each ndarrayindex * * @param index the matrix to getFloat indices from * @return the indices to getFloat */ public static INDArrayIndex[] create(INDArray index) { if (index.isMatrix()) { if (index.rows() > Integer.MAX_VALUE) throw new ND4JArraySizeException(); NDArrayIndex[] ret = new NDArrayIndex[(int) index.rows()]; for (int i = 0; i < index.rows(); i++) { INDArray row = index.getRow(i); val nums = new long[(int) index.getRow(i).columns()]; for (int j = 0; j < row.columns(); j++) { nums[j] = (int) row.getFloat(j); } NDArrayIndex idx = new NDArrayIndex(nums); ret[i] = idx; } return ret; } else if (index.isVector()) { long[] indices = NDArrayUtil.toLongs(index); return new NDArrayIndex[] {new NDArrayIndex(indices)}; } throw new IllegalArgumentException("Passed in ndarray must be a matrix or a vector"); }