Why is it that for the byte array dataBuffer
created by Nd4j.createTypedBuffer(bytes, DataType.FLOAT)
, when using Nd4j.create(dataBuffer, shape);
to create an ndarray
, the values of the ndarray
are float numbers that are the same as the integer values in the bytes
, instead of converting the byte array into floats?
@cqiaoYc I would recommend staying away from that in general. At least in M2.1. Before I rewrote it in the newer code (no the rewrite’s not done yet..) it was really ambiguous as to what the byte array is. I would manually convert it like I did later:
@Override
public void put(byte element) {
if (released.get())
throw new IllegalStateException(“You can’t use DataBuffer once it was released”);
switch (dataType()) {
case BOOL:
((BooleanIndexer) indexer).put(0, ArrayUtil.toBooleanArray(element));
break;
case BYTE:
((ByteIndexer) indexer).put(0,element);
break;
case UBYTE:
((UByteIndexer) indexer).put(0,ArrayUtil.toIntArraySimple(element));
break;
case UINT16:
((UShortIndexer) indexer).put(0,ArrayUtil.toIntArraySimple(element));
break;
case SHORT:
((ShortIndexer) indexer).put(0,ArrayUtil.toShorts(element));
break;
case UINT32:
((UIntIndexer) indexer).put(0,ArrayUtil.toLongArray(element));
break;
case INT:
((IntIndexer) indexer).put(0,ArrayUtil.toIntArraySimple(element));
break;
case UINT64:
((ULongIndexer) indexer).put(0,ArrayUtil.toBigInteger(element));
break;
case LONG:
((LongIndexer) indexer).put(0,ArrayUtil.toLongArray(element));
break;
case BFLOAT16:
((Bfloat16Indexer) indexer).put(0,ArrayUtil.toFloatArraySimple(element));
break;
case HALF:
((HalfIndexer) indexer).put(0,ArrayUtil.toFloatArraySimple(element));
break;
case FLOAT:
((FloatIndexer) indexer).put(0,ArrayUtil.toFloatArraySimple(element));
break;
case DOUBLE:
((DoubleIndexer) indexer).put(0,ArrayUtil.toDoubleArraySimple(element));
break;
default:
throw new UnsupportedOperationException("Unsupported data type: " + dataType());
}
}
It’s just an extra method call and should be enough for your use case.
@agibsonccc Maybe I didn’t make my requirement clear. First, save the bytes
obtained from ndArray.data().asBytes()
into a binary data file. Then, load the bytes
from the binary data file into memory. Finally, create an ndArray
based on the bytes
.
@cqiaoYc yes and I’m saying either create a wrapper method using what I specified. I’m also mentioning to you that in M2.1 the asBytes() needed a bit of rewriting due to what “bytes” can mean. The way I did it in the latest master should work for you.