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

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

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

Nd4j.createBufferDetached介绍

[英]Create a buffer equal of length prod(shape). This method is NOT affected by workspaces
[中]创建一个长度等于prod(shape)的缓冲区。此方法不受工作区的影响

代码示例

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

  1. public static DataBuffer createShapeInformation(long[] shape, long[] stride, long offset, long elementWiseStride, char order) {
  2. offset = 0;
  3. if (shape.length != stride.length)
  4. throw new IllegalStateException("Shape and stride must be the same length");
  5. int rank = shape.length;
  6. long shapeBuffer[] = new long[rank * 2 + 4];
  7. shapeBuffer[0] = rank;
  8. int count = 1;
  9. for (int e = 0; e < shape.length; e++)
  10. shapeBuffer[count++] = shape[e];
  11. for (int e = 0; e < stride.length; e++)
  12. shapeBuffer[count++] = stride[e];
  13. shapeBuffer[count++] = (int) offset;
  14. shapeBuffer[count++] = elementWiseStride;
  15. shapeBuffer[count] = (int) order;
  16. DataBuffer ret = Nd4j.createBufferDetached(shapeBuffer);
  17. ret.setConstant(true);
  18. return ret;
  19. }

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

  1. /**
  2. * Creates the shape information buffer
  3. * given the shape,stride
  4. * @param shape the shape for the buffer
  5. * @param stride the stride for the buffer
  6. * @param offset the offset for the buffer
  7. * @param elementWiseStride the element wise stride for the buffer
  8. * @param order the order for the buffer
  9. * @return the shape information buffer given the parameters
  10. */
  11. public static DataBuffer createShapeInformation(int[] shape, int[] stride, long offset, int elementWiseStride, char order) {
  12. if (shape.length != stride.length)
  13. throw new IllegalStateException("Shape and stride must be the same length");
  14. int rank = shape.length;
  15. int shapeBuffer[] = new int[rank * 2 + 4];
  16. shapeBuffer[0] = rank;
  17. int count = 1;
  18. for (int e = 0; e < shape.length; e++)
  19. shapeBuffer[count++] = shape[e];
  20. for (int e = 0; e < stride.length; e++)
  21. shapeBuffer[count++] = stride[e];
  22. shapeBuffer[count++] = (int) offset;
  23. shapeBuffer[count++] = elementWiseStride;
  24. shapeBuffer[count] = (int) order;
  25. DataBuffer ret = Nd4j.createBufferDetached(shapeBuffer);
  26. ret.setConstant(true);
  27. return ret;
  28. }

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

  1. /**
  2. * Read in an ndarray from a data input stream
  3. *
  4. * @param dis the data input stream to read from
  5. * @return the ndarray
  6. * @throws IOException
  7. */
  8. public static INDArray read(DataInputStream dis) throws IOException {
  9. DataBuffer shapeInformation = Nd4j.createBufferDetached(new long[1], DataBuffer.Type.LONG);
  10. shapeInformation.read(dis);
  11. int length = Shape.length(shapeInformation);
  12. DataBuffer data = CompressedDataBuffer.readUnknown(dis, length);
  13. return createArrayFromShapeBuffer(data, shapeInformation);
  14. }

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

  1. DataBuffer shapeBuff = Nd4j.createBufferDetached(new int[shapeBufferLength]);

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

  1. @Override
  2. public INDArray subArray(long[] offsets, int[] shape, int[] stride) {
  3. Nd4j.getCompressor().autoDecompress(this);
  4. int n = shape.length;
  5. // FIXME: shapeInfo should be used here
  6. if (shape.length < 1)
  7. return create(Nd4j.createBufferDetached(shape));
  8. if (offsets.length != n)
  9. throw new IllegalArgumentException("Invalid offset " + Arrays.toString(offsets));
  10. if (stride.length != n)
  11. throw new IllegalArgumentException("Invalid stride " + Arrays.toString(stride));
  12. if (Shape.contentEquals(shape, shapeOf())) {
  13. if (ArrayUtil.isZero(offsets)) {
  14. return this;
  15. } else {
  16. throw new IllegalArgumentException("Invalid subArray offsets");
  17. }
  18. }
  19. long[] dotProductOffsets = offsets;
  20. int[] dotProductStride = stride;
  21. long offset = Shape.offset(javaShapeInformation) + NDArrayIndex.offset(dotProductStride, dotProductOffsets);
  22. if (offset >= data().length())
  23. offset = ArrayUtil.sumLong(offsets);
  24. return create(data, Arrays.copyOf(shape, shape.length), stride, offset, ordering());
  25. }

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

  1. @Override
  2. public INDArray subArray(ShapeOffsetResolution resolution) {
  3. Nd4j.getCompressor().autoDecompress(this);
  4. long[] offsets = resolution.getOffsets();
  5. int[] shape = LongUtils.toInts(resolution.getShapes());
  6. int[] stride = LongUtils.toInts(resolution.getStrides());
  7. // if (offset() + resolution.getOffset() >= Integer.MAX_VALUE)
  8. // throw new IllegalArgumentException("Offset of array can not be >= Integer.MAX_VALUE");
  9. long offset = (offset() + resolution.getOffset());
  10. int n = shape.length;
  11. // FIXME: shapeInfo should be used here
  12. if (shape.length < 1)
  13. return create(Nd4j.createBufferDetached(shape));
  14. if (offsets.length != n)
  15. throw new IllegalArgumentException("Invalid offset " + Arrays.toString(offsets));
  16. if (stride.length != n)
  17. throw new IllegalArgumentException("Invalid stride " + Arrays.toString(stride));
  18. if (shape.length == rank() && Shape.contentEquals(shape, shapeOf())) {
  19. if (ArrayUtil.isZero(offsets)) {
  20. return this;
  21. } else {
  22. throw new IllegalArgumentException("Invalid subArray offsets");
  23. }
  24. }
  25. char newOrder = Shape.getOrder(shape, stride, 1);
  26. return create(data, Arrays.copyOf(shape, shape.length), stride, offset, newOrder);
  27. }

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

  1. public static DataBuffer createBufferDetached(int[] shape, DataBuffer.Type type) {
  2. int length = ArrayUtil.prod(shape);
  3. if (type == DataBuffer.Type.INT)
  4. return createBufferDetached(new int[length]);
  5. else if (type == DataBuffer.Type.HALF)
  6. return createBufferDetached(new float[length]);
  7. return type == DataBuffer.Type.DOUBLE ? createBufferDetached(new double[length]) : createBufferDetached(new float[length]);
  8. }

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

  1. shapeBuffer[count] = (int) order;
  2. DataBuffer ret = Nd4j.createBufferDetached(shapeBuffer);
  3. ret.setConstant(true);

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

  1. @Override
  2. public DataBuffer getConstantBuffer(long[] array) {
  3. // logger.info("getConstantBuffer(int[]) called");
  4. ArrayDescriptor descriptor = new ArrayDescriptor(array);
  5. Integer deviceId = AtomicAllocator.getInstance().getDeviceId();
  6. ensureMaps(deviceId);
  7. if (!buffersCache.get(deviceId).containsKey(descriptor)) {
  8. // we create new databuffer
  9. //logger.info("Creating new constant buffer...");
  10. DataBuffer buffer = Nd4j.createBufferDetached(array);
  11. if (constantOffsets.get(deviceId).get() + (array.length * 8) < MAX_CONSTANT_LENGTH) {
  12. buffer.setConstant(true);
  13. // now we move data to constant memory, and keep happy
  14. moveToConstantSpace(buffer);
  15. buffersCache.get(deviceId).put(descriptor, buffer);
  16. bytes.addAndGet(array.length * 8);
  17. }
  18. return buffer;
  19. } //else logger.info("Reusing constant buffer...");
  20. return buffersCache.get(deviceId).get(descriptor);
  21. }

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

  1. /**
  2. * This method returns DataBuffer with contant equal to input array.
  3. *
  4. * PLEASE NOTE: This method assumes that you'll never ever change values within result DataBuffer
  5. *
  6. * @param array
  7. * @return
  8. */
  9. @Override
  10. public DataBuffer getConstantBuffer(int[] array) {
  11. // logger.info("getConstantBuffer(int[]) called");
  12. ArrayDescriptor descriptor = new ArrayDescriptor(array);
  13. Integer deviceId = AtomicAllocator.getInstance().getDeviceId();
  14. ensureMaps(deviceId);
  15. if (!buffersCache.get(deviceId).containsKey(descriptor)) {
  16. // we create new databuffer
  17. //logger.info("Creating new constant buffer...");
  18. DataBuffer buffer = Nd4j.createBufferDetached(array);
  19. if (constantOffsets.get(deviceId).get() + (array.length * 4) < MAX_CONSTANT_LENGTH) {
  20. buffer.setConstant(true);
  21. // now we move data to constant memory, and keep happy
  22. moveToConstantSpace(buffer);
  23. buffersCache.get(deviceId).put(descriptor, buffer);
  24. bytes.addAndGet(array.length * 4);
  25. }
  26. return buffer;
  27. } //else logger.info("Reusing constant buffer...");
  28. return buffersCache.get(deviceId).get(descriptor);
  29. }

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

  1. /**
  2. * This method returns DataBuffer with contant equal to input array.
  3. *
  4. * PLEASE NOTE: This method assumes that you'll never ever change values within result DataBuffer
  5. *
  6. * @param array
  7. * @return
  8. */
  9. @Override
  10. public DataBuffer getConstantBuffer(int[] array) {
  11. // logger.info("getConstantBuffer(int[]) called");
  12. ArrayDescriptor descriptor = new ArrayDescriptor(array);
  13. Integer deviceId = AtomicAllocator.getInstance().getDeviceId();
  14. ensureMaps(deviceId);
  15. if (!buffersCache.get(deviceId).containsKey(descriptor)) {
  16. // we create new databuffer
  17. //logger.info("Creating new constant buffer...");
  18. DataBuffer buffer = Nd4j.createBufferDetached(array);
  19. if (constantOffsets.get(deviceId).get() + (array.length * 4) < MAX_CONSTANT_LENGTH) {
  20. buffer.setConstant(true);
  21. // now we move data to constant memory, and keep happy
  22. moveToConstantSpace(buffer);
  23. buffersCache.get(deviceId).put(descriptor, buffer);
  24. bytes.addAndGet(array.length * 4);
  25. }
  26. return buffer;
  27. } //else logger.info("Reusing constant buffer...");
  28. return buffersCache.get(deviceId).get(descriptor);
  29. }

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

  1. /**
  2. * This method returns DataBuffer with contant equal to input array.
  3. *
  4. * PLEASE NOTE: This method assumes that you'll never ever change values within result DataBuffer
  5. *
  6. * @param array
  7. * @return
  8. */
  9. @Override
  10. public DataBuffer getConstantBuffer(float[] array) {
  11. // logger.info("getConstantBuffer(float[]) called");
  12. ArrayDescriptor descriptor = new ArrayDescriptor(array);
  13. Integer deviceId = AtomicAllocator.getInstance().getDeviceId();
  14. ensureMaps(deviceId);
  15. if (!buffersCache.get(deviceId).containsKey(descriptor)) {
  16. // we create new databuffer
  17. //logger.info("Creating new constant buffer...");
  18. DataBuffer buffer = Nd4j.createBufferDetached(array);
  19. if (constantOffsets.get(deviceId).get() + (array.length * Nd4j.sizeOfDataType()) < MAX_CONSTANT_LENGTH) {
  20. buffer.setConstant(true);
  21. // now we move data to constant memory, and keep happy
  22. moveToConstantSpace(buffer);
  23. buffersCache.get(deviceId).put(descriptor, buffer);
  24. bytes.addAndGet(array.length * Nd4j.sizeOfDataType());
  25. }
  26. return buffer;
  27. } // else logger.info("Reusing constant buffer...");
  28. return buffersCache.get(deviceId).get(descriptor);
  29. }

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

  1. /**
  2. * This method returns DataBuffer with contant equal to input array.
  3. *
  4. * PLEASE NOTE: This method assumes that you'll never ever change values within result DataBuffer
  5. *
  6. * @param array
  7. * @return
  8. */
  9. @Override
  10. public DataBuffer getConstantBuffer(double[] array) {
  11. //logger.info("getConstantBuffer(double[]) called: {}", Arrays.toString(array));
  12. ArrayDescriptor descriptor = new ArrayDescriptor(array);
  13. Integer deviceId = AtomicAllocator.getInstance().getDeviceId();
  14. ensureMaps(deviceId);
  15. if (!buffersCache.get(deviceId).containsKey(descriptor)) {
  16. // we create new databuffer
  17. //logger.info("Creating new constant buffer...");
  18. DataBuffer buffer = Nd4j.createBufferDetached(array);
  19. if (constantOffsets.get(deviceId).get() + (array.length * Nd4j.sizeOfDataType()) < MAX_CONSTANT_LENGTH) {
  20. buffer.setConstant(true);
  21. // now we move data to constant memory, and keep happy
  22. moveToConstantSpace(buffer);
  23. buffersCache.get(deviceId).put(descriptor, buffer);
  24. bytes.addAndGet(array.length * Nd4j.sizeOfDataType());
  25. }
  26. return buffer;
  27. } //else logger.info("Reusing constant buffer...");
  28. return buffersCache.get(deviceId).get(descriptor);
  29. }

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

  1. /**
  2. * This method returns DataBuffer with contant equal to input array.
  3. *
  4. * PLEASE NOTE: This method assumes that you'll never ever change values within result DataBuffer
  5. *
  6. * @param array
  7. * @return
  8. */
  9. @Override
  10. public DataBuffer getConstantBuffer(float[] array) {
  11. // logger.info("getConstantBuffer(float[]) called");
  12. ArrayDescriptor descriptor = new ArrayDescriptor(array);
  13. Integer deviceId = AtomicAllocator.getInstance().getDeviceId();
  14. ensureMaps(deviceId);
  15. if (!buffersCache.get(deviceId).containsKey(descriptor)) {
  16. // we create new databuffer
  17. //logger.info("Creating new constant buffer...");
  18. DataBuffer buffer = Nd4j.createBufferDetached(array);
  19. if (constantOffsets.get(deviceId).get() + (array.length * Nd4j.sizeOfDataType()) < MAX_CONSTANT_LENGTH) {
  20. buffer.setConstant(true);
  21. // now we move data to constant memory, and keep happy
  22. moveToConstantSpace(buffer);
  23. buffersCache.get(deviceId).put(descriptor, buffer);
  24. bytes.addAndGet(array.length * Nd4j.sizeOfDataType());
  25. }
  26. return buffer;
  27. } // else logger.info("Reusing constant buffer...");
  28. return buffersCache.get(deviceId).get(descriptor);
  29. }

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

  1. /**
  2. * This method returns DataBuffer with contant equal to input array.
  3. *
  4. * PLEASE NOTE: This method assumes that you'll never ever change values within result DataBuffer
  5. *
  6. * @param array
  7. * @return
  8. */
  9. @Override
  10. public DataBuffer getConstantBuffer(double[] array) {
  11. //logger.info("getConstantBuffer(double[]) called: {}", Arrays.toString(array));
  12. ArrayDescriptor descriptor = new ArrayDescriptor(array);
  13. Integer deviceId = AtomicAllocator.getInstance().getDeviceId();
  14. ensureMaps(deviceId);
  15. if (!buffersCache.get(deviceId).containsKey(descriptor)) {
  16. // we create new databuffer
  17. //logger.info("Creating new constant buffer...");
  18. DataBuffer buffer = Nd4j.createBufferDetached(array);
  19. if (constantOffsets.get(deviceId).get() + (array.length * Nd4j.sizeOfDataType()) < MAX_CONSTANT_LENGTH) {
  20. buffer.setConstant(true);
  21. // now we move data to constant memory, and keep happy
  22. moveToConstantSpace(buffer);
  23. buffersCache.get(deviceId).put(descriptor, buffer);
  24. bytes.addAndGet(array.length * Nd4j.sizeOfDataType());
  25. }
  26. return buffer;
  27. } //else logger.info("Reusing constant buffer...");
  28. return buffersCache.get(deviceId).get(descriptor);
  29. }

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

  1. /**
  2. * Read in an ndarray from a data input stream
  3. *
  4. * @param dis the data input stream to read from
  5. * @return the ndarray
  6. * @throws IOException
  7. */
  8. public static INDArray read(DataInputStream dis) throws IOException {
  9. DataBuffer shapeInformation = Nd4j.createBufferDetached(new int[1], DataBuffer.Type.INT);
  10. shapeInformation.read(dis);
  11. int length = Shape.length(shapeInformation);
  12. DataBuffer data = CompressedDataBuffer.readUnknown(dis, length);
  13. return createArrayFromShapeBuffer(data, shapeInformation);
  14. }

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

  1. DataBuffer shapeBuff = Nd4j.createBufferDetached(new int[shapeBufferLength]);

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

  1. @Override
  2. public INDArray subArray(long[] offsets, int[] shape, int[] stride) {
  3. Nd4j.getCompressor().autoDecompress(this);
  4. int n = shape.length;
  5. // FIXME: shapeInfo should be used here
  6. if (shape.length < 1)
  7. return create(Nd4j.createBufferDetached(shape));
  8. if (offsets.length != n)
  9. throw new IllegalArgumentException("Invalid offset " + Arrays.toString(offsets));
  10. if (stride.length != n)
  11. throw new IllegalArgumentException("Invalid stride " + Arrays.toString(stride));
  12. if (Shape.contentEquals(shape, shapeOf())) {
  13. if (ArrayUtil.isZero(offsets)) {
  14. return this;
  15. } else {
  16. throw new IllegalArgumentException("Invalid subArray offsets");
  17. }
  18. }
  19. long[] dotProductOffsets = offsets;
  20. int[] dotProductStride = stride;
  21. long offset = Shape.offset(javaShapeInformation) + NDArrayIndex.offset(dotProductStride, dotProductOffsets);
  22. if (offset >= data().length())
  23. offset = ArrayUtil.sumLong(offsets);
  24. return create(data, Arrays.copyOf(shape, shape.length), stride, offset, ordering());
  25. }

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

  1. return create(Nd4j.createBufferDetached(shape));
  2. if (offsets.length != n)
  3. throw new IllegalArgumentException("Invalid offset " + Arrays.toString(offsets));

相关文章