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

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

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

Nd4j.valueArrayOf介绍

[英]Creates a row vector ndarray with the specified value as the only value in the ndarray Some people may know this as np.full
[中]创建一个行向量ndarray,其中指定的值是ndarray中的唯一值。有些人可能知道这是np。满的

代码示例

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

  1. @Override
  2. public INDArray doCreate(long[] shape, INDArray paramsView) {
  3. return Nd4j.valueArrayOf(shape,constant);
  4. }

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

  1. /**
  2. * Returns an ndarray with 1 if the element is epsilon equals
  3. *
  4. * @param other the number to compare
  5. * @return a ndarray with the given
  6. * binary conditions
  7. */
  8. @Override
  9. public INDArray epsi(Number other) {
  10. INDArray otherArr = Nd4j.valueArrayOf(shape(), other.doubleValue());
  11. return epsi(otherArr);
  12. }

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

  1. /**
  2. * Prepare the boundaries for processing
  3. * @param bounds the bounds
  4. * @param x the input in to the approximation
  5. * @return the lower and upper bounds as an array of ndarrays
  6. * (in that order) of the same shape as x
  7. */
  8. public static INDArray[] prepareBounds(INDArray bounds,INDArray x) {
  9. return new INDArray[] {Nd4j.valueArrayOf(x.shape(),bounds.getDouble(0)),
  10. Nd4j.valueArrayOf(x.shape(),bounds.getDouble(1))};
  11. }

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

  1. /**
  2. * Append the given
  3. * array with the specified value size
  4. * along a particular axis
  5. * @param arr the array to append to
  6. * @param padAmount the pad amount of the array to be returned
  7. * @param val the value to append
  8. * @param axis the axis to append to
  9. * @return the newly created array
  10. */
  11. public static INDArray prepend(INDArray arr, int padAmount, double val, int axis) {
  12. if (padAmount == 0)
  13. return arr;
  14. long[] paShape = ArrayUtil.copy(arr.shape());
  15. if (axis < 0)
  16. axis = axis + arr.shape().length;
  17. paShape[axis] = padAmount;
  18. INDArray concatArr = Nd4j.valueArrayOf(paShape, val);
  19. return Nd4j.concat(axis, concatArr, arr);
  20. }

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

  1. System.out.println(allOnes);
  2. INDArray allTens = Nd4j.valueArrayOf(nRows, nColumns, 10.0);
  3. System.out.println("\nNd4j.valueArrayOf(nRows, nColumns, 10.0)");
  4. System.out.println(allTens);

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

  1. /**
  2. * Append the given
  3. * array with the specified value size
  4. * along a particular axis
  5. * @param arr the array to append to
  6. * @param padAmount the pad amount of the array to be returned
  7. * @param val the value to append
  8. * @param axis the axis to append to
  9. * @return the newly created array
  10. */
  11. public static INDArray append(INDArray arr, int padAmount, double val, int axis) {
  12. if (padAmount == 0)
  13. return arr;
  14. long[] paShape = ArrayUtil.copy(arr.shape());
  15. if (axis < 0)
  16. axis = axis + arr.shape().length;
  17. paShape[axis] = padAmount;
  18. INDArray concatArray = Nd4j.valueArrayOf(paShape, val);
  19. return Nd4j.concat(axis, arr, concatArray);
  20. }

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

  1. public static void main(String[] args){
  2. int nRows = 3;
  3. int nCols = 5;
  4. long rngSeed = 12345;
  5. //Generate random numbers between -1 and +1
  6. INDArray random = Nd4j.rand(nRows, nCols, rngSeed).muli(2).subi(1);
  7. System.out.println("Array values:");
  8. System.out.println(random);
  9. //For example, we can conditionally replace values less than 0.0 with 0.0:
  10. INDArray randomCopy = random.dup();
  11. BooleanIndexing.replaceWhere(randomCopy, 0.0, Conditions.lessThan(0.0));
  12. System.out.println("After conditionally replacing negative values:\n" + randomCopy);
  13. //Or conditionally replace NaN values:
  14. INDArray hasNaNs = Nd4j.create(new double[]{1.0,1.0,Double.NaN,1.0});
  15. BooleanIndexing.replaceWhere(hasNaNs,0.0, Conditions.isNan());
  16. System.out.println("hasNaNs after replacing NaNs with 0.0:\n" + hasNaNs);
  17. //Or we can conditionally copy values from one array to another:
  18. randomCopy = random.dup();
  19. INDArray tens = Nd4j.valueArrayOf(nRows, nCols, 10.0);
  20. BooleanIndexing.replaceWhere(randomCopy, tens, Conditions.lessThan(0.0));
  21. System.out.println("Conditionally copying values from array 'tens', if original value is less than 0.0\n" + randomCopy);
  22. //One simple task is to count the number of values that match the condition
  23. MatchCondition op = new MatchCondition(random, Conditions.greaterThan(0.0));
  24. int countGreaterThanZero = Nd4j.getExecutioner().exec(op,Integer.MAX_VALUE).getInt(0); //MAX_VALUE = "along all dimensions" or equivalently "for entire array"
  25. System.out.println("Number of values matching condition 'greater than 0': " + countGreaterThanZero);
  26. }

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

  1. /**
  2. * A getter for the allocated ndarray
  3. * with this {@link SDVariable}.
  4. *
  5. * This getter will lazy initialize an array if one is not found
  6. * based on the associated shape and {@link WeightInitScheme}
  7. * if neither are found, an {@link ND4JIllegalStateException}
  8. * is thrown.
  9. *
  10. * If a {@link DifferentialFunction} is defined, note that
  11. * its getArr() method is called instead.
  12. * @return the {@link INDArray} associated with this variable.
  13. */
  14. public INDArray getArr() {
  15. if(sameDiff.arrayAlreadyExistsForVarName(getVarName()))
  16. return sameDiff.getArrForVarName(getVarName());
  17. //initialize value if it's actually a scalar constant (zero or 1 typically...)
  18. if(getScalarValue() != null && ArrayUtil.prod(getShape()) == 1) {
  19. INDArray arr = Nd4j.valueArrayOf(getShape(),
  20. getScalarValue().doubleValue());
  21. sameDiff.associateArrayWithVariable(arr,this);
  22. }
  23. else if(sameDiff.getShapeForVarName(getVarName()) == null)
  24. return null;
  25. else {
  26. INDArray newAlloc = getWeightInitScheme().create(sameDiff.getShapeForVarName(getVarName()));
  27. sameDiff.associateArrayWithVariable(newAlloc,this);
  28. }
  29. return sameDiff.getArrForVarName(getVarName());
  30. }

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

  1. arrayShape = new int[]{};
  2. INDArray array = Nd4j.valueArrayOf(arrayShape, (double) val);
  3. return array;
  4. } else if (tfTensor.getInt64ValCount() > 0) {
  5. arrayShape = new int[]{};
  6. INDArray array = Nd4j.valueArrayOf(arrayShape, (double) val);
  7. return array;
  8. } else if (tfTensor.getFloatValCount() > 0) {

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

  1. print("ARange", stepOfThreeTillTen);
  2. INDArray allEights = Nd4j.valueArrayOf(new int[] {2,3}, 8);
  3. print("2x3 Eights", allEights);
  4. print("Concatenated arrays on dimension 1", concatenatedAxisOne);
  5. INDArray [] verticalSplit = CustomOperations.split(Nd4j.valueArrayOf(new int[] {9, 9}, 9),
  6. 3);
  7. print("Vertical Split", verticalSplit);
  8. INDArray [] horizontalSplit = CustomOperations.hsplit(Nd4j.valueArrayOf(new int[]{10, 10}, 10),
  9. 5);
  10. print("Horizontal Split", horizontalSplit);

代码示例来源:origin: org.deeplearning4j/deeplearning4j-nn

  1. protected INDArray createBias(int nOut, double biasInit, INDArray biasParamView, boolean initializeParameters) {
  2. if (initializeParameters) {
  3. INDArray ret = Nd4j.valueArrayOf(nOut, biasInit);
  4. biasParamView.assign(ret);
  5. }
  6. return biasParamView;
  7. }

代码示例来源:origin: improbable-research/keanu

  1. public static INDArray valueArrayOf(long[] shape, double value, DataBuffer.Type bufferType) {
  2. Nd4j.setDataType(bufferType);
  3. switch (shape.length) {
  4. case 0:
  5. return scalar(value, bufferType);
  6. case 1:
  7. return reshapeToVector(Nd4j.valueArrayOf(shape, value));
  8. default:
  9. return Nd4j.valueArrayOf(shape, value);
  10. }
  11. }

代码示例来源:origin: org.nd4j/nd4j-api

  1. /**
  2. * Returns an ndarray with 1 if the element is epsilon equals
  3. *
  4. * @param other the number to compare
  5. * @return a ndarray with the given
  6. * binary conditions
  7. */
  8. @Override
  9. public INDArray epsi(Number other) {
  10. INDArray otherArr = Nd4j.valueArrayOf(shape(), other.doubleValue());
  11. return epsi(otherArr);
  12. }

代码示例来源:origin: improbable-research/keanu

  1. private static INDArray performOperationWithScalarTensorPreservingShape(INDArray left, INDArray right, BiFunction<INDArray, INDArray, INDArray> operation) {
  2. if (left.length() == 1 || right.length() == 1) {
  3. long[] resultShape = Shape.broadcastOutputShape(left.shape(), right.shape());
  4. INDArray result = (left.length() == 1) ?
  5. operation.apply(Nd4j.valueArrayOf(right.shape(), left.getDouble(0)), right) :
  6. operation.apply(left, Nd4j.valueArrayOf(left.shape(), right.getDouble(0)));
  7. return result.reshape(resultShape);
  8. } else {
  9. return operation.apply(left, right);
  10. }
  11. }

代码示例来源:origin: org.deeplearning4j/deeplearning4j-nn

  1. protected INDArray createVisibleBias(NeuralNetConfiguration conf, INDArray visibleBiasView,
  2. boolean initializeParameters) {
  3. org.deeplearning4j.nn.conf.layers.BasePretrainNetwork layerConf =
  4. (org.deeplearning4j.nn.conf.layers.BasePretrainNetwork) conf.getLayer();
  5. if (initializeParameters) {
  6. INDArray ret = Nd4j.valueArrayOf(layerConf.getNIn(), layerConf.getVisibleBiasInit());
  7. visibleBiasView.assign(ret);
  8. }
  9. return visibleBiasView;
  10. }

代码示例来源:origin: org.nd4j/nd4j-api

  1. /**
  2. * Append the given
  3. * array with the specified value size
  4. * along a particular axis
  5. * @param arr the array to append to
  6. * @param padAmount the pad amount of the array to be returned
  7. * @param val the value to append
  8. * @param axis the axis to append to
  9. * @return the newly created array
  10. */
  11. public static INDArray append(INDArray arr, int padAmount, double val, int axis) {
  12. if (padAmount == 0)
  13. return arr;
  14. int[] paShape = ArrayUtil.copy(arr.shape());
  15. if (axis < 0)
  16. axis = axis + arr.shape().length;
  17. paShape[axis] = padAmount;
  18. INDArray concatArray = Nd4j.valueArrayOf(paShape, val);
  19. return Nd4j.concat(axis, arr, concatArray);
  20. }

代码示例来源:origin: org.nd4j/nd4j-api

  1. /**
  2. * Append the given
  3. * array with the specified value size
  4. * along a particular axis
  5. * @param arr the array to append to
  6. * @param padAmount the pad amount of the array to be returned
  7. * @param val the value to append
  8. * @param axis the axis to append to
  9. * @return the newly created array
  10. */
  11. public static INDArray prepend(INDArray arr, int padAmount, double val, int axis) {
  12. if (padAmount == 0)
  13. return arr;
  14. int[] paShape = ArrayUtil.copy(arr.shape());
  15. if (axis < 0)
  16. axis = axis + arr.shape().length;
  17. paShape[axis] = padAmount;
  18. INDArray concatArr = Nd4j.valueArrayOf(paShape, val);
  19. return Nd4j.concat(axis, concatArr, arr);
  20. }

代码示例来源:origin: org.nd4j/nd4j-cuda-10.0

  1. ret = Nd4j.zeros(retShape);
  2. } else {
  3. ret = Nd4j.valueArrayOf(retShape, op.zeroDouble());

代码示例来源:origin: org.nd4j/nd4j-cuda-7.5

  1. protected void buildZ(IndexAccumulation op, int... dimension) {
  2. Arrays.sort(dimension);
  3. for (int i = 0; i < dimension.length; i++) {
  4. if (dimension[i] < 0)
  5. dimension[i] += op.x().rank();
  6. }
  7. //do op along all dimensions
  8. if (dimension.length == op.x().rank())
  9. dimension = new int[] {Integer.MAX_VALUE};
  10. int[] retShape = Shape.wholeArrayDimension(dimension) ? new int[] {1, 1}
  11. : ArrayUtil.removeIndex(op.x().shape(), dimension);
  12. //ensure vector is proper shape
  13. if (retShape.length == 1) {
  14. if (dimension[0] == 0)
  15. retShape = new int[] {1, retShape[0]};
  16. else
  17. retShape = new int[] {retShape[0], 1};
  18. } else if (retShape.length == 0) {
  19. retShape = new int[] {1, 1};
  20. }
  21. INDArray ret = null;
  22. if (Math.abs(op.zeroDouble()) < Nd4j.EPS_THRESHOLD) {
  23. ret = Nd4j.zeros(retShape);
  24. } else {
  25. ret = Nd4j.valueArrayOf(retShape, op.zeroDouble());
  26. }
  27. op.setZ(ret);
  28. }

代码示例来源:origin: improbable-research/keanu

  1. private static INDArray executeNd4jTransformOpWithPreservedScalarTensorShape(INDArray mask, INDArray right, DataBuffer.Type bufferType, QuadFunction<INDArray, INDArray, INDArray, Long, BaseTransformOp> baseTransformOpConstructor) {
  2. if (mask.length() == 1 || right.length() == 1) {
  3. long[] resultShape = Shape.broadcastOutputShape(mask.shape(), right.shape());
  4. if (mask.length() == 1) {
  5. mask = Nd4j.valueArrayOf(right.shape(), mask.getDouble(0));
  6. Nd4j.getExecutioner().exec(
  7. baseTransformOpConstructor.apply(mask, right, mask, mask.length())
  8. );
  9. } else {
  10. Nd4j.getExecutioner().exec(
  11. baseTransformOpConstructor.apply(mask,
  12. valueArrayOf(mask.shape(), right.getDouble(0), bufferType),
  13. mask,
  14. mask.length()
  15. )
  16. );
  17. }
  18. return mask.reshape(resultShape);
  19. } else {
  20. Nd4j.getExecutioner().exec(
  21. baseTransformOpConstructor.apply(mask, right, mask, mask.length())
  22. );
  23. return mask;
  24. }
  25. }

相关文章