|
| 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 svm_loss_naive(scores, y, W, reg=1e-3): |
| 52 | + """ |
| 53 | + Naive implementation of SVM loss function. |
| 54 | +
|
| 55 | + Inputs: |
| 56 | + - scores: scores for all training data (N, C) |
| 57 | + - y: correct labels for the training data |
| 58 | + - reg: regularization strength (lambd) |
| 59 | +
|
| 60 | + Outputs: |
| 61 | + - loss: data loss plus L2 regularization loss |
| 62 | + - grads: graidents of loss wrt scores |
| 63 | + """ |
| 64 | + |
| 65 | + N, C = scores.shape |
| 66 | + |
| 67 | + # Compute svm data loss |
| 68 | + loss = 0.0 |
| 69 | + for i in range(N): |
| 70 | + s = scores[i] # scores for the ith data |
| 71 | + correct_class = y[i] # correct class score |
| 72 | + |
| 73 | + for j in range(C): |
| 74 | + if j == y[i]: |
| 75 | + continue |
| 76 | + else: |
| 77 | + # loss += max(0, s[j] - s[correct_class] + 1.0) |
| 78 | + margin = s[j] - s[correct_class] + 1.0 |
| 79 | + if margin > 0: |
| 80 | + loss += margin |
| 81 | + loss /= N |
| 82 | + |
| 83 | + # Adding L2-regularization loss |
| 84 | + loss += 0.5 * reg * np.sum(W * W) |
| 85 | + |
| 86 | + # Compute gradient off loss function w.r.t. scores |
| 87 | + # We will write this part later |
| 88 | + grads = {} |
| 89 | + |
| 90 | + return loss, grads |
| 91 | + |
| 92 | +def svm_loss_half_vectorized(scores, y, W, reg=1e-3): |
| 93 | + """ |
| 94 | + Half-vectorized implementation of SVM loss function. |
| 95 | +
|
| 96 | + Inputs: |
| 97 | + - scores: scores for all training data (N, C) |
| 98 | + - y: correct labels for the training data |
| 99 | + - reg: regularization strength (lambd) |
| 100 | +
|
| 101 | + Outputs: |
| 102 | + - loss: data loss plus L2 regularization loss |
| 103 | + - grads: graidents of loss wrt scores |
| 104 | + """ |
| 105 | + |
| 106 | + N, C = scores.shape |
| 107 | + |
| 108 | + # Compute svm data loss |
| 109 | + loss = 0.0 |
| 110 | + for i in range(N): |
| 111 | + s = scores[i] # scores for the ith data |
| 112 | + correct_class = y[i] # correct class score |
| 113 | + |
| 114 | + margins = np.maximum(0.0, s - s[correct_class] + 1.0) |
| 115 | + margins[correct_class] = 0.0 |
| 116 | + loss += np.sum(margins) |
| 117 | + |
| 118 | + loss /= N |
| 119 | + |
| 120 | + # Adding L2-regularization loss |
| 121 | + loss += 0.5 * reg * np.sum(W * W) |
| 122 | + |
| 123 | + # Compute gradient off loss function w.r.t. scores |
| 124 | + # We will write this part later |
| 125 | + grads = {} |
| 126 | + |
| 127 | + return loss, grads |
| 128 | + |
| 129 | + |
| 130 | +def svm_loss(scores, y, W, reg=1e-3): |
| 131 | + """ |
| 132 | + Fully-vectorized implementation of SVM loss function. |
| 133 | +
|
| 134 | + Inputs: |
| 135 | + - scores: scores for all training data (N, C) |
| 136 | + - y: correct labels for the training data |
| 137 | + - reg: regularization strength (lambd) |
| 138 | +
|
| 139 | + Outputs: |
| 140 | + - loss: data loss plus L2 regularization loss |
| 141 | + - grads: graidents of loss wrt scores |
| 142 | + """ |
| 143 | + |
| 144 | + N = scores.shape[0] |
| 145 | + |
| 146 | + # Compute svm data loss |
| 147 | + correct_class_scores = scores[range(N), y] |
| 148 | + margins = np.maximum(0.0, scores - correct_class_scores[:, None] + 1.0) |
| 149 | + margins[range(N), y] = 0.0 |
| 150 | + loss = np.sum(margins) / N |
| 151 | + |
| 152 | + # Adding L2-regularization loss |
| 153 | + loss += 0.5 * reg * np.sum(W * W) |
| 154 | + |
| 155 | + # Compute gradient off loss function w.r.t. scores |
| 156 | + # We will write this part later |
| 157 | + grads = {} |
| 158 | + |
| 159 | + return loss, grads |
| 160 | + |
| 161 | + |
| 162 | +def softmax_loss_naive(scores, y, W, reg=1e-3): |
| 163 | + """ |
| 164 | + Softmax loss function, naive implementation (with loops) |
| 165 | +
|
| 166 | + Inputs have dimension D, there are C classes, and we operate on minibatches |
| 167 | + of N examples. |
| 168 | +
|
| 169 | + Inputs: |
| 170 | + - scores: A numpy array of shape (N, C). |
| 171 | + - y: A numpy array of shape (N,) containing training labels; |
| 172 | + - W: A numpy array of shape (D, C) containing weights. |
| 173 | + - reg: (float) regularization strength |
| 174 | +
|
| 175 | + Outputs: |
| 176 | + - loss as single float |
| 177 | + - gradient with respect to weights W; an array of same shape as W |
| 178 | + """ |
| 179 | + N, C = scores.shape |
| 180 | + |
| 181 | + # compute data loss |
| 182 | + loss = 0.0 |
| 183 | + for i in range(N): |
| 184 | + correct_class = y[i] |
| 185 | + score = scores[i] |
| 186 | + score -= np.max(scores) |
| 187 | + exp_score = np.exp(score) |
| 188 | + probs = exp_score / np.sum(exp_score) |
| 189 | + loss += -np.log(probs[correct_class]) |
| 190 | + |
| 191 | + loss /= N |
| 192 | + |
| 193 | + # compute regularization loss |
| 194 | + loss += 0.5 * reg * np.sum(W * W) |
| 195 | + |
| 196 | + # Compute gradient off loss function w.r.t. scores |
| 197 | + # We will write this part later |
| 198 | + grads = {} |
| 199 | + |
| 200 | + return loss, grads |
| 201 | + |
| 202 | + |
| 203 | +def softmax_loss(scores, y, W, reg=1e-3): |
| 204 | + """ |
| 205 | + Softmax loss function, naive implementation (with loops) |
| 206 | +
|
| 207 | + Inputs have dimension D, there are C classes, and we operate on minibatches |
| 208 | + of N examples. |
| 209 | +
|
| 210 | + Inputs: |
| 211 | + - scores: A numpy array of shape (N, C). |
| 212 | + - y: A numpy array of shape (N,) containing training labels; |
| 213 | + - W: A numpy array of shape (D, C) containing weights. |
| 214 | + - reg: (float) regularization strength |
| 215 | +
|
| 216 | + Outputs: |
| 217 | + - loss as single float |
| 218 | + - gradient with respect to weights W; an array of same shape as W |
| 219 | + """ |
| 220 | + N = scores.shape[0] # number of input data |
| 221 | + |
| 222 | + # compute data loss |
| 223 | + scores -= np.max(scores, axis=1, keepdims=True) |
| 224 | + exp_scores = np.exp(scores) |
| 225 | + probs = exp_scores / np.sum(exp_scores, axis=1, keepdims=True) |
| 226 | + loss = -np.sum(np.log(probs[range(N), y])) / N |
| 227 | + |
| 228 | + # compute regularization loss |
| 229 | + loss += 0.5 * reg * np.sum(W * W) |
| 230 | + |
| 231 | + # Compute gradient off loss function w.r.t. scores |
| 232 | + # We will write this part later |
| 233 | + grads = {} |
| 234 | + |
| 235 | + return loss, grads |
0 commit comments