Java Code Examples for org.nd4j.linalg.api.buffer.DataType#FLOAT16
The following examples show how to use
org.nd4j.linalg.api.buffer.DataType#FLOAT16 .
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: PythonNumpyTest.java From deeplearning4j with Apache License 2.0 | 6 votes |
@Parameterized.Parameters(name = "{index}: Testing with DataType={0}") public static DataType[] data() { return new DataType[] { DataType.BOOL, DataType.FLOAT16, DataType.BFLOAT16, DataType.FLOAT, DataType.DOUBLE, DataType.INT8, DataType.INT16, DataType.INT32, DataType.INT64, DataType.UINT8, DataType.UINT16, DataType.UINT32, DataType.UINT64 }; }
Example 2
Source File: PythonNumpyJobTest.java From deeplearning4j with Apache License 2.0 | 6 votes |
@Parameterized.Parameters(name = "{index}: Testing with DataType={0}") public static DataType[] params() { return new DataType[]{ DataType.BOOL, DataType.FLOAT16, DataType.BFLOAT16, DataType.FLOAT, DataType.DOUBLE, DataType.INT8, DataType.INT16, DataType.INT32, DataType.INT64, DataType.UINT8, DataType.UINT16, DataType.UINT32, DataType.UINT64 }; }
Example 3
Source File: PythonNumpyCollectionsTest.java From deeplearning4j with Apache License 2.0 | 6 votes |
@Parameterized.Parameters(name = "{index}: Testing with DataType={0}") public static DataType[] params() { return new DataType[]{ DataType.BOOL, DataType.FLOAT16, //DataType.BFLOAT16, DataType.FLOAT, DataType.DOUBLE, DataType.INT8, DataType.INT16, DataType.INT32, DataType.INT64, DataType.UINT8, DataType.UINT16, DataType.UINT32, DataType.UINT64 }; }
Example 4
Source File: ND4JUtil.java From konduit-serving with Apache License 2.0 | 5 votes |
public static DataType typeNDArrayTypeToNd4j(@NonNull NDArrayType type){ switch (type){ case DOUBLE: return DataType.DOUBLE; case FLOAT: return DataType.FLOAT; case FLOAT16: return DataType.FLOAT16; case BFLOAT16: return DataType.BFLOAT16; case INT64: return DataType.INT64; case INT32: return DataType.INT32; case INT16: return DataType.INT16; case INT8: return DataType.INT8; case UINT64: return DataType.UINT64; case UINT32: return DataType.UINT32; case UINT16: return DataType.UINT16; case UINT8: return DataType.UINT8; case BOOL: return DataType.BOOL; case UTF8: return DataType.UTF8; default: throw new UnsupportedOperationException("Unable to convert datatype to ND4J datatype: " + type); } }
Example 5
Source File: PythonNumpyBasicTest.java From deeplearning4j with Apache License 2.0 | 5 votes |
@Parameterized.Parameters(name = "{index}: Testing with DataType={0}, shape={2}") public static Collection params() { DataType[] types = new DataType[] { DataType.BOOL, DataType.FLOAT16, DataType.BFLOAT16, DataType.FLOAT, DataType.DOUBLE, DataType.INT8, DataType.INT16, DataType.INT32, DataType.INT64, DataType.UINT8, DataType.UINT16, DataType.UINT32, DataType.UINT64 }; long[][] shapes = new long[][]{ new long[]{2, 3}, new long[]{3}, new long[]{1}, new long[]{} // scalar }; List<Object[]> ret = new ArrayList<>(); for (DataType type: types){ for (long[] shape: shapes){ ret.add(new Object[]{type, shape, Arrays.toString(shape)}); } } return ret; }
Example 6
Source File: PythonObject.java From deeplearning4j with Apache License 2.0 | 4 votes |
public NumpyArray toNumpy() throws PythonException{ PyObject np = PyImport_ImportModule("numpy"); PyObject ndarray = PyObject_GetAttrString(np, "ndarray"); if (PyObject_IsInstance(nativePythonObject, ndarray) != 1){ throw new PythonException("Object is not a numpy array! Use Python.ndarray() to convert object to a numpy array."); } Py_DecRef(ndarray); Py_DecRef(np); Pointer objPtr = new Pointer(nativePythonObject); PyArrayObject npArr = new PyArrayObject(objPtr); Pointer ptr = PyArray_DATA(npArr); long[] shape = new long[PyArray_NDIM(npArr)]; SizeTPointer shapePtr = PyArray_SHAPE(npArr); if (shapePtr != null) shapePtr.get(shape, 0, shape.length); long[] strides = new long[shape.length]; SizeTPointer stridesPtr = PyArray_STRIDES(npArr); if (stridesPtr != null) stridesPtr.get(strides, 0, strides.length); int npdtype = PyArray_TYPE(npArr); DataType dtype; switch (npdtype){ case NPY_DOUBLE: dtype = DataType.DOUBLE; break; case NPY_FLOAT: dtype = DataType.FLOAT; break; case NPY_SHORT: dtype = DataType.SHORT; break; case NPY_INT: dtype = DataType.INT32; break; case NPY_LONG: dtype = DataType.LONG; break; case NPY_UINT: dtype = DataType.UINT32; break; case NPY_BYTE: dtype = DataType.INT8; break; case NPY_UBYTE: dtype = DataType.UINT8; break; case NPY_BOOL: dtype = DataType.BOOL; break; case NPY_HALF: dtype = DataType.FLOAT16; break; case NPY_LONGLONG: dtype = DataType.INT64; break; case NPY_USHORT: dtype = DataType.UINT16; break; case NPY_ULONG: case NPY_ULONGLONG: dtype = DataType.UINT64; break; default: throw new PythonException("Unsupported array data type: " + npdtype); } return new NumpyArray(ptr.address(), shape, strides, dtype); }
Example 7
Source File: BaseCpuDataBuffer.java From deeplearning4j with Apache License 2.0 | 4 votes |
/** * * @param length * @param elementSize */ public BaseCpuDataBuffer(long length, int elementSize) { if (length < 1) throw new IllegalArgumentException("Length must be >= 1"); initTypeAndSize(); allocationMode = AllocUtil.getAllocationModeFromContext(); this.length = length; this.underlyingLength = length; this.elementSize = (byte) elementSize; if (dataType() != DataType.UTF8) ptrDataBuffer = OpaqueDataBuffer.allocateDataBuffer(length, dataType(), false); if (dataType() == DataType.DOUBLE) { pointer = new PagedPointer(ptrDataBuffer.primaryBuffer(), length).asDoublePointer(); indexer = DoubleIndexer.create((DoublePointer) pointer); } else if (dataType() == DataType.FLOAT) { pointer = new PagedPointer(ptrDataBuffer.primaryBuffer(), length).asFloatPointer(); setIndexer(FloatIndexer.create((FloatPointer) pointer)); } else if (dataType() == DataType.INT32) { pointer = new PagedPointer(ptrDataBuffer.primaryBuffer(), length).asIntPointer(); setIndexer(IntIndexer.create((IntPointer) pointer)); } else if (dataType() == DataType.LONG) { pointer = new PagedPointer(ptrDataBuffer.primaryBuffer(), length).asLongPointer(); setIndexer(LongIndexer.create((LongPointer) pointer)); } else if (dataType() == DataType.SHORT) { pointer = new PagedPointer(ptrDataBuffer.primaryBuffer(), length).asShortPointer(); setIndexer(ShortIndexer.create((ShortPointer) pointer)); } else if (dataType() == DataType.BYTE) { pointer = new PagedPointer(ptrDataBuffer.primaryBuffer(), length).asBytePointer(); setIndexer(ByteIndexer.create((BytePointer) pointer)); } else if (dataType() == DataType.UBYTE) { pointer = new PagedPointer(ptrDataBuffer.primaryBuffer(), length).asBytePointer(); setIndexer(UByteIndexer.create((BytePointer) pointer)); } else if (dataType() == DataType.UTF8) { ptrDataBuffer = OpaqueDataBuffer.allocateDataBuffer(length, INT8, false); pointer = new PagedPointer(ptrDataBuffer.primaryBuffer(), length).asBytePointer(); setIndexer(ByteIndexer.create((BytePointer) pointer)); } else if(dataType() == DataType.FLOAT16){ pointer = new PagedPointer(ptrDataBuffer.primaryBuffer(), length).asShortPointer(); setIndexer(HalfIndexer.create((ShortPointer) pointer)); } else if(dataType() == DataType.BFLOAT16){ pointer = new PagedPointer(ptrDataBuffer.primaryBuffer(), length).asShortPointer(); setIndexer(Bfloat16Indexer.create((ShortPointer) pointer)); } else if(dataType() == DataType.BOOL){ pointer = new PagedPointer(ptrDataBuffer.primaryBuffer(), length).asBoolPointer(); setIndexer(BooleanIndexer.create((BooleanPointer) pointer)); } else if(dataType() == DataType.UINT16){ pointer = new PagedPointer(ptrDataBuffer.primaryBuffer(), length).asShortPointer(); setIndexer(UShortIndexer.create((ShortPointer) pointer)); } else if(dataType() == DataType.UINT32){ pointer = new PagedPointer(ptrDataBuffer.primaryBuffer(), length).asIntPointer(); setIndexer(UIntIndexer.create((IntPointer) pointer)); } else if (dataType() == DataType.UINT64) { pointer = new PagedPointer(ptrDataBuffer.primaryBuffer(), length).asLongPointer(); setIndexer(LongIndexer.create((LongPointer) pointer)); } Nd4j.getDeallocatorService().pickObject(this); }
Example 8
Source File: GlobalPoolingMaskingTests.java From deeplearning4j with Apache License 2.0 | 4 votes |
@Test public void testMaskLayerDataTypes(){ for(DataType dt : new DataType[]{DataType.FLOAT16, DataType.BFLOAT16, DataType.FLOAT, DataType.DOUBLE, DataType.INT8, DataType.INT16, DataType.INT32, DataType.INT64, DataType.UINT8, DataType.UINT16, DataType.UINT32, DataType.UINT64}){ INDArray mask = Nd4j.rand(DataType.FLOAT, 2, 10).addi(0.3).castTo(dt); for(DataType networkDtype : new DataType[]{DataType.FLOAT16, DataType.BFLOAT16, DataType.FLOAT, DataType.DOUBLE}){ INDArray in = Nd4j.rand(networkDtype, 2, 5, 10); INDArray label1 = Nd4j.rand(networkDtype, 2, 5); INDArray label2 = Nd4j.rand(networkDtype, 2, 5, 10); for(PoolingType pt : PoolingType.values()) { //System.out.println("Net: " + networkDtype + ", mask: " + dt + ", pt=" + pt); MultiLayerConfiguration conf = new NeuralNetConfiguration.Builder() .list() .layer(new GlobalPoolingLayer(pt)) .layer(new OutputLayer.Builder().nIn(5).nOut(5).activation(Activation.TANH).lossFunction(LossFunctions.LossFunction.MSE).build()) .build(); MultiLayerNetwork net = new MultiLayerNetwork(conf); net.init(); net.output(in, false, mask, null); net.output(in, false, mask, null); MultiLayerConfiguration conf2 = new NeuralNetConfiguration.Builder() .list() .layer(new RnnOutputLayer.Builder().nIn(5).nOut(5).activation(Activation.TANH).lossFunction(LossFunctions.LossFunction.MSE).build()) .build(); MultiLayerNetwork net2 = new MultiLayerNetwork(conf2); net2.init(); net2.output(in, false, mask, mask); net2.output(in, false, mask, mask); net.fit(in, label1, mask, null); net2.fit(in, label2, mask, mask); } } } }
Example 9
Source File: NumpyArray.java From deeplearning4j with Apache License 2.0 | 4 votes |
@Override public INDArray toJava(PythonObject pythonObject) { log.info("Converting PythonObject to INDArray..."); PyObject np = PyImport_ImportModule("numpy"); PyObject ndarray = PyObject_GetAttrString(np, "ndarray"); if (PyObject_IsInstance(pythonObject.getNativePythonObject(), ndarray) != 1) { Py_DecRef(ndarray); Py_DecRef(np); throw new PythonException("Object is not a numpy array! Use Python.ndarray() to convert object to a numpy array."); } Py_DecRef(ndarray); Py_DecRef(np); PyArrayObject npArr = new PyArrayObject(pythonObject.getNativePythonObject()); long[] shape = new long[PyArray_NDIM(npArr)]; SizeTPointer shapePtr = PyArray_SHAPE(npArr); if (shapePtr != null) shapePtr.get(shape, 0, shape.length); long[] strides = new long[shape.length]; SizeTPointer stridesPtr = PyArray_STRIDES(npArr); if (stridesPtr != null) stridesPtr.get(strides, 0, strides.length); int npdtype = PyArray_TYPE(npArr); DataType dtype; switch (npdtype) { case NPY_DOUBLE: dtype = DataType.DOUBLE; break; case NPY_FLOAT: dtype = DataType.FLOAT; break; case NPY_SHORT: dtype = DataType.SHORT; break; case NPY_INT: dtype = DataType.INT32; break; case NPY_LONG: dtype = DataType.INT64; break; case NPY_UINT: dtype = DataType.UINT32; break; case NPY_BYTE: dtype = DataType.INT8; break; case NPY_UBYTE: dtype = DataType.UINT8; break; case NPY_BOOL: dtype = DataType.BOOL; break; case NPY_HALF: dtype = DataType.FLOAT16; break; case NPY_LONGLONG: dtype = DataType.INT64; break; case NPY_USHORT: dtype = DataType.UINT16; break; case NPY_ULONG: case NPY_ULONGLONG: dtype = DataType.UINT64; break; default: throw new PythonException("Unsupported array data type: " + npdtype); } long size = 1; for (int i = 0; i < shape.length; size *= shape[i++]) ; INDArray ret; long address = PyArray_DATA(npArr).address(); String key = address + "_" + size + "_" + dtype; DataBuffer buff = cache.get(key); if (buff == null) { try (MemoryWorkspace ws = Nd4j.getMemoryManager().scopeOutOfWorkspaces()) { Pointer ptr = NativeOpsHolder.getInstance().getDeviceNativeOps().pointerForAddress(address); ptr = ptr.limit(size); ptr = ptr.capacity(size); buff = Nd4j.createBuffer(ptr, size, dtype); cache.put(key, buff); } } int elemSize = buff.getElementSize(); long[] nd4jStrides = new long[strides.length]; for (int i = 0; i < strides.length; i++) { nd4jStrides[i] = strides[i] / elemSize; } ret = Nd4j.create(buff, shape, nd4jStrides, 0, Shape.getOrder(shape, nd4jStrides, 1), dtype); Nd4j.getAffinityManager().tagLocation(ret, AffinityManager.Location.HOST); log.info("Done."); return ret; }