The root cause of this error is unfortunately not visible in this code snippet.
Could you post a minimal executable code snippet to reproduce this error with your model and random data, so that we could have a look?