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

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

本文整理了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

/**
 * Generate a linearly spaced vector
 *
 * @param lower upper bound
 * @param upper lower bound
 * @param num   the step size
 * @return the linearly spaced vector
 */
public static INDArray linspace(float lower, float upper, long num) {
  return linspace((double) lower, (double) upper, num);
}

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

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

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

/**
 * Linspace with complex numbers
 * @param i
 * @param i1
 * @param i2
 * @return
 */
public static IComplexNDArray complexLinSpace(int i, int i1, int i2) {
  return Nd4j.createComplex(Nd4j.linspace(i, i1, i2));
}

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

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

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

/**
 * Generate a linearly spaced vector
 *
 * @param lower upper bound
 * @param upper lower bound
 * @param num   the step size
 * @return the linearly spaced vector
 */
public static INDArray linspace(long lower, long upper, long num) {
  // for now we'll temporarily keep original impl
  if(lower == upper && num == 1) {
    return Nd4j.scalar(lower);
  }
  double approx = (double) num / ((double) (upper - lower) + 1);
  if (approx % 1 <= EPS_THRESHOLD) {
    // FIXME: int cast
    return INSTANCE.linspace((int) lower, (int) upper, (int) num);
  } else {
    return linspace((double) lower, (double) upper, (int) num);
  }
}

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

public static List<Pair<INDArray, String>> getAll3dTestArraysWithShape(long seed, long... shape) {
  if (shape.length != 3)
    throw new IllegalArgumentException("Shape is not length 3");
  List<Pair<INDArray, String>> list = new ArrayList<>();
  String baseMsg = "getAll3dTestArraysWithShape(" + seed + "," + Arrays.toString(shape) + ").get(";
  val len = ArrayUtil.prodLong(shape);
  //Basic 3d in C and F orders:
  Nd4j.getRandom().setSeed(seed);
  INDArray stdC = Nd4j.linspace(1, len, len).reshape('c', shape);
  INDArray stdF = Nd4j.linspace(1, len, len).reshape('f', shape);
  list.add(new Pair<>(stdC, baseMsg + "0)/Nd4j.linspace(1,len,len)(" + Arrays.toString(shape) + ",'c')"));
  list.add(new Pair<>(stdF, baseMsg + "1)/Nd4j.linspace(1,len,len(" + Arrays.toString(shape) + ",'f')"));
  //Various sub arrays:
  list.addAll(get3dSubArraysWithShape(seed, shape));
  //TAD
  list.addAll(get3dTensorAlongDimensionWithShape(seed, shape));
  //Permuted
  list.addAll(get3dPermutedWithShape(seed, shape));
  //Reshaped
  list.addAll(get3dReshapedWithShape(seed, shape));
  return list;
}

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

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

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

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

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

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

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

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

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

public static List<Pair<INDArray, String>> getAll4dTestArraysWithShape(int seed, int... shape) {
  if (shape.length != 4)
    throw new IllegalArgumentException("Shape is not length 4");
  List<Pair<INDArray, String>> list = new ArrayList<>();
  String baseMsg = "getAll4dTestArraysWithShape(" + seed + "," + Arrays.toString(shape) + ").get(";
  //Basic 4d in C and F orders:
  Nd4j.getRandom().setSeed(seed);
  int len = ArrayUtil.prod(shape);
  INDArray stdC = Nd4j.linspace(1, len, len).reshape('c', ArrayUtil.toLongArray(shape));
  INDArray stdF = Nd4j.linspace(1, len, len).reshape('f', ArrayUtil.toLongArray(shape));
  list.add(new Pair<>(stdC, baseMsg + "0)/Nd4j.rand(" + Arrays.toString(shape) + ",'c')"));
  list.add(new Pair<>(stdF, baseMsg + "1)/Nd4j.rand(" + Arrays.toString(shape) + ",'f')"));
  //Various sub arrays:
  list.addAll(get4dSubArraysWithShape(seed, shape));
  //TAD
  list.addAll(get4dTensorAlongDimensionWithShape(seed, shape));
  //Permuted
  list.addAll(get4dPermutedWithShape(seed, shape));
  //Reshaped
  list.addAll(get4dReshapedWithShape(seed, shape));
  return list;
}

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

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

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

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

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

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

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

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

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

public static void main(String[] args){
  //First example: Reverse op. This op reverses the values along a specified dimension
  //c++ code: https://github.com/deeplearning4j/libnd4j/blob/master/include/ops/declarable/generic/transforms/reverse.cpp#L15
  INDArray input = Nd4j.linspace(1, 50, 50).reshape(5,10);
  INDArray output = Nd4j.create(input.shape());
  CustomOp op = DynamicCustomOp.builder("reverse")
    .addInputs(input)
    .addOutputs(output)
    .addIntegerArguments(0) //Reverse along dimension 0
    .build();
  Nd4j.getExecutioner().exec(op);
  System.out.println(input);
  System.out.println();
  System.out.println(output);
  System.out.println("-------------");
  //Another example: meshgrid
  //c++ code: https://github.com/deeplearning4j/libnd4j/blob/master/include/ops/declarable/generic/broadcastable/meshgrid.cpp
  INDArray input1 = Nd4j.linspace(0, 1, 4);
  INDArray input2 = Nd4j.linspace(0, 1, 5);
  INDArray output1 = Nd4j.create(5,4);
  INDArray output2 = Nd4j.create(5, 4);
  op = DynamicCustomOp.builder("meshgrid")
    .addInputs(input1, input2)
    .addOutputs(output1, output2)
    .build();
  Nd4j.getExecutioner().exec(op);
  System.out.println(output1 + "\n\n" + output2);
}

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

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

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

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

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

newShape1[0] += 5;
int len = ArrayUtil.prod(newShape1);
INDArray temp1 = Nd4j.linspace(1, len, len).reshape(newShape1);
INDArray subset1 = temp1.get(NDArrayIndex.interval(2, shape[0] + 2), NDArrayIndex.all(), NDArrayIndex.all());
list.add(new Pair<>(subset1, baseMsg + ".get(0)"));
newShape2[1] += 5;
int len2 = ArrayUtil.prod(newShape2);
INDArray temp2 = Nd4j.linspace(1, len2, len2).reshape(newShape2);
INDArray subset2 = temp2.get(NDArrayIndex.all(), NDArrayIndex.interval(3, shape[1] + 3), NDArrayIndex.all());
list.add(new Pair<>(subset2, baseMsg + ".get(1)"));
newShape3[2] += 5;
int len3 = ArrayUtil.prod(newShape3);
INDArray temp3 = Nd4j.linspace(1, len3, len3).reshape(newShape3);
INDArray subset3 = temp3.get(NDArrayIndex.all(), NDArrayIndex.all(), NDArrayIndex.interval(4, shape[2] + 4));
list.add(new Pair<>(subset3, baseMsg + ".get(2)"));
newShape4[2] += 5;
int len4 = ArrayUtil.prod(newShape4);
INDArray temp4 = Nd4j.linspace(1, len4, len4).reshape(newShape4);
INDArray subset4 = temp4.get(NDArrayIndex.interval(4, shape[0] + 4), NDArrayIndex.interval(3, shape[1] + 3),
        NDArrayIndex.interval(2, shape[2] + 2));

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

public static Pair<INDArray, String> getReshapedWithShape(char ordering, long rows, long cols, long seed) {
  Nd4j.getRandom().setSeed(seed);
  long[] origShape = new long[3];
  if (rows % 2 == 0) {
    origShape[0] = rows / 2;
    origShape[1] = cols;
    origShape[2] = 2;
  } else if (cols % 2 == 0) {
    origShape[0] = rows;
    origShape[1] = cols / 2;
    origShape[2] = 2;
  } else {
    origShape[0] = 1;
    origShape[1] = rows;
    origShape[2] = cols;
  }
  int len = ArrayUtil.prod(origShape);
  INDArray orig = Nd4j.linspace(1, len, len).reshape(ordering, origShape);
  return new Pair<>(orig.reshape(ordering, rows, cols),
          "getReshapedWithShape(" + rows + "," + cols + "," + seed + ")");
}

相关文章