Solved by ptrblck in post #15 
              
              
                I’m not sure if a condition will save you, since you expect targets to have at least the length b+batch_size, while it seems to have a single element. 
I would recommend to take another look, how targets is defined and why it’s smaller than you expect.