|
| 1 | +import numpy as np |
| 2 | + |
| 3 | + |
| 4 | +def affine_forward(x, W, b): |
| 5 | + """ |
| 6 | + A linear mapping from inputs to scores. |
| 7 | + |
| 8 | + Inputs: |
| 9 | + - x: input matrix (N, d_1, ..., d_k) |
| 10 | + - W: weigh matrix (D, C) |
| 11 | + - b: bias vector (C, ) |
| 12 | + |
| 13 | + Outputs: |
| 14 | + - out: output of linear layer (N, C) |
| 15 | + """ |
| 16 | + x2d = np.reshape(x, (x.shape[0], -1)) # convert 4D input matrix to 2D |
| 17 | + out = np.dot(x2d, W) + b # linear transformation |
| 18 | + cache = (x, W, b) # keep for backward step (stay with us) |
| 19 | + return out, cache |
| 20 | + |
| 21 | + |
| 22 | +def affine_backward(dout, cache): |
| 23 | + """ |
| 24 | + Computes the backward pass for an affine layer. |
| 25 | +
|
| 26 | + Inputs: |
| 27 | + - dout: Upstream derivative, of shape (N, C) |
| 28 | + - cache: Tuple of: |
| 29 | + - x: Input data, of shape (N, d_1, ... d_k) |
| 30 | + - w: Weights, of shape (D, C) |
| 31 | + - b: biases, of shape (C,) |
| 32 | +
|
| 33 | + Outputs: |
| 34 | + - dx: Gradient with respect to x, of shape (N, d1, ..., d_k) |
| 35 | + - dw: Gradient with respect to w, of shape (D, C) |
| 36 | + - db: Gradient with respect to b, of shape (C,) |
| 37 | + """ |
| 38 | + x, w, b = cache |
| 39 | + x2d = np.reshape(x, (x.shape[0], -1)) |
| 40 | + |
| 41 | + # compute gradients |
| 42 | + db = np.sum(dout, axis=0) |
| 43 | + dw = np.dot(x2d.T, dout) |
| 44 | + dx = np.dot(dout, w.T) |
| 45 | + |
| 46 | + # reshape dx to match the size of x |
| 47 | + dx = dx.reshape(x.shape) |
| 48 | + |
| 49 | + return dx, dw, db |
| 50 | + |
| 51 | +def relu_forward(x): |
| 52 | + """Forward pass for a layer of rectified linear units. |
| 53 | +
|
| 54 | + Inputs: |
| 55 | + - x: a numpy array of any shape |
| 56 | +
|
| 57 | + Outputs: |
| 58 | + - out: output of relu, same shape as x |
| 59 | + - cache: x |
| 60 | + """ |
| 61 | + cache = x |
| 62 | + out = np.maximum(0, x) |
| 63 | + return out, cache |
| 64 | + |
| 65 | +def relu_backward(dout, cache): |
| 66 | + """Backward pass for a layer of rectified linear units. |
| 67 | +
|
| 68 | + Inputs: |
| 69 | + - dout: upstream derevatives, of any shape |
| 70 | + - cache: x, same shape as dout |
| 71 | +
|
| 72 | + Outputs: |
| 73 | + - dx: gradient of loss w.r.t x |
| 74 | + """ |
| 75 | + x = cache |
| 76 | + dx = dout * (x > 0) |
| 77 | + return dx |
| 78 | + |
| 79 | +def svm_loss(scores, y): |
| 80 | + """ |
| 81 | + Fully-vectorized implementation of SVM loss function. |
| 82 | +
|
| 83 | + Inputs: |
| 84 | + - scores: scores for all training data (N, C) |
| 85 | + - y: correct labels for the training data of shape (N,) |
| 86 | +
|
| 87 | + Outputs: |
| 88 | + - loss: data loss plus L2 regularization loss |
| 89 | + - grads: graidents of loss w.r.t scores |
| 90 | + """ |
| 91 | + |
| 92 | + N = scores.shape[0] |
| 93 | + |
| 94 | + # Compute svm data loss |
| 95 | + correct_class_scores = scores[range(N), y] |
| 96 | + margins = np.maximum(0.0, scores - correct_class_scores[:, None] + 1.0) |
| 97 | + margins[range(N), y] = 0.0 |
| 98 | + loss = np.sum(margins) / N |
| 99 | + |
| 100 | + # Compute gradient off loss function w.r.t. scores |
| 101 | + num_pos = np.sum(margins > 0, axis=1) |
| 102 | + dscores = np.zeros(scores.shape) |
| 103 | + dscores[margins > 0] = 1 |
| 104 | + dscores[range(N), y] -= num_pos |
| 105 | + dscores /= N |
| 106 | + |
| 107 | + return loss, dscores |
| 108 | + |
| 109 | + |
| 110 | +def softmax_loss(scores, y): |
| 111 | + """ |
| 112 | + Softmax loss function, fully vectorized implementation. |
| 113 | +
|
| 114 | + Inputs have dimension D, there are C classes, and we operate on minibatches |
| 115 | + of N examples. |
| 116 | +
|
| 117 | + Inputs: |
| 118 | + - scores: A numpy array of shape (N, C). |
| 119 | + - y: A numpy array of shape (N,) containing training labels; |
| 120 | +
|
| 121 | + Outputs: |
| 122 | + - loss as single float |
| 123 | + - gradient with respect to scores |
| 124 | + """ |
| 125 | + N = scores.shape[0] # number of input data |
| 126 | + |
| 127 | + # compute data loss |
| 128 | + shifted_logits = scores - np.max(scores, axis=1, keepdims=True) |
| 129 | + Z = np.sum(np.exp(shifted_logits), axis=1, keepdims=True) |
| 130 | + log_probs = shifted_logits - np.log(Z) |
| 131 | + probs = np.exp(log_probs) |
| 132 | + loss = -np.sum(log_probs[range(N), y]) / N |
| 133 | + |
| 134 | + # Compute gradient of loss function w.r.t. scores |
| 135 | + dscores = probs.copy() |
| 136 | + dscores[range(N), y] -= 1 |
| 137 | + dscores /= N |
| 138 | + |
| 139 | + return loss, dscores |
0 commit comments