Fix memory leak in scan related to the input and output storage of the inner function.

This fixes a memory exhaustion error on the GPU for the recurrent LSTM speech model.

It was also only lightly tested as it's really late.  Sorry if there are stupid typos or something like that.
If you change the code, update the comment to explain why we do this like this.