| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
1 parent 22dd82c commit ec2111a
7 files changed
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -9,14 +9,14 @@ | |||
| 9 | 9 | from keras.preprocessing import sequence | |
| 10 | 10 | ||
| 11 | 11 | from utils4e import (Sigmoid, dot_product, softmax1D, conv1D, gaussian_kernel, element_wise_product, vector_add, | |
| 12 | - random_weights, scalar_vector_product, matrix_multiplication, map_vector, mse_loss) | ||
| 12 | + random_weights, scalar_vector_product, matrix_multiplication, map_vector, mean_squared_error_loss) | ||
| 13 | 13 | ||
| 14 | 14 | ||
| 15 | 15 | class Node: | |
| 16 | 16 | """ | |
| 17 | 17 | A node in a computational graph contains the pointer to all its parents. | |
| 18 | - :param val: value of current node. | ||
| 19 | - :param parents: a container of all parents of current node. | ||
| 18 | + :param val: value of current node | ||
| 19 | + :param parents: a container of all parents of current node | ||
| 20 | 20 | """ | |
| 21 | 21 | ||
| 22 | 22 | def __init__(self, val=None, parents=None): | |
@@ -55,40 +55,40 @@ def forward(self, inputs): | |||
| 55 | 55 | raise NotImplementedError | |
| 56 | 56 | ||
| 57 | 57 | ||
| 58 | - class OutputLayer(Layer): | ||
| 59 | - """1D softmax output layer in 19.3.2""" | ||
| 58 | + class InputLayer(Layer): | ||
| 59 | + """1D input layer. Layer size is the same as input vector size.""" | ||
| 60 | 60 | ||
| 61 | 61 | def __init__(self, size=3): | |
| 62 | 62 | super().__init__(size) | |
| 63 | 63 | ||
| 64 | 64 | def forward(self, inputs): | |
| 65 | + """Take each value of the inputs to each unit in the layer.""" | ||
| 65 | 66 | assert len(self.nodes) == len(inputs) | |
| 66 | - res = softmax1D(inputs) | ||
| 67 | - for node, val in zip(self.nodes, res): | ||
| 68 | - node.val = val | ||
| 69 | - return res | ||
| 67 | + for node, inp in zip(self.nodes, inputs): | ||
| 68 | + node.val = inp | ||
| 69 | + return inputs | ||
| 70 | 70 | ||
| 71 | 71 | ||
| 72 | - class InputLayer(Layer): | ||
| 73 | - """1D input layer. Layer size is the same as input vector size.""" | ||
| 72 | + class OutputLayer(Layer): | ||
| 73 | + """1D softmax output layer in 19.3.2.""" | ||
| 74 | 74 | ||
| 75 | 75 | def __init__(self, size=3): | |
| 76 | 76 | super().__init__(size) | |
| 77 | 77 | ||
| 78 | 78 | def forward(self, inputs): | |
| 79 | - """Take each value of the inputs to each unit in the layer.""" | ||
| 80 | 79 | assert len(self.nodes) == len(inputs) | |
| 81 | - for node, inp in zip(self.nodes, inputs): | ||
| 82 | - node.val = inp | ||
| 83 | - return inputs | ||
| 80 | + res = softmax1D(inputs) | ||
| 81 | + for node, val in zip(self.nodes, res): | ||
| 82 | + node.val = val | ||
| 83 | + return res | ||
| 84 | 84 | ||
| 85 | 85 | ||
| 86 | 86 | class DenseLayer(Layer): | |
| 87 | 87 | """ | |
| 88 | 88 | 1D dense layer in a neural network. | |
| 89 | - :param in_size: input vector size, int. | ||
| 90 | - :param out_size: output vector size, int. | ||
| 91 | - :param activation: activation function, Activation object. | ||
| 89 | + :param in_size: (int) input vector size | ||
| 90 | + :param out_size: (int) output vector size | ||
| 91 | + :param activation: (Activation object) activation function | ||
| 92 | 92 | """ | |
| 93 | 93 | ||
| 94 | 94 | def __init__(self, in_size=3, out_size=3, activation=None): | |
@@ -124,7 +124,7 @@ def __init__(self, size=3, kernel_size=3): | |||
| 124 | 124 | node.weights = gaussian_kernel(kernel_size) | |
| 125 | 125 | ||
| 126 | 126 | def forward(self, features): | |
| 127 | - # each node in layer takes a channel in the features. | ||
| 127 | + # each node in layer takes a channel in the features | ||
| 128 | 128 | assert len(self.nodes) == len(features) | |
| 129 | 129 | res = [] | |
| 130 | 130 | # compute the convolution output of each channel, store it in node.val | |
@@ -154,7 +154,8 @@ def forward(self, features): | |||
| 154 | 154 | for i in range(len(self.nodes)): | |
| 155 | 155 | feature = features[i] | |
| 156 | 156 | # get the max value in a kernel_size * kernel_size area | |
| 157 | - out = [max(feature[i:i + self.kernel_size]) for i in range(len(feature) - self.kernel_size + 1)] | ||
| 157 | + out = [max(feature[i:i + self.kernel_size]) | ||
| 158 | + for i in range(len(feature) - self.kernel_size + 1)] | ||
| 158 | 159 | res.append(out) | |
| 159 | 160 | self.nodes[i].val = out | |
| 160 | 161 | return res | |
@@ -270,13 +271,13 @@ def adam(dataset, net, loss, epochs=1000, rho=(0.9, 0.999), delta=1 / 10 ** 8, | |||
| 270 | 271 | ||
| 271 | 272 | def BackPropagation(inputs, targets, theta, net, loss): | |
| 272 | 273 | """ | |
| 273 | - The back-propagation algorithm for multilayer networks in only one epoch, to calculate gradients of theta | ||
| 274 | - :param inputs: a batch of inputs in an array. Each input is an iterable object. | ||
| 275 | - :param targets: a batch of targets in an array. Each target is an iterable object. | ||
| 276 | - :param theta: parameters to be updated. | ||
| 277 | - :param net: a list of predefined layer objects representing their linear sequence. | ||
| 278 | - :param loss: a predefined loss function taking array of inputs and targets. | ||
| 279 | - :return: gradients of theta, loss of the input batch. | ||
| 274 | + The back-propagation algorithm for multilayer networks in only one epoch, to calculate gradients of theta. | ||
| 275 | + :param inputs: a batch of inputs in an array. Each input is an iterable object | ||
| 276 | + :param targets: a batch of targets in an array. Each target is an iterable object | ||
| 277 | + :param theta: parameters to be updated | ||
| 278 | + :param net: a list of predefined layer objects representing their linear sequence | ||
| 279 | + :param loss: a predefined loss function taking array of inputs and targets | ||
| 280 | + :return: gradients of theta, loss of the input batch | ||
| 280 | 281 | """ | |
| 281 | 282 | ||
| 282 | 283 | assert len(inputs) == len(targets) | |
@@ -325,9 +326,9 @@ def BackPropagation(inputs, targets, theta, net, loss): | |||
| 325 | 326 | class BatchNormalizationLayer(Layer): | |
| 326 | 327 | """Batch normalization layer.""" | |
| 327 | 328 | ||
| 328 | - def __init__(self, size, epsilon=0.001): | ||
| 329 | + def __init__(self, size, eps=0.001): | ||
| 329 | 330 | super().__init__(size) | |
| 330 | - self.epsilon = epsilon | ||
| 331 | + self.eps = eps | ||
| 331 | 332 | # self.weights = [beta, gamma] | |
| 332 | 333 | self.weights = [0, 0] | |
| 333 | 334 | self.inputs = None | |
@@ -341,7 +342,7 @@ def forward(self, inputs): | |||
| 341 | 342 | res = [] | |
| 342 | 343 | # get normalized value of each input | |
| 343 | 344 | for i in range(len(self.nodes)): | |
| 344 | - val = [(inputs[i] - mu) * self.weights[0] / np.sqrt(self.epsilon + stderr ** 2) + self.weights[1]] | ||
| 345 | + val = [(inputs[i] - mu) * self.weights[0] / np.sqrt(self.eps + stderr ** 2) + self.weights[1]] | ||
| 345 | 346 | res.append(val) | |
| 346 | 347 | self.nodes[i].val = val | |
| 347 | 348 | return res | |
@@ -375,7 +376,7 @@ def NeuralNetLearner(dataset, hidden_layer_sizes=None, learning_rate=0.01, epoch | |||
| 375 | 376 | raw_net.append(DenseLayer(hidden_input_size, output_size)) | |
| 376 | 377 | ||
| 377 | 378 | # update parameters of the network | |
| 378 | - learned_net = optimizer(dataset, raw_net, mse_loss, epochs, l_rate=learning_rate, | ||
| 379 | + learned_net = optimizer(dataset, raw_net, mean_squared_error_loss, epochs, l_rate=learning_rate, | ||
| 379 | 380 | batch_size=batch_size, verbose=verbose) | |
| 380 | 381 | ||
| 381 | 382 | def predict(example): | |
@@ -394,7 +395,7 @@ def predict(example): | |||
| 394 | 395 | return predict | |
| 395 | 396 | ||
| 396 | 397 | ||
| 397 | - def PerceptronLearner(dataset, learning_rate=0.01, epochs=100, verbose=None): | ||
| 398 | + def PerceptronLearner(dataset, learning_rate=0.01, epochs=100, optimizer=gradient_descent, batch_size=1, verbose=None): | ||
| 398 | 399 | """ | |
| 399 | 400 | Simple perceptron neural network. | |
| 400 | 401 | """ | |
@@ -405,7 +406,8 @@ def PerceptronLearner(dataset, learning_rate=0.01, epochs=100, verbose=None): | |||
| 405 | 406 | raw_net = [InputLayer(input_size), DenseLayer(input_size, output_size)] | |
| 406 | 407 | ||
| 407 | 408 | # update the network | |
| 408 | - learned_net = gradient_descent(dataset, raw_net, mse_loss, epochs, l_rate=learning_rate, verbose=verbose) | ||
| 409 | + learned_net = optimizer(dataset, raw_net, mean_squared_error_loss, epochs, l_rate=learning_rate, | ||
| 410 | + batch_size=batch_size, verbose=verbose) | ||
| 409 | 411 | ||
| 410 | 412 | def predict(example): | |
| 411 | 413 | layer_out = learned_net[1].forward(example) | |
@@ -419,7 +421,7 @@ def SimpleRNNLearner(train_data, val_data, epochs=2): | |||
| 419 | 421 | RNN example for text sentimental analysis. | |
| 420 | 422 | :param train_data: a tuple of (training data, targets) | |
| 421 | 423 | Training data: ndarray taking training examples, while each example is coded by embedding | |
| 422 | - Targets: ndarray taking targets of each example. Each target is mapped to an integer. | ||
| 424 | + Targets: ndarray taking targets of each example. Each target is mapped to an integer | ||
| 423 | 425 | :param val_data: a tuple of (validation data, targets) | |
| 424 | 426 | :param epochs: number of epochs | |
| 425 | 427 | :return: a keras model | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -968,7 +968,7 @@ def ada_boost(dataset, L, K): | |||
| 968 | 968 | h.append(h_k) | |
| 969 | 969 | error = sum(weight for example, weight in zip(examples, w) if example[target] != h_k(example)) | |
| 970 | 970 | # avoid divide-by-0 from either 0% or 100% error rates | |
| 971 | - error = clip(error, eps, 1 - eps) | ||
| 971 | + error = np.clip(error, eps, 1 - eps) | ||
| 972 | 972 | for j, example in enumerate(examples): | |
| 973 | 973 | if example[target] == h_k(example): | |
| 974 | 974 | w[j] *= error / (1 - error) | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -742,7 +742,7 @@ def ada_boost(dataset, L, K): | |||
| 742 | 742 | h.append(h_k) | |
| 743 | 743 | error = sum(weight for example, weight in zip(examples, w) if example[target] != h_k(example)) | |
| 744 | 744 | # avoid divide-by-0 from either 0% or 100% error rates | |
| 745 | - error = clip(error, eps, 1 - eps) | ||
| 745 | + error = np.clip(error, eps, 1 - eps) | ||
| 746 | 746 | for j, example in enumerate(examples): | |
| 747 | 747 | if example[target] == h_k(example): | |
| 748 | 748 | w[j] *= error / (1 - error) | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -11,8 +11,8 @@ def test_neural_net(): | |||
| 11 | 11 | iris = DataSet(name='iris') | |
| 12 | 12 | classes = ['setosa', 'versicolor', 'virginica'] | |
| 13 | 13 | iris.classes_to_numbers(classes) | |
| 14 | - nnl_adam = NeuralNetLearner(iris, [4], learning_rate=0.001, epochs=200, optimizer=adam) | ||
| 15 | 14 | nnl_gd = NeuralNetLearner(iris, [4], learning_rate=0.15, epochs=100, optimizer=gradient_descent) | |
| 15 | + nnl_adam = NeuralNetLearner(iris, [4], learning_rate=0.001, epochs=200, optimizer=adam) | ||
| 16 | 16 | tests = [([5.0, 3.1, 0.9, 0.1], 0), | |
| 17 | 17 | ([5.1, 3.5, 1.0, 0.0], 0), | |
| 18 | 18 | ([4.9, 3.3, 1.1, 0.1], 0), | |
@@ -22,25 +22,28 @@ def test_neural_net(): | |||
| 22 | 22 | ([7.5, 4.1, 6.2, 2.3], 2), | |
| 23 | 23 | ([7.3, 4.0, 6.1, 2.4], 2), | |
| 24 | 24 | ([7.0, 3.3, 6.1, 2.5], 2)] | |
| 25 | - assert grade_learner(nnl_adam, tests) >= 1 / 3 | ||
| 26 | 25 | assert grade_learner(nnl_gd, tests) >= 1 / 3 | |
| 27 | - assert err_ratio(nnl_adam, iris) < 0.21 | ||
| 28 | 26 | assert err_ratio(nnl_gd, iris) < 0.21 | |
| 27 | + assert grade_learner(nnl_adam, tests) >= 1 / 3 | ||
| 28 | + assert err_ratio(nnl_adam, iris) < 0.21 | ||
| 29 | 29 | ||
| 30 | 30 | ||
| 31 | 31 | def test_perceptron(): | |
| 32 | 32 | iris = DataSet(name='iris') | |
| 33 | 33 | classes = ['setosa', 'versicolor', 'virginica'] | |
| 34 | 34 | iris.classes_to_numbers(classes) | |
| 35 | - pl = PerceptronLearner(iris, learning_rate=0.01, epochs=100) | ||
| 35 | + pl_gd = PerceptronLearner(iris, learning_rate=0.01, epochs=100, optimizer=gradient_descent) | ||
| 36 | + pl_adam = PerceptronLearner(iris, learning_rate=0.01, epochs=100, optimizer=adam) | ||
| 36 | 37 | tests = [([5, 3, 1, 0.1], 0), | |
| 37 | 38 | ([5, 3.5, 1, 0], 0), | |
| 38 | 39 | ([6, 3, 4, 1.1], 1), | |
| 39 | 40 | ([6, 2, 3.5, 1], 1), | |
| 40 | 41 | ([7.5, 4, 6, 2], 2), | |
| 41 | 42 | ([7, 3, 6, 2.5], 2)] | |
| 42 | - assert grade_learner(pl, tests) > 1 / 2 | ||
| 43 | - assert err_ratio(pl, iris) < 0.4 | ||
| 43 | + assert grade_learner(pl_gd, tests) > 1 / 2 | ||
| 44 | + assert err_ratio(pl_gd, iris) < 0.4 | ||
| 45 | + assert grade_learner(pl_adam, tests) > 1 / 2 | ||
| 46 | + assert err_ratio(pl_adam, iris) < 0.4 | ||
| 44 | 47 | ||
| 45 | 48 | ||
| 46 | 49 | def test_rnn(): | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -173,10 +173,6 @@ def test_normalize(): | |||
| 173 | 173 | assert normalize([1, 2, 1]) == [0.25, 0.5, 0.25] | |
| 174 | 174 | ||
| 175 | 175 | ||
| 176 | - def test_clip(): | ||
| 177 | - assert [clip(x, 0, 1) for x in [-1, 0.5, 10]] == [0, 0.5, 1] | ||
| 178 | - | ||
| 179 | - | ||
| 180 | 176 | def test_gaussian(): | |
| 181 | 177 | assert gaussian(1, 0.5, 0.7) == 0.6664492057835993 | |
| 182 | 178 | assert gaussian(5, 2, 4.5) == 0.19333405840142462 | |
@@ -201,10 +197,6 @@ def test_distance_squared(): | |||
| 201 | 197 | assert distance_squared((1, 2), (5, 5)) == 25.0 | |
| 202 | 198 | ||
| 203 | 199 | ||
| 204 | - def test_vector_clip(): | ||
| 205 | - assert vector_clip((-1, 10), (0, 0), (9, 9)) == (0, 9) | ||
| 206 | - | ||
| 207 | - | ||
| 208 | 200 | def test_turn_heading(): | |
| 209 | 201 | assert turn_heading((0, 1), 1) == (-1, 0) | |
| 210 | 202 | assert turn_heading((0, 1), -1) == (1, 0) | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -233,34 +233,38 @@ def euclidean_distance(x, y): | |||
| 233 | 233 | return np.sqrt(sum((_x - _y) ** 2 for _x, _y in zip(x, y))) | |
| 234 | 234 | ||
| 235 | 235 | ||
| 236 | + def manhattan_distance(x, y): | ||
| 237 | + return sum(abs(_x - _y) for _x, _y in zip(x, y)) | ||
| 238 | + | ||
| 239 | + | ||
| 240 | + def hamming_distance(x, y): | ||
| 241 | + return sum(_x != _y for _x, _y in zip(x, y)) | ||
| 242 | + | ||
| 243 | + | ||
| 236 | 244 | def cross_entropy_loss(x, y): | |
| 237 | - return (-1.0 / len(x)) * sum(x * np.log(y) + (1 - x) * np.log(1 - y) for x, y in zip(x, y)) | ||
| 245 | + return (-1.0 / len(x)) * sum(_x * np.log(_y) + (1 - _x) * np.log(1 - _y) for _x, _y in zip(x, y)) | ||
| 246 | + | ||
| 247 | + | ||
| 248 | + def mean_squared_error_loss(x, y): | ||
| 249 | + return (1.0 / len(x)) * sum((_x - _y) ** 2 for _x, _y in zip(x, y)) | ||
| 238 | 250 | ||
| 239 | 251 | ||
| 240 | 252 | def rms_error(x, y): | |
| 241 | 253 | return np.sqrt(ms_error(x, y)) | |
| 242 | 254 | ||
| 243 | 255 | ||
| 244 | 256 | def ms_error(x, y): | |
| 245 | - return mean((x - y) ** 2 for x, y in zip(x, y)) | ||
| 257 | + return mean((_x - _y) ** 2 for _x, _y in zip(x, y)) | ||
| 246 | 258 | ||
| 247 | 259 | ||
| 248 | 260 | def mean_error(x, y): | |
| 249 | - return mean(abs(x - y) for x, y in zip(x, y)) | ||
| 250 | - | ||
| 251 | - | ||
| 252 | - def manhattan_distance(x, y): | ||
| 253 | - return sum(abs(_x - _y) for _x, _y in zip(x, y)) | ||
| 261 | + return mean(abs(_x - _y) for _x, _y in zip(x, y)) | ||
| 254 | 262 | ||
| 255 | 263 | ||
| 256 | 264 | def mean_boolean_error(x, y): | |
| 257 | 265 | return mean(_x != _y for _x, _y in zip(x, y)) | |
| 258 | 266 | ||
| 259 | 267 | ||
| 260 | - def hamming_distance(x, y): | ||
| 261 | - return sum(_x != _y for _x, _y in zip(x, y)) | ||
| 262 | - | ||
| 263 | - | ||
| 264 | 268 | def normalize(dist): | |
| 265 | 269 | """Multiply each number by a constant such that the sum is 1.0""" | |
| 266 | 270 | if isinstance(dist, dict): | |
@@ -277,20 +281,15 @@ def random_weights(min_value, max_value, num_weights): | |||
| 277 | 281 | return [random.uniform(min_value, max_value) for _ in range(num_weights)] | |
| 278 | 282 | ||
| 279 | 283 | ||
| 280 | - def clip(x, lowest, highest): | ||
| 281 | - """Return x clipped to the range [lowest..highest].""" | ||
| 282 | - return max(lowest, min(x, highest)) | ||
| 284 | + def sigmoid(x): | ||
| 285 | + """Return activation value of x with sigmoid function.""" | ||
| 286 | + return 1 / (1 + np.exp(-x)) | ||
| 283 | 287 | ||
| 284 | 288 | ||
| 285 | 289 | def sigmoid_derivative(value): | |
| 286 | 290 | return value * (1 - value) | |
| 287 | 291 | ||
| 288 | 292 | ||
| 289 | - def sigmoid(x): | ||
| 290 | - """Return activation value of x with sigmoid function.""" | ||
| 291 | - return 1 / (1 + np.exp(-x)) | ||
| 292 | - | ||
| 293 | - | ||
| 294 | 293 | def elu(x, alpha=0.01): | |
| 295 | 294 | return x if x > 0 else alpha * (np.exp(x) - 1) | |
| 296 | 295 | ||
@@ -389,13 +388,6 @@ def distance_squared(a, b): | |||
| 389 | 388 | return (xA - xB) ** 2 + (yA - yB) ** 2 | |
| 390 | 389 | ||
| 391 | 390 | ||
| 392 | - def vector_clip(vector, lowest, highest): | ||
| 393 | - """Return vector, except if any element is less than the corresponding | ||
| 394 | - value of lowest or more than the corresponding value of highest, clip to | ||
| 395 | - those values.""" | ||
| 396 | - return type(vector)(map(clip, vector, lowest, highest)) | ||
| 397 | - | ||
| 398 | - | ||
| 399 | 391 | # ______________________________________________________________________________ | |
| 400 | 392 | # Misc Functions | |
| 401 | 393 | ||
@@ -484,7 +476,6 @@ def failure_test(algorithm, tests): | |||
| 484 | 476 | to check for correctness. On the other hand, a lot of algorithms output something | |
| 485 | 477 | particular on fail (for example, False, or None). | |
| 486 | 478 | tests is a list with each element in the form: (values, failure_output).""" | |
| 487 | - from statistics import mean | ||
| 488 | 479 | return mean(int(algorithm(x) != y) for x, y in tests) | |
| 489 | 480 | ||
| 490 | 481 | ||
| Back | FazBrowse Home | New Git URL |
0 commit comments