4

I cannot convert double to Nd4j.INDArray without losing precision.

double[] weight = new double[]{-0.13404223866376802,-0.11294084872465669,0.11232944517596409,-0.01687720880184701,0.13288394029423414,-0.06916641552096867,0.1518882606786481};
INDArray array = Nd4j.create(weight, new int[]{1, 7});
System.out.println(array);

The output is

[-0.13,  -0.11,  0.11,  -0.02,  0.13,  -0.07,  0.15]

The desired output should be

[-0.13404223866376802,-0.11294084872465669,0.11232944517596409,-0.01687720880184701,0.13288394029423414,-0.06916641552096867,0.1518882606786481]

How convert with full precision ?

  • The point behind what @LeoAso is saying is that the INDArray isn't _actually_ truncating your data -- it stores and operates on the entire double. It's just what its toString() method does. His answer allows you to format the INDArray with the precision you want. – The AI Architect Mar 03 '18 at 05:13
  • @TheAIArchitect that's interesting. I just used INDArray's `toDoubleMatrix` and checked this. It appears that what I get back is different from the original double[][] used to create the INDArray, starting at the 7th decimal. It appears the data is truncated into a `FloatBuffer`, and when transformed back to `Double[][]`, random junk is added to the end. – rocksNwaves May 24 '21 at 20:51

3 Answers3

1

Use

int precision = 7;
String printed = new NDArrayStrings(precision).format(this);
System.out.println(printed);

with whatever precision you want. NDArrayStrings is in the org.nd4j.linalg.string package.

Leo Aso
  • 11,898
  • 3
  • 25
  • 46
1

Use this line before creating the INDArray:

Nd4j.setDefaultDataTypes(org.nd4j.linalg.api.buffer.DataType.DOUBLE, org.nd4j.linalg.api.buffer.DataType.FLOAT16);

This will change the default precision Nd4j to Double and Float16.

You can put a static call in your class so you call it once and forget about it.

    class Foo {
       static {
          putTheCallHere();
       }
    }
Phong Phuong
  • 369
  • 4
  • 8
0

I have same problem, with latest 0.9.x nd4j:

    new NDArrayStrings(15).format(Nd4j.create(new double[]{0.4,0.3}))
    [0.400000005960464,  0.300000011920929]

    Nd4j.create(new double[]{0.4}).getDouble(0)
    0.4000000059604645

Edit: now I see the reason, Nd4j only initialize to "float" numbers ... So only 7 digits are correct, better to use "getFloat"

    Nd4j.create(new double[]{0.4 }).getFloat(0)
    0.4
Trent
  • 9
  • 2