djl icon indicating copy to clipboard operation
djl copied to clipboard

ArrayIndexOutOfBoundsException: Index 0 out of bounds for length 0 with djl-zero

Open starzu-sentio opened this issue 2 years ago • 0 comments

Description

I'm getting the following exception when I'm trying to use a basic TabularRegression:

Exception in thread "main" ai.djl.translate.TranslateException: java.lang.ArrayIndexOutOfBoundsException: Index 0 out of bounds for length 0
	at ai.djl.inference.Predictor.batchPredict(Predictor.java:189)
	at ai.djl.inference.Predictor.predict(Predictor.java:126)
	...
Caused by: java.lang.ArrayIndexOutOfBoundsException: Index 0 out of bounds for length 0
	at ai.djl.ndarray.NDArray.split(NDArray.java:3089)
	at ai.djl.ndarray.NDArray.split(NDArray.java:3014)
	at ai.djl.translate.StackBatchifier.unbatchify(StackBatchifier.java:116)
	at ai.djl.inference.Predictor.processOutputs(Predictor.java:222)
	at ai.djl.inference.Predictor.batchPredict(Predictor.java:183)
	... 3 more

Expected Behavior

It should predict something.

How to Reproduce?

I'm using pretty trivial data to start:

DistPercentile,IsFirstIssue,IsLongRoute,Desirability
0.01,false,false,1.0
0.05,false,false,0.9
0.15,false,false,0.8
0.15,false,false,0.7
0.15,true,false,0.2
0.15,true,true,0.5

and my code:

case class Input(distPercentile: Float, isFirstIssue: Boolean, isLongRoute: Boolean)
case class Result(desirability: Float)

class Builder extends CsvDataset.CsvBuilder[Builder] {
  private val testCsvFormat = CSVFormat.DEFAULT.builder()
    .setHeader("DistPercentile", "IsFirstIssue", "IsLongRoute", "Desirability")
    .setSkipHeaderRecord(true)
    .build()

  this.setCsvFormat(testCsvFormat)
    .addNumericFeature("DistPercentile", true)
    .addCategoricalFeature("IsFirstIssue", false)
    .addCategoricalFeature("IsLongRoute", false)
    .addNumericLabel("Desirability", true)
    .setSampling(new BatchSampler(new RandomSampler(), 10))
    .optDevice(Device.cpu())
    .optCsvFile(Path.of(getClass.getClassLoader.getResource("djl.csv").toURI))
}

val dataset = new Builder().build()
val model = TabularRegression.train(dataset, Performance.ACCURATE)

def main(args: Array[String]): Unit = {
  val translator = new Translator[Input, Result] {
    override def processInput(ctx: TranslatorContext, input: Input): NDList = {
      new NDList(
        model.getNDManager.create(Array[Float](
          input.distPercentile,
          if (input.isFirstIssue) 1.0f else 0.0f,
          if (input.isLongRoute) 1.0f else 0.0f,
        )),
      )
    }
    override def processOutput(ctx: TranslatorContext, list: NDList): Result = {
      // I never reached this part, so it's probably broken as well, but it's not related. 
      Result(list.singletonOrThrow.getFloat(0))
    }
  }

  println(model.newPredictor(translator).predict(Input(0.95f, false, false)))
}

What have you tried to solve it?

It seems that in Predictor:183 we've got 2 elements in result and the second one has empty shape. It fails on this second one: image

starzu-sentio avatar Feb 12 '23 21:02 starzu-sentio