kronenthaler / libai

4 stars 2 forks source link

Input validation #23

Closed dktcoding closed 7 years ago

dktcoding commented 7 years ago

Hi there,

I was trying to complete the JavaDoc and input validation of the Matrix class, and hit a minor issue that actually needs discussion for all the classes, since there are a LOT of ways of actually implementing input validation and it would be nice to to it consistently across libai.

The idea I have right now it to validate all "big" (public) methods with Exceptions, like constructors, add, multiply, etc, and use assertions for performance critical methods like position(int, int) and position(int, int, double) where actually an ArrayIndexOutOfBoundsException is implied and will be risen if anything goes wrong.

Finally apply the same for things like MLP where (for instance) right now no validation is done about the length of patterns and answers and actually leads to some very obscure errors.

For private methods, we could add input validation for debugging, but it seems like an overkill since we can "be sure" about the inputs the methods are receiving...

What do you think?

PS: I couldn't add notes to the project... PPS: I'm momentarily back to work, so I might be a bit inactive for the next couple weeks.

kronenthaler commented 7 years ago

I think the simplest most uniform would be throwing an IllegalArgumentException with a very descriptive message. What i want to avoid is having a heavy validation on each operation, keeping it simple as a dimension matching check should be enough.

Regarding the neural networks, the validation of the pattern/answer dimension should be limited to column vectors, patterns and answers should be able to have different dimensions and the neural network should create the inner matrices with the proper dimensions.

dktcoding commented 7 years ago

That was my idea, I think that with good input validation in the constructors, and some very specific methods (for instance in the case of MLP also setTrainingType(int, double...)) should suffice.

I think I "miss wrote" on the pattern/answer dimension, I was referring to length of the arrays (the pattern input array needs to be the same length than the answers array), different dimensions (eg. 10 input neurons 5 output neurons) are actually working well.

kronenthaler commented 7 years ago

ah, true, they should match lengths.

dktcoding commented 7 years ago

Progress:

kronenthaler commented 7 years ago

All classes have been changed to incorporate validations. Some validations are now enforced statically by a type constrain (Column type instead of checking Matrix dimensions). What is not enforced by types it's checked on the train methods or constructors.