djl
djl copied to clipboard
Caused by: ai.djl.engine.EngineException: invalid index of a 0-dim tensor. Use `tensor.item()` in Python or `tensor.item[T]()` in C++ to convert a 0-dim tensor to a number
public void train(NDArray logMat, NDArray qMat, int batchSize, float learningRate, int epochs) {
Tools.CustomLogger customLogger = new Tools.CustomLogger("logs", "both");
Loss binaryCrossEntropy = Loss.sigmoidBinaryCrossEntropyLoss();
for (int epoch = 0; epoch < epochs; epoch++) {
float totalLoss = 0;
int batchCount = 0;
for (int i = 0; i < nUser; i += batchSize) {
int batchEnd = Math.min(i + batchSize, nUser);
NDArray batchTheta = thetaMat.get(String.format("%d:%d", i, batchEnd));
NDArray batchLog = logMat.get(String.format("%d:%d", i, batchEnd));
NDArray pred = forward(batchTheta, qMat);
NDArray loss = binaryCrossEntropy.evaluate(
new NDList(batchLog),
**new NDList(pred)).get(0);**
backwardAndUpdate(loss, batchTheta, qMat, learningRate);
totalLoss += loss.mean().getFloat();
batchCount++;
}
float avgLoss = totalLoss / batchCount;
customLogger.write(String.format("Epoch %d, Loss: %.4f", epoch + 1, avgLoss), "both");
}
}
“ NDArray loss = binaryCrossEntropy.evaluate(
new NDList(batchLog),
new NDList(pred)).get(0); “This line of code reports an error ”ai.djl.engine.EngineException: invalid index of a 0-dim tensor. use tensor.item() in Python or tensor.item[T]() in C++ to convert a 0-dim tensor to a number”, the solution on the web is ”Modify loss.data[0] to loss.item() ”, but there is no item() in DJL, and repeated use of chatgpt, claude, and even deepseek could not modify this error.
Full Error Reporting:
org.opentest4j.AssertionFailedError: Unexpected exception thrown: ai.djl.engine.EngineException: invalid index of a 0-dim tensor. Use tensor.item() in Python or tensor.item[T]() in C++ to convert a 0-dim tensor to a number
at com.idcd.model.IDCDModelTest.testTraining(IDCDModelTest.java:48)
at java.util.ArrayList.forEach(ArrayList.java:1259)
at java.util.ArrayList.forEach(ArrayList.java:1259)
Caused by: ai.djl.engine.EngineException: invalid index of a 0-dim tensor. Use tensor.item() in Python or tensor.item[T]() in C++ to convert a 0-dim tensor to a number
at ai.djl.pytorch.jni.PyTorchLibrary.torchIndexAdvGet(Native Method)
at ai.djl.pytorch.jni.JniUtils.indexAdv(JniUtils.java:423)
at ai.djl.pytorch.engine.PtNDArrayIndexer.get(PtNDArrayIndexer.java:74)
at ai.djl.ndarray.NDArray.get(NDArray.java:594)
at ai.djl.ndarray.NDArray.get(NDArray.java:583)
at ai.djl.ndarray.NDArray.get(NDArray.java:628)
at com.idcd.model.IDCDModel.train(IDCDModel.java:66)
at com.idcd.model.IDCDModelTest.lambda$0(IDCDModelTest.java:49)
... 72 more