Deep Java Library - NDArray set method does not allow modification of existing array

256 Views Asked by At

I've started using the Deep Java Library together with its underlying array manipulation package: ndarray.

The question is very simple. I want to modify the i-th element of an existing NDArray, however I cannot do that. How can I set the i-th element to a specific value?

The documentation mentions many set methods.

Here's a minimum reproducible example of what I've tried:

var manager = NDManager.newBaseManager();

var y = manager.create(new float[] {1, 2, 3, 4, 5});
System.out.println("y before modification: " + y);

y.set(new float[] {1, 100, 3, 4, 5});
System.out.println("y after setting the entire array: " + y);

// the following throws: "java.lang.UnsupportedOperationException: Tensor cannot be modified after creation"
y.set(new NDIndex("1"), 1000f);
System.out.println("y after setting the 1st element to 1000: " + y);

This the error thrown:

java.lang.UnsupportedOperationException: Tensor cannot be modified after creation

1

There are 1 best solutions below

2
Frank Liu On BEST ANSWER

The exception you got is because you are using TensorFlow engine. The TensorFlow engine doesn't support modification of NDArray after creation.

You can choose PyTorch engine. Your code should just work. You only need to replace your tensorflow dependency with pytorch in your project. If you have multiple engines in your classpath, you can explicitly select PyTorch:

var manager = NDManager.newBaseManager(Device.cpu(), "PyTorch");