org.nd4j.linalg.factory.Nd4j.linspace()方法的使用及代码示例

x33g5p2x  于2022-01-24 转载在 其他  
字(14.1k)|赞(0)|评价(0)|浏览(153)

本文整理了Java中org.nd4j.linalg.factory.Nd4j.linspace()方法的一些代码示例,展示了Nd4j.linspace()的具体用法。这些代码示例主要来源于Github/Stackoverflow/Maven等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。Nd4j.linspace()方法的具体详情如下:
包路径:org.nd4j.linalg.factory.Nd4j
类名称:Nd4j
方法名:linspace

Nd4j.linspace介绍

[英]Generate a linearly spaced vector
[中]生成一个线性间隔的向量

代码示例

代码示例来源:origin: deeplearning4j/nd4j

  1. /**
  2. * Generate a linearly spaced vector
  3. *
  4. * @param lower upper bound
  5. * @param upper lower bound
  6. * @param num the step size
  7. * @return the linearly spaced vector
  8. */
  9. public static INDArray linspace(float lower, float upper, long num) {
  10. return linspace((double) lower, (double) upper, num);
  11. }

代码示例来源:origin: deeplearning4j/dl4j-examples

  1. static INDArray arange(double start, double end, double step) {
  2. int elements = (int) ((end - start) / step);
  3. System.out.println(elements);
  4. return Nd4j.linspace(start, start + elements * step, elements + 1);
  5. }

代码示例来源:origin: deeplearning4j/nd4j

  1. /**
  2. * Linspace with complex numbers
  3. * @param i
  4. * @param i1
  5. * @param i2
  6. * @return
  7. */
  8. public static IComplexNDArray complexLinSpace(int i, int i1, int i2) {
  9. return Nd4j.createComplex(Nd4j.linspace(i, i1, i2));
  10. }

代码示例来源:origin: deeplearning4j/dl4j-examples

  1. public static void main(String[] args) {
  2. INDArray n = Nd4j.linspace(1,10000000,10000000);
  3. System.out.println("MMUL" + n.mmul(n.transpose()));
  4. }

代码示例来源:origin: deeplearning4j/nd4j

  1. /**
  2. * Generate a linearly spaced vector
  3. *
  4. * @param lower upper bound
  5. * @param upper lower bound
  6. * @param num the step size
  7. * @return the linearly spaced vector
  8. */
  9. public static INDArray linspace(long lower, long upper, long num) {
  10. // for now we'll temporarily keep original impl
  11. if(lower == upper && num == 1) {
  12. return Nd4j.scalar(lower);
  13. }
  14. double approx = (double) num / ((double) (upper - lower) + 1);
  15. if (approx % 1 <= EPS_THRESHOLD) {
  16. // FIXME: int cast
  17. return INSTANCE.linspace((int) lower, (int) upper, (int) num);
  18. } else {
  19. return linspace((double) lower, (double) upper, (int) num);
  20. }
  21. }

代码示例来源:origin: deeplearning4j/nd4j

  1. public static List<Pair<INDArray, String>> getAll3dTestArraysWithShape(long seed, long... shape) {
  2. if (shape.length != 3)
  3. throw new IllegalArgumentException("Shape is not length 3");
  4. List<Pair<INDArray, String>> list = new ArrayList<>();
  5. String baseMsg = "getAll3dTestArraysWithShape(" + seed + "," + Arrays.toString(shape) + ").get(";
  6. val len = ArrayUtil.prodLong(shape);
  7. //Basic 3d in C and F orders:
  8. Nd4j.getRandom().setSeed(seed);
  9. INDArray stdC = Nd4j.linspace(1, len, len).reshape('c', shape);
  10. INDArray stdF = Nd4j.linspace(1, len, len).reshape('f', shape);
  11. list.add(new Pair<>(stdC, baseMsg + "0)/Nd4j.linspace(1,len,len)(" + Arrays.toString(shape) + ",'c')"));
  12. list.add(new Pair<>(stdF, baseMsg + "1)/Nd4j.linspace(1,len,len(" + Arrays.toString(shape) + ",'f')"));
  13. //Various sub arrays:
  14. list.addAll(get3dSubArraysWithShape(seed, shape));
  15. //TAD
  16. list.addAll(get3dTensorAlongDimensionWithShape(seed, shape));
  17. //Permuted
  18. list.addAll(get3dPermutedWithShape(seed, shape));
  19. //Reshaped
  20. list.addAll(get3dReshapedWithShape(seed, shape));
  21. return list;
  22. }

代码示例来源:origin: deeplearning4j/nd4j

  1. public static List<Pair<INDArray, String>> get3dPermutedWithShape(long seed, long... shape) {
  2. Nd4j.getRandom().setSeed(seed);
  3. long[] createdShape = {shape[1], shape[2], shape[0]};
  4. int lencreatedShape = ArrayUtil.prod(createdShape);
  5. INDArray arr = Nd4j.linspace(1, lencreatedShape, lencreatedShape).reshape(createdShape);
  6. INDArray permuted = arr.permute(2, 0, 1);
  7. return Collections.singletonList(new Pair<>(permuted,
  8. "get3dPermutedWithShape(" + seed + "," + Arrays.toString(shape) + ").get(0)"));
  9. }

代码示例来源:origin: deeplearning4j/nd4j

  1. public static List<Pair<INDArray, String>> get3dReshapedWithShape(long seed, long... shape) {
  2. Nd4j.getRandom().setSeed(seed);
  3. long[] shape2d = {shape[0] * shape[2], shape[1]};
  4. int lenshape2d = ArrayUtil.prod(shape2d);
  5. INDArray array2d = Nd4j.linspace(1, lenshape2d, lenshape2d).reshape(shape2d);
  6. INDArray array3d = array2d.reshape(shape);
  7. return Collections.singletonList(new Pair<>(array3d,
  8. "get3dReshapedWithShape(" + seed + "," + Arrays.toString(shape) + ").get(0)"));
  9. }

代码示例来源:origin: deeplearning4j/nd4j

  1. /** Get an array of INDArrays (2d) all with the specified shape. Pair<INDArray,String> returned to aid
  2. * debugging: String contains information on how to reproduce the matrix (i.e., which function, and arguments)
  3. * Each NDArray in the returned array has been obtained by applying an operation such as transpose, tensorAlongDimension,
  4. * etc to an original array.
  5. */
  6. public static List<Pair<INDArray, String>> getAllTestMatricesWithShape(char ordering, int rows, int cols,
  7. int seed) {
  8. List<Pair<INDArray, String>> all = new ArrayList<>();
  9. Nd4j.getRandom().setSeed(seed);
  10. all.add(new Pair<>(Nd4j.linspace(1, rows * cols, rows * cols).reshape(ordering, rows, cols),
  11. "Nd4j..linspace(1,rows * cols,rows * cols).reshape(rows,cols)"));
  12. all.add(getTransposedMatrixWithShape(ordering, rows, cols, seed));
  13. all.addAll(getSubMatricesWithShape(ordering, rows, cols, seed));
  14. all.addAll(getTensorAlongDimensionMatricesWithShape(ordering, rows, cols, seed));
  15. all.add(getPermutedWithShape(ordering, rows, cols, seed));
  16. all.add(getReshapedWithShape(ordering, rows, cols, seed));
  17. return all;
  18. }

代码示例来源:origin: deeplearning4j/nd4j

  1. /** Get an array of INDArrays (2d) all with the specified shape. Pair<INDArray,String> returned to aid
  2. * debugging: String contains information on how to reproduce the matrix (i.e., which function, and arguments)
  3. * Each NDArray in the returned array has been obtained by applying an operation such as transpose, tensorAlongDimension,
  4. * etc to an original array.
  5. */
  6. public static List<Pair<INDArray, String>> getAllTestMatricesWithShape(long rows, long cols, long seed) {
  7. List<Pair<INDArray, String>> all = new ArrayList<>();
  8. Nd4j.getRandom().setSeed(seed);
  9. all.add(new Pair<>(Nd4j.linspace(1L, rows * cols, rows * cols).reshape(rows, cols),
  10. "Nd4j..linspace(1,rows * cols,rows * cols).reshape(rows,cols)"));
  11. all.add(getTransposedMatrixWithShape(rows, cols, seed));
  12. all.addAll(getSubMatricesWithShape(rows, cols, seed));
  13. all.addAll(getTensorAlongDimensionMatricesWithShape(rows, cols, seed));
  14. all.add(getPermutedWithShape(rows, cols, seed));
  15. all.add(getReshapedWithShape(rows, cols, seed));
  16. return all;
  17. }

代码示例来源:origin: deeplearning4j/nd4j

  1. public static List<Pair<INDArray, String>> getAll4dTestArraysWithShape(int seed, int... shape) {
  2. if (shape.length != 4)
  3. throw new IllegalArgumentException("Shape is not length 4");
  4. List<Pair<INDArray, String>> list = new ArrayList<>();
  5. String baseMsg = "getAll4dTestArraysWithShape(" + seed + "," + Arrays.toString(shape) + ").get(";
  6. //Basic 4d in C and F orders:
  7. Nd4j.getRandom().setSeed(seed);
  8. int len = ArrayUtil.prod(shape);
  9. INDArray stdC = Nd4j.linspace(1, len, len).reshape('c', ArrayUtil.toLongArray(shape));
  10. INDArray stdF = Nd4j.linspace(1, len, len).reshape('f', ArrayUtil.toLongArray(shape));
  11. list.add(new Pair<>(stdC, baseMsg + "0)/Nd4j.rand(" + Arrays.toString(shape) + ",'c')"));
  12. list.add(new Pair<>(stdF, baseMsg + "1)/Nd4j.rand(" + Arrays.toString(shape) + ",'f')"));
  13. //Various sub arrays:
  14. list.addAll(get4dSubArraysWithShape(seed, shape));
  15. //TAD
  16. list.addAll(get4dTensorAlongDimensionWithShape(seed, shape));
  17. //Permuted
  18. list.addAll(get4dPermutedWithShape(seed, shape));
  19. //Reshaped
  20. list.addAll(get4dReshapedWithShape(seed, shape));
  21. return list;
  22. }

代码示例来源:origin: deeplearning4j/nd4j

  1. INDArray orig1a = Nd4j.linspace(1, lenshape4d1, lenshape4d1).reshape(shape4d1);
  2. INDArray tad1a = orig1a.javaTensorAlongDimension(0, 0, 1, 2);
  3. INDArray orig1b = Nd4j.linspace(1, lenshape4d1, lenshape4d1).reshape(shape4d1);
  4. INDArray tad1b = orig1b.javaTensorAlongDimension(1, 0, 1, 2);
  5. INDArray orig2 = Nd4j.linspace(1, lenshape4d2, lenshape4d2).reshape(shape4d2);
  6. INDArray tad2 = orig2.javaTensorAlongDimension(1, 1, 2, 3);
  7. list.add(new Pair<>(tad2, baseMsg + ".get(2)"));
  8. INDArray orig3 = Nd4j.linspace(1, lenshape4d3, lenshape4d3).reshape(shape4d3);
  9. INDArray tad3 = orig3.javaTensorAlongDimension(1, 1, 3, 0);
  10. list.add(new Pair<>(tad3, baseMsg + ".get(3)"));
  11. INDArray orig4 = Nd4j.linspace(1, lenshape4d4, lenshape4d4).reshape(shape4d4);
  12. INDArray tad4 = orig4.javaTensorAlongDimension(1, 2, 0, 3);
  13. list.add(new Pair<>(tad4, baseMsg + ".get(4)"));

代码示例来源:origin: deeplearning4j/nd4j

  1. public static Pair<INDArray, String> getTransposedMatrixWithShape(char ordering, int rows, int cols, int seed) {
  2. Nd4j.getRandom().setSeed(seed);
  3. INDArray out = Nd4j.linspace(1, rows * cols, rows * cols).reshape(ordering, cols, rows);
  4. return new Pair<>(out.transpose(), "getTransposedMatrixWithShape(" + rows + "," + cols + "," + seed + ")");
  5. }

代码示例来源:origin: deeplearning4j/nd4j

  1. public static Pair<INDArray, String> getPermutedWithShape(char ordering, long rows, long cols, long seed) {
  2. Nd4j.getRandom().setSeed(seed);
  3. long len = rows * cols;
  4. INDArray arr = Nd4j.linspace(1, len, len).reshape(cols, rows);
  5. return new Pair<>(arr.permute(1, 0), "getPermutedWithShape(" + rows + "," + cols + "," + seed + ")");
  6. }

代码示例来源:origin: deeplearning4j/nd4j

  1. public static Pair<INDArray, String> getTransposedMatrixWithShape(long rows, long cols, long seed) {
  2. Nd4j.getRandom().setSeed(seed);
  3. INDArray out = Nd4j.linspace(1, rows * cols, rows * cols).reshape(cols, rows);
  4. return new Pair<>(out.transpose(), "getTransposedMatrixWithShape(" + rows + "," + cols + "," + seed + ")");
  5. }

代码示例来源:origin: deeplearning4j/dl4j-examples

  1. public static void main(String[] args){
  2. //First example: Reverse op. This op reverses the values along a specified dimension
  3. //c++ code: https://github.com/deeplearning4j/libnd4j/blob/master/include/ops/declarable/generic/transforms/reverse.cpp#L15
  4. INDArray input = Nd4j.linspace(1, 50, 50).reshape(5,10);
  5. INDArray output = Nd4j.create(input.shape());
  6. CustomOp op = DynamicCustomOp.builder("reverse")
  7. .addInputs(input)
  8. .addOutputs(output)
  9. .addIntegerArguments(0) //Reverse along dimension 0
  10. .build();
  11. Nd4j.getExecutioner().exec(op);
  12. System.out.println(input);
  13. System.out.println();
  14. System.out.println(output);
  15. System.out.println("-------------");
  16. //Another example: meshgrid
  17. //c++ code: https://github.com/deeplearning4j/libnd4j/blob/master/include/ops/declarable/generic/broadcastable/meshgrid.cpp
  18. INDArray input1 = Nd4j.linspace(0, 1, 4);
  19. INDArray input2 = Nd4j.linspace(0, 1, 5);
  20. INDArray output1 = Nd4j.create(5,4);
  21. INDArray output2 = Nd4j.create(5, 4);
  22. op = DynamicCustomOp.builder("meshgrid")
  23. .addInputs(input1, input2)
  24. .addOutputs(output1, output2)
  25. .build();
  26. Nd4j.getExecutioner().exec(op);
  27. System.out.println(output1 + "\n\n" + output2);
  28. }

代码示例来源:origin: deeplearning4j/nd4j

  1. int[] shape4d1 = {3, shape[0], shape[1], shape[2], shape[3]};
  2. int len = ArrayUtil.prod(shape4d1);
  3. INDArray orig1a = Nd4j.linspace(1, len, len).reshape(ArrayUtil.toLongArray(shape4d1));
  4. INDArray tad1a = orig1a.javaTensorAlongDimension(0, 1, 2, 3, 4);
  5. INDArray orig1b = Nd4j.linspace(1, len, len).reshape(ArrayUtil.toLongArray(shape4d1));
  6. INDArray tad1b = orig1b.javaTensorAlongDimension(2, 1, 2, 3, 4);
  7. INDArray orig2 = Nd4j.linspace(1, len2, len2).reshape(ArrayUtil.toLongArray(shape4d2));
  8. INDArray tad2 = orig2.javaTensorAlongDimension(1, 3, 4, 2, 1);
  9. list.add(new Pair<>(tad2, baseMsg + ".get(2)"));
  10. INDArray orig3 = Nd4j.linspace(1, len3, len3).reshape(ArrayUtil.toLongArray(shape4d3));
  11. INDArray tad3 = orig3.javaTensorAlongDimension(1, 4, 1, 3, 0);
  12. list.add(new Pair<>(tad3, baseMsg + ".get(3)"));
  13. INDArray orig4 = Nd4j.linspace(1, len4, len4).reshape(ArrayUtil.toLongArray(shape4d4));
  14. INDArray tad4 = orig4.javaTensorAlongDimension(1, 2, 0, 3, 1);
  15. list.add(new Pair<>(tad4, baseMsg + ".get(4)"));

代码示例来源:origin: deeplearning4j/nd4j

  1. public static List<Pair<INDArray, String>> getSubMatricesWithShape(char ordering, long rows, long cols, long seed) {
  2. //Create 3 identical matrices. Could do get() on single original array, but in-place modifications on one
  3. //might mess up tests for another
  4. Nd4j.getRandom().setSeed(seed);
  5. long[] shape = new long[] {2 * rows + 4, 2 * cols + 4};
  6. int len = ArrayUtil.prod(shape);
  7. INDArray orig = Nd4j.linspace(1, len, len).reshape(ordering, shape);
  8. INDArray first = orig.get(NDArrayIndex.interval(0, rows), NDArrayIndex.interval(0, cols));
  9. Nd4j.getRandom().setSeed(seed);
  10. orig = Nd4j.linspace(1, len, len).reshape(shape);
  11. INDArray second = orig.get(NDArrayIndex.interval(3, rows + 3), NDArrayIndex.interval(3, cols + 3));
  12. Nd4j.getRandom().setSeed(seed);
  13. orig = Nd4j.linspace(1, len, len).reshape(ordering, shape);
  14. INDArray third = orig.get(NDArrayIndex.interval(rows, 2 * rows), NDArrayIndex.interval(cols, 2 * cols));
  15. String baseMsg = "getSubMatricesWithShape(" + rows + "," + cols + "," + seed + ")";
  16. List<Pair<INDArray, String>> list = new ArrayList<>(3);
  17. list.add(new Pair<>(first, baseMsg + ".get(0)"));
  18. list.add(new Pair<>(second, baseMsg + ".get(1)"));
  19. list.add(new Pair<>(third, baseMsg + ".get(2)"));
  20. return list;
  21. }

代码示例来源:origin: deeplearning4j/nd4j

  1. newShape1[0] += 5;
  2. int len = ArrayUtil.prod(newShape1);
  3. INDArray temp1 = Nd4j.linspace(1, len, len).reshape(newShape1);
  4. INDArray subset1 = temp1.get(NDArrayIndex.interval(2, shape[0] + 2), NDArrayIndex.all(), NDArrayIndex.all());
  5. list.add(new Pair<>(subset1, baseMsg + ".get(0)"));
  6. newShape2[1] += 5;
  7. int len2 = ArrayUtil.prod(newShape2);
  8. INDArray temp2 = Nd4j.linspace(1, len2, len2).reshape(newShape2);
  9. INDArray subset2 = temp2.get(NDArrayIndex.all(), NDArrayIndex.interval(3, shape[1] + 3), NDArrayIndex.all());
  10. list.add(new Pair<>(subset2, baseMsg + ".get(1)"));
  11. newShape3[2] += 5;
  12. int len3 = ArrayUtil.prod(newShape3);
  13. INDArray temp3 = Nd4j.linspace(1, len3, len3).reshape(newShape3);
  14. INDArray subset3 = temp3.get(NDArrayIndex.all(), NDArrayIndex.all(), NDArrayIndex.interval(4, shape[2] + 4));
  15. list.add(new Pair<>(subset3, baseMsg + ".get(2)"));
  16. newShape4[2] += 5;
  17. int len4 = ArrayUtil.prod(newShape4);
  18. INDArray temp4 = Nd4j.linspace(1, len4, len4).reshape(newShape4);
  19. INDArray subset4 = temp4.get(NDArrayIndex.interval(4, shape[0] + 4), NDArrayIndex.interval(3, shape[1] + 3),
  20. NDArrayIndex.interval(2, shape[2] + 2));

代码示例来源:origin: deeplearning4j/nd4j

  1. public static Pair<INDArray, String> getReshapedWithShape(char ordering, long rows, long cols, long seed) {
  2. Nd4j.getRandom().setSeed(seed);
  3. long[] origShape = new long[3];
  4. if (rows % 2 == 0) {
  5. origShape[0] = rows / 2;
  6. origShape[1] = cols;
  7. origShape[2] = 2;
  8. } else if (cols % 2 == 0) {
  9. origShape[0] = rows;
  10. origShape[1] = cols / 2;
  11. origShape[2] = 2;
  12. } else {
  13. origShape[0] = 1;
  14. origShape[1] = rows;
  15. origShape[2] = cols;
  16. }
  17. int len = ArrayUtil.prod(origShape);
  18. INDArray orig = Nd4j.linspace(1, len, len).reshape(ordering, origShape);
  19. return new Pair<>(orig.reshape(ordering, rows, cols),
  20. "getReshapedWithShape(" + rows + "," + cols + "," + seed + ")");
  21. }

相关文章