deepjavalibrary / djl

An Engine-Agnostic Deep Learning Framework in Java
https://djl.ai
Apache License 2.0
4.05k stars 648 forks source link

sequenceMask method throws error when NDArray dimension is 2 but `use_sequence_length` is True #802

Closed markbookk closed 3 years ago

markbookk commented 3 years ago

Description

Currently sequenceMask in MxNDArray.java throws an error if the dimensions is 2 and use_sequence_length is set True (which currently is always the case).

Expected Behavior

Only throw error when dimensions of NDArray is less than 3 and use_sequence_length is False. If use_sequence_length then throw error if less than 2.

Error Message

sequenceMask is not supported for NDArray with less than 3 dimensions

How to Reproduce?

https://github.com/markbookk/java-d2l-IDE/blob/02a5ce874aa8d3fabc3ce944d5ab765b71c8f6fe/section9_6-7/src/main/java/Main.java#L41

Steps to reproduce

  1. This repo contains multiple projects, but if needed section9_6-7 uses sequenceMask as an example of what will throw an error which shouldn't.

What have you tried to solve it?

  1. Changing the condition to only throw error if the dimensions are less than 2 works fine. This only works though because use_sequence_length is currently always True.
  2. I haven't fully implemented this but another solution is to add a method that allows the user to enter the boolean value use_sequence_length and handle throwing the error based on the boolean.

Environment Info

djl 0.11.0 SNAPSHOT
markbookk commented 3 years ago

@frankfliu @stu1130 @zachgk