(input_, targets, label, learning_rate, W, V)
| 244 | |
| 245 | |
| 246 | def sgd(input_, targets, label, learning_rate, W, V): |
| 247 | # W[input_] shape: D |
| 248 | # V[:,targets] shape: D x N |
| 249 | # activation shape: N |
| 250 | # print("input_:", input_, "targets:", targets) |
| 251 | activation = W[input_].dot(V[:,targets]) |
| 252 | prob = sigmoid(activation) |
| 253 | |
| 254 | # gradients |
| 255 | gV = np.outer(W[input_], prob - label) # D x N |
| 256 | gW = np.sum((prob - label)*V[:,targets], axis=1) # D |
| 257 | |
| 258 | V[:,targets] -= learning_rate*gV # D x N |
| 259 | W[input_] -= learning_rate*gW # D |
| 260 | |
| 261 | # return cost (binary cross entropy) |
| 262 | cost = label * np.log(prob + 1e-10) + (1 - label) * np.log(1 - prob + 1e-10) |
| 263 | return cost.sum() |
| 264 | |
| 265 | |
| 266 | def load_model(savedir): |
no test coverage detected