| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
1 parent e5204f6 commit 483bf81
19 files changed
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -209,7 +209,7 @@ def init_examples(examples, idx_i, idx_t, o_units): | |||
| 209 | 209 | # 19.4.1 Stochastic gradient descent | |
| 210 | 210 | ||
| 211 | 211 | ||
| 212 | - def gradient_descent(dataset, net, loss, epochs=1000, l_rate=0.01, batch_size=1): | ||
| 212 | + def gradient_descent(dataset, net, loss, epochs=1000, l_rate=0.01, batch_size=1, verbose=None): | ||
| 213 | 213 | """ | |
| 214 | 214 | gradient descent algorithm to update the learnable parameters of a network. | |
| 215 | 215 | :return: the updated network. | |
@@ -236,15 +236,15 @@ def gradient_descent(dataset, net, loss, epochs=1000, l_rate=0.01, batch_size=1 | |||
| 236 | 236 | for j in range(len(weights[i])): | |
| 237 | 237 | net[i].nodes[j].weights = weights[i][j] | |
| 238 | 238 | ||
| 239 | - if (e+1) % 10 == 0: | ||
| 239 | + if verbose and (e+1) % verbose == 0: | ||
| 240 | 240 | print("epoch:{}, total_loss:{}".format(e+1,total_loss)) | |
| 241 | 241 | return net | |
| 242 | 242 | ||
| 243 | 243 | ||
| 244 | 244 | # 19.4.2 Other gradient-based optimization algorithms | |
| 245 | 245 | ||
| 246 | 246 | ||
| 247 | - def adam_optimizer(dataset, net, loss, epochs=1000, rho=(0.9, 0.999), delta=1/10**8, l_rate=0.001, batch_size=1): | ||
| 247 | + def adam_optimizer(dataset, net, loss, epochs=1000, rho=(0.9, 0.999), delta=1/10**8, l_rate=0.001, batch_size=1, verbose=None): | ||
| 248 | 248 | """ | |
| 249 | 249 | Adam optimizer in Figure 19.6 to update the learnable parameters of a network. | |
| 250 | 250 | Required parameters are similar to gradient descent. | |
@@ -288,7 +288,7 @@ def adam_optimizer(dataset, net, loss, epochs=1000, rho=(0.9, 0.999), delta=1/1 | |||
| 288 | 288 | for j in range(len(weights[i])): | |
| 289 | 289 | net[i].nodes[j].weights = weights[i][j] | |
| 290 | 290 | ||
| 291 | - if (e+1) % 10 == 0: | ||
| 291 | + if verbose and (e+1) % verbose == 0: | ||
| 292 | 292 | print("epoch:{}, total_loss:{}".format(e+1,total_loss)) | |
| 293 | 293 | return net | |
| 294 | 294 | ||
@@ -382,7 +382,7 @@ def get_batch(examples, batch_size=1): | |||
| 382 | 382 | # example of NNs | |
| 383 | 383 | ||
| 384 | 384 | ||
| 385 | - def neural_net_learner(dataset, hidden_layer_sizes=[4], learning_rate=0.01, epochs=100, optimizer=gradient_descent, batch_size=1): | ||
| 385 | + def neural_net_learner(dataset, hidden_layer_sizes=[4], learning_rate=0.01, epochs=100, optimizer=gradient_descent, batch_size=1, verbose=None): | ||
| 386 | 386 | """Example of a simple dense multilayer neural network. | |
| 387 | 387 | :param hidden_layer_sizes: size of hidden layers in the form of a list""" | |
| 388 | 388 | ||
@@ -399,7 +399,7 @@ def neural_net_learner(dataset, hidden_layer_sizes=[4], learning_rate=0.01, epoc | |||
| 399 | 399 | raw_net.append(DenseLayer(hidden_input_size, output_size)) | |
| 400 | 400 | ||
| 401 | 401 | # update parameters of the network | |
| 402 | - learned_net = optimizer(dataset, raw_net, mse_loss, epochs, l_rate=learning_rate, batch_size=batch_size) | ||
| 402 | + learned_net = optimizer(dataset, raw_net, mse_loss, epochs, l_rate=learning_rate, batch_size=batch_size, verbose=verbose) | ||
| 403 | 403 | ||
| 404 | 404 | def predict(example): | |
| 405 | 405 | n_layers = len(learned_net) | |
@@ -417,7 +417,7 @@ def predict(example): | |||
| 417 | 417 | return predict | |
| 418 | 418 | ||
| 419 | 419 | ||
| 420 | - def perceptron_learner(dataset, learning_rate=0.01, epochs=100): | ||
| 420 | + def perceptron_learner(dataset, learning_rate=0.01, epochs=100, verbose=None): | ||
| 421 | 421 | """ | |
| 422 | 422 | Example of a simple perceptron neural network. | |
| 423 | 423 | """ | |
@@ -427,7 +427,7 @@ def perceptron_learner(dataset, learning_rate=0.01, epochs=100): | |||
| 427 | 427 | # initialize the network, add dense layer | |
| 428 | 428 | raw_net = [InputLayer(input_size), DenseLayer(input_size, output_size)] | |
| 429 | 429 | # update the network | |
| 430 | - learned_net = gradient_descent(dataset, raw_net, mse_loss, epochs, l_rate=learning_rate) | ||
| 430 | + learned_net = gradient_descent(dataset, raw_net, mse_loss, epochs, l_rate=learning_rate, verbose=verbose) | ||
| 431 | 431 | ||
| 432 | 432 | def predict(example): | |
| 433 | 433 | ||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -1,6 +1,6 @@ | |||
| 1 | 1 | from utils4e import ( | |
| 2 | 2 | removeall, unique, mode, argmax_random_tie, isclose, dotproduct, weighted_sample_with_replacement, | |
| 3 | - num_or_str, normalize, clip, print_table, open_data, probability, random_weights | ||
| 3 | + num_or_str, normalize, clip, print_table, open_data, probability, random_weights, euclidean_distance | ||
| 4 | 4 | ) | |
| 5 | 5 | ||
| 6 | 6 | import copy | |
@@ -382,8 +382,8 @@ def cross_validation(learner, size, dataset, k=10, trials=1): | |||
| 382 | 382 | examples = dataset.examples | |
| 383 | 383 | random.shuffle(dataset.examples) | |
| 384 | 384 | for fold in range(k): | |
| 385 | - train_data, val_data = train_test_split(dataset, fold * (n / k), | ||
| 386 | - (fold + 1) * (n / k)) | ||
| 385 | + train_data, val_data = train_test_split(dataset, fold * (n // k), | ||
| 386 | + (fold + 1) * (n // k)) | ||
| 387 | 387 | dataset.examples = train_data | |
| 388 | 388 | h = learner(dataset, size) | |
| 389 | 389 | fold_errs += err_ratio(h, dataset, train_data) | |
@@ -393,6 +393,37 @@ def cross_validation(learner, size, dataset, k=10, trials=1): | |||
| 393 | 393 | return fold_errs/k | |
| 394 | 394 | ||
| 395 | 395 | ||
| 396 | + def cross_validation_nosize(learner, dataset, k=10, trials=1): | ||
| 397 | + """Do k-fold cross_validate and return their mean. | ||
| 398 | + That is, keep out 1/k of the examples for testing on each of k runs. | ||
| 399 | + Shuffle the examples first; if trials>1, average over several shuffles. | ||
| 400 | + Returns Training error, Validataion error""" | ||
| 401 | + k = k or len(dataset.examples) | ||
| 402 | + if trials > 1: | ||
| 403 | + trial_errs = 0 | ||
| 404 | + for t in range(trials): | ||
| 405 | + errs = cross_validation(learner, dataset, | ||
| 406 | + k=10, trials=1) | ||
| 407 | + trial_errs += errs | ||
| 408 | + return trial_errs/trials | ||
| 409 | + else: | ||
| 410 | + fold_errs = 0 | ||
| 411 | + n = len(dataset.examples) | ||
| 412 | + examples = dataset.examples | ||
| 413 | + random.shuffle(dataset.examples) | ||
| 414 | + for fold in range(k): | ||
| 415 | + train_data, val_data = train_test_split(dataset, fold * (n // k), | ||
| 416 | + (fold + 1) * (n // k)) | ||
| 417 | + dataset.examples = train_data | ||
| 418 | + h = learner(dataset) | ||
| 419 | + fold_errs += err_ratio(h, dataset, train_data) | ||
| 420 | + | ||
| 421 | + # Reverting back to original once test is completed | ||
| 422 | + dataset.examples = examples | ||
| 423 | + return fold_errs/k | ||
| 424 | + | ||
| 425 | + | ||
| 426 | + | ||
| 396 | 427 | def err_ratio(predict, dataset, examples=None, verbose=0): | |
| 397 | 428 | """Return the proportion of the examples that are NOT correctly predicted. | |
| 398 | 429 | verbose - 0: No output; 1: Output wrong; 2 (or greater): Output correct""" | |
@@ -521,6 +552,8 @@ def LinearLearner(dataset, learning_rate=0.01, epochs=100): | |||
| 521 | 552 | for example in examples: | |
| 522 | 553 | x = [1] + example | |
| 523 | 554 | y = dotproduct(w, x) | |
| 555 | + # if threshold: | ||
| 556 | + # y = threshold(y) | ||
| 524 | 557 | t = example[idx_t] | |
| 525 | 558 | err.append(t - y) | |
| 526 | 559 | ||
@@ -554,17 +587,20 @@ def LogisticLinearLeaner(dataset, learning_rate=0.01, epochs=100): | |||
| 554 | 587 | ||
| 555 | 588 | for epoch in range(epochs): | |
| 556 | 589 | err = [] | |
| 590 | + h= [] | ||
| 557 | 591 | # Pass over all examples | |
| 558 | 592 | for example in examples: | |
| 559 | 593 | x = [1] + example | |
| 560 | 594 | y = 1/(1 + math.exp(-dotproduct(w, x))) | |
| 561 | - h = [y * (1-y)] | ||
| 595 | + h.append(y * (1-y)) | ||
| 562 | 596 | t = example[idx_t] | |
| 563 | 597 | err.append(t - y) | |
| 564 | 598 | ||
| 565 | 599 | # update weights | |
| 566 | 600 | for i in range(len(w)): | |
| 567 | - w[i] = w[i] + learning_rate * (dotproduct(dotproduct(err,h), X_col[i]) / num_examples) | ||
| 601 | + buffer = [x*y for x,y in zip(err, h)] | ||
| 602 | + # w[i] = w[i] + learning_rate * (dotproduct(err, X_col[i]) / num_examples) | ||
| 603 | + w[i] = w[i] + learning_rate * (dotproduct(buffer, X_col[i]) / num_examples) | ||
| 568 | 604 | ||
| 569 | 605 | def predict(example): | |
| 570 | 606 | x = [1] + example | |
@@ -580,6 +616,7 @@ def NearestNeighborLearner(dataset, k=1): | |||
| 580 | 616 | """k-NearestNeighbor: the k nearest neighbors vote.""" | |
| 581 | 617 | def predict(example): | |
| 582 | 618 | """Find the k closest items, and have them vote for the best.""" | |
| 619 | + example.pop(dataset.target) | ||
| 583 | 620 | best = heapq.nsmallest(k, ((dataset.distance(e, example), e) | |
| 584 | 621 | for e in dataset.examples)) | |
| 585 | 622 | return mode(e[dataset.target] for (d, e) in best) | |
@@ -829,6 +866,6 @@ def compare(algorithms=None, datasets=None, k=10, trials=1): | |||
| 829 | 866 | Majority(7, 100), Parity(7, 100), Xor(100)] # of datasets | |
| 830 | 867 | ||
| 831 | 868 | print_table([[a.__name__.replace('Learner', '')] + | |
| 832 | - [cross_validation(a, d, k, trials) for d in datasets] | ||
| 869 | + [cross_validation_nosize(a, d, k, trials) for d in datasets] | ||
| 833 | 870 | for a in algorithms], | |
| 834 | - header=[''] + [d.name[0:7] for d in datasets], numfmt='%.2f') | ||
| 871 | + header=[''] + [d.name[0:7] for d in datasets], numfmt='{0:.2f}') | ||
| Back | FazBrowse Home | New Git URL |
0 commit comments