I found the answer here: Convolutional Neural Network (2 week, programming exercise 2) - #2 by fabhertz