Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- conv = Conv3x3(8) # 28x28x1 -> 26x26x8
- pool = MaxPool2() # 26x26x8 -> 13x13x8
- softmax = Softmax(13 * 13 * 8, 10) # 13x13x8 -> 10
- def forward(image, label):
- '''
- Completes a forward pass of the CNN and calculates the accuracy and
- cross-entropy loss.
- - image is a 2d numpy array
- - label is a digit
- '''
- # We transform the image from [0, 255] to [-0.5, 0.5] to make it easier
- # to work with. This is standard practice.
- out = conv.forward((image / 255) - 0.5)
- out = pool.forward(out)
- out = softmax.forward(out)
- # Calculate cross-entropy loss and accuracy. np.log() is the natural log.
- loss = -np.log(out[label])
- acc = 1 if np.argmax(out) == label else 0
- return out, loss, acc
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement