master 9c35878543c9 cached
556 files
27.1 MB
3.5M tokens
2112 symbols
1 requests
Copy disabled (too large) Download .txt
Showing preview only (13,907K chars total). Download the full file to get everything.
Repository: lazyprogrammer/machine_learning_examples
Branch: master
Commit: 9c35878543c9
Files: 556
Total size: 27.1 MB

Directory structure:
gitextract_oeszk7b8/

├── .gitignore
├── README.md
├── ab_testing/
│   ├── advertisement_clicks.csv
│   ├── bayesian_bandit.py
│   ├── bayesian_normal.py
│   ├── bayesian_starter.py
│   ├── cdfs_and_percentiles.py
│   ├── chisquare.py
│   ├── ci_comparison.py
│   ├── client.py
│   ├── comparing_epsilons.py
│   ├── convergence.py
│   ├── demo.py
│   ├── epsilon_greedy.py
│   ├── epsilon_greedy_starter.py
│   ├── ex_chisq.py
│   ├── ex_ttest.py
│   ├── extra_reading.txt
│   ├── optimistic.py
│   ├── optimistic_starter.py
│   ├── server_solution.py
│   ├── server_starter.py
│   ├── ttest.py
│   ├── ucb1.py
│   └── ucb1_starter.py
├── airline/
│   ├── ann.py
│   ├── international-airline-passengers.csv
│   ├── lr.py
│   └── rnn.py
├── ann_class/
│   ├── backprop.py
│   ├── extra_reading.txt
│   ├── forwardprop.py
│   ├── regression.py
│   ├── sklearn_ann.py
│   ├── tf_example.py
│   └── xor_donut.py
├── ann_class2/
│   ├── __init__.py
│   ├── adam.py
│   ├── batch_norm_tf.py
│   ├── batch_norm_theano.py
│   ├── cntk_example.py
│   ├── dropout_tensorflow.py
│   ├── dropout_theano.py
│   ├── extra_reading.txt
│   ├── grid_search.py
│   ├── keras_example.py
│   ├── keras_functional.py
│   ├── mlp.py
│   ├── momentum.py
│   ├── mxnet_example.py
│   ├── pytorch_batchnorm.py
│   ├── pytorch_dropout.py
│   ├── pytorch_example.py
│   ├── pytorch_example2.py
│   ├── random_search.py
│   ├── rmsprop.py
│   ├── rmsprop_test.py
│   ├── sgd.py
│   ├── tensorflow1.py
│   ├── tensorflow2.py
│   ├── tf_with_save.py
│   ├── theano1.py
│   ├── theano2.py
│   ├── theano_ann.py
│   └── util.py
├── ann_logistic_extra/
│   ├── __init__.py
│   ├── ann_predict.py
│   ├── ann_train.py
│   ├── ecommerce_data.csv
│   ├── logistic_predict.py
│   ├── logistic_softmax_train.py
│   ├── logistic_train.py
│   └── process.py
├── bayesian_ml/
│   ├── 1/
│   │   ├── Q.csv
│   │   ├── README
│   │   ├── Xtest.csv
│   │   ├── Xtrain.csv
│   │   ├── nb.py
│   │   ├── ytest.csv
│   │   └── ytrain.csv
│   ├── 2/
│   │   ├── Q.csv
│   │   ├── README
│   │   ├── Xtest.csv
│   │   ├── Xtrain.csv
│   │   ├── em.py
│   │   ├── probit.py
│   │   ├── ytest.csv
│   │   └── ytrain.csv
│   ├── 3/
│   │   ├── X_set1.csv
│   │   ├── X_set2.csv
│   │   ├── X_set3.csv
│   │   ├── run.py
│   │   ├── y_set1.csv
│   │   ├── y_set2.csv
│   │   ├── y_set3.csv
│   │   ├── z_set1.csv
│   │   ├── z_set2.csv
│   │   └── z_set3.csv
│   └── 4/
│       ├── data.txt
│       ├── emgmm.py
│       ├── npbgmm.py
│       └── vigmm.py
├── best_fit_line.py
├── calculus/
│   ├── WHERE ARE THE NOTEBOOKS.txt
│   └── extra_reading.txt
├── chatgpt_trading/
│   ├── WHERE ARE THE NOTEBOOKS.txt
│   └── extra_reading.txt
├── cnn_class/
│   ├── WHERE ARE THE NOTEBOOKS.txt
│   ├── benchmark.py
│   ├── blur.py
│   ├── cifar.py
│   ├── cnn_tf.py
│   ├── cnn_tf_plot_filters.py
│   ├── cnn_theano.py
│   ├── cnn_theano_plot_filters.py
│   ├── custom_blur.py
│   ├── echo.py
│   ├── edge.py
│   ├── edge_benchmark.py
│   ├── exercises.txt
│   ├── extra_reading.txt
│   └── keras_example.py
├── cnn_class2/
│   ├── WHERE ARE THE NOTEBOOKS.txt
│   ├── class_activation_maps.py
│   ├── extra_reading.txt
│   ├── fashion.py
│   ├── fashion2.py
│   ├── make_limited_datasets.py
│   ├── siamese.py
│   ├── ssd.py
│   ├── style_transfer1.py
│   ├── style_transfer2.py
│   ├── style_transfer3.py
│   ├── test_softmax.py
│   ├── tf_resnet.py
│   ├── tf_resnet_convblock.py
│   ├── tf_resnet_convblock_starter.py
│   ├── tf_resnet_first_layers.py
│   ├── tf_resnet_first_layers_starter.py
│   ├── tf_resnet_identity_block.py
│   ├── tf_resnet_identity_block_starter.py
│   ├── use_pretrained_weights_resnet.py
│   ├── use_pretrained_weights_vgg.py
│   └── util.py
├── data_csv/
│   ├── X.txt
│   ├── X_orig.txt
│   ├── legend.txt
│   ├── readme.txt
│   └── y.txt
├── financial_engineering/
│   └── go_here_instead.txt
├── hmm_class/
│   ├── __init__.py
│   ├── coin_data.txt
│   ├── edgar_allan_poe.txt
│   ├── extra_reading.txt
│   ├── frost.py
│   ├── generate_c.py
│   ├── generate_ht.py
│   ├── hmm_classifier.py
│   ├── hmmc.py
│   ├── hmmc_concat.py
│   ├── hmmc_scaled_concat.py
│   ├── hmmc_scaled_concat_diag.py
│   ├── hmmc_tf.py
│   ├── hmmc_theano.py
│   ├── hmmc_theano2.py
│   ├── hmmd.py
│   ├── hmmd_scaled.py
│   ├── hmmd_tf.py
│   ├── hmmd_theano.py
│   ├── hmmd_theano2.py
│   ├── robert_frost.txt
│   ├── scan1.py
│   ├── scan2.py
│   ├── scan3.py
│   ├── site_data.csv
│   ├── sites.py
│   ├── tf_scan1.py
│   ├── tf_scan2.py
│   └── tf_scan3.py
├── keras_examples/
│   ├── ann.py
│   ├── basic_mlp.py
│   ├── batchnorm.py
│   ├── cnn.py
│   ├── cnn_cifar.py
│   ├── cnn_dropout_batchnorm.py
│   ├── dropout.py
│   ├── sentiment_analysis.py
│   ├── sine.py
│   ├── sine2.py
│   ├── translation.py
│   └── util.py
├── kerascv/
│   ├── extra_reading.txt
│   ├── imagenet_label_names.json
│   ├── makelist.py
│   └── pascal2coco.py
├── linear_algebra/
│   ├── WHERE ARE THE NOTEBOOKS.txt
│   └── extra_reading.txt
├── linear_regression_class/
│   ├── data_1d.csv
│   ├── data_2d.csv
│   ├── data_poly.csv
│   ├── gd.py
│   ├── generate_1d.py
│   ├── generate_2d.py
│   ├── generate_poly.py
│   ├── gradient_descent.py
│   ├── l1_regularization.py
│   ├── l2_regularization.py
│   ├── lr_1d.py
│   ├── lr_2d.py
│   ├── lr_poly.py
│   ├── mlr02.xls
│   ├── moore.csv
│   ├── moore.py
│   ├── overfitting.py
│   └── systolic.py
├── logistic_regression_class/
│   ├── bad_xor.py
│   ├── l1_regularization.py
│   ├── logistic1.py
│   ├── logistic2.py
│   ├── logistic3.py
│   ├── logistic4.py
│   ├── logistic_donut.py
│   ├── logistic_visualize.py
│   └── logistic_xor.py
├── matrix_calculus/
│   └── extra_reading.txt
├── mnist_csv/
│   ├── Q.txt
│   ├── Xtest.txt
│   ├── Xtrain.txt
│   ├── label_test.txt
│   └── label_train.txt
├── naive_bayes/
│   ├── WHERE ARE THE NOTEBOOKS.txt
│   └── extra_reading.txt
├── nlp_class/
│   ├── all_book_titles.txt
│   ├── article_spinner.py
│   ├── cipher_placeholder.py
│   ├── electronics/
│   │   ├── negative.review
│   │   ├── positive.review
│   │   └── unlabeled.review
│   ├── extra_reading.txt
│   ├── lsa.py
│   ├── nb.py
│   ├── sentiment.py
│   ├── spam2.py
│   ├── spambase.data
│   └── stopwords.txt
├── nlp_class2/
│   ├── bow_classifier.py
│   ├── extra_reading.txt
│   ├── glove.py
│   ├── glove_svd.py
│   ├── glove_tf.py
│   ├── glove_theano.py
│   ├── logistic.py
│   ├── markov.py
│   ├── ner.txt
│   ├── ner_baseline.py
│   ├── ner_rnn.py
│   ├── ner_tf.py
│   ├── neural_network.py
│   ├── neural_network2.py
│   ├── pmi.py
│   ├── pos_baseline.py
│   ├── pos_hmm.py
│   ├── pos_ner_keras.py
│   ├── pos_rnn.py
│   ├── pos_tf.py
│   ├── pretrained_glove.py
│   ├── pretrained_w2v.py
│   ├── recursive_tensorflow.py
│   ├── recursive_theano.py
│   ├── rntn_tensorflow.py
│   ├── rntn_tensorflow_rnn.py
│   ├── rntn_theano.py
│   ├── tfidf_tsne.py
│   ├── util.py
│   ├── visualize_countries.py
│   ├── w2v_model.npz
│   ├── w2v_word2idx.json
│   ├── word2vec.py
│   ├── word2vec_tf.py
│   └── word2vec_theano.py
├── nlp_class3/
│   ├── attention.py
│   ├── bilstm_mnist.py
│   ├── bilstm_test.py
│   ├── cnn_toxic.py
│   ├── convert_twitter.py
│   ├── extra_reading.txt
│   ├── lstm_toxic.py
│   ├── memory_network.py
│   ├── poetry.py
│   ├── simple_rnn_test.py
│   └── wseq2seq.py
├── nlp_v2/
│   ├── WHERE ARE THE NOTEBOOKS.txt
│   └── extra_reading.txt
├── numpy_class/
│   ├── classification_example.py
│   ├── dot_for.py
│   ├── exercises/
│   │   ├── ex1.py
│   │   ├── ex2.py
│   │   ├── ex3.py
│   │   ├── ex4.py
│   │   ├── ex5.py
│   │   ├── ex6.py
│   │   ├── ex7.py
│   │   ├── ex8.py
│   │   └── ex9.py
│   ├── manual_data_loading.py
│   ├── python3/
│   │   ├── dot_for.py
│   │   └── manual_data_loading.py
│   ├── regression_example.py
│   ├── table1.csv
│   └── table2.csv
├── openai/
│   ├── extra_reading.txt
│   └── replies.json
├── probability/
│   ├── WHERE ARE THE NOTEBOOKS.txt
│   └── extra_reading.txt
├── prophet/
│   └── extra_reading.txt
├── pytorch/
│   ├── .gitignore
│   ├── WHERE ARE THE NOTEBOOKS.txt
│   ├── aapl_msi_sbux.csv
│   ├── ann_regression.py
│   ├── exercises.txt
│   ├── extra_reading.txt
│   ├── plot_rl_rewards.py
│   └── rl_trader.py
├── recommenders/
│   ├── autorec.py
│   ├── extra_reading.txt
│   ├── itembased.py
│   ├── mf.py
│   ├── mf2.py
│   ├── mf_keras.py
│   ├── mf_keras_deep.py
│   ├── mf_keras_res.py
│   ├── preprocess.py
│   ├── preprocess2dict.py
│   ├── preprocess2sparse.py
│   ├── preprocess_shrink.py
│   ├── rbm_tf_k.py
│   ├── rbm_tf_k_faster.py
│   ├── spark.py
│   ├── spark2.py
│   ├── tfidf.py
│   └── userbased.py
├── rl/
│   ├── approx_control.py
│   ├── approx_prediction.py
│   ├── bayesian_bandit.py
│   ├── bayesian_normal.py
│   ├── bayesian_starter.py
│   ├── cartpole.py
│   ├── cartpole_gym0.19.py
│   ├── comparing_epsilons.py
│   ├── comparing_explore_exploit_methods.py
│   ├── epsilon_greedy.py
│   ├── epsilon_greedy_starter.py
│   ├── extra_reading.txt
│   ├── grid_world.py
│   ├── iterative_policy_evaluation_deterministic.py
│   ├── iterative_policy_evaluation_probabilistic.py
│   ├── linear_rl_trader.py
│   ├── monte_carlo.py
│   ├── monte_carlo_es.py
│   ├── monte_carlo_no_es.py
│   ├── optimistic.py
│   ├── optimistic_initial_values.py
│   ├── optimistic_starter.py
│   ├── plot_rl_rewards.py
│   ├── policy_iteration_deterministic.py
│   ├── policy_iteration_probabilistic.py
│   ├── q_learning.py
│   ├── sarsa.py
│   ├── td0_prediction.py
│   ├── tic_tac_toe.py
│   ├── ucb1.py
│   ├── ucb1_starter.py
│   └── value_iteration.py
├── rl2/
│   ├── a3c/
│   │   ├── main.py
│   │   ├── nets.py
│   │   ├── thread_example.py
│   │   └── worker.py
│   ├── atari/
│   │   ├── dqn_tf.py
│   │   └── dqn_theano.py
│   ├── cartpole/
│   │   ├── dqn_tf.py
│   │   ├── dqn_theano.py
│   │   ├── pg_tf.py
│   │   ├── pg_theano.py
│   │   ├── q_learning.py
│   │   ├── q_learning_bins.py
│   │   ├── random_search.py
│   │   ├── save_a_video.py
│   │   ├── td_lambda.py
│   │   ├── tf_warmup.py
│   │   └── theano_warmup.py
│   ├── extra_reading.txt
│   ├── gym_tutorial.py
│   └── mountaincar/
│       ├── n_step.py
│       ├── pg_tf.py
│       ├── pg_tf_random.py
│       ├── pg_theano.py
│       ├── pg_theano_random.py
│       ├── q_learning.py
│       └── td_lambda.py
├── rl2v2/
│   └── extra_reading.txt
├── rl3/
│   ├── a2c/
│   │   ├── a2c.py
│   │   ├── atari_wrappers.py
│   │   ├── main.py
│   │   ├── neural_network.py
│   │   ├── play.py
│   │   └── subproc_vec_env.py
│   ├── ddpg.py
│   ├── es_flappy.py
│   ├── es_mnist.py
│   ├── es_mujoco.py
│   ├── es_simple.py
│   ├── extra_reading.txt
│   ├── flappy2envs.py
│   ├── gym_review.py
│   ├── plot_ddpg_result.py
│   ├── plot_es_flappy_results.py
│   ├── plot_es_mujoco_results.py
│   └── sample_test.py
├── rl3v2/
│   ├── extra_reading.txt
│   ├── visualize_es.py
│   └── visualize_hill_climbing.py
├── rnn_class/
│   ├── WHERE ARE THE NOTEBOOKS.txt
│   ├── __init__.py
│   ├── batch_gru.py
│   ├── batch_parity.py
│   ├── batch_units.py
│   ├── batch_wiki.py
│   ├── brown.py
│   ├── exercises.txt
│   ├── extra_reading.txt
│   ├── gru.py
│   ├── gru_nonorm_part1_wikipedia_word2idx.json
│   ├── gru_nonorm_part1_word_embeddings.npy
│   ├── lstm.py
│   ├── mlp_parity.py
│   ├── poetry_classifier.py
│   ├── rrnn_language.py
│   ├── srn_language.py
│   ├── srn_language_tf.py
│   ├── srn_parity.py
│   ├── srn_parity_tf.py
│   ├── tf_parity.py
│   ├── util.py
│   ├── visualize_embeddings.py
│   └── wiki.py
├── stats/
│   └── extra_reading.txt
├── supervised_class/
│   ├── app.py
│   ├── app_caller.py
│   ├── app_trainer.py
│   ├── bayes.py
│   ├── dt.py
│   ├── dt_without_recursion.py
│   ├── knn.py
│   ├── knn_donut.py
│   ├── knn_fail.py
│   ├── knn_vectorized.py
│   ├── knn_xor.py
│   ├── multinomialnb.py
│   ├── nb.py
│   ├── perceptron.py
│   ├── regression.py
│   └── util.py
├── supervised_class2/
│   ├── adaboost.py
│   ├── bagging_classification.py
│   ├── bagging_regression.py
│   ├── bias_variance_demo.py
│   ├── bootstrap.py
│   ├── extra_reading.txt
│   ├── knn_dt_demo.py
│   ├── rf_classification.py
│   ├── rf_regression.py
│   ├── rf_vs_bag.py
│   ├── rf_vs_bag2.py
│   └── util.py
├── svm_class/
│   ├── crossval.py
│   ├── extra_reading.txt
│   ├── fake_neural_net.py
│   ├── kernel_svm_gradient_primal.py
│   ├── linear_svm_gradient.py
│   ├── rbfnetwork.py
│   ├── real_neural_net.py
│   ├── regression.py
│   ├── svm_gradient.py
│   ├── svm_medical.py
│   ├── svm_mnist.py
│   ├── svm_smo.py
│   ├── svm_spam.py
│   └── util.py
├── tensorflow/
│   └── input_data.py
├── tf2.0/
│   ├── .gitignore
│   ├── WHERE ARE THE NOTEBOOKS.txt
│   ├── aapl_msi_sbux.csv
│   ├── auto-mpg.data
│   ├── daily-minimum-temperatures-in-me.csv
│   ├── exercises.txt
│   ├── extra_reading.txt
│   ├── fake_util.py
│   ├── keras_trader.py
│   ├── mlp_trader.py
│   ├── moore.csv
│   ├── plot_rl_rewards.py
│   ├── rl_trader.py
│   ├── sbux.csv
│   └── xor3d.py
├── timeseries/
│   ├── WHERE ARE THE NOTEBOOKS.txt
│   └── extra_reading.txt
├── transformers/
│   ├── WHERE ARE THE NOTEBOOKS.txt
│   └── extra_reading.txt
├── unsupervised_class/
│   ├── __init__.py
│   ├── books.py
│   ├── choose_k.py
│   ├── evolution.py
│   ├── gmm.py
│   ├── gmm_mnist.py
│   ├── hcluster.py
│   ├── kmeans.py
│   ├── kmeans_fail.py
│   ├── kmeans_mnist.py
│   ├── kmeans_visualize.py
│   ├── neural_kmeans.py
│   └── tweets.py
├── unsupervised_class2/
│   ├── __init__.py
│   ├── autoencoder.py
│   ├── autoencoder_tf.py
│   ├── compare_pca_svd.py
│   ├── extra_reading.txt
│   ├── gaussian_nb.py
│   ├── pca.py
│   ├── pca_impl.py
│   ├── rbm.py
│   ├── rbm_tf.py
│   ├── sk_mlp.py
│   ├── tsne_books.py
│   ├── tsne_donut.py
│   ├── tsne_mnist.py
│   ├── tsne_visualization.py
│   ├── tsne_xor.py
│   ├── umap_transformer.py
│   ├── unsupervised.py
│   ├── util.py
│   ├── vanishing.py
│   ├── visualize_features.py
│   └── xwing.py
└── unsupervised_class3/
    ├── autoencoder_tf.py
    ├── autoencoder_theano.py
    ├── bayes_classifier_gaussian.py
    ├── bayes_classifier_gmm.py
    ├── dcgan_tf.py
    ├── dcgan_theano.py
    ├── extra_reading.txt
    ├── parameterize_guassian.py
    ├── test_stochastic_tensor.py
    ├── util.py
    ├── vae_tf.py
    ├── vae_theano.py
    └── visualize_latent_space.py

================================================
FILE CONTENTS
================================================

================================================
FILE: .gitignore
================================================
*.DS_Store
*.pyc
large_files
large_files/*
nlp_class2/chunking/*


================================================
FILE: README.md
================================================
machine_learning_examples
=========================

A collection of machine learning examples and tutorials.

Find associated tutorials at https://lazyprogrammer.me

Find associated courses at https://deeplearningcourses.com

Please note that not all code from all courses will be found in this repository. Some newer code examples (e.g. most of Tensorflow 2.0) were done in Google Colab. Therefore, you should check the instructions given in the lectures for the course you are taking.


How to I find the code for a particular course?
===============================================

The code for each course is separated by folder. You can determine which folder corresponds with which course by watching the "Where to get the code" lecture inside the course (usually Lecture 2 or 3).

Remember: one folder = one course.


Why you should not fork this repo
=================================

I've noticed that many people have out-of-date forks. Thus, I recommend not forking this repository if you take one of my courses. I am constantly updating my courses, and your fork will soon become out-of-date. You should clone the repository instead to make it easy to get updates (i.e. just "git pull" randomly and frequently).


Where is the code for your latest courses?
==========================================

Beginning with Tensorflow 2, I started to use Google Colab. For those courses, unless otherwise noted, the code will be on Google Colab. Links to the notebooks are provided in the course. See the lecture "Where to get the code" for further details.


VIP Course Links
===================

**Advanced AI: Deep Reinforcement Learning in PyTorch (v2)**

https://deeplearningcourses.com/c/deep-reinforcement-learning-in-pytorch


**Data Science: Transformers for Natural Language Processing**

https://deeplearningcourses.com/c/data-science-transformers-nlp


**Machine Learning: Natural Language Processing in Python (V2)**

https://deeplearningcourses.com/c/natural-language-processing-in-python


**Time Series Analysis, Forecasting, and Machine Learning**

https://deeplearningcourses.com/c/time-series-analysis


**Financial Engineering and Artificial Intelligence in Python**

https://deeplearningcourses.com/c/ai-finance


**PyTorch: Deep Learning and Artificial Intelligence**

https://deeplearningcourses.com/c/pytorch-deep-learning


**Tensorflow 2.0: Deep Learning and Artificial Intelligence** (VIP Version)

https://deeplearningcourses.com/c/deep-learning-tensorflow-2


**Math 0-1: Linear Algebra for Data Science & Machine Learning**

https://deeplearningcourses.com/c/linear-algebra-data-science

**Math 0-1: Probability for Data Science & Machine Learning**

https://deeplearningcourses.com/c/probability-data-science-machine-learning


Deep Learning Courses Exclusives
================================

Data Science: Bayesian Linear Regression in Python
https://deeplearningcourses.com/c/bayesian-linear-regression-in-python

Data Science: Bayesian Classification in Python
https://deeplearningcourses.com/c/bayesian-classification-in-python

Classical Statistical Inference and A/B Testing in Python
https://deeplearningcourses.com/c/statistical-inference-in-python

Linear Programming for Linear Regression in Python
https://deeplearningcourses.com/c/linear-programming-python

MATLAB for Students, Engineers, and Professionals in STEM
https://deeplearningcourses.com/c/matlab



Other Course Links
==================

Generative AI: ChatGPT & OpenAI LLMs in Python
https://deeplearningcourses.com/c/genai-openai-chatgpt

Math 0-1: Matrix Calculus for Data Science & Machine Learning
https://deeplearningcourses.com/c/matrix-calculus-machine-learning

Machine Learning: Modern Computer Vision & Generative AI
https://deeplearningcourses.com/c/computer-vision-kerascv

DeepFakes & Voice Cloning: Machine Learning The Easy Way
https://deeplearningcourses.com/c/deepfakes-voice-cloning

Financial Analysis: Build a ChatGPT Pairs Trading Bot
https://deeplearningcourses.com/c/chatgpt-pairs-trading

Math 0-1: Calculus for Data Science & Machine Learning
https://deeplearningcourses.com/c/calculus-data-science

Data Science & Machine Learning: Naive Bayes in Python
https://deeplearningcourses.com/c/data-science-machine-learning-naive-bayes-in-python

Cutting-Edge AI: Deep Reinforcement Learning in Python
https://deeplearningcourses.com/c/cutting-edge-artificial-intelligence

Recommender Systems and Deep Learning in Python
https://deeplearningcourses.com/c/recommender-systems

Machine Learning and AI: Support Vector Machines in Python
https://deeplearningcourses.com/c/support-vector-machines-in-python

Deep Learning: Advanced Computer Vision
https://deeplearningcourses.com/c/advanced-computer-vision

Deep Learning: Advanced NLP and RNNs
https://deeplearningcourses.com/c/deep-learning-advanced-nlp

Deep Learning: GANs and Variational Autoencoders
https://deeplearningcourses.com/c/deep-learning-gans-and-variational-autoencoders

Advanced AI: Deep Reinforcement Learning in Python
https://deeplearningcourses.com/c/deep-reinforcement-learning-in-python

Artificial Intelligence: Reinforcement Learning in Python
https://deeplearningcourses.com/c/artificial-intelligence-reinforcement-learning-in-python

Natural Language Processing with Deep Learning in Python
https://deeplearningcourses.com/c/natural-language-processing-with-deep-learning-in-python

Deep Learning: Recurrent Neural Networks in Python
https://deeplearningcourses.com/c/deep-learning-recurrent-neural-networks-in-python

Unsupervised Machine Learning: Hidden Markov Models in Python
https://deeplearningcourses.com/c/unsupervised-machine-learning-hidden-markov-models-in-python

Deep Learning Prerequisites: The Numpy Stack in Python
https://deeplearningcourses.com/c/deep-learning-prerequisites-the-numpy-stack-in-python

Deep Learning Prerequisites: Linear Regression in Python
https://deeplearningcourses.com/c/data-science-linear-regression-in-python

Deep Learning Prerequisites: Logistic Regression in Python
https://deeplearningcourses.com/c/data-science-logistic-regression-in-python

Data Science: Deep Learning and Neural Networks in Python
https://deeplearningcourses.com/c/data-science-deep-learning-in-python

Cluster Analysis and Unsupervised Machine Learning in Python
https://deeplearningcourses.com/c/cluster-analysis-unsupervised-machine-learning-python

Data Science: Supervised Machine Learning in Python
https://deeplearningcourses.com/c/data-science-supervised-machine-learning-in-python

Bayesian Machine Learning in Python: A/B Testing
https://deeplearningcourses.com/c/bayesian-machine-learning-in-python-ab-testing

Data Science: Natural Language Processing in Python
https://deeplearningcourses.com/c/data-science-natural-language-processing-in-python

Modern Deep Learning in Python
https://deeplearningcourses.com/c/data-science-deep-learning-in-theano-tensorflow

Ensemble Machine Learning in Python: Random Forest and AdaBoost
https://deeplearningcourses.com/c/machine-learning-in-python-random-forest-adaboost

Deep Learning: Convolutional Neural Networks in Python
https://deeplearningcourses.com/c/deep-learning-convolutional-neural-networks-theano-tensorflow

Unsupervised Deep Learning in Python
https://deeplearningcourses.com/c/unsupervised-deep-learning-in-python


================================================
FILE: ab_testing/advertisement_clicks.csv
================================================
advertisement_id,action
B,1
B,1
A,0
B,0
A,1
A,0
B,0
A,1
B,0
A,0
B,1
A,0
B,1
B,0
B,1
B,1
B,0
A,0
B,0
B,0
B,0
A,0
A,1
A,1
B,0
B,0
A,0
B,0
A,0
B,1
A,0
B,0
B,0
A,0
A,0
B,0
B,0
A,1
B,0
B,1
B,1
B,0
B,0
A,0
A,1
A,0
A,1
B,1
A,1
A,0
B,1
B,1
B,1
A,0
A,1
A,0
B,0
B,0
A,0
A,0
A,1
A,1
B,0
A,0
A,0
B,0
B,0
A,0
A,0
A,1
B,0
A,1
A,1
B,0
B,1
A,0
B,1
B,1
A,1
A,1
B,1
B,0
A,0
B,0
B,0
B,0
A,0
B,0
B,0
A,1
A,1
B,0
B,0
A,0
A,0
B,1
B,1
A,0
A,0
A,1
A,0
B,0
A,1
A,0
A,0
B,1
A,0
A,0
B,1
B,0
A,1
A,0
B,0
B,0
B,0
B,0
B,0
B,1
B,0
A,1
B,1
A,0
A,1
B,1
A,0
B,0
B,0
B,0
B,0
A,0
A,1
A,1
B,1
B,0
B,1
A,0
B,0
A,0
A,0
B,0
A,0
B,0
A,0
A,0
A,0
A,1
A,1
B,0
B,1
A,1
B,0
B,0
A,0
A,1
B,1
A,0
B,0
B,0
B,0
A,0
A,0
B,1
A,0
B,1
B,1
B,0
A,1
A,1
B,1
B,0
B,0
A,1
B,0
A,0
B,0
B,1
A,0
B,1
A,0
A,1
A,0
A,0
A,0
B,0
A,0
B,1
B,1
A,0
B,0
A,1
A,0
A,0
B,0
B,1
B,1
A,0
A,0
B,0
A,0
B,1
B,0
B,0
B,0
B,0
A,0
A,0
A,0
A,0
B,1
A,0
A,1
B,0
B,0
A,0
B,0
A,0
B,1
B,0
B,1
A,1
A,1
A,0
A,0
A,0
A,0
A,0
A,1
A,0
B,1
B,0
A,0
A,1
A,0
B,1
A,0
B,0
A,0
B,1
A,0
A,1
B,0
A,0
B,1
B,1
A,1
B,1
B,0
B,0
A,0
A,0
A,0
A,0
B,1
A,0
A,0
B,0
A,1
A,0
B,0
B,0
A,1
B,1
B,0
B,1
B,1
A,0
B,1
B,0
B,1
B,1
B,1
B,0
A,1
A,0
B,0
A,0
A,0
A,0
A,1
A,0
B,0
A,1
B,0
A,0
A,0
B,0
A,0
B,1
A,1
A,0
A,0
B,1
A,1
A,0
A,1
B,0
B,0
A,0
B,0
B,1
A,1
A,1
B,1
B,0
A,1
B,1
A,1
B,0
B,0
A,1
A,0
B,0
B,1
B,1
B,0
A,0
A,0
B,0
B,0
A,0
A,0
A,0
B,0
A,0
B,1
B,0
B,0
A,1
B,1
B,0
B,1
A,0
A,1
B,0
A,0
A,0
B,0
B,0
B,0
A,1
B,0
B,0
B,0
A,0
B,0
A,0
A,1
A,0
B,0
B,1
B,0
A,0
B,0
A,1
A,1
B,0
B,0
B,0
B,0
B,0
A,0
B,1
B,0
B,0
A,0
B,0
A,1
A,0
A,1
A,0
B,0
A,1
B,0
B,0
A,0
A,1
B,0
B,1
A,0
A,0
A,0
B,1
A,1
A,1
B,1
A,1
B,0
A,1
A,0
A,0
B,1
A,0
B,1
A,0
B,0
B,0
B,1
B,0
A,0
A,0
B,1
A,0
A,0
B,0
A,0
A,0
A,0
B,0
B,1
B,0
A,0
B,1
B,0
B,1
A,0
B,1
B,1
B,0
A,0
B,0
B,1
B,0
B,0
A,0
A,0
B,1
B,1
B,0
B,0
A,0
A,1
B,1
A,1
A,1
B,0
B,0
A,0
A,1
A,0
B,0
B,1
A,0
A,0
A,1
B,0
A,0
B,0
B,0
A,0
A,0
B,0
A,0
A,0
A,1
A,0
B,1
B,0
B,0
B,1
B,0
A,0
B,0
A,0
B,0
A,0
B,0
A,1
B,0
A,0
B,1
B,1
A,0
B,0
B,0
A,0
B,0
B,1
A,1
A,0
B,0
B,0
A,0
A,0
B,0
A,1
B,0
B,1
B,0
A,1
B,1
A,1
B,0
B,1
B,1
A,0
B,0
A,0
B,0
B,1
A,1
A,0
A,0
B,0
B,0
A,0
A,0
B,0
A,0
B,0
A,0
A,0
B,0
A,0
A,0
B,1
A,0
A,0
A,0
B,0
A,1
B,0
A,1
A,0
A,1
B,0
A,1
A,0
A,0
A,1
A,1
B,1
A,1
B,1
B,0
B,1
A,1
B,0
A,0
A,1
B,0
A,1
A,0
A,1
A,0
A,0
B,0
A,0
B,0
A,1
A,0
B,0
A,1
B,0
B,0
B,1
B,1
B,1
A,1
B,0
A,0
A,1
B,1
B,1
A,0
B,1
A,0
A,1
B,0
A,1
B,0
A,0
A,0
B,1
A,0
B,0
B,0
A,0
A,0
A,0
A,1
A,0
B,1
B,1
B,1
B,0
A,0
A,0
B,0
A,0
B,1
A,0
A,0
B,0
A,0
A,0
A,1
A,0
A,1
A,0
B,0
A,0
B,0
B,0
A,0
B,0
A,0
B,0
B,0
B,0
B,1
B,0
B,1
A,0
A,0
A,0
A,1
A,0
B,1
A,1
A,1
A,0
B,0
B,0
B,0
A,0
B,1
B,1
A,0
A,0
B,0
A,1
A,1
B,0
B,0
B,0
A,0
B,1
A,0
A,0
A,1
B,1
A,0
B,1
B,0
B,0
B,0
A,0
A,0
B,0
A,0
A,0
B,1
A,1
A,0
B,1
B,1
A,0
A,1
A,0
A,0
B,1
A,0
A,0
B,0
B,0
A,1
A,0
B,0
B,0
A,1
A,0
B,0
B,1
B,1
A,0
A,0
B,0
A,0
A,1
A,0
B,0
B,0
A,0
A,0
B,0
B,1
B,0
A,0
A,1
A,0
A,0
A,0
B,0
A,0
B,1
B,0
B,1
B,0
A,0
B,0
A,1
B,1
A,1
B,0
A,0
A,1
A,0
B,0
B,0
B,0
A,0
A,1
A,0
A,0
A,1
B,0
A,0
A,0
A,0
B,0
B,0
B,1
B,1
B,1
A,0
A,1
B,1
A,0
B,0
B,0
B,1
A,0
A,1
B,0
B,1
B,0
A,1
A,0
A,0
A,1
B,1
B,0
A,0
A,0
B,0
B,1
A,0
A,0
B,1
B,0
B,1
B,0
B,0
B,0
A,1
B,0
B,1
B,0
B,1
A,0
A,1
B,0
A,0
A,0
B,0
A,0
A,0
A,1
B,0
A,0
B,1
A,1
A,0
B,0
A,0
A,0
A,1
B,1
A,0
A,0
A,0
A,0
A,1
B,1
A,0
A,0
B,0
A,0
A,0
B,0
B,0
A,0
B,0
A,1
A,0
A,0
A,1
B,0
B,1
A,0
A,0
B,1
A,1
B,1
A,0
B,0
A,0
A,0
A,0
A,0
A,0
B,0
B,1
B,0
A,0
A,1
A,0
A,0
B,0
A,0
A,0
B,1
B,0
B,0
B,0
B,0
B,0
B,0
A,1
A,1
B,0
A,0
B,1
A,1
B,0
A,0
B,0
B,0
A,1
A,0
B,0
B,1
A,1
A,0
A,0
B,0
A,1
A,0
A,0
A,0
B,0
B,1
B,1
B,1
B,1
A,1
A,0
A,1
A,1
B,0
B,0
B,0
B,0
A,1
A,0
B,0
A,1
B,0
A,0
B,0
B,0
B,0
A,1
B,0
A,0
B,0
A,0
A,1
B,0
A,1
B,0
B,0
B,0
B,0
A,0
A,1
A,1
B,0
B,0
B,0
B,0
B,0
A,0
B,1
B,0
A,0
B,0
A,0
A,0
A,0
A,0
A,1
B,1
B,1
A,1
A,0
A,0
A,0
B,1
B,1
A,0
B,1
B,0
A,0
A,1
A,0
A,1
B,1
A,0
A,1
B,0
B,1
B,0
B,0
A,0
A,0
B,1
A,0
A,0
A,0
A,1
A,1
A,0
A,1
A,0
B,0
A,1
B,0
A,0
A,1
B,1
A,0
A,0
A,0
B,1
B,0
B,0
A,0
B,0
A,0
B,1
A,0
A,0
B,0
A,0
B,0
A,0
B,1
A,0
A,0
A,0
B,0
A,1
B,0
A,0
B,0
A,0
B,1
A,0
A,1
A,0
B,1
B,0
B,1
B,0
B,0
B,0
B,1
A,0
A,0
B,0
A,1
A,0
B,1
B,0
B,0
A,0
B,0
A,0
B,0
B,0
B,0
A,0
B,1
B,0
B,0
B,1
A,0
B,0
B,0
A,0
A,0
A,0
B,1
A,0
B,1
B,0
B,1
A,0
B,0
B,1
B,1
B,0
B,0
A,0
B,0
B,0
B,0
B,0
A,0
A,0
B,1
A,0
B,0
B,1
A,1
A,0
A,0
A,0
A,1
B,0
A,0
A,1
A,1
B,0
A,0
A,1
A,0
A,1
B,0
A,1
B,1
A,0
A,0
A,1
B,1
A,0
A,0
A,0
B,0
A,0
B,0
A,0
A,0
B,1
A,1
B,0
A,0
B,0
B,1
A,0
B,1
A,1
B,1
B,1
A,0
B,0
B,0
B,0
A,1
B,1
A,0
B,0
A,1
B,1
A,0
B,0
B,1
A,0
B,0
A,1
B,0
B,0
B,0
B,0
A,0
A,0
A,0
B,0
A,1
B,0
B,1
B,0
A,0
B,0
B,0
B,0
B,0
B,1
B,0
A,0
A,0
A,0
B,1
B,0
B,1
B,0
A,1
A,1
A,1
B,0
B,0
B,0
B,0
A,0
A,0
B,0
B,1
A,1
A,0
B,0
B,1
A,1
B,1
A,0
B,1
B,0
A,0
A,0
A,1
A,1
A,0
A,0
B,1
A,1
B,0
B,1
A,0
A,1
A,0
A,0
A,0
A,0
B,0
B,0
B,0
B,0
A,0
A,0
A,1
B,0
B,1
A,0
A,0
B,1
B,1
B,1
A,0
A,0
A,0
A,0
A,0
B,0
B,0
A,1
B,0
B,0
A,0
A,0
B,0
A,1
A,0
A,0
A,1
A,0
B,0
B,0
A,0
B,1
A,0
A,0
A,0
A,0
B,0
B,0
A,1
B,1
B,1
A,0
A,0
A,0
B,0
A,0
A,1
A,1
B,0
A,0
A,1
A,0
A,0
A,0
A,0
A,1
A,0
A,0
A,0
A,0
A,0
B,0
A,0
B,0
A,0
B,1
A,0
B,0
A,0
B,1
B,1
B,0
B,0
B,1
A,1
A,0
B,0
B,1
A,0
A,1
B,0
A,0
B,1
A,1
B,0
B,0
B,0
A,0
A,0
A,1
B,0
B,1
B,0
A,0
A,1
B,0
A,1
A,0
A,1
B,0
B,0
A,0
B,0
A,0
B,1
A,0
A,0
A,0
A,1
A,0
B,1
B,1
B,0
A,1
B,1
A,0
A,0
B,0
A,0
A,0
A,0
A,0
A,0
A,1
B,0
A,1
A,0
B,0
B,1
B,0
B,0
A,1
B,0
A,0
B,0
B,0
A,1
B,1
B,1
A,0
A,1
A,0
A,0
B,0
A,1
B,1
A,0
A,1
A,0
A,0
A,1
A,0
B,0
A,1
A,1
A,1
A,0
A,0
A,0
A,0
A,0
B,1
B,0
B,1
B,0
B,0
B,1
B,1
A,0
B,1
B,1
B,0
A,0
B,0
A,0
B,1
B,1
A,0
B,0
A,0
B,0
A,1
B,1
B,0
B,1
B,0
B,0
B,1
A,0
A,0
B,1
B,1
B,1
A,1
B,1
B,0
B,1
A,0
A,0
B,0
B,0
B,0
A,0
A,1
A,0
B,0
A,0
B,1
B,0
B,1
B,1
A,0
B,0
B,0
A,1
A,0
B,0
B,0
A,1
B,1
A,0
A,1
B,1
B,1
A,0
A,0
B,1
B,1
A,1
B,0
B,1
B,0
A,0
A,0
B,0
B,1
B,1
B,0
B,0
A,0
A,0
B,0
A,1
B,0
A,1
A,0
B,1
A,1
A,0
A,0
A,0
B,1
B,1
A,0
B,1
A,1
A,0
B,0
A,1
A,0
A,1
A,1
A,1
B,0
B,0
B,1
B,0
B,0
B,0
B,0
B,1
B,0
A,1
A,0
B,1
A,1
B,0
B,0
B,0
A,0
B,0
A,0
B,1
A,0
B,0
A,0
A,1
B,1
B,1
B,0
B,0
A,0
B,1
A,0
A,1
A,1
B,0
B,0
B,0
B,1
A,1
B,0
B,0
A,0
B,0
A,0
A,1
B,1
B,1
A,0
B,0
B,0
B,1
A,0
A,0
B,0
A,0
B,0
A,0
B,0
A,1
A,0
A,0
B,1
A,0
B,1
B,1
A,0
B,0
B,0
B,0
B,0
B,0
B,1
B,0
A,0
A,0
A,0
A,0
A,0
A,1
A,0
A,0
B,0
B,0
A,0
A,0
A,0
B,1
B,1
A,0
B,0
B,1
A,1
B,0
A,0
B,0
A,0
B,0
B,0
B,0
A,0
B,0
A,0
B,0
A,0
A,0
A,1
A,1
B,0
A,0
B,0
A,1
A,0
B,1
B,0
B,0
A,0
B,0
A,0
A,0
A,0
A,0
B,0
A,0
A,0
A,1
A,0
A,0
A,0
B,0
A,0
B,0
B,1
A,0
A,0
B,0
A,0
A,1
B,1
A,1
B,0
B,0
B,0
B,1
B,0
B,1
B,1
B,1
B,0
A,0
A,1
A,0
B,0
A,0
B,0
B,1
A,1
B,0
A,1
B,1
A,0
A,1
A,1
A,0
B,0
A,0
A,0
A,0
A,1
A,0
B,1
B,1
B,0
B,0
B,1
B,0
B,0
B,0
A,0
A,0
B,0
A,0
B,0
A,0
B,0
B,1
A,0
B,0
B,0
B,0
B,1
B,0
B,1
B,1
B,1
B,1
A,1
B,1
A,0
B,0
B,1
B,0
B,1
A,0
A,1
A,0
B,0
A,0
A,0
B,0
B,1
A,0
B,0
B,1
A,0
A,0
A,0
B,0
A,0
B,0
A,0
B,0
A,0
A,0
A,0
A,0
B,0
B,1
A,0
B,0
B,0
A,0
B,1
B,0
B,1
A,0
B,1
B,1
B,1
B,1
B,0
B,0
B,0
B,0
B,1
B,0
B,0
B,0
A,0
A,0
A,0
A,0
B,1
B,0
A,0
B,0
A,0
B,1
A,0
A,1
B,1
B,0
A,1
B,0
B,0
B,0
A,0
A,1
B,0
B,0
B,1
A,0
A,0
B,1
B,1
A,0
A,0
A,0
A,1
A,0
B,0
B,0
A,1
B,1
A,1
B,0
A,0
B,0
A,0
A,1
B,0
A,0
B,0
B,1
B,0
A,1
A,1
B,1
A,0
B,1
B,0
A,0
B,0
A,0
A,1
A,1
A,0
B,0
A,0
A,0
B,0
A,1
A,0
A,0
B,1
B,0
B,0
B,0
B,0
B,1
A,1
B,0
A,0
B,0
A,1
B,0
B,0
B,1
B,1
B,1
B,0
A,0
B,0
B,0
A,0
B,0
B,1
B,0
B,0
A,0
B,0
A,0
B,1
B,0
A,1
A,0
B,1
B,0
B,1
A,0
A,1
B,0
B,1
B,0
B,0
A,0
B,1
A,0
B,1
A,0
B,0
B,0
B,0
B,0
B,1
A,0
B,1
B,0
B,1
B,0
B,0
A,0
B,1
A,0
A,0
B,1
B,1
A,0
B,0
B,0
B,0
B,1
A,1
B,0
B,0
B,1
A,1
B,0
A,0
A,1
B,1
B,0
B,0
B,0
A,1
A,0
A,0
A,1
B,1
A,0
A,0
A,1
B,0
A,0
A,0
B,1
B,0
A,0
B,1
A,0
B,0
B,0
A,0
B,1
B,1
A,0
B,1
B,1
A,1
A,0
A,0
B,0
B,0
B,0
B,1
B,1
B,0
A,0
B,0
A,0
A,0
A,0
A,0
A,1
B,1
A,1
A,0
B,1
A,0
B,1
A,0
A,0
B,0
B,1
A,0
A,1
B,0
A,0
A,1
B,0
A,0
B,0
B,1
B,0
B,0
A,0
B,0
A,0
A,1
B,1
A,1
B,0
B,0
B,0
A,1
B,0
A,1
B,1
B,1
B,1
A,0
A,1
A,0
B,1
B,0
B,1
A,0
B,0
A,1
A,1
A,1
A,0
A,0
A,0
A,1
B,1
B,1
A,0
B,0
B,0
A,1
A,1
A,0
B,0
A,0
A,1
B,0
A,1
B,0
A,0
B,0
A,0
A,0
B,0
B,0
A,0
A,0
B,0
A,1
A,0
B,1
B,0
A,0
A,1
B,1
B,1
B,1
A,0
A,0
B,0
A,1
A,0
B,0
A,0
A,0
B,0
B,1
B,0
A,0
B,1
A,1
B,1
B,0
A,1
B,0
A,0
A,0
B,0
A,0
B,0
A,0
A,0
B,0
A,0
B,0
B,0
B,1
B,0
A,1
A,0
A,0
B,1
B,0
B,0
A,0
B,1
B,1
B,1
B,1
A,1
A,0
A,1
A,0
A,0
A,0
A,0
B,0
B,0
A,0
A,0
A,0
B,1
B,0
B,1
A,0
A,1
B,0
A,0
B,0
B,1
A,0
A,1
A,1
A,0
A,0
A,0
A,1
A,0
A,0
A,0
A,1
A,0


================================================
FILE: ab_testing/bayesian_bandit.py
================================================
# From the course: Bayesin Machine Learning in Python: A/B Testing
# https://deeplearningcourses.com/c/bayesian-machine-learning-in-python-ab-testing
# https://www.udemy.com/bayesian-machine-learning-in-python-ab-testing
from __future__ import print_function, division
from builtins import range
# Note: you may need to update your version of future
# sudo pip install -U future


import matplotlib.pyplot as plt
import numpy as np
from scipy.stats import beta


# np.random.seed(2)
NUM_TRIALS = 2000
BANDIT_PROBABILITIES = [0.2, 0.5, 0.75]


class Bandit:
  def __init__(self, p):
    self.p = p
    self.a = 1
    self.b = 1
    self.N = 0 # for information only

  def pull(self):
    return np.random.random() < self.p

  def sample(self):
    return np.random.beta(self.a, self.b)

  def update(self, x):
    self.a += x
    self.b += 1 - x
    self.N += 1


def plot(bandits, trial):
  x = np.linspace(0, 1, 200)
  for b in bandits:
    y = beta.pdf(x, b.a, b.b)
    plt.plot(x, y, label=f"real p: {b.p:.4f}, win rate = {b.a - 1}/{b.N}")
  plt.title(f"Bandit distributions after {trial} trials")
  plt.legend()
  plt.show()


def experiment():
  bandits = [Bandit(p) for p in BANDIT_PROBABILITIES]

  sample_points = [5,10,20,50,100,200,500,1000,1500,1999]
  rewards = np.zeros(NUM_TRIALS)
  for i in range(NUM_TRIALS):
    # Thompson sampling
    j = np.argmax([b.sample() for b in bandits])

    # plot the posteriors
    if i in sample_points:
      plot(bandits, i)

    # pull the arm for the bandit with the largest sample
    x = bandits[j].pull()

    # update rewards
    rewards[i] = x

    # update the distribution for the bandit whose arm we just pulled
    bandits[j].update(x)

  # print total reward
  print("total reward earned:", rewards.sum())
  print("overall win rate:", rewards.sum() / NUM_TRIALS)
  print("num times selected each bandit:", [b.N for b in bandits])


if __name__ == "__main__":
  experiment()


================================================
FILE: ab_testing/bayesian_normal.py
================================================
# https://deeplearningcourses.com/c/artificial-intelligence-reinforcement-learning-in-python
# https://www.udemy.com/artificial-intelligence-reinforcement-learning-in-python
from __future__ import print_function, division
from builtins import range
# Note: you may need to update your version of future
# sudo pip install -U future

import numpy as np
import matplotlib.pyplot as plt
from scipy.stats import norm


np.random.seed(1)
NUM_TRIALS = 2000
BANDIT_MEANS = [1, 2, 3]


class Bandit:
  def __init__(self, true_mean):
    self.true_mean = true_mean
    # parameters for mu - prior is N(0,1)
    self.m = 0
    self.lambda_ = 1
    self.tau = 1
    self.N = 0

  def pull(self):
    return np.random.randn() / np.sqrt(self.tau) + self.true_mean

  def sample(self):
    return np.random.randn() / np.sqrt(self.lambda_) + self.m

  def update(self, x):
    self.m = (self.tau * x + self.lambda_ * self.m) / (self.tau + self.lambda_)
    self.lambda_ += self.tau
    self.N += 1


def plot(bandits, trial):
  x = np.linspace(-3, 6, 200)
  for b in bandits:
    y = norm.pdf(x, b.m, np.sqrt(1. / b.lambda_))
    plt.plot(x, y, label=f"real mean: {b.true_mean:.4f}, num plays: {b.N}")
  plt.title(f"Bandit distributions after {trial} trials")
  plt.legend()
  plt.show()


def run_experiment():
  bandits = [Bandit(m) for m in BANDIT_MEANS]

  sample_points = [5,10,20,50,100,200,500,1000,1500,1999]
  rewards = np.empty(NUM_TRIALS)
  for i in range(NUM_TRIALS):
    # Thompson sampling
    j = np.argmax([b.sample() for b in bandits])

    # plot the posteriors
    if i in sample_points:
      plot(bandits, i)

    # pull the arm for the bandit with the largest sample
    x = bandits[j].pull()

    # update the distribution for the bandit whose arm we just pulled
    bandits[j].update(x)

    # update rewards
    rewards[i] = x

  cumulative_average = np.cumsum(rewards) / (np.arange(NUM_TRIALS) + 1)

  # plot moving average ctr
  plt.plot(cumulative_average)
  for m in BANDIT_MEANS:
    plt.plot(np.ones(NUM_TRIALS)*m)
  plt.show()

  return cumulative_average

if __name__ == '__main__':
  run_experiment()




================================================
FILE: ab_testing/bayesian_starter.py
================================================
# From the course: Bayesin Machine Learning in Python: A/B Testing
# https://deeplearningcourses.com/c/bayesian-machine-learning-in-python-ab-testing
# https://www.udemy.com/bayesian-machine-learning-in-python-ab-testing
from __future__ import print_function, division
from builtins import range
# Note: you may need to update your version of future
# sudo pip install -U future


import matplotlib.pyplot as plt
import numpy as np
from scipy.stats import beta


# np.random.seed(2)
NUM_TRIALS = 2000
BANDIT_PROBABILITIES = [0.2, 0.5, 0.75]


class Bandit:
  def __init__(self, p):
    self.p = p
    self.a = # TODO
    self.b = # TODO
    self.N = 0 # for information only

  def pull(self):
    return np.random.random() < self.p

  def sample(self):
    return # TODO - draw a sample from Beta(a, b)

  def update(self, x):
    self.a = # TODO
    self.b = # TODO
    self.N += 1


def plot(bandits, trial):
  x = np.linspace(0, 1, 200)
  for b in bandits:
    y = beta.pdf(x, b.a, b.b)
    plt.plot(x, y, label=f"real p: {b.p:.4f}, win rate = {b.a - 1}/{b.N}")
  plt.title(f"Bandit distributions after {trial} trials")
  plt.legend()
  plt.show()


def experiment():
  bandits = [Bandit(p) for p in BANDIT_PROBABILITIES]

  sample_points = [5,10,20,50,100,200,500,1000,1500,1999]
  rewards = np.zeros(NUM_TRIALS)
  for i in range(NUM_TRIALS):
    # Thompson sampling
    j = # TODO

    # plot the posteriors
    if i in sample_points:
      plot(bandits, i)

    # pull the arm for the bandit with the largest sample
    x = bandits[j].pull()

    # update rewards
    rewards[i] = x

    # update the distribution for the bandit whose arm we just pulled
    bandits[j].update(x)

  # print total reward
  print("total reward earned:", rewards.sum())
  print("overall win rate:", rewards.sum() / NUM_TRIALS)
  print("num times selected each bandit:", [b.N for b in bandits])


if __name__ == "__main__":
  experiment()


================================================
FILE: ab_testing/cdfs_and_percentiles.py
================================================
import numpy as np
import matplotlib.pyplot as plt
from scipy.stats import norm


mu = 170
sd = 7


# generate samples from our distribution
x = norm.rvs(loc=mu, scale=sd, size=100)

# maximum likelihood mean
x.mean()

# maximum likelihood variance
x.var()

# maximum likelihood std
x.std()

# unbiased variance
x.var(ddof=1)

# unbiased std
x.std(ddof=1)

# at what height are you in the 95th percentile?
norm.ppf(0.95, loc=mu, scale=sd)

# you are 160 cm tall, what percentile are you in?
norm.cdf(160, loc=mu, scale=sd)

# you are 180 cm tall, what is the probability that someone is taller than you?
1 - norm.cdf(180, loc=mu, scale=sd)

================================================
FILE: ab_testing/chisquare.py
================================================
# From the course: Bayesin Machine Learning in Python: A/B Testing
# https://deeplearningcourses.com/c/bayesian-machine-learning-in-python-ab-testing
# https://www.udemy.com/bayesian-machine-learning-in-python-ab-testing
from __future__ import print_function, division
from builtins import range
# Note: you may need to update your version of future
# sudo pip install -U future


import numpy as np
import matplotlib.pyplot as plt
from scipy.stats import chi2, chi2_contingency

# contingency table
#        click       no click
#------------------------------
# ad A |   a            b
# ad B |   c            d
#
# chi^2 = (ad - bc)^2 (a + b + c + d) / [ (a + b)(c + d)(a + c)(b + d)]
# degrees of freedom = (#cols - 1) x (#rows - 1) = (2 - 1)(2 - 1) = 1

# short example

# T = np.array([[36, 14], [30, 25]])
# c2 = np.linalg.det(T)**2 * T.sum() / ( T[0].sum()*T[1].sum()*T[:,0].sum()*T[:,1].sum() )
# p_value = 1 - chi2.cdf(x=c2, df=1)

# equivalent:
# (36-31.429)**2/31.429+(14-18.571)**2/18.571 + (30-34.571)**2/34.571 + (25-20.429)**2/20.429


class DataGenerator:
  def __init__(self, p1, p2):
    self.p1 = p1
    self.p2 = p2

  def next(self):
    click1 = 1 if (np.random.random() < self.p1) else 0
    click2 = 1 if (np.random.random() < self.p2) else 0
    return click1, click2


def get_p_value(T):
  # same as scipy.stats.chi2_contingency(T, correction=False)
  det = T[0,0]*T[1,1] - T[0,1]*T[1,0]
  c2 = float(det) / T[0].sum() * det / T[1].sum() * T.sum() / T[:,0].sum() / T[:,1].sum()
  p = 1 - chi2.cdf(x=c2, df=1)
  return p


def run_experiment(p1, p2, N):
  data = DataGenerator(p1, p2)
  p_values = np.empty(N)
  T = np.zeros((2, 2)).astype(np.float32)
  for i in range(N):
    c1, c2 = data.next()
    T[0,c1] += 1
    T[1,c2] += 1
    # ignore the first 10 values
    if i < 10:
      p_values[i] = None
    else:
      p_values[i] = get_p_value(T)
  plt.plot(p_values)
  plt.plot(np.ones(N)*0.05)
  plt.show()

run_experiment(0.1, 0.11, 20000)


================================================
FILE: ab_testing/ci_comparison.py
================================================
# From the course: Bayesin Machine Learning in Python: A/B Testing
# https://deeplearningcourses.com/c/bayesian-machine-learning-in-python-ab-testing
# https://www.udemy.com/bayesian-machine-learning-in-python-ab-testing
from __future__ import print_function, division
from builtins import range
# Note: you may need to update your version of future
# sudo pip install -U future


import matplotlib.pyplot as plt
import numpy as np
from scipy.stats import beta, norm

T = 501 # number of coin tosses
true_ctr = 0.5
a, b = 1, 1 # beta priors
plot_indices = (10, 20, 30, 50, 100, 200, 500)
data = np.empty(T)
for i in range(T):
  x = 1 if np.random.random() < true_ctr else 0
  data[i] = x

  # update a and b
  a += x
  b += 1 - x

  if i in plot_indices:
    # maximum likelihood estimate of ctr
    p = data[:i].mean()
    n = i + 1 # number of samples collected so far
    std = np.sqrt(p*(1-p)/n)
    
    # gaussian
    x = np.linspace(0, 1, 200)
    g = norm.pdf(x, loc=p, scale=std)
    plt.plot(x, g, label='Gaussian Approximation')

    # beta
    posterior = beta.pdf(x, a=a, b=b)
    plt.plot(x, posterior, label='Beta Posterior')
    plt.legend()
    plt.title("N = %s" % n)
    plt.show()


================================================
FILE: ab_testing/client.py
================================================
# From the course: Bayesin Machine Learning in Python: A/B Testing
# https://deeplearningcourses.com/c/bayesian-machine-learning-in-python-ab-testing
# https://www.udemy.com/bayesian-machine-learning-in-python-ab-testing
from __future__ import print_function, division
from builtins import range
# Note: you may need to update your version of future
# sudo pip install -U future


import requests
import numpy as np
import pandas as pd
from scipy import stats

# get data
df = pd.read_csv('advertisement_clicks.csv')
a = df[df['advertisement_id'] == 'A']
b = df[df['advertisement_id'] == 'B']
a = a['action'].values
b = b['action'].values

print("a.mean:", a.mean())
print("b.mean:", b.mean())


i = 0
j = 0
count = 0
while i < len(a) and j < len(b):
  # quit when there's no data left for either ad
  r = requests.get('http://localhost:8888/get_ad')
  # print(r.content)
  r = r.json()
  if r['advertisement_id'] == 'A':
    action = a[i]
    i += 1
  else:
    action = b[j]
    j += 1

  if action == 1:
    # only click the ad if our dataset determines that we should
    requests.post(
      'http://localhost:8888/click_ad',
      data={'advertisement_id': r['advertisement_id']}
    )

  # log some stats
  count += 1
  if count % 50 == 0:
    print("Seen %s ads, A: %s, B: %s" % (count, i, j))


================================================
FILE: ab_testing/comparing_epsilons.py
================================================
# https://deeplearningcourses.com/c/artificial-intelligence-reinforcement-learning-in-python
# https://www.udemy.com/artificial-intelligence-reinforcement-learning-in-python
from __future__ import print_function, division
from builtins import range
# Note: you may need to update your version of future
# sudo pip install -U future

import numpy as np
import matplotlib.pyplot as plt


class BanditArm:
  def __init__(self, m):
    self.m = m
    self.m_estimate = 0
    self.N = 0

  def pull(self):
    return np.random.randn() + self.m

  def update(self, x):
    self.N += 1
    self.m_estimate = (1 - 1.0/self.N)*self.m_estimate + 1.0/self.N*x


def run_experiment(m1, m2, m3, eps, N):
  bandits = [BanditArm(m1), BanditArm(m2), BanditArm(m3)]

  # count number of suboptimal choices
  means = np.array([m1, m2, m3])
  true_best = np.argmax(means)
  count_suboptimal = 0

  data = np.empty(N)
  
  for i in range(N):
    # epsilon greedy
    p = np.random.random()
    if p < eps:
      j = np.random.choice(len(bandits))
    else:
      j = np.argmax([b.m_estimate for b in bandits])
    x = bandits[j].pull()
    bandits[j].update(x)

    if j != true_best:
      count_suboptimal += 1

    # for the plot
    data[i] = x
  cumulative_average = np.cumsum(data) / (np.arange(N) + 1)

  # plot moving average ctr
  plt.plot(cumulative_average)
  plt.plot(np.ones(N)*m1)
  plt.plot(np.ones(N)*m2)
  plt.plot(np.ones(N)*m3)
  plt.xscale('log')
  plt.show()

  for b in bandits:
    print(b.m_estimate)

  print("percent suboptimal for epsilon = %s:" % eps, float(count_suboptimal) / N)

  return cumulative_average

if __name__ == '__main__':
  m1, m2, m3 = 1.5, 2.5, 3.5
  c_1 = run_experiment(m1, m2, m3, 0.1, 100000)
  c_05 = run_experiment(m1, m2, m3, 0.05, 100000)
  c_01 = run_experiment(m1, m2, m3, 0.01, 100000)

  # log scale plot
  plt.plot(c_1, label='eps = 0.1')
  plt.plot(c_05, label='eps = 0.05')
  plt.plot(c_01, label='eps = 0.01')
  plt.legend()
  plt.xscale('log')
  plt.show()


  # linear plot
  plt.plot(c_1, label='eps = 0.1')
  plt.plot(c_05, label='eps = 0.05')
  plt.plot(c_01, label='eps = 0.01')
  plt.legend()
  plt.show()



================================================
FILE: ab_testing/convergence.py
================================================
# From the course: Bayesin Machine Learning in Python: A/B Testing
# https://deeplearningcourses.com/c/bayesian-machine-learning-in-python-ab-testing
# https://www.udemy.com/bayesian-machine-learning-in-python-ab-testing
from __future__ import print_function, division
from builtins import range
# Note: you may need to update your version of future
# sudo pip install -U future


import matplotlib.pyplot as plt
import numpy as np
from bayesian_bandit import Bandit


def run_experiment(p1, p2, p3, N):
  bandits = [Bandit(p1), Bandit(p2), Bandit(p3)]

  data = np.empty(N)
  
  for i in range(N):
    # thompson sampling
    j = np.argmax([b.sample() for b in bandits])
    x = bandits[j].pull()
    bandits[j].update(x)

    # for the plot
    data[i] = x
  cumulative_average_ctr = np.cumsum(data) / (np.arange(N) + 1)

  # plot moving average ctr
  plt.plot(cumulative_average_ctr)
  plt.plot(np.ones(N)*p1)
  plt.plot(np.ones(N)*p2)
  plt.plot(np.ones(N)*p3)
  plt.ylim((0,1))
  plt.xscale('log')
  plt.show()


run_experiment(0.2, 0.25, 0.3, 100000)

================================================
FILE: ab_testing/demo.py
================================================
# From the course: Bayesin Machine Learning in Python: A/B Testing
# https://deeplearningcourses.com/c/bayesian-machine-learning-in-python-ab-testing
# https://www.udemy.com/bayesian-machine-learning-in-python-ab-testing
from __future__ import print_function, division
from builtins import range
# Note: you may need to update your version of future
# sudo pip install -U future


import numpy as np
import matplotlib.pyplot as plt
from scipy.stats import beta

def plot(a, b, trial, ctr):
  x = np.linspace(0, 1, 200)
  y = beta.pdf(x, a, b)
  mean = float(a) / (a + b)
  plt.plot(x, y)
  plt.title("Distributions after %s trials, true rate = %.1f, mean = %.2f" % (trial, ctr, mean))
  plt.show()

true_ctr = 0.3
a, b = 1, 1 # beta parameters
show = [0, 5, 10, 25, 50, 100, 200, 300, 500, 700, 1000, 1500]
for t in range(1501):
  coin_toss_result = (np.random.random() < true_ctr)
  if coin_toss_result:
    a += 1
  else:
    b += 1

  if t in show:
    plot(a, b, t+1, true_ctr)


================================================
FILE: ab_testing/epsilon_greedy.py
================================================
# From the course: Bayesin Machine Learning in Python: A/B Testing
# https://deeplearningcourses.com/c/bayesian-machine-learning-in-python-ab-testing
# https://www.udemy.com/bayesian-machine-learning-in-python-ab-testing
from __future__ import print_function, division
from builtins import range
# Note: you may need to update your version of future
# sudo pip install -U future


import matplotlib.pyplot as plt
import numpy as np


NUM_TRIALS = 10000
EPS = 0.1
BANDIT_PROBABILITIES = [0.2, 0.5, 0.75]


class BanditArm:
  def __init__(self, p):
    # p: the win rate
    self.p = p
    self.p_estimate = 0.
    self.N = 0. # num samples collected so far

  def pull(self):
    # draw a 1 with probability p
    return np.random.random() < self.p

  def update(self, x):
    self.N += 1.
    self.p_estimate = ((self.N - 1)*self.p_estimate + x) / self.N


def choose_random_argmax(a):
  idx = np.argwhere(np.amax(a) == a).flatten()
  return np.random.choice(idx)


def experiment():
  bandits = [BanditArm(p) for p in BANDIT_PROBABILITIES]

  rewards = np.zeros(NUM_TRIALS)
  num_times_explored = 0
  num_times_exploited = 0
  num_optimal = 0
  optimal_j = np.argmax([b.p for b in bandits])
  print("optimal j:", optimal_j)

  for i in range(NUM_TRIALS):

    # use epsilon-greedy to select the next bandit
    if np.random.random() < EPS:
      num_times_explored += 1
      j = np.random.randint(len(bandits))
    else:
      num_times_exploited += 1
      j = choose_random_argmax([b.p_estimate for b in bandits])

    if j == optimal_j:
      num_optimal += 1

    # pull the arm for the bandit with the largest sample
    x = bandits[j].pull()

    # update rewards log
    rewards[i] = x

    # update the distribution for the bandit whose arm we just pulled
    bandits[j].update(x)

    

  # print mean estimates for each bandit
  for b in bandits:
    print("mean estimate:", b.p_estimate)

  # print total reward
  print("total reward earned:", rewards.sum())
  print("overall win rate:", rewards.sum() / NUM_TRIALS)
  print("num_times_explored:", num_times_explored)
  print("num_times_exploited:", num_times_exploited)
  print("num times selected optimal bandit:", num_optimal)

  # plot the results
  cumulative_rewards = np.cumsum(rewards)
  win_rates = cumulative_rewards / (np.arange(NUM_TRIALS) + 1)
  plt.plot(win_rates)
  plt.plot(np.ones(NUM_TRIALS)*np.max(BANDIT_PROBABILITIES))
  plt.show()

if __name__ == "__main__":
  experiment()


================================================
FILE: ab_testing/epsilon_greedy_starter.py
================================================
# From the course: Bayesin Machine Learning in Python: A/B Testing
# https://deeplearningcourses.com/c/bayesian-machine-learning-in-python-ab-testing
# https://www.udemy.com/bayesian-machine-learning-in-python-ab-testing
from __future__ import print_function, division
from builtins import range
# Note: you may need to update your version of future
# sudo pip install -U future


import matplotlib.pyplot as plt
import numpy as np


NUM_TRIALS = 10000
EPS = 0.1
BANDIT_PROBABILITIES = [0.2, 0.5, 0.75]


class BanditArm:
  def __init__(self, p):
    # p: the win rate
    self.p = p
    self.p_estimate = # TODO
    self.N = # TODO

  def pull(self):
    # draw a 1 with probability p
    return np.random.random() < self.p

  def update(self, x):
    self.N = # TODO
    self.p_estimate = # TODO


def experiment():
  bandits = [BanditArm(p) for p in BANDIT_PROBABILITIES]

  rewards = np.zeros(NUM_TRIALS)
  num_times_explored = 0
  num_times_exploited = 0
  num_optimal = 0
  optimal_j = np.argmax([b.p for b in bandits])
  print("optimal j:", optimal_j)

  for i in range(NUM_TRIALS):

    # use epsilon-greedy to select the next bandit
    if np.random.random() < EPS:
      num_times_explored += 1
      j = # TODO
    else:
      num_times_exploited += 1
      j = # TODO

    if j == optimal_j:
      num_optimal += 1

    # pull the arm for the bandit with the largest sample
    x = bandits[j].pull()

    # update rewards log
    rewards[i] = x

    # update the distribution for the bandit whose arm we just pulled
    bandits[j].update(x)

    

  # print mean estimates for each bandit
  for b in bandits:
    print("mean estimate:", b.p_estimate)

  # print total reward
  print("total reward earned:", rewards.sum())
  print("overall win rate:", rewards.sum() / NUM_TRIALS)
  print("num_times_explored:", num_times_explored)
  print("num_times_exploited:", num_times_exploited)
  print("num times selected optimal bandit:", num_optimal)

  # plot the results
  cumulative_rewards = np.cumsum(rewards)
  win_rates = cumulative_rewards / (np.arange(NUM_TRIALS) + 1)
  plt.plot(win_rates)
  plt.plot(np.ones(NUM_TRIALS)*np.max(BANDIT_PROBABILITIES))
  plt.show()

if __name__ == "__main__":
  experiment()


================================================
FILE: ab_testing/ex_chisq.py
================================================
# From the course: Bayesin Machine Learning in Python: A/B Testing
# https://deeplearningcourses.com/c/bayesian-machine-learning-in-python-ab-testing
# https://www.udemy.com/bayesian-machine-learning-in-python-ab-testing
from __future__ import print_function, division
from builtins import range
# Note: you may need to update your version of future
# sudo pip install -U future


import numpy as np
import pandas as pd
from scipy.stats import chi2, chi2_contingency

# contingency table
#        click       no click
#------------------------------
# ad A |   a            b
# ad B |   c            d


def get_p_value(T):
  # same as scipy.stats.chi2_contingency(T, correction=False)
  det = T[0,0]*T[1,1] - T[0,1]*T[1,0]
  c2 = float(det) / T[0].sum() * det / T[1].sum() * T.sum() / T[:,0].sum() / T[:,1].sum()
  p = 1 - chi2.cdf(x=c2, df=1)
  return p

# get data
df = pd.read_csv('advertisement_clicks.csv')
a = df[df['advertisement_id'] == 'A']
b = df[df['advertisement_id'] == 'B']
a = a['action']
b = b['action']

A_clk = a.sum()
A_noclk = a.size - a.sum()
B_clk = b.sum()
B_noclk = b.size - b.sum()

T = np.array([[A_clk, A_noclk], [B_clk, B_noclk]])

print(get_p_value(T))

================================================
FILE: ab_testing/ex_ttest.py
================================================
# From the course: Bayesin Machine Learning in Python: A/B Testing
# https://deeplearningcourses.com/c/bayesian-machine-learning-in-python-ab-testing
# https://www.udemy.com/bayesian-machine-learning-in-python-ab-testing
from __future__ import print_function, division
from builtins import range
# Note: you may need to update your version of future
# sudo pip install -U future


import numpy as np
import pandas as pd
from scipy import stats

# get data
df = pd.read_csv('advertisement_clicks.csv')
a = df[df['advertisement_id'] == 'A']
b = df[df['advertisement_id'] == 'B']
a = a['action']
b = b['action']

print("a.mean:", a.mean())
print("b.mean:", b.mean())

# built-in t-test:
t, p = stats.ttest_ind(a, b)
print("t:\t", t, "p:\t", p)

# welch's t-test:
t, p = stats.ttest_ind(a, b, equal_var=False)
print("Welch's t-test:")
print("t:\t", t, "p:\t", p)

# welch's t-test manual:
N1 = len(a)
s1_sq = a.var()
N2 = len(b)
s2_sq = b.var()
t = (a.mean() - b.mean()) / np.sqrt(s1_sq / N1 + s2_sq / N2)

nu1 = N1 - 1
nu2 = N2 - 1
df = (s1_sq / N1 + s2_sq / N2)**2 / ( (s1_sq*s1_sq) / (N1*N1 * nu1) + (s2_sq*s2_sq) / (N2*N2 * nu2) )
p = (1 - stats.t.cdf(np.abs(t), df=df))*2
print("Manual Welch t-test")
print("t:\t", t, "p:\t", p)

================================================
FILE: ab_testing/extra_reading.txt
================================================
The Unbiased Estimate of the Covariance Matrix
https://lazyprogrammer.me/covariance-matrix-divide-by-n-or-n-1/

Algorithms for the multi-armed bandit problem
https://www.cs.mcgill.ca/~vkules/bandits.pdf

UCB REVISITED: IMPROVED REGRET BOUNDS FOR THE STOCHASTIC MULTI-ARMED BANDIT PROBLEM
http://personal.unileoben.ac.at/rortner/Pubs/UCBRev.pdf

Finite-time Analysis of the Multiarmed Bandit Problem
https://link.springer.com/article/10.1023/A:1013689704352

A Tutorial on Thompson Sampling
https://web.stanford.edu/~bvr/pubs/TS_Tutorial.pdf

An Empirical Evaluation of Thompson Sampling
https://papers.nips.cc/paper/4321-an-empirical-evaluation-of-thompson-sampling.pdf

Analysis of Thompson Sampling for the Multi-armed Bandit Problem
http://proceedings.mlr.press/v23/agrawal12/agrawal12.pdf

================================================
FILE: ab_testing/optimistic.py
================================================
# From the course: Bayesin Machine Learning in Python: A/B Testing
# https://deeplearningcourses.com/c/bayesian-machine-learning-in-python-ab-testing
# https://www.udemy.com/bayesian-machine-learning-in-python-ab-testing
from __future__ import print_function, division
from builtins import range
# Note: you may need to update your version of future
# sudo pip install -U future


import matplotlib.pyplot as plt
import numpy as np


NUM_TRIALS = 10000
EPS = 0.1
BANDIT_PROBABILITIES = [0.2, 0.5, 0.75]


class Bandit:
  def __init__(self, p):
    # p: the win rate
    self.p = p
    self.p_estimate = 5.
    self.N = 1. # num samples collected so far

  def pull(self):
    # draw a 1 with probability p
    return np.random.random() < self.p

  def update(self, x):
    self.N += 1.
    self.p_estimate = ((self.N - 1)*self.p_estimate + x) / self.N


def experiment():
  bandits = [Bandit(p) for p in BANDIT_PROBABILITIES]

  rewards = np.zeros(NUM_TRIALS)
  for i in range(NUM_TRIALS):
    # use optimistic initial values to select the next bandit
    j = np.argmax([b.p_estimate for b in bandits])

    # pull the arm for the bandit with the largest sample
    x = bandits[j].pull()

    # update rewards log
    rewards[i] = x

    # update the distribution for the bandit whose arm we just pulled
    bandits[j].update(x)


  # print mean estimates for each bandit
  for b in bandits:
    print("mean estimate:", b.p_estimate)

  # print total reward
  print("total reward earned:", rewards.sum())
  print("overall win rate:", rewards.sum() / NUM_TRIALS)
  print("num times selected each bandit:", [b.N for b in bandits])

  # plot the results
  cumulative_rewards = np.cumsum(rewards)
  win_rates = cumulative_rewards / (np.arange(NUM_TRIALS) + 1)
  plt.ylim([0, 1])
  plt.plot(win_rates)
  plt.plot(np.ones(NUM_TRIALS)*np.max(BANDIT_PROBABILITIES))
  plt.show()

if __name__ == "__main__":
  experiment()


================================================
FILE: ab_testing/optimistic_starter.py
================================================
# From the course: Bayesin Machine Learning in Python: A/B Testing
# https://deeplearningcourses.com/c/bayesian-machine-learning-in-python-ab-testing
# https://www.udemy.com/bayesian-machine-learning-in-python-ab-testing
from __future__ import print_function, division
from builtins import range
# Note: you may need to update your version of future
# sudo pip install -U future


import matplotlib.pyplot as plt
import numpy as np


NUM_TRIALS = 10000
EPS = 0.1
BANDIT_PROBABILITIES = [0.2, 0.5, 0.75]


class Bandit:
  def __init__(self, p):
    # p: the win rate
    self.p = p
    self.p_estimate = # TODO
    self.N = # TODO

  def pull(self):
    # draw a 1 with probability p
    return np.random.random() < self.p

  def update(self, x):
    # TODO
    self.p_estimate = # TODO


def experiment():
  bandits = [Bandit(p) for p in BANDIT_PROBABILITIES]

  rewards = np.zeros(NUM_TRIALS)
  for i in range(NUM_TRIALS):
    # use optimistic initial values to select the next bandit
    j = # TODO

    # pull the arm for the bandit with the largest sample
    x = bandits[j].pull()

    # update rewards log
    rewards[i] = x

    # update the distribution for the bandit whose arm we just pulled
    bandits[j].update(x)


  # print mean estimates for each bandit
  for b in bandits:
    print("mean estimate:", b.p_estimate)

  # print total reward
  print("total reward earned:", rewards.sum())
  print("overall win rate:", rewards.sum() / NUM_TRIALS)
  print("num times selected each bandit:", [b.N for b in bandits])

  # plot the results
  cumulative_rewards = np.cumsum(rewards)
  win_rates = cumulative_rewards / (np.arange(NUM_TRIALS) + 1)
  plt.ylim([0, 1])
  plt.plot(win_rates)
  plt.plot(np.ones(NUM_TRIALS)*np.max(BANDIT_PROBABILITIES))
  plt.show()

if __name__ == "__main__":
  experiment()


================================================
FILE: ab_testing/server_solution.py
================================================
# From the course: Bayesin Machine Learning in Python: A/B Testing
# https://deeplearningcourses.com/c/bayesian-machine-learning-in-python-ab-testing
# https://www.udemy.com/bayesian-machine-learning-in-python-ab-testing
from __future__ import print_function, division
from builtins import range
# Note: you may need to update your version of future
# sudo pip install -U future


import numpy as np
from flask import Flask, jsonify, request
from scipy.stats import beta

# create an app
app = Flask(__name__)


# define bandit
# there's no "pull arm" here
# since that's technically now the user/client
class Bandit:
  def __init__(self, name):
    self.clks = 0
    self.views = 0
    self.name = name

  def sample(self):
    # Beta(1, 1) is the prior
    a = 1 + self.clks
    b = 1 + self.views - self.clks
    return np.random.beta(a, b)

  def add_click(self):
    self.clks += 1

  def add_view(self):
    self.views += 1

    # print some helpful stats
    if self.views % 50 == 0:
      print("%s: clks=%s, views=%s" % (self.name, self.clks, self.views))


# initialize bandits
banditA = Bandit('A')
banditB = Bandit('B')



@app.route('/get_ad')
def get_ad():
  if banditA.sample() > banditB.sample():
    ad = 'A'
    banditA.add_view()
  else:
    ad = 'B'
    banditB.add_view()
  return jsonify({'advertisement_id': ad})


@app.route('/click_ad', methods=['POST'])
def click_ad():
  result = 'OK'
  if request.form['advertisement_id'] == 'A':
    banditA.add_click()
  elif request.form['advertisement_id'] == 'B':
    banditB.add_click()
  else:
    result = 'Invalid Input.'

  # nothing to return really
  return jsonify({'result': result})


if __name__ == '__main__':
  app.run(host='127.0.0.1', port='8888')

================================================
FILE: ab_testing/server_starter.py
================================================
# From the course: Bayesin Machine Learning in Python: A/B Testing
# https://deeplearningcourses.com/c/bayesian-machine-learning-in-python-ab-testing
# https://www.udemy.com/bayesian-machine-learning-in-python-ab-testing
from __future__ import print_function, division
from builtins import range
# Note: you may need to update your version of future
# sudo pip install -U future


import numpy as np
from flask import Flask, jsonify, request
from scipy.stats import beta

# create an app
app = Flask(__name__)


# define bandits
# there's no "pull arm" here
# since that's technically now the user/client
class Bandit:
  def __init__(self, name):
    self.name = name

  def sample(self):
    # TODO
    return 1

  # TODO - what else does the Bandit need to do?


# initialize bandits
banditA = Bandit('A')
banditB = Bandit('B')



@app.route('/get_ad')
def get_ad():
  # TODO
  return jsonify({'advertisement_id': 'A'})


@app.route('/click_ad', methods=['POST'])
def click_ad():
  result = 'OK'
  if request.form['advertisement_id'] == 'A':
    # TODO
    pass
  elif request.form['advertisement_id'] == 'B':
    # TODO
    pass
  else:
    result = 'Invalid Input.'

  # nothing to return really
  return jsonify({'result': result})


if __name__ == '__main__':
  app.run(host='127.0.0.1', port='8888')

================================================
FILE: ab_testing/ttest.py
================================================
# From the course: Bayesin Machine Learning in Python: A/B Testing
# https://deeplearningcourses.com/c/bayesian-machine-learning-in-python-ab-testing
# https://www.udemy.com/bayesian-machine-learning-in-python-ab-testing
from __future__ import print_function, division
from builtins import range
# Note: you may need to update your version of future
# sudo pip install -U future


import numpy as np
from scipy import stats

# generate data
N = 10
a = np.random.randn(N) + 2 # mean 2, variance 1
b = np.random.randn(N) # mean 0, variance 1

# roll your own t-test:
var_a = a.var(ddof=1) # unbiased estimator, divide by N-1 instead of N
var_b = b.var(ddof=1)
s = np.sqrt( (var_a + var_b) / 2 ) # balanced standard deviation
t = (a.mean() - b.mean()) / (s * np.sqrt(2.0/N)) # t-statistic
df = 2*N - 2 # degrees of freedom
p = 1 - stats.t.cdf(np.abs(t), df=df) # one-sided test p-value
print("t:\t", t, "p:\t", 2*p) # two-sided test p-value

# built-in t-test:
t2, p2 = stats.ttest_ind(a, b)
print("t2:\t", t2, "p2:\t", p2)

================================================
FILE: ab_testing/ucb1.py
================================================
# https://deeplearningcourses.com/c/artificial-intelligence-reinforcement-learning-in-python
# https://www.udemy.com/artificial-intelligence-reinforcement-learning-in-python
# https://books.google.ca/books?id=_ATpBwAAQBAJ&lpg=PA201&ots=rinZM8jQ6s&dq=hoeffding%20bound%20gives%20probability%20%22greater%20than%201%22&pg=PA201#v=onepage&q&f=false
from __future__ import print_function, division
from builtins import range
# Note: you may need to update your version of future
# sudo pip install -U future

import numpy as np
import matplotlib.pyplot as plt


NUM_TRIALS = 100000
EPS = 0.1
BANDIT_PROBABILITIES = [0.2, 0.5, 0.75]


class Bandit:
  def __init__(self, p):
    # p: the win rate
    self.p = p
    self.p_estimate = 0.
    self.N = 0. # num samples collected so far

  def pull(self):
    # draw a 1 with probability p
    return np.random.random() < self.p

  def update(self, x):
    self.N += 1.
    self.p_estimate = ((self.N - 1)*self.p_estimate + x) / self.N


def ucb(mean, n, nj):
  return mean + np.sqrt(2*np.log(n) / nj)


def run_experiment():
  bandits = [Bandit(p) for p in BANDIT_PROBABILITIES]
  rewards = np.empty(NUM_TRIALS)
  total_plays = 0

  # initialization: play each bandit once
  for j in range(len(bandits)):
    x = bandits[j].pull()
    total_plays += 1
    bandits[j].update(x)
  
  for i in range(NUM_TRIALS):
    j = np.argmax([ucb(b.p_estimate, total_plays, b.N) for b in bandits])
    x = bandits[j].pull()
    total_plays += 1
    bandits[j].update(x)

    # for the plot
    rewards[i] = x
  cumulative_average = np.cumsum(rewards) / (np.arange(NUM_TRIALS) + 1)

  # plot moving average ctr
  plt.plot(cumulative_average)
  plt.plot(np.ones(NUM_TRIALS)*np.max(BANDIT_PROBABILITIES))
  plt.xscale('log')
  plt.show()

  # plot moving average ctr linear
  plt.plot(cumulative_average)
  plt.plot(np.ones(NUM_TRIALS)*np.max(BANDIT_PROBABILITIES))
  plt.show()

  for b in bandits:
    print(b.p_estimate)

  print("total reward earned:", rewards.sum())
  print("overall win rate:", rewards.sum() / NUM_TRIALS)
  print("num times selected each bandit:", [b.N for b in bandits])

  return cumulative_average

if __name__ == '__main__':
  run_experiment()



================================================
FILE: ab_testing/ucb1_starter.py
================================================
# https://deeplearningcourses.com/c/artificial-intelligence-reinforcement-learning-in-python
# https://www.udemy.com/artificial-intelligence-reinforcement-learning-in-python
# https://books.google.ca/books?id=_ATpBwAAQBAJ&lpg=PA201&ots=rinZM8jQ6s&dq=hoeffding%20bound%20gives%20probability%20%22greater%20than%201%22&pg=PA201#v=onepage&q&f=false
from __future__ import print_function, division
from builtins import range
# Note: you may need to update your version of future
# sudo pip install -U future

import numpy as np
import matplotlib.pyplot as plt


NUM_TRIALS = 100000
EPS = 0.1
BANDIT_PROBABILITIES = [0.2, 0.5, 0.75]


class Bandit:
  def __init__(self, p):
    # p: the win rate
    self.p = p
    self.p_estimate = 0.
    self.N = 0. # num samples collected so far

  def pull(self):
    # draw a 1 with probability p
    return np.random.random() < self.p

  def update(self, x):
    self.N += 1.
    self.p_estimate = ((self.N - 1)*self.p_estimate + x) / self.N


def ucb(mean, n, nj):
  return # TODO


def run_experiment():
  bandits = [Bandit(p) for p in BANDIT_PROBABILITIES]
  rewards = np.empty(NUM_TRIALS)
  total_plays = 0

  # initialization: play each bandit once
  for j in range(len(bandits)):
    x = bandits[j].pull()
    total_plays += 1
    bandits[j].update(x)
  
  for i in range(NUM_TRIALS):
    j = # TODO
    x = bandits[j].pull()
    total_plays += 1
    bandits[j].update(x)

    # for the plot
    rewards[i] = x
  cumulative_average = np.cumsum(rewards) / (np.arange(NUM_TRIALS) + 1)

  # plot moving average ctr
  plt.plot(cumulative_average)
  plt.plot(np.ones(NUM_TRIALS)*np.max(BANDIT_PROBABILITIES))
  plt.xscale('log')
  plt.show()

  # plot moving average ctr linear
  plt.plot(cumulative_average)
  plt.plot(np.ones(NUM_TRIALS)*np.max(BANDIT_PROBABILITIES))
  plt.show()

  for b in bandits:
    print(b.p_estimate)

  print("total reward earned:", rewards.sum())
  print("overall win rate:", rewards.sum() / NUM_TRIALS)
  print("num times selected each bandit:", [b.N for b in bandits])

  return cumulative_average

if __name__ == '__main__':
  run_experiment()



================================================
FILE: airline/ann.py
================================================
# The corresponding tutorial for this code was released EXCLUSIVELY as a bonus
# If you want to learn about future bonuses, please sign up for my newsletter at:
# https://lazyprogrammer.me

import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import theano
import theano.tensor as T
from sklearn.utils import shuffle

def init_weight(M1, M2):
    return np.random.randn(M1, M2) / np.sqrt(M1 + M2)

def myr2(T, Y):
    Ym = T.mean()
    sse = (T - Y).dot(T - Y)
    sst = (T - Ym).dot(T - Ym)
    return 1 - sse / sst

class HiddenLayer(object):
    def __init__(self, M1, M2, f, an_id):
        self.id = an_id
        self.M1 = M1
        self.M2 = M2
        self.f = f
        W = init_weight(M1, M2)
        b = np.zeros(M2)
        self.W = theano.shared(W, 'W_%s' % self.id)
        self.b = theano.shared(b, 'b_%s' % self.id)
        self.params = [self.W, self.b]

    def forward(self, X):
        return self.f(X.dot(self.W) + self.b)


class ANN(object):
    def __init__(self, hidden_layer_sizes):
        self.hidden_layer_sizes = hidden_layer_sizes

    def fit(self, X, Y, activation=T.tanh, learning_rate=1e-3, mu=0.5, reg=0, epochs=5000, batch_sz=None, print_period=100, show_fig=True):
        X = X.astype(np.float32)
        Y = Y.astype(np.float32)

        # initialize hidden layers
        N, D = X.shape
        self.hidden_layers = []
        M1 = D
        count = 0
        for M2 in self.hidden_layer_sizes:
            h = HiddenLayer(M1, M2, activation, count)
            self.hidden_layers.append(h)
            M1 = M2
            count += 1
        W = np.random.randn(M1) / np.sqrt(M1)
        b = 0.0
        self.W = theano.shared(W, 'W_last')
        self.b = theano.shared(b, 'b_last')

        if batch_sz is None:
            batch_sz = N

        # collect params for later use
        self.params = [self.W, self.b]
        for h in self.hidden_layers:
            self.params += h.params

        # for momentum
        dparams = [theano.shared(np.zeros(p.get_value().shape)) for p in self.params]

        # set up theano functions and variables
        thX = T.matrix('X')
        thY = T.vector('Y')
        Yhat = self.forward(thX)

        rcost = reg*T.mean([(p*p).sum() for p in self.params])
        cost = T.mean((thY - Yhat).dot(thY - Yhat)) + rcost
        prediction = self.forward(thX)
        grads = T.grad(cost, self.params)

        # momentum only
        updates = [
            (p, p + mu*dp - learning_rate*g) for p, dp, g in zip(self.params, dparams, grads)
        ] + [
            (dp, mu*dp - learning_rate*g) for dp, g in zip(dparams, grads)
        ]

        train_op = theano.function(
            inputs=[thX, thY],
            outputs=[cost, prediction],
            updates=updates,
        )

        self.predict_op = theano.function(
            inputs=[thX],
            outputs=prediction,
        )

        n_batches = N / batch_sz
        # print "N:", N, "batch_sz:", batch_sz
        # print "n_batches:", n_batches
        costs = []
        for i in xrange(epochs):
            X, Y = shuffle(X, Y)
            for j in xrange(n_batches):
                Xbatch = X[j*batch_sz:(j*batch_sz+batch_sz)]
                Ybatch = Y[j*batch_sz:(j*batch_sz+batch_sz)]

                c, p = train_op(Xbatch, Ybatch)
                costs.append(c)
                if (j+1) % print_period == 0:
                    print "i:", i, "j:", j, "nb:", n_batches, "cost:", c
        
        if show_fig:
            plt.plot(costs)
            plt.show()

    def forward(self, X):
        Z = X
        for h in self.hidden_layers:
            Z = h.forward(Z)
        return Z.dot(self.W) + self.b

    def score(self, X, Y):
        Yhat = self.predict_op(X)
        return myr2(Y, Yhat)

    def predict(self, X):
        return self.predict_op(X)

# we need to skip the 3 footer rows
# skipfooter does not work with the default engine, 'c'
# so we need to explicitly set it to 'python'
df = pd.read_csv('international-airline-passengers.csv', engine='python', skipfooter=3)

# rename the columns because they are ridiculous
df.columns = ['month', 'num_passengers']

# plot the data so we know what it looks like
# plt.plot(df.num_passengers)
# plt.show()

# let's try with only the time series itself
series = df.num_passengers.as_matrix()
# series = (series - series.mean()) / series.std() # normalize the values so they have mean 0 and variance 1
series = series.astype(np.float32)
series = series - series.min()
series = series / series.max()

# let's see if we can use D past values to predict the next value
N = len(series)
for D in (2,3,4,5,6,7):
    n = N - D
    X = np.zeros((n, D))
    for d in xrange(D):
        X[:,d] = series[d:d+n]
    Y = series[D:D+n]

    print "series length:", n
    Xtrain = X[:n/2]
    Ytrain = Y[:n/2]
    Xtest = X[n/2:]
    Ytest = Y[n/2:]

    model = ANN([200])
    model.fit(Xtrain, Ytrain, activation=T.tanh)
    print "train score:", model.score(Xtrain, Ytrain)
    print "test score:", model.score(Xtest, Ytest)

    # plot the prediction with true values
    plt.plot(series)

    train_series = np.empty(n)
    train_series[:n/2] = model.predict(Xtrain) 
    train_series[n/2:] = np.nan
    # prepend d nan's since the train series is only of size N - D
    plt.plot(np.concatenate([np.full(d, np.nan), train_series]))

    test_series = np.empty(n)
    test_series[:n/2] = np.nan
    test_series[n/2:] = model.predict(Xtest)
    plt.plot(np.concatenate([np.full(d, np.nan), test_series]))

    plt.show()


================================================
FILE: airline/international-airline-passengers.csv
================================================
"Month","International airline passengers: monthly totals in thousands. Jan 49 ? Dec 60"
"1949-01",112
"1949-02",118
"1949-03",132
"1949-04",129
"1949-05",121
"1949-06",135
"1949-07",148
"1949-08",148
"1949-09",136
"1949-10",119
"1949-11",104
"1949-12",118
"1950-01",115
"1950-02",126
"1950-03",141
"1950-04",135
"1950-05",125
"1950-06",149
"1950-07",170
"1950-08",170
"1950-09",158
"1950-10",133
"1950-11",114
"1950-12",140
"1951-01",145
"1951-02",150
"1951-03",178
"1951-04",163
"1951-05",172
"1951-06",178
"1951-07",199
"1951-08",199
"1951-09",184
"1951-10",162
"1951-11",146
"1951-12",166
"1952-01",171
"1952-02",180
"1952-03",193
"1952-04",181
"1952-05",183
"1952-06",218
"1952-07",230
"1952-08",242
"1952-09",209
"1952-10",191
"1952-11",172
"1952-12",194
"1953-01",196
"1953-02",196
"1953-03",236
"1953-04",235
"1953-05",229
"1953-06",243
"1953-07",264
"1953-08",272
"1953-09",237
"1953-10",211
"1953-11",180
"1953-12",201
"1954-01",204
"1954-02",188
"1954-03",235
"1954-04",227
"1954-05",234
"1954-06",264
"1954-07",302
"1954-08",293
"1954-09",259
"1954-10",229
"1954-11",203
"1954-12",229
"1955-01",242
"1955-02",233
"1955-03",267
"1955-04",269
"1955-05",270
"1955-06",315
"1955-07",364
"1955-08",347
"1955-09",312
"1955-10",274
"1955-11",237
"1955-12",278
"1956-01",284
"1956-02",277
"1956-03",317
"1956-04",313
"1956-05",318
"1956-06",374
"1956-07",413
"1956-08",405
"1956-09",355
"1956-10",306
"1956-11",271
"1956-12",306
"1957-01",315
"1957-02",301
"1957-03",356
"1957-04",348
"1957-05",355
"1957-06",422
"1957-07",465
"1957-08",467
"1957-09",404
"1957-10",347
"1957-11",305
"1957-12",336
"1958-01",340
"1958-02",318
"1958-03",362
"1958-04",348
"1958-05",363
"1958-06",435
"1958-07",491
"1958-08",505
"1958-09",404
"1958-10",359
"1958-11",310
"1958-12",337
"1959-01",360
"1959-02",342
"1959-03",406
"1959-04",396
"1959-05",420
"1959-06",472
"1959-07",548
"1959-08",559
"1959-09",463
"1959-10",407
"1959-11",362
"1959-12",405
"1960-01",417
"1960-02",391
"1960-03",419
"1960-04",461
"1960-05",472
"1960-06",535
"1960-07",622
"1960-08",606
"1960-09",508
"1960-10",461
"1960-11",390
"1960-12",432

International airline passengers: monthly totals in thousands. Jan 49 ? Dec 60



================================================
FILE: airline/lr.py
================================================
# The corresponding tutorial for this code was released EXCLUSIVELY as a bonus
# If you want to learn about future bonuses, please sign up for my newsletter at:
# https://lazyprogrammer.me

import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
from sklearn.linear_model import LinearRegression

# we need to skip the 3 footer rows
# skipfooter does not work with the default engine, 'c'
# so we need to explicitly set it to 'python'
df = pd.read_csv('international-airline-passengers.csv', engine='python', skipfooter=3)

# rename the columns because they are ridiculous
df.columns = ['month', 'num_passengers']

# plot the data so we know what it looks like
plt.plot(df.num_passengers)
plt.show()

# let's try with only the time series itself
series = df.num_passengers.as_matrix()

# let's see if we can use D past values to predict the next value
N = len(series)
for D in (2,3,4,5,6,7):
    n = N - D
    X = np.zeros((n, D))
    for d in xrange(D):
        X[:,d] = series[d:d+n]
    Y = series[D:D+n]

    print "series length:", n
    Xtrain = X[:n/2]
    Ytrain = Y[:n/2]
    Xtest = X[n/2:]
    Ytest = Y[n/2:]

    model = LinearRegression()
    model.fit(Xtrain, Ytrain)
    print "train score:", model.score(Xtrain, Ytrain)
    print "test score:", model.score(Xtest, Ytest)

    # plot the prediction with true values
    plt.plot(series)

    train_series = np.empty(n)
    train_series[:n/2] = model.predict(Xtrain) 
    train_series[n/2:] = np.nan
    # prepend d nan's since the train series is only of size N - D
    plt.plot(np.concatenate([np.full(d, np.nan), train_series]))

    test_series = np.empty(n)
    test_series[:n/2] = np.nan
    test_series[n/2:] = model.predict(Xtest)
    plt.plot(np.concatenate([np.full(d, np.nan), test_series]))

    plt.show()


================================================
FILE: airline/rnn.py
================================================
# The corresponding tutorial for this code was released EXCLUSIVELY as a bonus
# If you want to learn about future bonuses, please sign up for my newsletter at:
# https://lazyprogrammer.me

import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import theano
import theano.tensor as T
from sklearn.utils import shuffle
from datetime import datetime

import os
import sys
sys.path.append(os.path.abspath('..'))
from rnn_class.lstm import LSTM
from rnn_class.gru import GRU

def init_weight(M1, M2):
    return np.random.randn(M1, M2) / np.sqrt(M1 + M2)

def myr2(T, Y):
    Ym = T.mean()
    sse = (T - Y).dot(T - Y)
    sst = (T - Ym).dot(T - Ym)
    return 1 - sse / sst

class RNN(object):
    def __init__(self, hidden_layer_sizes):
        self.hidden_layer_sizes = hidden_layer_sizes

    def fit(self, X, Y, activation=T.tanh, learning_rate=1e-1, mu=0.5, reg=0, epochs=2000, show_fig=False):
        N, t, D = X.shape

        self.hidden_layers = []
        Mi = D
        for Mo in self.hidden_layer_sizes:
            ru = GRU(Mi, Mo, activation)
            self.hidden_layers.append(ru)
            Mi = Mo

        Wo = np.random.randn(Mi) / np.sqrt(Mi)
        bo = 0.0
        self.Wo = theano.shared(Wo)
        self.bo = theano.shared(bo)
        self.params = [self.Wo, self.bo]
        for ru in self.hidden_layers:
            self.params += ru.params

        lr = T.scalar('lr')
        thX = T.matrix('X')
        thY = T.scalar('Y')
        Yhat = self.forward(thX)[-1]

        # let's return py_x too so we can draw a sample instead
        self.predict_op = theano.function(
            inputs=[thX],
            outputs=Yhat,
            allow_input_downcast=True,
        )
        
        cost = T.mean((thY - Yhat)*(thY - Yhat))
        grads = T.grad(cost, self.params)
        dparams = [theano.shared(p.get_value()*0) for p in self.params]

        updates = [
            (p, p + mu*dp - lr*g) for p, dp, g in zip(self.params, dparams, grads)
        ] + [
            (dp, mu*dp - lr*g) for dp, g in zip(dparams, grads)
        ]

        self.train_op = theano.function(
            inputs=[lr, thX, thY],
            outputs=cost,
            updates=updates
        )

        costs = []
        for i in xrange(epochs):
            t0 = datetime.now()
            X, Y = shuffle(X, Y)
            n_correct = 0
            n_total = 0
            cost = 0
            for j in xrange(N):
                
                c = self.train_op(learning_rate, X[j], Y[j])
                cost += c
            if i % 10 == 0:
                print "i:", i, "cost:", cost, "time for epoch:", (datetime.now() - t0)
            if (i+1) % 500 == 0:
                learning_rate /= 10
            costs.append(cost)

        if show_fig:
            plt.plot(costs)
            plt.show()

    def forward(self, X):
        Z = X
        for h in self.hidden_layers:
            Z = h.output(Z)
        return Z.dot(self.Wo) + self.bo

    def score(self, X, Y):
        Yhat = self.predict(X)
        return myr2(Y, Yhat)

    def predict(self, X):
        N = len(X)
        Yhat = np.empty(N)
        for i in xrange(N):
            Yhat[i] = self.predict_op(X[i])
        return Yhat

# we need to skip the 3 footer rows
# skipfooter does not work with the default engine, 'c'
# so we need to explicitly set it to 'python'
df = pd.read_csv('international-airline-passengers.csv', engine='python', skipfooter=3)

# rename the columns because they are ridiculous
df.columns = ['month', 'num_passengers']

# plot the data so we know what it looks like
# plt.plot(df.num_passengers)
# plt.show()

# let's try with only the time series itself
series = df.num_passengers.as_matrix()
# series = (series - series.mean()) / series.std() # normalize the values so they have mean 0 and variance 1
series = series.astype(np.float32)
series = series - series.min()
series = series / series.max()

# let's see if we can use D past values to predict the next value
N = len(series)
for D in (2,3,4,5):
    n = N - D
    X = np.zeros((n, D))
    for d in xrange(D):
        X[:,d] = series[d:d+n]
    Y = series[D:D+n]

    print "series length:", n
    Xtrain = X[:n/2]
    Ytrain = Y[:n/2]
    Xtest = X[n/2:]
    Ytest = Y[n/2:]

    Ntrain = len(Xtrain)
    Xtrain = Xtrain.reshape(Ntrain, D, 1)
    Ntest = len(Xtest)
    Xtest = Xtest.reshape(Ntest, D, 1)

    model = RNN([50])
    model.fit(Xtrain, Ytrain, activation=T.tanh)
    print "train score:", model.score(Xtrain, Ytrain)
    print "test score:", model.score(Xtest, Ytest)

    # plot the prediction with true values
    plt.plot(series)

    train_series = np.empty(n)
    train_series[:n/2] = model.predict(Xtrain) 
    train_series[n/2:] = np.nan
    # prepend d nan's since the train series is only of size N - D
    plt.plot(np.concatenate([np.full(d, np.nan), train_series]))

    test_series = np.empty(n)
    test_series[:n/2] = np.nan
    test_series[n/2:] = model.predict(Xtest)
    plt.plot(np.concatenate([np.full(d, np.nan), test_series]))

    plt.show()


================================================
FILE: ann_class/backprop.py
================================================
# backpropagation example for deep learning in python class.
# with sigmoid activation
#
# the notes for this class can be found at: 
# https://deeplearningcourses.com/c/data-science-deep-learning-in-python
# https://www.udemy.com/data-science-deep-learning-in-python
from __future__ import print_function, division
from builtins import range
# Note: you may need to update your version of future
# sudo pip install -U future

import numpy as np
import matplotlib.pyplot as plt

np.random.seed(1)

def forward(X, W1, b1, W2, b2):
    Z = 1 / (1 + np.exp(-X.dot(W1) - b1))
    A = Z.dot(W2) + b2
    expA = np.exp(A)
    Y = expA / expA.sum(axis=1, keepdims=True)
    return Y, Z


# determine the classification rate
# num correct / num total
def classification_rate(Y, P):
    n_correct = 0
    n_total = 0
    for i in range(len(Y)):
        n_total += 1
        if Y[i] == P[i]:
            n_correct += 1
    return float(n_correct) / n_total


def derivative_w2(Z, T, Y):
    N, K = T.shape
    M = Z.shape[1] # H is (N, M)

    # # slow
    # ret1 = np.zeros((M, K))
    # for n in xrange(N):
    #     for m in xrange(M):
    #         for k in xrange(K):
    #             ret1[m,k] += (T[n,k] - Y[n,k])*Z[n,m]

    # # a bit faster - let's not loop over m
    # ret2 = np.zeros((M, K))
    # for n in xrange(N):
    #     for k in xrange(K):
    #         ret2[:,k] += (T[n,k]* - Y[n,k])*Z[n,:]

    # assert(np.abs(ret1 - ret2).sum() < 0.00001)

    # # even faster  - let's not loop over k either
    # ret3 = np.zeros((M, K))
    # for n in xrange(N): # slow way first
    #     ret3 += np.outer( Z[n], T[n] - Y[n] )

    # assert(np.abs(ret1 - ret3).sum() < 0.00001)

    # fastest - let's not loop over anything
    ret4 = Z.T.dot(T - Y)
    # assert(np.abs(ret1 - ret4).sum() < 0.00001)

    return ret4


def derivative_w1(X, Z, T, Y, W2):
    N, D = X.shape
    M, K = W2.shape

    # slow way first
    # ret1 = np.zeros((X.shape[1], M))
    # for n in xrange(N):
    #     for k in xrange(K):
    #         for m in xrange(M):
    #             for d in xrange(D):
    #                 ret1[d,m] += (T[n,k] - Y[n,k])*W2[m,k]*Z[n,m]*(1 - Z[n,m])*X[n,d]

    # fastest
    dZ = (T - Y).dot(W2.T) * Z * (1 - Z)
    ret2 = X.T.dot(dZ)

    # assert(np.abs(ret1 - ret2).sum() < 0.00001)

    return ret2


def derivative_b2(T, Y):
    return (T - Y).sum(axis=0)


def derivative_b1(T, Y, W2, Z):
    return ((T - Y).dot(W2.T) * Z * (1 - Z)).sum(axis=0)


def cost(T, Y):
    tot = T * np.log(Y)
    return tot.sum()


def main():
    # create the data
    Nclass = 500
    D = 2 # dimensionality of input
    M = 3 # hidden layer size
    K = 3 # number of classes

    X1 = np.random.randn(Nclass, D) + np.array([0, -2])
    X2 = np.random.randn(Nclass, D) + np.array([2, 2])
    X3 = np.random.randn(Nclass, D) + np.array([-2, 2])
    X = np.vstack([X1, X2, X3])

    Y = np.array([0]*Nclass + [1]*Nclass + [2]*Nclass)
    N = len(Y)
    # turn Y into an indicator matrix for training
    T = np.zeros((N, K))
    for i in range(N):
        T[i, Y[i]] = 1

    # let's see what it looks like
    plt.scatter(X[:,0], X[:,1], c=Y, s=100, alpha=0.5)
    plt.show()

    # randomly initialize weights
    W1 = np.random.randn(D, M)
    b1 = np.random.randn(M)
    W2 = np.random.randn(M, K)
    b2 = np.random.randn(K)

    learning_rate = 1e-3
    costs = []
    for epoch in range(1000):
        output, hidden = forward(X, W1, b1, W2, b2)
        if epoch % 100 == 0:
            c = cost(T, output)
            P = np.argmax(output, axis=1)
            r = classification_rate(Y, P)
            print("cost:", c, "classification_rate:", r)
            costs.append(c)

        # this is gradient ASCENT, not DESCENT
        # be comfortable with both!
        # oldW2 = W2.copy()

        gW2 = derivative_w2(hidden, T, output)
        gb2 = derivative_b2(T, output)
        gW1 = derivative_w1(X, hidden, T, output, W2)
        gb1 = derivative_b1(T, output, W2, hidden)

        W2 += learning_rate * gW2
        b2 += learning_rate * gb2
        W1 += learning_rate * gW1
        b1 += learning_rate * gb1

    plt.plot(costs)
    plt.show()


if __name__ == '__main__':
    main()



================================================
FILE: ann_class/extra_reading.txt
================================================
The Chain Rule of Calculus
http://tutorial.math.lamar.edu/Classes/CalcI/ChainRule.aspx

Yes you should understand backprop by Andrej Karpathy
https://medium.com/@karpathy/yes-you-should-understand-backprop-e2f06eab496b

The Matrix Cookbook
https://www.math.uwaterloo.ca/~hwolkowi/matrixcookbook.pdf

Rumelhart, D. E., Hinton, G. E., and Williams, R. J. (1986) Learning representations by back-propagating errors.
https://www.iro.umontreal.ca/~vincentp/ift3395/lectures/backprop_old.pdf

================================================
FILE: ann_class/forwardprop.py
================================================
# forward propagation example for deep learning in python class.
#
# the notes for this class can be found at: 
# https://deeplearningcourses.com/c/data-science-deep-learning-in-python
# https://www.udemy.com/data-science-deep-learning-in-python

from __future__ import print_function, division
from builtins import range
# Note: you may need to update your version of future
# sudo pip install -U future



import numpy as np
import matplotlib.pyplot as plt

Nclass = 500

X1 = np.random.randn(Nclass, 2) + np.array([0, -2])
X2 = np.random.randn(Nclass, 2) + np.array([2, 2])
X3 = np.random.randn(Nclass, 2) + np.array([-2, 2])
X = np.vstack([X1, X2, X3])

Y = np.array([0]*Nclass + [1]*Nclass + [2]*Nclass)

# let's see what it looks like
plt.scatter(X[:,0], X[:,1], c=Y, s=100, alpha=0.5)
plt.show()

# randomly initialize weights
D = 2 # dimensionality of input
M = 3 # hidden layer size
K = 3 # number of classes
W1 = np.random.randn(D, M)
b1 = np.random.randn(M)
W2 = np.random.randn(M, K)
b2 = np.random.randn(K)

def sigmoid(a):
    return 1 / (1 + np.exp(-a))

def forward(X, W1, b1, W2, b2):
    Z = sigmoid(X.dot(W1) + b1) # sigmoid
    # Z = np.tanh(X.dot(W1) + b1) # tanh
    # Z = np.maximum(X.dot(W1) + b1, 0) # relu
    A = Z.dot(W2) + b2
    expA = np.exp(A)
    Y = expA / expA.sum(axis=1, keepdims=True)
    return Y

# determine the classification rate
# num correct / num total
def classification_rate(Y, P):
    n_correct = 0
    n_total = 0
    for i in range(len(Y)):
        n_total += 1
        if Y[i] == P[i]:
            n_correct += 1
    return float(n_correct) / n_total

P_Y_given_X = forward(X, W1, b1, W2, b2)
P = np.argmax(P_Y_given_X, axis=1)

# verify we chose the correct axis
assert(len(P) == len(Y))

print("Classification rate for randomly chosen weights:", classification_rate(Y, P))



================================================
FILE: ann_class/regression.py
================================================
from __future__ import print_function, division
from builtins import range
# Note: you may need to update your version of future
# sudo pip install -U future

import numpy as np
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D

# NOTE: some people using the default Python
# installation on Mac have had trouble with Axes3D
# Switching to Python 3 (brew install python3) or
# using Linux are both viable work-arounds





# generate and plot the data
N = 500
X = np.random.random((N, 2))*4 - 2 # in between (-2, +2)
Y = X[:,0]*X[:,1] # makes a saddle shape
# note: in this script "Y" will be the target,
#       "Yhat" will be prediction

fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')
ax.scatter(X[:,0], X[:,1], Y)
plt.show()





# make a neural network and train it
D = 2
M = 100 # number of hidden units

# layer 1
W = np.random.randn(D, M) / np.sqrt(D)
b = np.zeros(M)

# layer 2
V = np.random.randn(M) / np.sqrt(M)
c = 0




# how to get the output
# consider the params global
def forward(X):
  Z = X.dot(W) + b
  Z = Z * (Z > 0) # relu
  # Z = np.tanh(Z)

  Yhat = Z.dot(V) + c
  return Z, Yhat




# how to train the params
def derivative_V(Z, Y, Yhat):
  return (Y - Yhat).dot(Z)

def derivative_c(Y, Yhat):
  return (Y - Yhat).sum()

def derivative_W(X, Z, Y, Yhat, V):
  # dZ = np.outer(Y - Yhat, V) * (1 - Z * Z) # this is for tanh activation
  dZ = np.outer(Y - Yhat, V) * (Z > 0) # relu
  return X.T.dot(dZ)

def derivative_b(Z, Y, Yhat, V):
  # dZ = np.outer(Y - Yhat, V) * (1 - Z * Z) # this is for tanh activation
  dZ = np.outer(Y - Yhat, V) * (Z > 0) # this is for relu activation
  return dZ.sum(axis=0)

def update(X, Z, Y, Yhat, W, b, V, c, learning_rate=1e-4):
  gV = derivative_V(Z, Y, Yhat)
  gc = derivative_c(Y, Yhat)
  gW = derivative_W(X, Z, Y, Yhat, V)
  gb = derivative_b(Z, Y, Yhat, V)

  V += learning_rate*gV
  c += learning_rate*gc
  W += learning_rate*gW
  b += learning_rate*gb

  return W, b, V, c




# so we can plot the costs later
def get_cost(Y, Yhat):
  return ((Y - Yhat)**2).mean()



# run a training loop
# plot the costs
# and plot the final result
costs = []
for i in range(200):
  Z, Yhat = forward(X)
  W, b, V, c = update(X, Z, Y, Yhat, W, b, V, c)
  cost = get_cost(Y, Yhat)
  costs.append(cost)
  if i % 25 == 0:
    print(cost)

# plot the costs
plt.plot(costs)
plt.show()

# plot the prediction with the data
fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')
ax.scatter(X[:,0], X[:,1], Y)

# surface plot
line = np.linspace(-2, 2, 20)
xx, yy = np.meshgrid(line, line)
Xgrid = np.vstack((xx.flatten(), yy.flatten())).T
_, Yhat = forward(Xgrid)
ax.plot_trisurf(Xgrid[:,0], Xgrid[:,1], Yhat, linewidth=0.2, antialiased=True)
plt.show()




# plot magnitude of residuals
Ygrid = Xgrid[:,0]*Xgrid[:,1]
R = np.abs(Ygrid - Yhat)

plt.scatter(Xgrid[:,0], Xgrid[:,1], c=R)
plt.show()

fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')
ax.plot_trisurf(Xgrid[:,0], Xgrid[:,1], R, linewidth=0.2, antialiased=True)
plt.show()




================================================
FILE: ann_class/sklearn_ann.py
================================================
# Train a neural network in just 3 lines of code!
#
# the notes for this class can be found at: 
# https://deeplearningcourses.com/c/data-science-deep-learning-in-python
# https://www.udemy.com/data-science-deep-learning-in-python
from __future__ import print_function, division
from builtins import range
# Note: you may need to update your version of future
# sudo pip install -U future


import sys
sys.path.append('../ann_logistic_extra')
from process import get_data

from sklearn.neural_network import MLPClassifier
from sklearn.utils import shuffle

# get the data
Xtrain, Ytrain, Xtest, Ytest = get_data()

# create the neural network
model = MLPClassifier(hidden_layer_sizes=(20, 20), max_iter=2000)

# train the neural network
model.fit(Xtrain, Ytrain)

# print the train and test accuracy
train_accuracy = model.score(Xtrain, Ytrain)
test_accuracy = model.score(Xtest, Ytest)
print("train accuracy:", train_accuracy, "test accuracy:", test_accuracy)


================================================
FILE: ann_class/tf_example.py
================================================
# neural network in TensorFlow very simple example.
#
# the notes for this class can be found at: 
# https://deeplearningcourses.com/c/data-science-deep-learning-in-python
# https://www.udemy.com/data-science-deep-learning-in-python
from __future__ import print_function, division
from builtins import range
# Note: you may need to update your version of future
# sudo pip install -U future

import tensorflow as tf
import numpy as np
import matplotlib.pyplot as plt


# create random training data again
Nclass = 500
D = 2 # dimensionality of input
M = 3 # hidden layer size
K = 3 # number of classes

X1 = np.random.randn(Nclass, D) + np.array([0, -2])
X2 = np.random.randn(Nclass, D) + np.array([2, 2])
X3 = np.random.randn(Nclass, D) + np.array([-2, 2])
X = np.vstack([X1, X2, X3]).astype(np.float32)

Y = np.array([0]*Nclass + [1]*Nclass + [2]*Nclass)

# let's see what it looks like
plt.scatter(X[:,0], X[:,1], c=Y, s=100, alpha=0.5)
plt.show()

N = len(Y)
# turn Y into an indicator matrix for training
T = np.zeros((N, K))
for i in range(N):
    T[i, Y[i]] = 1


# tensor flow variables are not the same as regular Python variables
def init_weights(shape):
    return tf.Variable(tf.random_normal(shape, stddev=0.01))


def forward(X, W1, b1, W2, b2):
    Z = tf.nn.sigmoid(tf.matmul(X, W1) + b1)
    return tf.matmul(Z, W2) + b2


tfX = tf.placeholder(tf.float32, [None, D])
tfY = tf.placeholder(tf.float32, [None, K])

W1 = init_weights([D, M]) # create symbolic variables
b1 = init_weights([M])
W2 = init_weights([M, K])
b2 = init_weights([K])

logits = forward(tfX, W1, b1, W2, b2)

cost = tf.reduce_mean(
  tf.nn.softmax_cross_entropy_with_logits_v2(
    labels=tfY,
    logits=logits
  )
) # compute costs
# WARNING: This op expects unscaled logits,
# since it performs a softmax on logits
# internally for efficiency.
# Do not call this op with the output of softmax,
# as it will produce incorrect results.

train_op = tf.train.GradientDescentOptimizer(0.05).minimize(cost) # construct an optimizer
# input parameter is the learning rate

predict_op = tf.argmax(logits, 1)
# input parameter is the axis on which to choose the max

# just stuff that has to be done
sess = tf.Session()
init = tf.global_variables_initializer()
sess.run(init)

for i in range(1000):
    sess.run(train_op, feed_dict={tfX: X, tfY: T})
    pred = sess.run(predict_op, feed_dict={tfX: X, tfY: T})
    if i % 100 == 0:
        print("Accuracy:", np.mean(Y == pred))



================================================
FILE: ann_class/xor_donut.py
================================================
# revisiting the XOR and donut problems to show how features
# can be learned automatically using neural networks.
#
# the notes for this class can be found at: 
# https://deeplearningcourses.com/c/data-science-deep-learning-in-python
# https://www.udemy.com/data-science-deep-learning-in-python

from __future__ import print_function, division
from builtins import range
# Note: you may need to update your version of future
# sudo pip install -U future


import numpy as np
import matplotlib.pyplot as plt

# for binary classification! no softmax here

def forward(X, W1, b1, W2, b2):
    # sigmoid
    # Z = 1 / (1 + np.exp( -(X.dot(W1) + b1) ))

    # tanh
    # Z = np.tanh(X.dot(W1) + b1)

    # relu
    Z = X.dot(W1) + b1
    Z = Z * (Z > 0)

    activation = Z.dot(W2) + b2
    Y = 1 / (1 + np.exp(-activation))
    return Y, Z


def predict(X, W1, b1, W2, b2):
    Y, _ = forward(X, W1, b1, W2, b2)
    return np.round(Y)


def derivative_w2(Z, T, Y):
    # Z is (N, M)
    return (T - Y).dot(Z)

def derivative_b2(T, Y):
    return (T - Y).sum()


def derivative_w1(X, Z, T, Y, W2):
    # dZ = np.outer(T-Y, W2) * Z * (1 - Z) # this is for sigmoid activation
    # dZ = np.outer(T-Y, W2) * (1 - Z * Z) # this is for tanh activation
    dZ = np.outer(T-Y, W2) * (Z > 0) # this is for relu activation
    return X.T.dot(dZ)


def derivative_b1(Z, T, Y, W2):
    # dZ = np.outer(T-Y, W2) * Z * (1 - Z) # this is for sigmoid activation
    # dZ = np.outer(T-Y, W2) * (1 - Z * Z) # this is for tanh activation
    dZ = np.outer(T-Y, W2) * (Z > 0) # this is for relu activation
    return dZ.sum(axis=0)


def get_log_likelihood(T, Y):
    return np.sum(T*np.log(Y) + (1-T)*np.log(1-Y))



def test_xor():
    X = np.array([[0, 0], [0, 1], [1, 0], [1, 1]])
    Y = np.array([0, 1, 1, 0])
    W1 = np.random.randn(2, 5)
    b1 = np.zeros(5)
    W2 = np.random.randn(5)
    b2 = 0
    LL = [] # keep track of log-likelihoods
    learning_rate = 1e-2
    regularization = 0.
    last_error_rate = None
    for i in range(30000):
        pY, Z = forward(X, W1, b1, W2, b2)
        ll = get_log_likelihood(Y, pY)
        prediction = predict(X, W1, b1, W2, b2)
        er = np.mean(prediction != Y)

        LL.append(ll)

        # get gradients
        gW2 = derivative_w2(Z, Y, pY)
        gb2 = derivative_b2(Y, pY)
        gW1 = derivative_w1(X, Z, Y, pY, W2)
        gb1 = derivative_b1(Z, Y, pY, W2)

        W2 += learning_rate * (gW2 - regularization * W2)
        b2 += learning_rate * (gb2 - regularization * b2)
        W1 += learning_rate * (gW1 - regularization * W1)
        b1 += learning_rate * (gb1 - regularization * b1)
        if i % 1000 == 0:
            print(ll)

    print("final classification rate:", np.mean(prediction == Y))
    plt.plot(LL)
    plt.show()


def test_donut():
    # donut example
    N = 1000
    R_inner = 5
    R_outer = 10

    # distance from origin is radius + random normal
    # angle theta is uniformly distributed between (0, 2pi)
    R1 = np.random.randn(N//2) + R_inner
    theta = 2*np.pi*np.random.random(N//2)
    X_inner = np.concatenate([[R1 * np.cos(theta)], [R1 * np.sin(theta)]]).T

    R2 = np.random.randn(N//2) + R_outer
    theta = 2*np.pi*np.random.random(N//2)
    X_outer = np.concatenate([[R2 * np.cos(theta)], [R2 * np.sin(theta)]]).T

    X = np.concatenate([ X_inner, X_outer ])
    Y = np.array([0]*(N//2) + [1]*(N//2))

    n_hidden = 8
    W1 = np.random.randn(2, n_hidden)
    b1 = np.random.randn(n_hidden)
    W2 = np.random.randn(n_hidden)
    b2 = np.random.randn(1)
    LL = [] # keep track of log-likelihoods
    learning_rate = 0.00005
    regularization = 0.2
    last_error_rate = None
    for i in range(3000):
        pY, Z = forward(X, W1, b1, W2, b2)
        ll = get_log_likelihood(Y, pY)
        prediction = predict(X, W1, b1, W2, b2)
        er = np.abs(prediction - Y).mean()
        LL.append(ll)

        # get gradients
        gW2 = derivative_w2(Z, Y, pY)
        gb2 = derivative_b2(Y, pY)
        gW1 = derivative_w1(X, Z, Y, pY, W2)
        gb1 = derivative_b1(Z, Y, pY, W2)

        W2 += learning_rate * (gW2 - regularization * W2)
        b2 += learning_rate * (gb2 - regularization * b2)
        W1 += learning_rate * (gW1 - regularization * W1)
        b1 += learning_rate * (gb1 - regularization * b1)
        if i % 300 == 0:
            print("i:", i, "ll:", ll, "classification rate:", 1 - er)
    plt.plot(LL)
    plt.show()


if __name__ == '__main__':
    test_xor()
    # test_donut()

    




================================================
FILE: ann_class2/__init__.py
================================================


================================================
FILE: ann_class2/adam.py
================================================
# Compare RMSprop with momentum vs. Adam
# For the class Data Science: Practical Deep Learning Concepts in Theano and TensorFlow
# https://deeplearningcourses.com/c/data-science-deep-learning-in-theano-tensorflow
# https://www.udemy.com/data-science-deep-learning-in-theano-tensorflow
from __future__ import print_function, division
from builtins import range
# Note: you may need to update your version of future
# sudo pip install -U future

import numpy as np
from sklearn.utils import shuffle
import matplotlib.pyplot as plt

from util import get_normalized_data, error_rate, cost, y2indicator
from mlp import forward, derivative_w2, derivative_w1, derivative_b2, derivative_b1


def main():
    max_iter = 10
    print_period = 10

    Xtrain, Xtest, Ytrain, Ytest = get_normalized_data()
    reg = 0.01

    Ytrain_ind = y2indicator(Ytrain)
    Ytest_ind = y2indicator(Ytest)

    N, D = Xtrain.shape
    batch_sz = 500
    n_batches = N // batch_sz

    M = 300
    K = 10
    W1_0 = np.random.randn(D, M) / np.sqrt(D)
    b1_0 = np.zeros(M)
    W2_0 = np.random.randn(M, K) / np.sqrt(M)
    b2_0 = np.zeros(K)

    W1 = W1_0.copy()
    b1 = b1_0.copy()
    W2 = W2_0.copy()
    b2 = b2_0.copy()

    # 1st moment
    mW1 = 0
    mb1 = 0
    mW2 = 0
    mb2 = 0

    # 2nd moment
    vW1 = 0
    vb1 = 0
    vW2 = 0
    vb2 = 0

    # hyperparams
    lr0 = 0.001
    beta1 = 0.9
    beta2 = 0.999
    eps = 1e-8

    # 1. Adam
    loss_adam = []
    err_adam = []
    t = 1
    for i in range(max_iter):
        for j in range(n_batches):
            Xbatch = Xtrain[j*batch_sz:(j*batch_sz + batch_sz),]
            Ybatch = Ytrain_ind[j*batch_sz:(j*batch_sz + batch_sz),]
            pYbatch, Z = forward(Xbatch, W1, b1, W2, b2)

            # updates
            # gradients
            gW2 = derivative_w2(Z, Ybatch, pYbatch) + reg*W2
            gb2 = derivative_b2(Ybatch, pYbatch) + reg*b2
            gW1 = derivative_w1(Xbatch, Z, Ybatch, pYbatch, W2) + reg*W1
            gb1 = derivative_b1(Z, Ybatch, pYbatch, W2) + reg*b1

            # new m
            mW1 = beta1 * mW1 + (1 - beta1) * gW1
            mb1 = beta1 * mb1 + (1 - beta1) * gb1
            mW2 = beta1 * mW2 + (1 - beta1) * gW2
            mb2 = beta1 * mb2 + (1 - beta1) * gb2

            # new v
            vW1 = beta2 * vW1 + (1 - beta2) * gW1 * gW1
            vb1 = beta2 * vb1 + (1 - beta2) * gb1 * gb1
            vW2 = beta2 * vW2 + (1 - beta2) * gW2 * gW2
            vb2 = beta2 * vb2 + (1 - beta2) * gb2 * gb2

            # bias correction
            correction1 = 1 - beta1 ** t
            hat_mW1 = mW1 / correction1
            hat_mb1 = mb1 / correction1
            hat_mW2 = mW2 / correction1
            hat_mb2 = mb2 / correction1

            correction2 = 1 - beta2 ** t
            hat_vW1 = vW1 / correction2
            hat_vb1 = vb1 / correction2
            hat_vW2 = vW2 / correction2
            hat_vb2 = vb2 / correction2

            # update t
            t += 1

            # apply updates to the params
            W1 = W1 - lr0 * hat_mW1 / (np.sqrt(hat_vW1) + eps)
            b1 = b1 - lr0 * hat_mb1 / (np.sqrt(hat_vb1) + eps)
            W2 = W2 - lr0 * hat_mW2 / (np.sqrt(hat_vW2) + eps)
            b2 = b2 - lr0 * hat_mb2 / (np.sqrt(hat_vb2) + eps)


            if j % print_period == 0:
                pY, _ = forward(Xtest, W1, b1, W2, b2)
                l = cost(pY, Ytest_ind)
                loss_adam.append(l)
                print("Cost at iteration i=%d, j=%d: %.6f" % (i, j, l))

                err = error_rate(pY, Ytest)
                err_adam.append(err)
                print("Error rate:", err)

    pY, _ = forward(Xtest, W1, b1, W2, b2)
    print("Final error rate:", error_rate(pY, Ytest))


    # 2. RMSprop with momentum
    W1 = W1_0.copy()
    b1 = b1_0.copy()
    W2 = W2_0.copy()
    b2 = b2_0.copy()
    loss_rms = []
    err_rms = []

    # comparable hyperparameters for fair comparison
    lr0 = 0.001
    mu = 0.9
    decay_rate = 0.999
    eps = 1e-8

    # rmsprop cache
    cache_W2 = 1
    cache_b2 = 1
    cache_W1 = 1
    cache_b1 = 1

    # momentum
    dW1 = 0
    db1 = 0
    dW2 = 0
    db2 = 0
    
    for i in range(max_iter):
        for j in range(n_batches):
            Xbatch = Xtrain[j*batch_sz:(j*batch_sz + batch_sz),]
            Ybatch = Ytrain_ind[j*batch_sz:(j*batch_sz + batch_sz),]
            pYbatch, Z = forward(Xbatch, W1, b1, W2, b2)

            # derivatives
            gW2 = derivative_w2(Z, Ybatch, pYbatch) + reg*W2
            gb2 = derivative_b2(Ybatch, pYbatch) + reg*b2
            gW1 = derivative_w1(Xbatch, Z, Ybatch, pYbatch, W2) + reg*W1
            gb1 = derivative_b1(Z, Ybatch, pYbatch, W2) + reg*b1

            # caches
            cache_W2 = decay_rate*cache_W2 + (1 - decay_rate)*gW2*gW2
            cache_b2 = decay_rate*cache_b2 + (1 - decay_rate)*gb2*gb2
            cache_W1 = decay_rate*cache_W1 + (1 - decay_rate)*gW1*gW1
            cache_b1 = decay_rate*cache_b1 + (1 - decay_rate)*gb1*gb1

            # momentum
            dW2 = mu * dW2 + (1 - mu) * lr0 * gW2 / (np.sqrt(cache_W2) + eps)
            db2 = mu * db2 + (1 - mu) * lr0 * gb2 / (np.sqrt(cache_b2) + eps)
            dW1 = mu * dW1 + (1 - mu) * lr0 * gW1 / (np.sqrt(cache_W1) + eps)
            db1 = mu * db1 + (1 - mu) * lr0 * gb1 / (np.sqrt(cache_b1) + eps)

            # updates
            W2 -= dW2
            b2 -= db2
            W1 -= dW1
            b1 -= db1

            if j % print_period == 0:
                pY, _ = forward(Xtest, W1, b1, W2, b2)
                l = cost(pY, Ytest_ind)
                loss_rms.append(l)
                print("Cost at iteration i=%d, j=%d: %.6f" % (i, j, l))

                err = error_rate(pY, Ytest)
                err_rms.append(err)
                print("Error rate:", err)

    pY, _ = forward(Xtest, W1, b1, W2, b2)
    print("Final error rate:", error_rate(pY, Ytest))

    plt.plot(loss_adam, label='adam')
    plt.plot(loss_rms, label='rmsprop')
    plt.legend()
    plt.show()


if __name__ == '__main__':
    main()

================================================
FILE: ann_class2/batch_norm_tf.py
================================================
from __future__ import print_function, division
from builtins import range
# Note: you may need to update your version of future
# sudo pip install -U future

import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import tensorflow as tf
from sklearn.utils import shuffle
from sklearn.model_selection import train_test_split
from util import get_normalized_data


def init_weight(M1, M2):
  return np.random.randn(M1, M2) * np.sqrt(2.0 / M1)


class HiddenLayerBatchNorm(object):
  def __init__(self, M1, M2, f):
    self.M1 = M1
    self.M2 = M2
    self.f = f

    W = init_weight(M1, M2).astype(np.float32)
    gamma = np.ones(M2).astype(np.float32)
    beta = np.zeros(M2).astype(np.float32)

    self.W = tf.Variable(W)
    self.gamma = tf.Variable(gamma)
    self.beta = tf.Variable(beta)

    # for test time
    self.running_mean = tf.Variable(np.zeros(M2).astype(np.float32), trainable=False)
    self.running_var = tf.Variable(np.zeros(M2).astype(np.float32), trainable=False)

  def forward(self, X, is_training, decay=0.9):
    activation = tf.matmul(X, self.W)
    if is_training:
      batch_mean, batch_var = tf.nn.moments(activation, [0])
      update_running_mean = tf.assign(
        self.running_mean,
        self.running_mean * decay + batch_mean * (1 - decay)
      )
      update_running_var = tf.assign(
        self.running_var,
        self.running_var * decay + batch_var * (1 - decay)
      )
      
      with tf.control_dependencies([update_running_mean, update_running_var]):
        out = tf.nn.batch_normalization(
          activation,
          batch_mean,
          batch_var,
          self.beta,
          self.gamma,
          1e-4
        )
    else:
      out = tf.nn.batch_normalization(
        activation,
        self.running_mean,
        self.running_var,
        self.beta,
        self.gamma,
        1e-4
      )
    return self.f(out)


class HiddenLayer(object):
  def __init__(self, M1, M2, f):
    self.M1 = M1
    self.M2 = M2
    self.f = f
    W = np.random.randn(M1, M2) * np.sqrt(2.0 / M1)
    b = np.zeros(M2)
    self.W = tf.Variable(W.astype(np.float32))
    self.b = tf.Variable(b.astype(np.float32))

  def forward(self, X):
    return self.f(tf.matmul(X, self.W) + self.b)


class ANN(object):
  def __init__(self, hidden_layer_sizes):
    self.hidden_layer_sizes = hidden_layer_sizes

  def set_session(self, session):
    self.session = session

  def fit(self, X, Y, Xtest, Ytest, activation=tf.nn.relu, learning_rate=1e-2, epochs=15, batch_sz=100, print_period=100, show_fig=True):
    X = X.astype(np.float32)
    Y = Y.astype(np.int32)

    # initialize hidden layers
    N, D = X.shape
    self.layers = []
    M1 = D
    for M2 in self.hidden_layer_sizes:
      h = HiddenLayerBatchNorm(M1, M2, activation)
      self.layers.append(h)
      M1 = M2
      
    # final layer
    K = len(set(Y))
    h = HiddenLayer(M1, K, lambda x: x)
    self.layers.append(h)

    if batch_sz is None:
      batch_sz = N


    # note! we will need to build the output differently
    # for train and test (prediction)

    # set up theano functions and variables
    tfX = tf.placeholder(tf.float32, shape=(None, D), name='X')
    tfY = tf.placeholder(tf.int32, shape=(None,), name='Y')

    # for later use
    self.tfX = tfX

    # for training
    logits = self.forward(tfX, is_training=True)
    cost = tf.reduce_mean(
      tf.nn.sparse_softmax_cross_entropy_with_logits(
        logits=logits,
        labels=tfY
      )
    )
    # train_op = tf.train.AdamOptimizer(learning_rate).minimize(cost)
    # train_op = tf.train.RMSPropOptimizer(learning_rate, decay=0.99, momentum=0.9).minimize(cost)
    train_op = tf.train.MomentumOptimizer(learning_rate, momentum=0.9, use_nesterov=True).minimize(cost)
    # train_op = tf.train.GradientDescentOptimizer(learning_rate).minimize(cost)

    # for testing
    test_logits = self.forward(tfX, is_training=False)
    self.predict_op = tf.argmax(test_logits, 1)

    # accuracy = tf.reduce_mean(1.0*(tfY == tf.argmax(logits, 1)))

    # init the variables
    self.session.run(tf.global_variables_initializer())

    n_batches = N // batch_sz
    costs = []
    for i in range(epochs):
      if n_batches > 1:
        X, Y = shuffle(X, Y)
      for j in range(n_batches):
        Xbatch = X[j*batch_sz:(j*batch_sz+batch_sz)]
        Ybatch = Y[j*batch_sz:(j*batch_sz+batch_sz)]

        c, _, lgts = self.session.run([cost, train_op, logits], feed_dict={tfX: Xbatch, tfY: Ybatch})
        costs.append(c)
        if (j+1) % print_period == 0:
          acc = np.mean(Ybatch == np.argmax(lgts, axis=1))
          print("epoch:", i, "batch:", j, "n_batches:", n_batches, "cost:", c, "acc: %.2f" % acc)
          # print('dbg:', self.session.run(self.layers[0].running_mean).sum())

      print("Train acc:", self.score(X, Y), "Test acc:", self.score(Xtest, Ytest))
    
    if show_fig:
      plt.plot(costs)
      plt.show()

  def forward(self, X, is_training):
    out = X
    for h in self.layers[:-1]:
      out = h.forward(out, is_training)
    out = self.layers[-1].forward(out)
    return out

  def score(self, X, Y):
    P = self.predict(X)
    return np.mean(Y == P)

  def predict(self, X):
    return self.session.run(self.predict_op, feed_dict={self.tfX: X})



def main():
  # step 1: get the data and define all the usual variables
  Xtrain, Xtest, Ytrain, Ytest = get_normalized_data()

  ann = ANN([500, 300])

  session = tf.InteractiveSession()
  ann.set_session(session)

  ann.fit(Xtrain, Ytrain, Xtest, Ytest, show_fig=True)

  print("Train accuracy:", ann.score(Xtrain, Ytrain))
  print("Test accuracy:", ann.score(Xtest, Ytest))


if __name__ == '__main__':
  main()


================================================
FILE: ann_class2/batch_norm_theano.py
================================================
from __future__ import print_function, division
from builtins import range
# Note: you may need to update your version of future
# sudo pip install -U future

import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import theano
import theano.tensor as T
from theano.tensor.nnet.bn import batch_normalization_train, batch_normalization_test
from sklearn.utils import shuffle
from sklearn.model_selection import train_test_split
from util import get_normalized_data


def init_weight(M1, M2):
  return np.random.randn(M1, M2) * np.sqrt(2.0 / M1)


class HiddenLayerBatchNorm(object):
  def __init__(self, M1, M2, f):
    self.M1 = M1
    self.M2 = M2
    self.f = f

    W = init_weight(M1, M2)
    gamma = np.ones(M2)
    beta = np.zeros(M2)

    self.W = theano.shared(W)
    self.gamma = theano.shared(gamma)
    self.beta = theano.shared(beta)

    self.params = [self.W, self.gamma, self.beta]

    # for test time
    # self.running_mean = T.zeros(M2)
    # self.running_var = T.zeros(M2)
    self.running_mean = theano.shared(np.zeros(M2))
    self.running_var = theano.shared(np.zeros(M2))

  def forward(self, X, is_training):
    activation = X.dot(self.W)
    if is_training:
      # returns:
      #   batch-normalized output
      #   batch mean
      #   batch variance
      #   running mean (for later use as population mean estimate)
      #   running var (for later use as population var estimate)
      out, batch_mean, batch_invstd, new_running_mean, new_running_var = batch_normalization_train(
        activation,
        self.gamma,
        self.beta,
        running_mean=self.running_mean,
        running_var=self.running_var,
      )

      self.running_update = [
        (self.running_mean, new_running_mean),
        (self.running_var, new_running_var),
      ]

      # if you don't trust the built-in bn function
      # batch_var = 1 / (batch_invstd * batch_invstd)
      # self.running_update = [
      #   (self.running_mean, 0.9*self.running_mean + 0.1*batch_mean),
      #   (self.running_var, 0.9*self.running_var + 0.1*batch_var),
      # ]

    else:
      out = batch_normalization_test(
        activation,
        self.gamma,
        self.beta,
        self.running_mean,
        self.running_var
      )
    return self.f(out)


class HiddenLayer(object):
  def __init__(self, M1, M2, f):
    self.M1 = M1
    self.M2 = M2
    self.f = f
    W = init_weight(M1, M2)
    b = np.zeros(M2)
    self.W = theano.shared(W)
    self.b = theano.shared(b)
    self.params = [self.W, self.b]

  def forward(self, X):
    return self.f(X.dot(self.W) + self.b)


def momentum_updates(cost, params, lr, mu):
  grads = T.grad(cost, params)
  updates = []

  for p, g in zip(params, grads):
    dp = theano.shared(p.get_value() * 0)
    new_dp = mu*dp - lr*g
    new_p = p + new_dp
    updates.append((dp, new_dp))
    updates.append((p, new_p))
  return updates


class ANN(object):
  def __init__(self, hidden_layer_sizes):
    self.hidden_layer_sizes = hidden_layer_sizes

  def fit(self, X, Y, Xtest, Ytest, activation=T.nnet.relu, learning_rate=1e-2, mu=0.9, epochs=15, batch_sz=100, print_period=100, show_fig=True):
    X = X.astype(np.float32)
    Y = Y.astype(np.int32)

    # initialize hidden layers
    N, D = X.shape
    self.layers = []
    M1 = D
    for M2 in self.hidden_layer_sizes:
      h = HiddenLayerBatchNorm(M1, M2, activation)
      self.layers.append(h)
      M1 = M2
      
    # final layer
    K = len(set(Y))
    h = HiddenLayer(M1, K, T.nnet.softmax)
    self.layers.append(h)

    if batch_sz is None:
      batch_sz = N

    # collect params for later use
    self.params = []
    for h in self.layers:
      self.params += h.params

    # note! we will need to build the output differently
    # for train and test (prediction)

    # set up theano functions and variables
    thX = T.matrix('X')
    thY = T.ivector('Y')

    # for training
    p_y_given_x = self.forward(thX, is_training=True)

    cost = -T.mean(T.log(p_y_given_x[T.arange(thY.shape[0]), thY]))
    prediction = T.argmax(p_y_given_x, axis=1)
    grads = T.grad(cost, self.params)

    # momentum only
    updates = momentum_updates(cost, self.params, learning_rate, mu)
    for layer in self.layers[:-1]:
      updates += layer.running_update

    train_op = theano.function(
      inputs=[thX, thY],
      outputs=[cost, prediction],
      updates=updates,
    )

    # for testing
    test_p_y_given_x = self.forward(thX, is_training=False)
    test_prediction = T.argmax(test_p_y_given_x, axis=1)

    self.predict = theano.function(
      inputs=[thX],
      outputs=test_prediction,
    )

    n_batches = N // batch_sz
    costs = []
    for i in range(epochs):
      if n_batches > 1:
        X, Y = shuffle(X, Y)
      for j in range(n_batches):
        Xbatch = X[j*batch_sz:(j*batch_sz+batch_sz)]
        Ybatch = Y[j*batch_sz:(j*batch_sz+batch_sz)]

        c, p = train_op(Xbatch, Ybatch)
        costs.append(c)
        if (j+1) % print_period == 0:
          accuracy = np.mean(p == Ybatch)
          print("epoch:", i, "batch:", j, "n_batches:", n_batches, "cost:", c, "accuracy:", accuracy)

      print("Train acc:", self.score(X, Y), "Test acc:", self.score(Xtest, Ytest))
    
    if show_fig:
      plt.plot(costs)
      plt.show()

  def forward(self, X, is_training):
    out = X
    for h in self.layers[:-1]:
      out = h.forward(out, is_training)
    out = self.layers[-1].forward(out)
    return out

  def score(self, X, Y):
    P = self.predict(X)
    return np.mean(Y == P)



def main():
  # step 1: get the data and define all the usual variables
  Xtrain, Xtest, Ytrain, Ytest = get_normalized_data()

  ann = ANN([500, 300])
  ann.fit(Xtrain, Ytrain, Xtest, Ytest, show_fig=True)

  print("Train accuracy:", ann.score(Xtrain, Ytrain))
  print("Test accuracy:", ann.score(Xtest, Ytest))


if __name__ == '__main__':
  main()


================================================
FILE: ann_class2/cntk_example.py
================================================
# https://deeplearningcourses.com/c/data-science-deep-learning-in-theano-tensorflow
# https://www.udemy.com/data-science-deep-learning-in-theano-tensorflow
from __future__ import print_function, division
from builtins import range
# Note: you may need to update your version of future
# sudo pip install -U future


# simple installation with pip:
#
# just one line: pip install <....>.whl
#
# Linux
# https://docs.microsoft.com/en-us/cognitive-toolkit/setup-linux-python
#
# Windows
# https://docs.microsoft.com/en-us/cognitive-toolkit/setup-windows-python
#
# (No Mac)


# Note: useful to understand keras_example.py first

import numpy as np
import matplotlib.pyplot as plt
from util import get_normalized_data, y2indicator


import cntk as C
from cntk.train import Trainer
from cntk.learners import adam
from cntk.ops import relu
from cntk.layers import Dense, Sequential
from cntk.losses import cross_entropy_with_softmax
from cntk.metrics import classification_error
from cntk.train.training_session import *


# get the data, same as Theano + Tensorflow examples
Xtrain, Xtest, Ytrain, Ytest = get_normalized_data()

# get shapes
N, D = Xtrain.shape
K = len(set(Ytrain))

# we want one-hot encoded labels
Ytrain = y2indicator(Ytrain)
Ytest = y2indicator(Ytest)

# split the data
X = X.astype(np.float32)
Y = Y.astype(np.float32)
Xtest = Xtest.astype(np.float32)
Ytest = Ytest.astype(np.float32)


# the model will be a sequence of layers
model = Sequential(
  [
    Dense(500, activation=relu),
    Dense(300, activation=relu),
    Dense(K, activation=None),
  ]
)


# define the inputs and labels
inputs = C.input_variable(D, np.float32, name='inputs')
labels = C.input_variable(K, np.float32, name='labels')


# get the output
logits = model(inputs)


# define loss / metrics
# like Tensorflow the softmax is done
# internally (if needed), so all we need are the logits
ce = cross_entropy_with_softmax(logits, labels)
pe = classification_error(logits, labels)



# training config
batch_size = 32
epochs = 15
n_batches = len(Xtrain) // batch_size


# do the training

# specify the training algorithm
trainer = Trainer(logits, (ce, pe), adam(logits.parameters, lr=1e-2, momentum=0.9))


# helper function
def get_output(node, X, Y):
  ret = node.forward(dict(inputs=X, labels=Y))
  return list(ret[1].values())[0].mean()


costs = []
errors = []
test_costs = []
test_errors = []
for i in range(epochs):
  cost = 0
  err = 0
  for j in range(n_batches):
    Xbatch = Xtrain[j*batch_size:(j+1)*batch_size]
    Ybatch = Ytrain[j*batch_size:(j+1)*batch_size]

    ret = trainer.train_minibatch(
      dict(inputs=Xbatch, labels=Ybatch),
      outputs=(ce, pe)
    )

    cost += ret[1][ce].mean()
    err += ret[1][pe].mean()
  costs.append(cost / n_batches)
  errors.append(err / n_batches)

  # test set calculations
  test_cost = get_output(ce, Xtest, Ytest)
  test_err = get_output(pe, Xtest, Ytest)

  test_costs.append(test_cost)
  test_errors.append(test_err)

  print("epoch i:", i, "cost:", test_cost, "err:", test_err)

plt.plot(costs, label='train cost')
plt.plot(test_costs, label='test cost')
plt.legend()
plt.title("cost")
plt.show()

plt.plot(errors, label='train error')
plt.plot(test_errors, label='test error')
plt.legend()
plt.title("error")
plt.show()


================================================
FILE: ann_class2/dropout_tensorflow.py
================================================
from __future__ import print_function, division
from builtins import range
# Note: you may need to update your version of future
# sudo pip install -U future

# For the class Data Science: Practical Deep Learning Concepts in Theano and TensorFlow
# https://deeplearningcourses.com/c/data-science-deep-learning-in-theano-tensorflow
# https://www.udemy.com/data-science-deep-learning-in-theano-tensorflow
import numpy as np
import tensorflow as tf
import matplotlib.pyplot as plt

from util import get_normalized_data
from sklearn.utils import shuffle


class HiddenLayer(object):
    def __init__(self, M1, M2):
        self.M1 = M1
        self.M2 = M2
        W = np.random.randn(M1, M2) * np.sqrt(2.0 / M1)
        b = np.zeros(M2)
        self.W = tf.Variable(W.astype(np.float32))
        self.b = tf.Variable(b.astype(np.float32))
        self.params = [self.W, self.b]

    def forward(self, X):
        return tf.nn.relu(tf.matmul(X, self.W) + self.b)


class ANN(object):
    def __init__(self, hidden_layer_sizes, p_keep):
        self.hidden_layer_sizes = hidden_layer_sizes
        self.dropout_rates = p_keep

    def fit(self, X, Y, Xvalid, Yvalid, lr=1e-4, mu=0.9, decay=0.9, epochs=15, batch_sz=100, print_every=50):
        X = X.astype(np.float32)
        Y = Y.astype(np.int64)
        Xvalid = Xvalid.astype(np.float32)
        Yvalid = Yvalid.astype(np.int64)

        # initialize hidden layers
        N, D = X.shape
        K = len(set(Y))
        self.hidden_layers = []
        M1 = D
        for M2 in self.hidden_layer_sizes:
            h = HiddenLayer(M1, M2)
            self.hidden_layers.append(h)
            M1 = M2
        W = np.random.randn(M1, K) * np.sqrt(2.0 / M1)
        b = np.zeros(K)
        self.W = tf.Variable(W.astype(np.float32))
        self.b = tf.Variable(b.astype(np.float32))

        # collect params for later use
        self.params = [self.W, self.b]
        for h in self.hidden_layers:
            self.params += h.params

        # set up theano functions and variables
        inputs = tf.placeholder(tf.float32, shape=(None, D), name='inputs')
        labels = tf.placeholder(tf.int64, shape=(None,), name='labels')
        logits = self.forward(inputs)

        cost = tf.reduce_mean(
            tf.nn.sparse_softmax_cross_entropy_with_logits(
                logits=logits,
                labels=labels
            )
        )
        train_op = tf.train.RMSPropOptimizer(lr, decay=decay, momentum=mu).minimize(cost)
        # train_op = tf.train.MomentumOptimizer(lr, momentum=mu).minimize(cost)
        # train_op = tf.train.AdamOptimizer(lr).minimize(cost)
        prediction = self.predict(inputs)

        # validation cost will be calculated separately since nothing will be dropped
        test_logits = self.forward_test(inputs)
        test_cost = tf.reduce_mean(
            tf.nn.sparse_softmax_cross_entropy_with_logits(
                logits=test_logits,
                labels=labels
            )
        )

        n_batches = N // batch_sz
        costs = []
        init = tf.global_variables_initializer()
        with tf.Session() as session:
            session.run(init)
            for i in range(epochs):
                print("epoch:", i, "n_batches:", n_batches)
                X, Y = shuffle(X, Y)
                for j in range(n_batches):
                    Xbatch = X[j*batch_sz:(j*batch_sz+batch_sz)]
                    Ybatch = Y[j*batch_sz:(j*batch_sz+batch_sz)]

                    session.run(train_op, feed_dict={inputs: Xbatch, labels: Ybatch})

                    if j % print_every == 0:
                        c = session.run(test_cost, feed_dict={inputs: Xvalid, labels: Yvalid})
                        p = session.run(prediction, feed_dict={inputs: Xvalid})
                        costs.append(c)
                        e = error_rate(Yvalid, p)
                        print("i:", i, "j:", j, "nb:", n_batches, "cost:", c, "error rate:", e)
        
        plt.plot(costs)
        plt.show()

    def forward(self, X):
        # tf.nn.dropout scales inputs by 1/p_keep
        # therefore, during test time, we don't have to scale anything
        Z = X
        Z = tf.nn.dropout(Z, self.dropout_rates[0])
        for h, p in zip(self.hidden_layers, self.dropout_rates[1:]):
            Z = h.forward(Z)
            Z = tf.nn.dropout(Z, p)
        return tf.matmul(Z, self.W) + self.b

    def forward_test(self, X):
        Z = X
        for h in self.hidden_layers:
            Z = h.forward(Z)
        return tf.matmul(Z, self.W) + self.b

    def predict(self, X):
        pY = self.forward_test(X)
        return tf.argmax(pY, 1)


def error_rate(p, t):
    return np.mean(p != t)


def relu(a):
    return a * (a > 0)


def main():
    # step 1: get the data and define all the usual variables
    Xtrain, Xtest, Ytrain, Ytest = get_normalized_data()

    ann = ANN([500, 300], [0.8, 0.5, 0.5])
    ann.fit(Xtrain, Ytrain, Xtest, Ytest)


if __name__ == '__main__':
    main()

================================================
FILE: ann_class2/dropout_theano.py
================================================
# A 1-hidden-layer neural network in Theano.
# This code is not optimized for speed.
# It's just to get something working, using the principles we know.

# For the class Data Science: Practical Deep Learning Concepts in Theano and TensorFlow
# https://deeplearningcourses.com/c/data-science-deep-learning-in-theano-tensorflow
# https://www.udemy.com/data-science-deep-learning-in-theano-tensorflow
from __future__ import print_function, division
from builtins import range
# Note: you may need to update your version of future
# sudo pip install -U future

import numpy as np
import theano
import theano.tensor as T
import matplotlib.pyplot as plt
from theano.tensor.shared_randomstreams import RandomStreams
from util import get_normalized_data
from sklearn.utils import shuffle


def momentum_updates(cost, params, lr, mu):
  grads = T.grad(cost, params)
  updates = []

  for p, g in zip(params, grads):
    dp = theano.shared(p.get_value() * 0)
    new_dp = mu*dp - lr*g
    new_p = p + new_dp
    updates.append((dp, new_dp))
    updates.append((p, new_p))
  return updates


class HiddenLayer(object):
    def __init__(self, M1, M2, an_id):
        self.id = an_id
        self.M1 = M1
        self.M2 = M2
        W = np.random.randn(M1, M2) * np.sqrt(2.0 / M1)
        b = np.zeros(M2)
        self.W = theano.shared(W, 'W_%s' % self.id)
        self.b = theano.shared(b, 'b_%s' % self.id)
        self.params = [self.W, self.b]

    def forward(self, X):
        return T.nnet.relu(X.dot(self.W) + self.b)


class ANN(object):
    def __init__(self, hidden_layer_sizes, p_keep):
        self.hidden_layer_sizes = hidden_layer_sizes
        self.dropout_rates = p_keep

    def fit(self, X, Y, Xvalid, Yvalid, learning_rate=1e-2, mu=0.9, decay=0.9, epochs=10, batch_sz=100, show_fig=False):
        X = X.astype(np.float32)
        Y = Y.astype(np.int32)
        Xvalid = Xvalid.astype(np.float32)
        Yvalid = Yvalid.astype(np.int32)

        self.rng = RandomStreams()

        # initialize hidden layers
        N, D = X.shape
        K = len(set(Y))
        self.hidden_layers = []
        M1 = D
        count = 0
        for M2 in self.hidden_layer_sizes:
            h = HiddenLayer(M1, M2, count)
            self.hidden_layers.append(h)
            M1 = M2
            count += 1
        W = np.random.randn(M1, K) * np.sqrt(2.0 / M1)
        b = np.zeros(K)
        self.W = theano.shared(W, 'W_logreg')
        self.b = theano.shared(b, 'b_logreg')

        # collect params for later use
        self.params = [self.W, self.b]
        for h in self.hidden_layers:
            self.params += h.params

        # set up theano functions and variables
        thX = T.matrix('X')
        thY = T.ivector('Y')
        pY_train = self.forward_train(thX)

        # this cost is for training
        cost = -T.mean(T.log(pY_train[T.arange(thY.shape[0]), thY]))
        updates = momentum_updates(cost, self.params, learning_rate, mu)

        train_op = theano.function(
            inputs=[thX, thY],
            updates=updates
        )

        # for evaluation and prediction
        pY_predict = self.forward_predict(thX)
        cost_predict = -T.mean(T.log(pY_predict[T.arange(thY.shape[0]), thY]))
        prediction = self.predict(thX)
        cost_predict_op = theano.function(inputs=[thX, thY], outputs=[cost_predict, prediction])

        n_batches = N // batch_sz
        costs = []
        for i in range(epochs):
            X, Y = shuffle(X, Y)
            for j in range(n_batches):
                Xbatch = X[j*batch_sz:(j*batch_sz+batch_sz)]
                Ybatch = Y[j*batch_sz:(j*batch_sz+batch_sz)]

                train_op(Xbatch, Ybatch)

                if j % 50 == 0:
                    c, p = cost_predict_op(Xvalid, Yvalid)
                    costs.append(c)
                    e = error_rate(Yvalid, p)
                    print("i:", i, "j:", j, "nb:", n_batches, "cost:", c, "error rate:", e)
        
        if show_fig:
            plt.plot(costs)
            plt.show()

    def forward_train(self, X):
        Z = X
        for h, p in zip(self.hidden_layers, self.dropout_rates[:-1]):
            mask = self.rng.binomial(n=1, p=p, size=Z.shape)
            Z = mask * Z
            Z = h.forward(Z)
        mask = self.rng.binomial(n=1, p=self.dropout_rates[-1], size=Z.shape)
        Z = mask * Z
        return T.nnet.softmax(Z.dot(self.W) + self.b)

    def forward_predict(self, X):
        Z = X
        for h, p in zip(self.hidden_layers, self.dropout_rates[:-1]):
            Z = h.forward(p * Z)
        return T.nnet.softmax((self.dropout_rates[-1] * Z).dot(self.W) + self.b)

    def predict(self, X):
        pY = self.forward_predict(X)
        return T.argmax(pY, axis=1)


def error_rate(p, t):
    return np.mean(p != t)


def relu(a):
    return a * (a > 0)


def main():
    # step 1: get the data and define all the usual variables
    Xtrain, Xtest, Ytrain, Ytest = get_normalized_data()

    ann = ANN([500, 300], [0.8, 0.5, 0.5])
    ann.fit(Xtrain, Ytrain, Xtest, Ytest, show_fig=True)


if __name__ == '__main__':
    main()


================================================
FILE: ann_class2/extra_reading.txt
================================================
The Marginal Value of Adaptive Gradient Methods in Machine Learning
https://arxiv.org/abs/1705.08292

Asynchronous Stochastic Gradient Descent with Delay Compensation for Distributed Deep Learning
https://arxiv.org/abs/1609.08326

Asynchronous Stochastic Gradient Descent with Variance Reduction for Non-Convex Optimization
https://arxiv.org/abs/1604.03584

Adam: A Method for Stochastic Optimization
https://arxiv.org/abs/1412.6980

Large Scale Distributed Deep Networks
https://static.googleusercontent.com/media/research.google.com/en//archive/large_deep_networks_nips2012.pdf

Batch Normalization: Accelerating Deep Network Training by Reducing Internal Covariate Shift
Sergey Ioffe, Christian Szegedy
https://arxiv.org/abs/1502.03167

Xavier (Glorot) Normal Initializer
http://jmlr.org/proceedings/papers/v9/glorot10a/glorot10a.pdf

He Normal Initializer
http://arxiv.org/abs/1502.01852

For understanding Nesterov Momentum:
Advances in optimizing Recurrent Networks by Yoshua Bengio, Section 3.5
http://arxiv.org/pdf/1212.0901v2.pdf

Dropout: A Simple Way to Prevent Neural Networks from Overfitting
https://www.cs.toronto.edu/~hinton/absps/JMLRdropout.pdf

The Loss Surfaces of Multilayer Networks
https://arxiv.org/pdf/1412.0233.pdf

================================================
FILE: ann_class2/grid_search.py
================================================
# For the class Data Science: Practical Deep Learning Concepts in Theano and TensorFlow
# https://deeplearningcourses.com/c/data-science-deep-learning-in-theano-tensorflow
# https://www.udemy.com/data-science-deep-learning-in-theano-tensorflow
from __future__ import print_function, division
from builtins import range
# Note: you may need to update your version of future
# sudo pip install -U future


import theano.tensor as T
from theano_ann import ANN
from util import get_spiral, get_clouds
from sklearn.utils import shuffle
import matplotlib.pyplot as plt
import numpy as np


def grid_search():
  # get the data and split into train/test
  X, Y = get_spiral()
  # X, Y = get_clouds()
  X, Y = shuffle(X, Y)
  Ntrain = int(0.7*len(X))
  Xtrain, Ytrain = X[:Ntrain], Y[:Ntrain]
  Xtest, Ytest = X[Ntrain:], Y[Ntrain:]

  # hyperparameters to try
  hidden_layer_sizes = [
    [300],
    [100,100],
    [50,50,50],
  ]
  learning_rates = [1e-4, 1e-3, 1e-2]
  l2_penalties = [0., 0.1, 1.0]

  # loop through all possible hyperparameter settings
  best_validation_rate = 0
  best_hls = None
  best_lr = None
  best_l2 = None
  for hls in hidden_layer_sizes:
    for lr in learning_rates:
      for l2 in l2_penalties:
        model = ANN(hls)
        model.fit(Xtrain, Ytrain, learning_rate=lr, reg=l2, mu=0.99, epochs=3000, show_fig=False)
        validation_accuracy = model.score(Xtest, Ytest)
        train_accuracy = model.score(Xtrain, Ytrain)
        print(
          "validation_accuracy: %.3f, train_accuracy: %.3f, settings: %s, %s, %s" %
            (validation_accuracy, train_accuracy, hls, lr, l2)
        )
        if validation_accuracy > best_validation_rate:
          best_validation_rate = validation_accuracy
          best_hls = hls
          best_lr = lr
          best_l2 = l2
  print("Best validation_accuracy:", best_validation_rate)
  print("Best settings:")
  print("hidden_layer_sizes:", best_hls)
  print("learning_rate:", best_lr)
  print("l2:", best_l2)


# def one():
#   X, Y = get_spiral()
#   X, Y = shuffle(X, Y)
#   Ntrain = int(0.7*len(X))
#   Xtrain, Ytrain = X[:Ntrain], Y[:Ntrain]
#   # plt.scatter(Xtrain[:,0], Xtrain[:,1], c=Ytrain)
#   # plt.show()
#   Xtest, Ytest = X[Ntrain:], Y[Ntrain:]

#   model = ANN([100,100])
#   model.fit(Xtrain, Ytrain, activation=T.nnet.relu, learning_rate=1e-3, mu=0.99, reg=0., epochs=3000, show_fig=True)
#   validation_accuracy = model.score(Xtest, Ytest)
#   print("validation_accuracy:", validation_accuracy)
#   print("train accuracy:", model.score(Xtrain, Ytrain))


if __name__ == '__main__':
  grid_search()
  # one()


================================================
FILE: ann_class2/keras_example.py
================================================
# https://deeplearningcourses.com/c/data-science-deep-learning-in-theano-tensorflow
# https://www.udemy.com/data-science-deep-learning-in-theano-tensorflow
from __future__ import print_function, division
from builtins import range
# Note: you may need to update your version of future
# sudo pip install -U future

from keras.models import Sequential
from keras.layers import Dense, Activation
from util import get_normalized_data, y2indicator

import matplotlib.pyplot as plt

# NOTE: do NOT name your file keras.py because it will conflict
# with importing keras

# installation is easy! just the usual "sudo pip(3) install keras"


# get the data, same as Theano + Tensorflow examples
# no need to split now, the fit() function will do it
Xtrain, Xtest, Ytrain, Ytest = get_normalized_data()

# get shapes
N, D = Xtrain.shape
K = len(set(Ytrain))

# by default Keras wants one-hot encoded labels
# there's another cost function we can use
# where we can just pass in the integer labels directly
# just like Tensorflow / Theano
Ytrain = y2indicator(Ytrain)
Ytest = y2indicator(Ytest)


# the model will be a sequence of layers
model = Sequential()


# ANN with layers [784] -> [500] -> [300] -> [10]
model.add(Dense(units=500, input_dim=D))
model.add(Activation('relu'))
model.add(Dense(units=300)) # don't need to specify input_dim
model.add(Activation('relu'))
model.add(Dense(units=K))
model.add(Activation('softmax'))


# list of losses: https://keras.io/losses/
# list of optimizers: https://keras.io/optimizers/
# list of metrics: https://keras.io/metrics/
model.compile(
  loss='categorical_crossentropy',
  optimizer='adam',
  metrics=['accuracy']
)

# note: multiple ways to choose a backend
# either theano, tensorflow, or cntk
# https://keras.io/backend/


# gives us back a <keras.callbacks.History object at 0x112e61a90>
r = model.fit(Xtrain, Ytrain, validation_data=(Xtest, Ytest), epochs=15, batch_size=32)
print("Returned:", r)

# print the available keys
# should see: dict_keys(['val_loss', 'acc', 'loss', 'val_acc'])
print(r.history.keys())

# plot some data
plt.plot(r.history['loss'], label='loss')
plt.plot(r.history['val_loss'], label='val_loss')
plt.legend()
plt.show()

# accuracies
plt.plot(r.history['accuracy'], label='acc')
plt.plot(r.history['val_accuracy'], label='val_acc')
plt.legend()
plt.show()




================================================
FILE: ann_class2/keras_functional.py
================================================
# https://deeplearningcourses.com/c/data-science-deep-learning-in-theano-tensorflow
# https://www.udemy.com/data-science-deep-learning-in-theano-tensorflow
from __future__ import print_function, division
from builtins import range
# Note: you may need to update your version of future
# sudo pip install -U future

from keras.models import Model
from keras.layers import Dense, Input
from util import get_normalized_data, y2indicator

import matplotlib.pyplot as plt

# NOTE: do NOT name your file keras.py because it will conflict
# with importing keras

# installation is easy! just the usual "sudo pip(3) install keras"


# get the data, same as Theano + Tensorflow examples
# no need to split now, the fit() function will do it
Xtrain, Xtest, Ytrain, Ytest = get_normalized_data()

# get shapes
N, D = Xtrain.shape
K = len(set(Ytrain))

# by default Keras wants one-hot encoded labels
# there's another cost function we can use
# where we can just pass in the integer labels directly
# just like Tensorflow / Theano
Ytrain = y2indicator(Ytrain)
Ytest = y2indicator(Ytest)


# ANN with layers [784] -> [500] -> [300] -> [10]
i = Input(shape=(D,))
x = Dense(500, activation='relu')(i)
x = Dense(300, activation='relu')(x)
x = Dense(K, activation='softmax')(x)
model = Model(inputs=i, outputs=x)


# list of losses: https://keras.io/losses/
# list of optimizers: https://keras.io/optimizers/
# list of metrics: https://keras.io/metrics/
model.compile(
  loss='categorical_crossentropy',
  optimizer='adam',
  metrics=['accuracy']
)

# note: multiple ways to choose a backend
# either theano, tensorflow, or cntk
# https://keras.io/backend/


# gives us back a <keras.callbacks.History object at 0x112e61a90>
r = model.fit(Xtrain, Ytrain, validation_data=(Xtest, Ytest), epochs=15, batch_size=32)
print("Returned:", r)

# print the available keys
# should see: dict_keys(['val_loss', 'acc', 'loss', 'val_acc'])
print(r.history.keys())

# plot some data
plt.plot(r.history['loss'], label='loss')
plt.plot(r.history['val_loss'], label='val_loss')
plt.legend()
plt.show()

# accuracies
plt.plot(r.history['accuracy'], label='acc')
plt.plot(r.history['val_accuracy'], label='val_acc')
plt.legend()
plt.show()




================================================
FILE: ann_class2/mlp.py
================================================
# Simple multi-layer preceptron / neural network in Python and Numpy
# For the class Data Science: Practical Deep Learning Concepts in Theano and TensorFlow
# https://deeplearningcourses.com/c/data-science-deep-learning-in-theano-tensorflow
# https://www.udemy.com/data-science-deep-learning-in-theano-tensorflow
from __future__ import print_function, division
from builtins import range
# Note: you may need to update your version of future
# sudo pip install -U future

import numpy as np

def forward(X, W1, b1, W2, b2):
    # sigmoid
    # Z = 1 / (1 + np.exp(-( X.dot(W1) + b1 )))

    # relu
    Z = X.dot(W1) + b1
    Z[Z < 0] = 0

    A = Z.dot(W2) + b2
    expA = np.exp(A)
    Y = expA / expA.sum(axis=1, keepdims=True)
    return Y, Z

def derivative_w2(Z, T, Y):
    return Z.T.dot(Y - T)

def derivative_b2(T, Y):
    return (Y - T).sum(axis=0)

def derivative_w1(X, Z, T, Y, W2):
    # return X.T.dot( ( ( Y-T ).dot(W2.T) * ( Z*(1 - Z) ) ) ) # for sigmoid
    return X.T.dot( ( ( Y-T ).dot(W2.T) * (Z > 0) ) ) # for relu

def derivative_b1(Z, T, Y, W2):
    # return (( Y-T ).dot(W2.T) * ( Z*(1 - Z) )).sum(axis=0) # for sigmoid
    return (( Y-T ).dot(W2.T) * (Z > 0)).sum(axis=0) # for relu


================================================
FILE: ann_class2/momentum.py
================================================
# Compare momentum with regular gradient descent
# For the class Data Science: Practical Deep Learning Concepts in Theano and TensorFlow
# https://deeplearningcourses.com/c/data-science-deep-learning-in-theano-tensorflow
# https://www.udemy.com/data-science-deep-learning-in-theano-tensorflow

# NOTE: MUST restrict initial values of W by dividing by #
# NOTE: sigmoid vs. rectifier for hiddens
# We get 15% error rate with sigmoid, 3% error rate with ReLU
from __future__ import print_function, division
from builtins import range
# Note: you may need to update your version of future
# sudo pip install -U future

import numpy as np
from sklearn.utils import shuffle
import matplotlib.pyplot as plt

from util import get_normalized_data, error_rate, cost, y2indicator
from mlp import forward, derivative_w2, derivative_w1, derivative_b2, derivative_b1


def main():
    # compare 3 scenarios:
    # 1. batch SGD
    # 2. batch SGD with momentum
    # 3. batch SGD with Nesterov momentum

    max_iter = 20 # make it 30 for sigmoid
    print_period = 50

    Xtrain, Xtest, Ytrain, Ytest = get_normalized_data()
    lr = 0.00004
    reg = 0.01

    Ytrain_ind = y2indicator(Ytrain)
    Ytest_ind = y2indicator(Ytest)

    N, D = Xtrain.shape
    batch_sz = 500
    n_batches = N // batch_sz

    M = 300
    K = 10
    W1 = np.random.randn(D, M) / np.sqrt(D)
    b1 = np.zeros(M)
    W2 = np.random.randn(M, K) / np.sqrt(M)
    b2 = np.zeros(K)

    # save initial weights
    W1_0 = W1.copy()
    b1_0 = b1.copy()
    W2_0 = W2.copy()
    b2_0 = b2.copy()

    # 1. batch
    losses_batch = []
    errors_batch = []
    for i in range(max_iter):
        Xtrain, Ytrain, Ytrain_ind = shuffle(Xtrain, Ytrain, Ytrain_ind)
        for j in range(n_batches):
            Xbatch = Xtrain[j*batch_sz:(j*batch_sz + batch_sz),]
            Ybatch = Ytrain_ind[j*batch_sz:(j*batch_sz + batch_sz),]
            pYbatch, Z = forward(Xbatch, W1, b1, W2, b2)
            # print "first batch cost:", cost(pYbatch, Ybatch)

            # gradients
            gW2 = derivative_w2(Z, Ybatch, pYbatch) + reg*W2
            gb2 = derivative_b2(Ybatch, pYbatch) + reg*b2
            gW1 = derivative_w1(Xbatch, Z, Ybatch, pYbatch, W2) + reg*W1
            gb1 = derivative_b1(Z, Ybatch, pYbatch, W2) + reg*b1

            # updates
            W2 -= lr*gW2
            b2 -= lr*gb2
            W1 -= lr*gW1
            b1 -= lr*gb1

            if j % print_period == 0:
                pY, _ = forward(Xtest, W1, b1, W2, b2)
                l = cost(pY, Ytest_ind)
                losses_batch.append(l)
                print("Cost at iteration i=%d, j=%d: %.6f" % (i, j, l))

                e = error_rate(pY, Ytest)
                errors_batch.append(e)
                print("Error rate:", e)

    pY, _ = forward(Xtest, W1, b1, W2, b2)
    print("Final error rate:", error_rate(pY, Ytest))

    # 2. batch with momentum
    W1 = W1_0.copy()
    b1 = b1_0.copy()
    W2 = W2_0.copy()
    b2 = b2_0.copy()
    losses_momentum = []
    errors_momentum = []
    mu = 0.9
    dW2 = 0
    db2 = 0
    dW1 = 0
    db1 = 0
    for i in range(max_iter):
        Xtrain, Ytrain, Ytrain_ind = shuffle(Xtrain, Ytrain, Ytrain_ind)
        for j in range(n_batches):
            Xbatch = Xtrain[j*batch_sz:(j*batch_sz + batch_sz),]
            Ybatch = Ytrain_ind[j*batch_sz:(j*batch_sz + batch_sz),]
            pYbatch, Z = forward(Xbatch, W1, b1, W2, b2)

            # gradients
            gW2 = derivative_w2(Z, Ybatch, pYbatch) + reg*W2
            gb2 = derivative_b2(Ybatch, pYbatch) + reg*b2
            gW1 = derivative_w1(Xbatch, Z, Ybatch, pYbatch, W2) + reg*W1
            gb1 = derivative_b1(Z, Ybatch, pYbatch, W2) + reg*b1

            # update velocities
            dW2 = mu*dW2 - lr*gW2
            db2 = mu*db2 - lr*gb2
            dW1 = mu*dW1 - lr*gW1
            db1 = mu*db1 - lr*gb1

            # updates
            W2 += dW2
            b2 += db2
            W1 += dW1
            b1 += db1

            if j % print_period == 0:
                pY, _ = forward(Xtest, W1, b1, W2, b2)
                l = cost(pY, Ytest_ind)
                losses_momentum.append(l)
                print("Cost at iteration i=%d, j=%d: %.6f" % (i, j, l))

                e = error_rate(pY, Ytest)
                errors_momentum.append(e)
                print("Error rate:", e)
    pY, _ = forward(Xtest, W1, b1, W2, b2)
    print("Final error rate:", error_rate(pY, Ytest))


    # 3. batch with Nesterov momentum
    W1 = W1_0.copy()
    b1 = b1_0.copy()
    W2 = W2_0.copy()
    b2 = b2_0.copy()

    losses_nesterov = []
    errors_nesterov = []

    mu = 0.9
    vW2 = 0
    vb2 = 0
    vW1 = 0
    vb1 = 0
    for i in range(max_iter):
        Xtrain, Ytrain, Ytrain_ind = shuffle(Xtrain, Ytrain, Ytrain_ind)
        for j in range(n_batches):
            Xbatch = Xtrain[j*batch_sz:(j*batch_sz + batch_sz),]
            Ybatch = Ytrain_ind[j*batch_sz:(j*batch_sz + batch_sz),]
            pYbatch, Z = forward(Xbatch, W1, b1, W2, b2)

            # updates
            gW2 = derivative_w2(Z, Ybatch, pYbatch) + reg*W2
            gb2 = derivative_b2(Ybatch, pYbatch) + reg*b2
            gW1 = derivative_w1(Xbatch, Z, Ybatch, pYbatch, W2) + reg*W1
            gb1 = derivative_b1(Z, Ybatch, pYbatch, W2) + reg*b1

            # v update
            vW2 = mu*vW2 - lr*gW2
            vb2 = mu*vb2 - lr*gb2
            vW1 = mu*vW1 - lr*gW1
            vb1 = mu*vb1 - lr*gb1

            # param update
            W2 += mu*vW2 - lr*gW2
            b2 += mu*vb2 - lr*gb2
            W1 += mu*vW1 - lr*gW1
            b1 += mu*vb1 - lr*gb1

            if j % print_period == 0:
                pY, _ = forward(Xtest, W1, b1, W2, b2)
                l = cost(pY, Ytest_ind)
                losses_nesterov.append(l)
                print("Cost at iteration i=%d, j=%d: %.6f" % (i, j, l))

                e = error_rate(pY, Ytest)
                errors_nesterov.append(e)
                print("Error rate:", e)
    pY, _ = forward(Xtest, W1, b1, W2, b2)
    print("Final error rate:", error_rate(pY, Ytest))



    plt.plot(losses_batch, label="batch")
    plt.plot(losses_momentum, label="momentum")
    plt.plot(losses_nesterov, label="nesterov")
    plt.legend()
    plt.show()



if __name__ == '__main__':
    main()


================================================
FILE: ann_class2/mxnet_example.py
================================================
# https://deeplearningcourses.com/c/data-science-deep-learning-in-theano-tensorflow
# https://www.udemy.com/data-science-deep-learning-in-theano-tensorflow
from __future__ import print_function, division
from builtins import range
# Note: you may need to update your version of future
# sudo pip install -U future


# installation is just one line:
# https://mxnet.incubator.apache.org/get_started/install.html
#
# Mac:
# pip install mxnet
#
# Linux (GPU):
# pip install mxnet-cu80
#
# Windows (a little more involved):
# https://mxnet.incubator.apache.org/get_started/windows_setup.html

import mxnet as mx
import numpy as np
import matplotlib.pyplot as plt
from util import get_normalized_data, y2indicator



# get the data, same as Theano + Tensorflow examples
# no need to split now, the fit() function will do it
Xtrain, Xtest, Ytrain, Ytest = get_normalized_data()

# get shapes
N, D = Xtrain.shape
K = len(set(Ytrain))

# training config
batch_size = 32
epochs = 15


# convert the data into a format appropriate for input into mxnet
train_iterator = mx.io.NDArrayIter(
  Xtrain,
  Ytrain,
  batch_size,
  shuffle=True
)
test_iterator = mx.io.NDArrayIter(Xtest, Ytest, batch_size)



# define a placeholder to represent the inputs
data = mx.sym.var('data')


# define the model architecture
a1 = mx.sym.FullyConnected(data=data, num_hidden=500)
z1 = mx.sym.Activation(data=a1, act_type="relu")
a2 = mx.sym.FullyConnected(data=z1, num_hidden = 300)
z2 = mx.sym.Activation(data=a2, act_type="relu")
a3 = mx.sym.FullyConnected(data=z2, num_hidden=K)
y  = mx.sym.SoftmaxOutput(data=a3, name='softmax')




# train it

# required in order for progress to be printed
import logging
logging.getLogger().setLevel(logging.DEBUG)

# use mx.gpu() if you have gpu
model = mx.mod.Module(symbol=y, context=mx.cpu())
model.fit(
  train_iterator, # train data
  eval_data=test_iterator,  # validation data
  optimizer=mx.optimizer.Adam(),
  eval_metric='acc',  # report accuracy during training
  batch_end_callback = mx.callback.Speedometer(batch_size, 100), # output progress for each 100 data batches
  num_epoch=epochs,
)
# no return value
# list of optimizers: https://mxnet.incubator.apache.org/api/python/optimization.html


# test it
# predict accuracy of mlp
acc = mx.metric.Accuracy()
model.score(test_iterator, acc)
print(acc)
print(acc.get())


# currently, there is no good way to plot the training loss / accuracy history
# https://github.com/apache/incubator-mxnet/issues/2873
#
# some have suggested parsing the logs
# https://github.com/apache/incubator-mxnet/blob/master/example/kaggle-ndsb1/training_curves.py


================================================
FILE: ann_class2/pytorch_batchnorm.py
================================================
# https://deeplearningcourses.com/c/data-science-deep-learning-in-theano-tensorflow
# https://www.udemy.com/data-science-deep-learning-in-theano-tensorflow
from __future__ import print_function, division
from builtins import range
# Note: you may need to update your version of future
# sudo pip install -U future

# Note: is helpful to look at keras_example.py first


import numpy as np
import matplotlib.pyplot as plt
from util import get_normalized_data

import torch
from torch.autograd import Variable
from torch import optim



# get the data, same as Theano + Tensorflow examples
# no need to split now, the fit() function will do it
Xtrain, Xtest, Ytrain, Ytest = get_normalized_data()

# get shapes
_, D = Xtrain.shape
K = len(set(Ytrain))

# Note: no need to convert Y to indicator matrix


# the model will be a sequence of layers
model = torch.nn.Sequential()


# ANN with layers [784] -> [500] -> [300] -> [10]
model.add_module("dense1", torch.nn.Linear(D, 500))
model.add_module("bn1", torch.nn.BatchNorm1d(500))
model.add_module("relu1", torch.nn.ReLU())
model.add_module("dense2", torch.nn.Linear(500, 300))
model.add_module("bn2", torch.nn.BatchNorm1d(300))
model.add_module("relu2", torch.nn.ReLU())
model.add_module("dense3", torch.nn.Linear(300, K))
# Note: no final softmax!
# just like Tensorflow, it's included in cross-entropy function


# define a loss function
# other loss functions can be found here:
# http://pytorch.org/docs/master/nn.html#loss-functions
loss = torch.nn.CrossEntropyLoss(size_average=True)
# Note: this returns a function!
# e.g. use it like: loss(logits, labels)


# define an optimizer
# other optimizers can be found here:
# http://pytorch.org/docs/master/optim.html
optimizer = optim.Adam(model.parameters(), lr=1e-4)



# define the training procedure
# i.e. one step of gradient descent
# there are lots of steps
# so we encapsulate it in a function
# Note: inputs and labels are torch tensors
def train(model, loss, optimizer, inputs, labels):
  # set the model to training mode
  # because batch norm has 2 different modes!
  model.train()

  inputs = Variable(inputs, requires_grad=False)
  labels = Variable(labels, requires_grad=False)

  # Reset gradient
  optimizer.zero_grad()

  # Forward
  logits = model.forward(inputs)
  output = loss.forward(logits, labels)

  # Backward
  output.backward()

  # Update parameters
  optimizer.step()

  # what's the difference between backward() and step()?

  return output.item()


# similar to train() but not doing the backprop step
def get_cost(model, loss, inputs, labels):
  # set the model to testing mode
  # because batch norm has 2 different modes!
  model.eval()

  inputs = Variable(inputs, requires_grad=False)
  labels = Variable(labels, requires_grad=False)

  # Forward
  logits = model.forward(inputs)
  output = loss.forward(logits, labels)

  return output.item()


# define the prediction procedure
# also encapsulate these steps
# Note: inputs is a torch tensor
def predict(model, inputs):
  # set the model to testing mode
  # because batch norm has 2 different modes!
  model.eval()

  inputs = Variable(inputs, requires_grad=False)
  logits = model.forward(inputs)
  return logits.data.numpy().argmax(axis=1)


# return the accuracy
# labels is a torch tensor
# to get back the internal numpy data
# use the instance method .numpy()
def score(model, inputs, labels):
  predictions = predict(model, inputs)
  return np.mean(labels.numpy() == predictions)


### prepare for training loop ###

# convert the data arrays into torch tensors
Xtrain = torch.from_numpy(Xtrain).float()
Ytrain = torch.from_numpy(Ytrain).long()
Xtest = torch.from_numpy(Xtest).float()
Ytest = torch.from_numpy(Ytest).long()

# training parameters
epochs = 15
batch_size = 32
n_batches = Xtrain.size()[0] // batch_size

# things to keep track of
train_costs = []
test_costs = []
train_accuracies = []
test_accuracies = []

# main training loop
for i in range(epochs):
  cost = 0
  test_cost = 0
  for j in range(n_batches):
    Xbatch = Xtrain[j*batch_size:(j+1)*batch_size]
    Ybatch = Ytrain[j*batch_size:(j+1)*batch_size]
    cost += train(model, loss, optimizer, Xbatch, Ybatch)

  
  # we could have also calculated the train cost here
  # but I wanted to show you that we could also return it
  # from the train function itself
  train_acc = score(model, Xtrain, Ytrain)
  test_acc = score(model, Xtest, Ytest)
  test_cost = get_cost(model, loss, Xtest, Ytest)

  print("Epoch: %d, cost: %f, acc: %.2f" % (i, test_cost, test_acc))

  # for plotting
  train_costs.append(cost / n_batches)
  train_accuracies.append(train_acc)
  test_costs.append(test_cost)
  test_accuracies.append(test_acc)



# plot the results
plt.plot(train_costs, label='Train cost')
plt.plot(test_costs, label='Test cost')
plt.title('Cost')
plt.legend()
plt.show()

plt.plot(train_accuracies, label='Train accuracy')
plt.plot(test_accuracies, label='Test accuracy')
plt.title('Accuracy')
plt.legend()
plt.show()


================================================
FILE: ann_class2/pytorch_dropout.py
================================================
# https://deeplearningcourses.com/c/data-science-deep-learning-in-theano-tensorflow
# https://www.udemy.com/data-science-deep-learning-in-theano-tensorflow
from __future__ import print_function, division
from builtins import range
# Note: you may need to update your version of future
# sudo pip install -U future

# Note: is helpful to look at keras_example.py first


import numpy as np
import matplotlib.pyplot as plt
from util import get_normalized_data

import torch
from torch.autograd import Variable
from torch import optim



# get the data, same as Theano + Tensorflow examples
# no need to split now, the fit() function will do it
Xtrain, Xtest, Ytrain, Ytest = get_normalized_data()

# get shapes
_, D = Xtrain.shape
K = len(set(Ytrain))

# Note: no need to convert Y to indicator matrix


# the model will be a sequence of layers
model = torch.nn.Sequential()


# ANN with layers [784] -> [500] -> [300] -> [10]
# NOTE: the "p" is p_drop, not p_keep
model.add_module("dropout1", torch.nn.Dropout(p=0.2))
model.add_module("dense1", torch.nn.Linear(D, 500))
model.add_module("relu1", torch.nn.ReLU())
model.add_module("dropout2", torch.nn.Dropout(p=0.5))
model.add_module("dense2", torch.nn.Linear(500, 300))
model.add_module("relu2", torch.nn.ReLU())
model.add_module("dropout3", torch.nn.Dropout(p=0.5))
model.add_module("dense3", torch.nn.Linear(300, K))
# Note: no final softmax!
# just like Tensorflow, it's included in cross-entropy function


# define a loss function
# other loss functions can be found here:
# http://pytorch.org/docs/master/nn.html#loss-functions
loss = torch.nn.CrossEntropyLoss(size_average=True)
# Note: this returns a function!
# e.g. use it like: loss(logits, labels)


# define an optimizer
# other optimizers can be found here:
# http://pytorch.org/docs/master/optim.html
optimizer = optim.Adam(model.parameters(), lr=1e-4)


# define the training procedure
# i.e. one step of gradient descent
# there are lots of steps
# so we encapsulate it in a function
# Note: inputs and labels are torch tensors
def train(model, loss, optimizer, inputs, labels):
  # set the model to training mode
  # because dropout has 2 different modes!
  model.train()

  inputs = Variable(inputs, requires_grad=False)
  labels = Variable(labels, requires_grad=False)

  # Reset gradient
  optimizer.zero_grad()

  # Forward
  logits = model.forward(inputs)
  output = loss.forward(logits, labels)

  # Backward
  output.backward()

  # Update parameters
  optimizer.step()

  # what's the difference between backward() and step()?

  return output.item()


# similar to train() but not doing the backprop step
def get_cost(model, loss, inputs, labels):
  # set the model to testing mode
  # because dropout has 2 different modes!
  model.eval()

  inputs = Variable(inputs, requires_grad=False)
  labels = Variable(labels, requires_grad=False)

  # Forward
  logits = model.forward(inputs)
  output = loss.forward(logits, labels)

  return output.item()


# define the prediction procedure
# also encapsulate these steps
# Note: inputs is a torch tensor
def predict(model, inputs):
  # set the model to testing mode
  # because dropout has 2 different modes!
  model.eval()

  inputs = Variable(inputs, requires_grad=False)
  logits = model.forward(inputs)
  return logits.data.numpy().argmax(axis=1)


# return the accuracy
# labels is a torch tensor
# to get back the internal numpy data
# use the instance method .numpy()
def score(model, inputs, labels):
  predictions = predict(model, inputs)
  return np.mean(labels.numpy() == predictions)


### prepare for training loop ###

# convert the data arrays into torch tensors
Xtrain = torch.from_numpy(Xtrain).float()
Ytrain = torch.from_numpy(Ytrain).long()
Xtest = torch.from_numpy(Xtest).float()
Ytest = torch.from_numpy(Ytest).long()

# training parameters
epochs = 15
batch_size = 32
n_batches = Xtrain.size()[0] // batch_size

# things to keep track of
train_costs = []
test_costs = []
train_accuracies = []
test_accuracies = []

# main training loop
for i in range(epochs):
  cost = 0
  test_cost = 0
  for j in range(n_batches):
    Xbatch = Xtrain[j*batch_size:(j+1)*batch_size]
    Ybatch = Ytrain[j*batch_size:(j+1)*batch_size]
    cost += train(model, loss, optimizer, Xbatch, Ybatch)

  
  # we could have also calculated the train cost here
  # but I wanted to show you that we could also return it
  # from the train function itself
  train_acc = score(model, Xtrain, Ytrain)
  test_acc = score(model, Xtest, Ytest)
  test_cost = get_cost(model, loss, Xtest, Ytest)

  print("Epoch: %d, cost: %f, acc: %.2f" % (i, test_cost, test_acc))

  # for plotting
  train_costs.append(cost / n_batches)
  train_accuracies.append(train_acc)
  test_costs.append(test_cost)
  test_accuracies.append(test_acc)



# plot the results
plt.plot(train_costs, label='Train cost')
plt.plot(test_costs, label='Test cost')
plt.title('Cost')
plt.legend()
plt.show()

plt.plot(train_accuracies, label='Train accuracy')
plt.plot(test_accuracies, label='Test accuracy')
plt.title('Accuracy')
plt.legend()
plt.show()


================================================
FILE: ann_class2/pytorch_example.py
================================================
# https://deeplearningcourses.com/c/data-science-deep-learning-in-theano-tensorflow
# https://www.udemy.com/data-science-deep-learning-in-theano-tensorflow
from __future__ import print_function, division
from builtins import range
# Note: you may need to update your version of future
# sudo pip install -U future

# Note: is helpful to look at keras_example.py first


import numpy as np
import matplotlib.pyplot as plt
from util import get_normalized_data

import torch
from torch.autograd import Variable
from torch import optim



# get the data, same as Theano + Tensorflow examples
# no need to split now, the fit() function will do it
Xtrain, Xtest, Ytrain, Ytest = get_normalized_data()

# get shapes
_, D = Xtrain.shape
K = len(set(Ytrain))

# Note: no need to convert Y to indicator matrix


# the model will be a sequence of layers
model = torch.nn.Sequential()


# ANN with layers [784] -> [500] -> [300] -> [10]
model.add_module("dense1", torch.nn.Linear(D, 500))
model.add_module("relu1", torch.nn.ReLU())
model.add_module("dense2", torch.nn.Linear(500, 300))
model.add_module("relu2", torch.nn.ReLU())
model.add_module("dense3", torch.nn.Linear(300, K))
# Note: no final softmax!
# just like Tensorflow, it's included in cross-entropy function



# define a loss function
# other loss functions can be found here:
# http://pytorch.org/docs/master/nn.html#loss-functions
loss = torch.nn.CrossEntropyLoss(size_average=True)



# define an optimizer
# other optimizers can be found here:
# http://pytorch.org/docs/master/optim.html
optimizer = optim.Adam(model.parameters())



# define the training procedure
# i.e. one step of gradient descent
# there are lots of steps
# so we encapsulate it in a function
# Note: inputs and labels are torch tensors
def train(model, loss, optimizer, inputs, labels):
  # https://discuss.pytorch.org/t/why-is-it-recommended-to-wrap-your-data-with-variable-each-step-of-the-iterations-rather-than-before-training-starts/12683
  inputs = Variable(inputs, requires_grad=False)
  labels = Variable(labels, requires_grad=False)

  # Reset gradient
  # https://discuss.pytorch.org/t/why-do-we-need-to-set-the-gradients-manually-to-zero-in-pytorch/4903/7
  optimizer.zero_grad()

  # Forward
  logits = model.forward(inputs)
  output = loss.forward(logits, labels)

  # Backward
  output.backward()

  # Update parameters
  optimizer.step()

  # what's the difference between backward() and step()?
  # https://discuss.pytorch.org/t/what-does-the-backward-function-do/9944
  return output.item()


# define the prediction procedure
# also encapsulate these steps
# Note: inputs is a torch tensor
def predict(model, inputs):
  inputs = Variable(inputs, requires_grad=False)
  logits = model.forward(inputs)
  return logits.data.numpy().argmax(axis=1)



### prepare for training loop ###

# convert the data arrays into torch tensors
Xtrain = torch.from_numpy(Xtrain).float()
Ytrain = torch.from_numpy(Ytrain).long()
Xtest = torch.from_numpy(Xtest).float()



epochs = 15
batch_size = 32
n_batches = Xtrain.size()[0] // batch_size

costs = []
test_accuracies = []
for i in range(epochs):
  cost = 0.
  for j in range(n_batches):
    Xbatch = Xtrain[j*batch_size:(j+1)*batch_size]
    Ybatch = Ytrain[j*batch_size:(j+1)*batch_size]
    cost += train(model, loss, optimizer, Xbatch, Ybatch)

  Ypred = predict(model, Xtest)
  acc = np.mean(Ytest == Ypred)
  print("Epoch: %d, cost: %f, acc: %.2f" % (i, cost / n_batches, acc))

  # for plotting
  costs.append(cost / n_batches)
  test_accuracies.append(acc)


# EXERCISE: plot test cost + training accuracy too

# plot the results
plt.plot(costs)
plt.title('Training cost')
plt.show()

plt.plot(test_accuracies)
plt.title('Test accuracies')
plt.show()


================================================
FILE: ann_class2/pytorch_example2.py
================================================
# https://deeplearningcourses.com/c/data-science-deep-learning-in-theano-tensorflow
# https://www.udemy.com/data-science-deep-learning-in-theano-tensorflow
from __future__ import print_function, division
from builtins import range
# Note: you may need to update your version of future
# sudo pip install -U future

# Note: is helpful to look at keras_example.py first


import numpy as np
import matplotlib.pyplot as plt
from util import get_normalized_data

import torch
from torch.autograd import Variable
from torch import optim



# get the data, same as Theano + Tensorflow examples
# no need to split now, the fit() function will do it
Xtrain, Xtest, Ytrain, Ytest = get_normalized_data()

# get shapes
_, D = Xtrain.shape
K = len(set(Ytrain))

# Note: no need to convert Y to indicator matrix


# the model will be a sequence of layers
model = torch.nn.Sequential()


# ANN with layers [784] -> [500] -> [300] -> [10]
model.add_module("dense1", torch.nn.Linear(D, 500))
model.add_module("relu1", torch.nn.ReLU())
model.add_module("dense2", torch.nn.Linear(500, 300))
model.add_module("relu2", torch.nn.ReLU())
model.add_module("dense3", torch.nn.Linear(300, K))
# Note: no final softmax!
# just like Tensorflow, it's included in cross-entropy function


# define a loss function
# other loss functions can be found here:
# http://pytorch.org/docs/master/nn.html#loss-functions
loss = torch.nn.CrossEntropyLoss(size_average=True)
# Note: this returns a function!
# e.g. use it like: loss(logits, labels)


# define an optimizer
# other optimizers can be found here:
# http://pytorch.org/docs/master/optim.html
optimizer = optim.Adam(model.parameters(), lr=1e-4)



# define the training procedure
# i.e. one step of gradient descent
# there are lots of steps
# so we encapsulate it in a function
# Note: inputs and labels are torch tensors
def train(model, loss, optimizer, inputs, labels):
  inputs = Variable(inputs, requires_grad=False)
  labels = Variable(labels, requires_grad=False)

  # Reset gradient
  optimizer.zero_grad()

  # Forward
  logits = model.forward(inputs)
  output = loss.forward(logits, labels)

  # Backward
  output.backward()

  # Update parameters
  optimizer.step()

  # what's the difference between backward() and step()?

  return output.item()


# similar to train() but not doing the backprop step
def get_cost(model, loss, inputs, labels):
  inputs = Variable(inputs, requires_grad=False)
  labels = Variable(labels, requires_grad=False)

  # Forward
  logits = model.forward(inputs)
  output = loss.forward(logits, labels)

  return output.item()


# define the prediction procedure
# also encapsulate these steps
# Note: inputs is a torch tensor
def predict(model, inputs):
  inputs = Variable(inputs, requires_grad=False)
  logits = model.forward(inputs)
  return logits.data.numpy().argmax(axis=1)


# return the accuracy
# labels is a torch tensor
# to get back the internal numpy data
# use the instance method .numpy()
def score(model, inputs, labels):
  predictions = predict(model, inputs)
  return np.mean(labels.numpy() == predictions)


### prepare for training loop ###

# convert the data arrays into torch tensors
Xtrain = torch.from_numpy(Xtrain).float()
Ytrain = torch.from_numpy(Ytrain).long()
Xtest = torch.from_numpy(Xtest).float()
Ytest = torch.from_numpy(Ytest).long()

# training parameters
epochs = 15
batch_size = 32
n_batches = Xtrain.size()[0] // batch_size

# things to keep track of
train_costs = []
test_costs = []
train_accuracies = []
test_accuracies = []

# main training loop
for i in range(epochs):
  cost = 0
  test_cost = 0
  for j in range(n_batches):
    Xbatch = Xtrain[j*batch_size:(j+1)*batch_size]
    Ybatch = Ytrain[j*batch_size:(j+1)*batch_size]
    cost += train(model, loss, optimizer, Xbatch, Ybatch)

  
  # we could have also calculated the train cost here
  # but I wanted to show you that we could also return it
  # from the train function itself
  train_acc = score(model, Xtrain, Ytrain)
  test_acc = score(model, Xtest, Ytest)
  test_cost = get_cost(model, loss, Xtest, Ytest)

  print("Epoch: %d, cost: %f, acc: %.2f" % (i, test_cost, test_acc))

  # for plotting
  train_costs.append(cost / n_batches)
  train_accuracies.append(train_acc)
  test_costs.append(test_cost)
  test_accuracies.append(test_acc)



# plot the results
plt.plot(train_costs, label='Train cost')
plt.plot(test_costs, label='Test cost')
plt.title('Cost')
plt.legend()
plt.show()

plt.plot(train_accuracies, label='Train accuracy')
plt.plot(test_accuracies, label='Test accuracy')
plt.title('Accuracy')
plt.legend()
plt.show()


================================================
FILE: ann_class2/random_search.py
================================================
# For the class Data Science: Practical Deep Learning Concepts in Theano and TensorFlow
# https://deeplearningcourses.com/c/data-science-deep-learning-in-theano-tensorflow
# https://www.udemy.com/data-science-deep-learning-in-theano-tensorflow
from __future__ import print_function, division
from builtins import range
# Note: you may need to update your version of future
# sudo pip install -U future


# import theano.tensor as T
from theano_ann import ANN
from util import get_spiral, get_clouds
from sklearn.utils import shuffle
import matplotlib.pyplot as plt
import numpy as np


def random_search():
  # get the data and split into train/test
  X, Y = get_spiral()
  # X, Y = get_clouds()
  X, Y = shuffle(X, Y)
  Ntrain = int(0.7*len(X))
  Xtrain, Ytrain = X[:Ntrain], Y[:Ntrain]
  Xtest, Ytest = X[Ntrain:], Y[Ntrain:]

  # starting hyperparameters
  M = 20
  nHidden = 2
  log_lr = -4
  log_l2 = -2 # since we always want it to be positive
  max_tries = 30
  

  # loop through all possible hyperparameter settings
  best_validation_rate = 0
  best_hls = None
  best_lr = None
  best_l2 = None
  for _ in range(max_tries):
    model = ANN([M]*nHidden)
    model.fit(
      Xtrain, Ytrain,
      learning_rate=10**log_lr, reg=10**log_l2,
      mu=0.99, epochs=3000, show_fig=False
    )
    validation_accuracy = model.score(Xtest, Ytest)
    train_accuracy = model.score(Xtrain, Ytrain)
    print(
      "validation_accuracy: %.3f, train_accuracy: %.3f, settings: %s, %s, %s" %
        (validation_accuracy, train_accuracy, [M]*nHidden, log_lr, log_l2)
    )
    if validation_accuracy > best_validation_rate:
      best_validation_rate = validation_accuracy
      best_M = M
      best_nHidden = nHidden
      best_lr = log_lr
      best_l2 = log_l2

    # select new hyperparams
    nHidden = best_nHidden + np.random.randint(-1, 2) # -1, 0, or 1
    nHidden = max(1, nHidden)
    M = best_M + np.random.randint(-1, 2)*10
    M = max(10, M)
    log_lr = best_lr + np.random.randint(-1, 2)
    log_l2 = best_l2 + np.random.randint(-1, 2)
  print("Best validation_accuracy:", best_validation_rate)
  print("Best settings:")
  print("best_M:", best_M)
  print("best_nHidden:", best_nHidden)
  print("learning_rate:", best_lr)
  print("l2:", best_l2)


if __name__ == '__main__':
  random_search()


================================================
FILE: ann_class2/rmsprop.py
================================================
# Compare RMSprop vs. constant learning rate
# For the class Data Science: Practical Deep Learning Concepts in Theano and TensorFlow
# https://deeplearningcourses.com/c/data-science-deep-learning-in-theano-tensorflow
# https://www.udemy.com/data-science-deep-learning-in-theano-tensorflow
from __future__ import print_function, division
from builtins import range
# Note: you may need to update your version of future
# sudo pip install -U future

import numpy as np
from sklearn.utils import shuffle
import matplotlib.pyplot as plt

from util import get_normalized_data, error_rate, cost, y2indicator
from mlp import forward, derivative_w2, derivative_w1, derivative_b2, derivative_b1


def main():
    max_iter = 20 # make it 30 for sigmoid
    print_period = 10

    Xtrain, Xtest, Ytrain, Ytest = get_normalized_data()
    lr = 0.00004
    reg = 0.01

    Ytrain_ind = y2indicator(Ytrain)
    Ytest_ind = y2indicator(Ytest)

    N, D = Xtrain.shape
    batch_sz = 500
    n_batches = N // batch_sz

    M = 300
    K = 10
    W1 = np.random.randn(D, M) / np.sqrt(D)
    b1 = np.zeros(M)
    W2 = np.random.randn(M, K) / np.sqrt(M)
    b2 = np.zeros(K)

    # 1. const
    # cost = -16
    LL_batch = []
    CR_batch = []
    for i in range(max_iter):
        for j in range(n_batches):
            Xbatch = Xtrain[j*batch_sz:(j*batch_sz + batch_sz),]
            Ybatch = Ytrain_ind[j*batch_sz:(j*batch_sz + batch_sz),]
            pYbatch, Z = forward(Xbatch, W1, b1, W2, b2)
            # print "first batch cost:", cost(pYbatch, Ybatch)

            # gradients
            gW2 = derivative_w2(Z, Ybatch, pYbatch) + reg*W2
            gb2 = derivative_b2(Ybatch, pYbatch) + reg*b2
            gW1 = derivative_w1(Xbatch, Z, Ybatch, pYbatch, W2) + reg*W1
            gb1 = derivative_b1(Z, Ybatch, pYbatch, W2) + reg*b1

            # updates
            W2 -= lr*gW2
            b2 -= lr*gb2
            W1 -= lr*gW1
            b1 -= lr*gb1

            if j % print_period == 0:
                # calculate just for LL
                pY, _ = forward(Xtest, W1, b1, W2, b2)
                # print "pY:", pY
                ll = cost(pY, Ytest_ind)
                LL_batch.append(ll)
                print("Cost at iteration i=%d, j=%d: %.6f" % (i, j, ll))

                err = error_rate(pY, Ytest)
                CR_batch.append(err)
                print("Error rate:", err)

    pY, _ = forward(Xtest, W1, b1, W2, b2)
    print("Final error rate:", error_rate(pY, Ytest))


    # 2. RMSprop
    W1 = np.random.randn(D, M) / np.sqrt(D)
    b1 = np.zeros(M)
    W2 = np.random.randn(M, K) / np.sqrt(M)
    b2 = np.zeros(K)
    LL_rms = []
    CR_rms = []
    lr0 = 0.001 # if you set this too high you'll get NaN!
    cache_W2 = 1
    cache_b2 = 1
    cache_W1 = 1
    cache_b1 = 1
    decay_rate = 0.999
    eps = 1e-10
    for i in range(max_iter):
        for j in range(n_batches):
            Xbatch = Xtrain[j*batch_sz:(j*batch_sz + batch_sz),]
            Ybatch = Ytrain_ind[j*batch_sz:(j*batch_sz + batch_sz),]
            pYbatch, Z = forward(Xbatch, W1, b1, W2, b2)
            # print "first batch cost:", cost(pYbatch, Ybatch)

            # gradients
            gW2 = derivative_w2(Z, Ybatch, pYbatch) + reg*W2
            gb2 = derivative_b2(Ybatch, pYbatch) + reg*b2
            gW1 = derivative_w1(Xbatch, Z, Ybatch, pYbatch, W2) + reg*W1
            gb1 = derivative_b1(Z, Ybatch, pYbatch, W2) + reg*b1

            # caches
            cache_W2 = decay_rate*cache_W2 + (1 - decay_rate)*gW2*gW2
            cache_b2 = decay_rate*cache_b2 + (1 - decay_rate)*gb2*gb2
            cache_W1 = decay_rate*cache_W1 + (1 - decay_rate)*gW1*gW1
            cache_b1 = decay_rate*cache_b1 + (1 - decay_rate)*gb1*gb1

            # updates
            W2 -= lr0 * gW2 / (np.sqrt(cache_W2) + eps)
            b2 -= lr0 * gb2 / (np.sqrt(cache_b2) + eps)
            W1 -= lr0 * gW1 / (np.sqrt(cache_W1) + eps)
            b1 -= lr0 * gb1 / (np.sqrt(cache_b1) + eps)

            if j % print_period == 0:
                # calculate just for LL
                pY, _ = forward(Xtest, W1, b1, W2, b2)
                # print "pY:", pY
                ll = cost(pY, Ytest_ind)
                LL_rms.append(ll)
                print("Cost at iteration i=%d, j=%d: %.6f" % (i, j, ll))

                err = error_rate(pY, Ytest)
                CR_rms.append(err)
                print("Error rate:", err)

    pY, _ = forward(Xtest, W1, b1, W2, b2)
    print("Final error rate:", error_rate(pY, Ytest))

    plt.plot(LL_batch, label='const')
    plt.plot(LL_rms, label='rms')
    plt.legend()
    plt.show()


if __name__ == '__main__':
    main()

================================================
FILE: ann_class2/rmsprop_test.py
================================================
# https://deeplearningcourses.com/c/data-science-deep-learning-in-theano-tensorflow
# https://www.udemy.com/data-science-deep-learning-in-theano-tensorflow
from __future__ import print_function, division
from builtins import range
# Note: you may need to update your version of future
# sudo pip install -U future

from keras.models import Sequential
from keras.layers import Dense, Activation
from util import get_normalized_data, y2indicator

import matplotlib.pyplot as plt
import numpy as np
import tensorflow as tf

import theano
import theano.tensor as T


# RMSprop experiment
# to compare TF / Keras / Theano

N = 10
D = 2
X = np.random.randn(N, D).astype(np.float32)
w = np.array([0.5, -0.5], dtype=np.float32)
Y = X.dot(w) + 1
Y = Y.reshape(-1, 1)



# keras
# the model will be a sequence of layers
model = Sequential()
model.add(Dense(units=1, input_dim=D))


# copy the weights for later
weights = model.layers[0].get_weights()
w0 = weights[0].copy()
b0 = weights[1].copy()


model.compile(
  loss='mean_squared_error',
  optimizer='rmsprop',
)


r = model.fit(X, Y, epochs=15, batch_size=10)


# print the available keys
print(r.history.keys())



# tf
inputs = tf.placeholder(tf.float32, shape=(None, 2))
targets = tf.placeholder(tf.float32, shape=(None, 1))
tfw = tf.Variable(w0)
tfb = tf.Variable(b0)
pred = tf.matmul(inputs, tfw) + tfb

loss = tf.reduce_mean(tf.square(targets - pred))
train_op = tf.train.RMSPropOptimizer(1e-3, epsilon=1e-8).minimize(loss)

tflosses = []
init = tf.global_variables_initializer()
with tf.Session() as sess:
  sess.run(init)
  for e in range(15):
    _, l = sess.run([train_op, loss], feed_dict={inputs: X, targets: Y})
    tflosses.append(l)



# theano
def rmsprop(cost, params, lr=1e-3, decay=0.9, eps=1e-8):
  # return updates
  lr = np.float32(lr)
  decay = np.float32(decay)
  eps = np.float32(eps)

  updates = []
  grads = T.grad(cost, params)

  # tf-like
  # caches = [theano.shared(np.ones_like(p.get_value(), dtype=np.float32)) for p in params]

  # keras-like
  caches = [theano.shared(np.zeros_like(p.get_value(), dtype=np.float32)) for p in params]
  
  new_caches = []
  for c, g in zip(caches, grads):
    new_c = decay*c + (np.float32(1) - decay)*g*g
    updates.append((c, new_c))
    new_caches.append(new_c)

  for p, new_c, g in zip(params, new_caches, grads):
    new_p = p - lr*g / T.sqrt(new_c + eps)
    updates.append((p, new_p))

  return updates

thX = T.matrix('X')
thY = T.matrix('Y')
thw = theano.shared(w0)
thb = theano.shared(b0)
thP = thX.dot(thw) + thb
cost = T.mean((thY - thP)**2)
params = [thw, thb]
updates = rmsprop(cost, params)

train_op = theano.function(
  inputs=[thX, thY],
  outputs=cost,
  updates=updates,
)

thlosses = []
for e in range(15):
  c = train_op(X, Y)
  thlosses.append(c)


# plot results
plt.plot(r.history['loss'], label='keras loss')
plt.plot(tflosses, label='tf loss')
plt.plot(thlosses, label='theano loss')
plt.legend()
plt.show()




================================================
FILE: ann_class2/sgd.py
================================================
# In this file we compare the progression of the cost function vs. iteration
# for 3 cases:
# 1) full gradient descent
# 2) mini-batch gradient descent
# 3) stochastic gradient descent
#
# For the class Data Science: Practical Deep Learning Concepts in Theano and TensorFlow
# https://deeplearningcourses.com/c/data-science-deep-learning-in-theano-tensorflow
# https://www.udemy.com/data-science-deep-learning-in-theano-tensorflow
from __future__ import print_function, division
from builtins import range
# Note: you may need to update your version of future
# sudo pip install -U future

import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
from sklearn.utils import shuffle
from datetime import datetime

from util import get_normalized_data, forward, error_rate, cost, gradW, gradb, y2indicator


def main():
    Xtrain, Xtest, Ytrain, Ytest = get_normalized_data()
    print("Performing logistic regression...")

    N, D = Xtrain.shape
    Ytrain_ind = y2indicator(Ytrain)
    Ytest_ind = y2indicator(Ytest)

    # 1. full
    W = np.random.randn(D, 10) / np.sqrt(D)
    W0 = W.copy() # save for later
    b = np.zeros(10)
    test_losses_full = []
    lr = 0.9
    reg = 0.
    t0 = datetime.now()
    last_dt = 0
    intervals = []
    for i in range(50):
        p_y = forward(Xtrain, W, b)

        gW = gradW(Ytrain_ind, p_y, Xtrain) / N
        gb = gradb(Ytrain_ind, p_y) / N

        W += lr*(gW - reg*W)
        b += lr*(gb - reg*b)

        p_y_test = forward(Xtest, W, b)
        test_loss = cost(p_y_test, Ytest_ind)
        dt = (datetime.now() - t0).total_seconds()

        # save these
        dt2 = dt - last_dt
        last_dt = dt
        intervals.append(dt2)

        test_losses_full.append([dt, test_loss])
        if (i + 1) % 10 == 0:
            print("Cost at iteration %d: %.6f" % (i + 1, test_loss))
    p_y = forward(Xtest, W, b)
    print("Final error rate:", error_rate(p_y, Ytest))
    print("Elapsted time for full GD:", datetime.now() - t0)

    # save the max time so we don't surpass it in subsequent iterations
    max_dt = dt
    avg_interval_dt = np.mean(intervals)


    # 2. stochastic
    W = W0.copy()
    b = np.zeros(10)
    test_losses_sgd = []
    lr = 0.001
    reg = 0.

    t0 = datetime.now()
    last_dt_calculated_loss = 0
    done = False
    for i in range(50): # takes very long since we're computing cost for 41k samples
        tmpX, tmpY = shuffle(Xtrain, Ytrain_ind)
        for n in range(N):
            x = tmpX[n,:].reshape(1,D)
            y = tmpY[n,:].reshape(1,10)
            p_y = forward(x, W, b)

            gW = gradW(y, p_y, x)
            gb = gradb(y, p_y)

            W += lr*(gW - reg*W)
            b += lr*(gb - reg*b)

            dt = (datetime.now() - t0).total_seconds()
            dt2 = dt - last_dt_calculated_loss

            if dt2 > avg_interval_dt:
                last_dt_calculated_loss = dt
                p_y_test = forward(Xtest, W, b)
                test_loss = cost(p_y_test, Ytest_ind)
                test_losses_sgd.append([dt, test_loss])

            # time to quit
            if dt > max_dt:
                done = True
                break
        if done:
            break

        if (i + 1) % 1 == 0:
            print("Cost at iteration %d: %.6f" % (i + 1, test_loss))
    p_y = forward(Xtest, W, b)
    print("Final error rate:", error_rate(p_y, Ytest))
    print("Elapsted time for SGD:", datetime.now() - t0)


    # 3. mini-batch
    W = W0.copy()
    b = np.zeros(10)
    test_losses_batch = []
    batch_sz = 500
    lr = 0.08
    reg = 0.
    n_batches = int(np.ceil(N / batch_sz))


    t0 = datetime.now()
    last_dt_calculated_loss = 0
    done = False
    for i in range(50):
        tmpX, tmpY = shuffle(Xtrain, Ytrain_ind)
        for j in range(n_batches):
            x = tmpX[j*batch_sz:(j + 1)*batch_sz,:]
            y = tmpY[j*batch_sz:(j + 1)*batch_sz,:]
            p_y = forward(x, W, b)

            current_batch_sz = len(x)
            gW = gradW(y, p_y, x) / current_batch_sz
            gb = gradb(y, p_y) / current_batch_sz

            W += lr*(gW - reg*W)
            b += lr*(gb - reg*b)

            dt = (datetime.now() - t0).total_seconds()
            dt2 = dt - last_dt_calculated_loss

            if dt2 > avg_interval_dt:
                last_dt_calculated_loss = dt
                p_y_test = forward(Xtest, W, b)
                test_loss = cost(p_y_test, Ytest_ind)
                test_losses_batch.append([dt, test_loss])

            # time to quit
            if dt > max_dt:
                done = True
                break
        if done:
            break

        if (i + 1) % 10 == 0:
            print("Cost at iteration %d: %.6f" % (i + 1, test_loss))
    p_y = forward(Xtest, W, b)
    print("Final error rate:", error_rate(p_y, Ytest))
    print("Elapsted time for mini-batch GD:", datetime.now() - t0)


    # convert to numpy arrays
    test_losses_full = np.array(test_losses_full)
    test_losses_sgd = np.array(test_losses_sgd)
    test_losses_batch = np.array(test_losses_batch)

    
    plt.plot(test_losses_full[:,0], test_losses_full[:,1], label="full")
    plt.plot(test_losses_sgd[:,0], test_losses_sgd[:,1], label="sgd")
    plt.plot(test_losses_batch[:,0], test_losses_batch[:,1], label="mini-batch")
    plt.legend()
    plt.show()



if __name__ == '__main__':
    main()

================================================
FILE: ann_class2/tensorflow1.py
================================================
# This tutorial is analogous to theano1.py
# It introduces basic variables and functions
# and shows how you can optimize a function.
# I compare this to theano1.py multiple times.
# So you might want to check that out first.

# For the class Data Science: Practical Deep Learning Concepts in Theano and TensorFlow
# https://deeplearningcourses.com/c/data-science-deep-learning-in-theano-tensorflow
# https://www.udemy.com/data-science-deep-learning-in-theano-tensorflow
from __future__ import print_function, division
from builtins import range
# Note: you may need to update your version of future
# sudo pip install -U future

import numpy as np
import tensorflow as tf


# you have to specify the type
A = tf.placeholder(tf.float32, shape=(5, 5), name='A')


# but shape and name are optional
v = tf.placeholder(tf.float32)


# I think this name is more appropriate than 'dot'
w = tf.matmul(A, v)


# similar to Theano, you need to "feed" the variables values.
# In TensorFlow you do the "actual work" in a "session".

with tf.Session() as session:
    # the values are fed in via the appropriately named argument "feed_dict"
    # v needs to be of shape=(5, 1) not just shape=(5,)
    # it's more like "real" matrix multiplication
    output = session.run(w, feed_dict={A: np.random.randn(5, 5), v: np.random.randn(5, 1)})

    # what's this output that is returned by the session? let's print it
    print(output, type(output))

    # luckily, the output type is just a numpy array. back to safety!


# TensorFlow variables are like Theano shared variables.
# But Theano variables are like TensorFlow placeholders.
# Are you confused yet?

# A tf variable can be initialized with a numpy array or a tf array
# or more correctly, anything that can be turned into a tf tensor
shape = (2, 2)
x = tf.Variable(tf.random_normal(shape))
# x = tf.Variable(np.random.randn(2, 2))
t = tf.Variable(0) # a scalar

# you need to "initialize" the variables first
init = tf.global_variables_initializer()

with tf.Session() as session:
    out = session.run(init) # and then "run" the init operation
    print(out) # it's just None

    # eval() in tf is like get_value() in Theano
    print(x.eval()) # the initial value of x
    print(t.eval())


# let's now try to find the minimum of a simple cost function like we did in Theano
u = tf.Variable(20.0)
cost = u*u + u + 1.0

# One difference between Theano and TensorFlow is that you don't write the updates
# yourself in TensorFlow. You choose an optimizer that implements the algorithm you want.
# 0.3 is the learning rate. Documentation lists the params.
train_op = tf.train.GradientDescentOptimizer(0.3).minimize(cost)

# let's run a session again
init = tf.global_variables_initializer()
with tf.Session() as session:
    session.run(init)

    # Strangely, while the weight update is automated, the loop itself is not.
    # So we'll just call train_op until convergence.
    # This is useful for us anyway since we want to track the cost function.
    for i in range(12):
        session.run(train_op)
        print("i = %d, cost = %.3f, u = %.3f" % (i, cost.eval(), u.eval()))



================================================
FILE: ann_class2/tensorflow2.py
================================================
# 2-hidden layer NN in TensorFlow
# This code is not optimized for speed.
# It's just to get something working, using the principles we know.

# For the class Data Science: Practical Deep Learning Concepts in Theano and TensorFlow
# https://deeplearningcourses.com/c/data-science-deep-learning-in-theano-tensorflow
# https://www.udemy.com/data-science-deep-learning-in-theano-tensorflow
from __future__ import print_function, division
from builtins import range
# Note: you may need to update your version of future
# sudo pip install -U future

import numpy as np
import tensorflow as tf

import matplotlib.pyplot as plt

from util import get_normalized_data, y2indicator


def error_rate(p, t):
    return np.mean(p != t)


# copy this first part from theano2.py
def main():
    # step 1: get the data and define all the usual variables
    Xtrain, Xtest, Ytrain, Ytest = get_normalized_data()

    max_iter = 15
    print_period = 50

    lr = 0.00004
    reg = 0.01

    Ytrain_ind = y2indicator(Ytrain)
    Ytest_ind = y2indicator(Ytest)

    N, D = Xtrain.shape
    batch_sz = 500
    n_batches = N // batch_sz

    # add an extra layer just for fun
    M1 = 300
    M2 = 100
    K = 10
    W1_init = np.random.randn(D, M1) / np.sqrt(D)
    b1_init = np.zeros(M1)
    W2_init = np.random.randn(M1, M2) / np.sqrt(M1)
    b2_init = np.zeros(M2)
    W3_init = np.random.randn(M2, K) / np.sqrt(M2)
    b3_init = np.zeros(K)


    # define variables and expressions
    X = tf.placeholder(tf.float32, shape=(None, D), name='X')
    T = tf.placeholder(tf.float32, shape=(None, K), name='T')
    W1 = tf.Variable(W1_init.astype(np.float32))
    b1 = tf.Variable(b1_init.astype(np.float32))
    W2 = tf.Variable(W2_init.astype(np.float32))
    b2 = tf.Variable(b2_init.astype(np.float32))
    W3 = tf.Variable(W3_init.astype(np.float32))
    b3 = tf.Variable(b3_init.astype(np.float32))

    # define the model
    Z1 = tf.nn.relu( tf.matmul(X, W1) + b1 )
    Z2 = tf.nn.relu( tf.matmul(Z1, W2) + b2 )
    Yish = tf.matmul(Z2, W3) + b3 # remember, the cost function does the softmaxing! weird, right?

    # softmax_cross_entropy_with_logits take in the "logits"
    # if you wanted to know the actual output of the neural net,
    # you could pass "Yish" into tf.nn.softmax(logits)
    cost = tf.reduce_sum(tf.nn.softmax_cross_entropy_with_logits_v2(logits=Yish, labels=T))

    # we choose the optimizer but don't implement the algorithm ourselves
    # let's go with RMSprop, since we just learned about it.
    # it includes momentum!
    train_op = tf.train.RMSPropOptimizer(lr, decay=0.99, momentum=0.9).minimize(cost)

    # we'll use this to calculate the error rate
    predict_op = tf.argmax(Yish, 1)

    costs = []
    init = tf.global_variables_initializer()
    with tf.Session() as session:
        session.run(init)

        for i in range(max_iter):
            for j in range(n_batches):
                Xbatch = Xtrain[j*batch_sz:(j*batch_sz + batch_sz),]
                Ybatch = Ytrain_ind[j*batch_sz:(j*batch_sz + batch_sz),]

                session.run(train_op, feed_dict={X: Xbatch, T: Ybatch})
                if j % print_period == 0:
                    test_cost = session.run(cost, feed_dict={X: Xtest, T: Ytest_ind})
                    prediction = session.run(predict_op, feed_dict={X: Xtest})
                    err = error_rate(prediction, Ytest)
                    print("Cost / err at iteration i=%d, j=%d: %.3f / %.3f" % (i, j, test_cost, err))
                    costs.append(test_cost)

    plt.plot(costs)
    plt.show()
    # increase max_iter and notice how the test cost starts to increase.
    # are we overfitting by adding that extra layer?
    # how would you add regularization to this model?


if __name__ == '__main__':
    main()


================================================
FILE: ann_class2/tf_with_save.py
================================================
# https://deeplearningcourses.com/c/data-science-deep-learning-in-theano-tensorflow
# https://www.udemy.com/data-science-deep-learning-in-theano-tensorflow
from __future__ import print_function, division
from builtins import range
# Note: you may need to update your version of future
# sudo pip install -U future

import os
import json
import numpy as np
import tensorflow as tf

import matplotlib.pyplot as plt

from util import get_normalized_data, y2indicator


def error_rate(p, t):
  return np.mean(p != t)


class TFLogistic:
  def __init__(self, savefile, D=None, K=None):
    self.savefile = savefile
    if D and K:
      # we can define some parts in the model to be able to make predictions
      self.build(D, K)

  def build(self, D, K):
    W0 = np.random.randn(D, K) * np.sqrt(2.0 / D)
    b0 = np.zeros(K)

    # define variables and expressions
    self.inputs = tf.placeholder(tf.float32, shape=(None, D), name='inputs')
    self.targets = tf.placeholder(tf.int64, shape=(None,), name='targets')
    self.W = tf.Variable(W0.astype(np.float32), name='W')
    self.b = tf.Variable(b0.astype(np.float32), name='b')

    # variables must exist when calling this
    # try putting this line in the constructor and see what happens
    self.saver = tf.train.Saver({'W': self.W, 'b': self.b})

    logits = tf.matmul(self.inputs, self.W) + self.b
    cost = tf.reduce_mean(
        tf.nn.sparse_softmax_cross_entropy_with_logits(
            logits=logits,
            labels=self.targets
        )
    )
    self.predict_op = tf.argmax(logits, 1)
    return cost


  def fit(self, X, Y, Xtest, Ytest):
    N, D = X.shape
    K = len(set(Y))

    # hyperparams
    max_iter = 30
    lr = 1e-3
    mu = 0.9
    regularization = 1e-1
    batch_sz = 100
    n_batches = N // batch_sz

    cost = self.build(D, K)
    l2_penalty = regularization*tf.reduce_mean(self.W**2) / 2
    cost += l2_penalty
    train_op = tf.train.MomentumOptimizer(lr, momentum=mu).minimize(cost)

    costs = []
    init = tf.global_variables_initializer()
    with tf.Session() as session:
        session.run(init)

        for i in range(max_iter):
            for j in range(n_batches):
                Xbatch = X[j*batch_sz:(j*batch_sz + batch_sz),]
                Ybatch = Y[j*batch_sz:(j*batch_sz + batch_sz),]

                session.run(train_op, feed_dict={self.inputs: Xbatch, self.targets: Ybatch})
                if j % 200 == 0:
                    test_cost = session.run(cost, feed_dict={self.inputs: Xtest, self.targets: Ytest})
                    Ptest = session.run(self.predict_op, feed_dict={self.inputs: Xtest})
                    err = error_rate(Ptest, Ytest)
                    print("Cost / err at iteration i=%d, j=%d: %.3f / %.3f" % (i, j, test_cost, err))
                    costs.append(test_cost)

        # save the model
        self.saver.save(session, self.savefile)

    # save dimensions for later
    self.D = D
    self.K = K

    plt.plot(costs)
    plt.show()


  def predict(self, X):
    with tf.Session() as session:
      # restore the model
      self.saver.restore(session, self.savefile)
      P = session.run(self.predict_op, feed_dict={self.inputs: X})
    return P


  def score(self, X, Y):
    return 1 - error_rate(self.predict(X), Y)

  def save(self, filename):
    j = {
      'D': self.D,
      'K': self.K,
      'model': self.savefile
    }
    with open(filename, 'w') as f:
      json.dump(j, f)

  @staticmethod
  def load(filename):
    with open(filename) as f:
      j = json.load(f)
    return TFLogistic(j['model'], j['D'], j['K'])


def main():
    Xtrain, Xtest, Ytrain, Ytest = get_normalized_data()

    model = TFLogistic("./tf.model")
    model.fit(Xtrain, Ytrain, Xtest, Ytest)

    # test out restoring the model via the predict function
    print("final train accuracy:", model.score(Xtrain, Ytrain))
    print("final test accuracy:", model.score(Xtest, Ytest))

    # save the model
    model.save("my_trained_model.json")

    # load and score again
    model = TFLogistic.load("my_trained_model.json")
    print("final train accuracy (after reload):", model.score(Xtrain, Ytrain))
    print("final test accuracy (after reload):", model.score(Xtest, Ytest))


if __name__ == '__main__':
    main()

================================================
FILE: ann_class2/theano1.py
================================================
# Theano basics.
# For the class Data Science: Practical Deep Learning Concepts in Theano and TensorFlow
# https://deeplearningcourses.com/c/data-science-deep-learning-in-theano-tensorflow
# https://www.udemy.com/data-science-deep-learning-in-theano-tensorflow
from __future__ import print_function, division
from builtins import range
# Note: you may need to update your version of future
# sudo pip install -U future

import theano.tensor as T

# just some different types of variables
c = T.scalar('c')
v = T.vector('v')
A = T.matrix('A')


# we can define a matrix multiplication
w = A.dot(v)

# how do these variables actually take on values?
import theano

matrix_times_vector = theano.function(inputs=[A, v], outputs=w)

# let's import numpy so we can create real arrays
import numpy as np
A_val = np.array([[1,2], [3,4]])
v_val = np.array([5,6])

w_val = matrix_times_vector(A_val, v_val)
print(w_val)

# let's create a shared variable to we can do gradient descent
# this adds another layer of complexity to the theano function

x = theano.shared(20.0, 'x')

# the first argument is its initial value, the second is its name

# a cost function that has a minimum value
cost = x*x + x + 1

# in theano, you don't have to compute gradients yourself!
x_update = x - 0.3*T.grad(cost, x)

# x is not an "input", it's a thing you update
# in later examples, data and labels would go into the inputs
# and model params would go in the updates
# updates takes in a list of tuples, each tuple has 2 things in it:
# 1) the shared variable to update, 2) the update expression
train = theano.function(inputs=[], outputs=cost, updates=[(x, x_update)])

# write your own loop to call the training function.
# it has no arguments!
for i in range(25):
    cost_val = train()
    print(cost_val)

# print the optimal value of x
print(x.get_value())


================================================
FILE: ann_class2/theano2.py
================================================
# A 1-hidden-layer neural network in Theano.
# This code is not optimized for speed.
# It's just to get something working, using the principles we know.

# For the class Data Science: Practical Deep Learning Concepts in Theano and TensorFlow
# https://deeplearningcourses.com/c/data-science-deep-learning-in-theano-tensorflow
# https://www.udemy.com/data-science-deep-learning-in-theano-tensorflow
from __future__ import print_function, division
from builtins import range
# Note: you may need to update your version of future
# sudo pip install -U future

import numpy as np
import theano
import theano.tensor as T
import matplotlib.pyplot as plt

from util import get_normalized_data, y2indicator


def error_rate(p, t):
    return np.mean(p != t)


def relu(a):
    return a * (a > 0)


def main():
    # step 1: get the data and define all the usual variables
    Xtrain, Xtest, Ytrain, Ytest = get_normalized_data()

    max_iter = 20
    print_period = 10

    lr = 0.0004
    reg = 0.01

    Xtrain = Xtrain.astype(np.float32)
    Ytrain = Ytrain.astype(np.float32)
    Xtest = Xtest.astype(np.float32)
    Ytest = Ytest.astype(np.float32)
    Ytrain_ind = y2indicator(Ytrain).astype(np.float32)
    Ytest_ind = y2indicator(Ytest).astype(np.float32)

    N, D = Xtrain.shape
    batch_sz = 500
    n_batches = N // batch_sz

    M = 300
    K = 10
    W1_init = np.random.randn(D, M) / np.sqrt(D)
    b1_init = np.zeros(M)
    W2_init = np.random.randn(M, K) / np.sqrt(M)
    b2_init = np.zeros(K)

    # step 2: define theano variables and expressions
    thX = T.matrix('X')
    thT = T.matrix('T')
    W1 = theano.shared(W1_init, 'W1')
    b1 = theano.shared(b1_init, 'b1')
    W2 = theano.shared(W2_init, 'W2')
    b2 = theano.shared(b2_init, 'b2')

    # we can use the built-in theano functions to do relu and softmax
    thZ = relu( thX.dot(W1) + b1 ) # relu is new in version 0.7.1 but just in case you don't have it
    thY = T.nnet.softmax( thZ.dot(W2) + b2 )

    # define the cost function and prediction
    cost = -(thT * T.log(thY)).sum() + reg*((W1*W1).sum() + (b1*b1).sum() + (W2*W2).sum() + (b2*b2).sum())
    prediction = T.argmax(thY, axis=1)

    # step 3: training expressions and functions
    # we can just include regularization as part of the cost because it is also automatically differentiated!
    update_W1 = W1 - lr*T.grad(cost, W1)
    update_b1 = b1 - lr*T.grad(cost, b1)
    update_W2 = W2 - lr*T.grad(cost, W2)
    update_b2 = b2 - lr*T.grad(cost, b2)

    train = theano.function(
        inputs=[thX, thT],
        updates=[(W1, update_W1), (b1, update_b1), (W2, update_W2), (b2, update_b2)],
    )

    # create another function for this because we want it over the whole dataset
    get_prediction = theano.function(
        inputs=[thX, thT],
        outputs=[cost, prediction],
    )

    costs = []
    for i in range(max_iter):
        for j in range(n_batches):
            Xbatch = Xtrain[j*batch_sz:(j*batch_sz + batch_sz),]
            Ybatch = Ytrain_ind[j*batch_sz:(j*batch_sz + batch_sz),]

            train(Xbatch, Ybatch)
            if j % print_period == 0:
                cost_val, prediction_val = get_prediction(Xtest, Ytest_ind)
                err = error_rate(prediction_val, Ytest)
                print("Cost / err at iteration i=%d, j=%d: %.3f / %.3f" % (i, j, cost_val, err))
                costs.append(cost_val)

    plt.plot(costs)
    plt.show()

    # how would you incorporate momentum into the gradient descent procedure?


if __name__ == '__main__':
    main()


================================================
FILE: ann_class2/theano_ann.py
================================================
# For the class Data Science: Practical Deep Learning Concepts in Theano and TensorFlow
# https://deeplearningcourses.com/c/data-science-deep-learning-in-theano-tensorflow
# https://www.udemy.com/data-science-deep-learning-in-theano-tensorflow
from __future__ import print_function, division
from builtins import range
# Note: you may need to update your version of future
# sudo pip install -U future

import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import theano
import theano.tensor as T
from sklearn.utils import shuffle


def init_weight(M1, M2):
  return np.random.randn(M1, M2) * np.sqrt(2.0 / M1)


class HiddenLayer(object):
    def __init__(self, M1, M2, f):
        self.M1 = M1
        self.M2 = M2
        self.f = f
        W = init_weight(M1, M2)
        b = np.zeros(M2)
        self.W = theano.shared(W)
        self.b = theano.shared(b)
        self.params = [self.W, self.b]

    def forward(self, X):
        if self.f == T.nnet.relu:
            return self.f(X.dot(self.W) + self.b, alpha=0.1)
        return self.f(X.dot(self.W) + self.b)


class ANN(object):
    def __init__(self, hidden_layer_sizes):
        self.hidden_layer_sizes = hidden_layer_sizes

    def fit(self, X, Y, activation=T.nnet.relu, learning_rate=1e-3, mu=0.0, reg=0, epochs=100, batch_sz=None, print_period=100, show_fig=True):
        X = X.astype(np.float32)
        Y = Y.astype(np.int32)

        # initialize hidden layers
        N, D = X.shape
        self.layers = []
        M1 = D
        for M2 in self.hidden_layer_sizes:
            h = HiddenLayer(M1, M2, activation)
            self.layers.append(h)
            M1 = M2
        
        # final layer
        K = len(set(Y))
        # print("K:", K)
        h = HiddenLayer(M1, K, T.nnet.softmax)
        self.layers.append(h)

        if batch_sz is None:
            batch_sz = N

        # collect params for later use
        self.params = []
        for h in self.layers:
            self.params += h.params

        # for momentum
        dparams = [theano.shared(np.zeros_like(p.get_value())) for p in self.params]

        # set up theano functions and variables
        thX = T.matrix('X')
        thY = T.ivector('Y')
        p_y_given_x = self.forward(thX)

        rcost = reg*T.mean([(p*p).sum() for p in self.params])
        cost = -T.mean(T.log(p_y_given_x[T.arange(thY.shape[0]), thY])) #+ rcost
        prediction = T.argmax(p_y_given_x, axis=1)
        grads = T.grad(cost, self.params)

        # momentum only
        updates = [
            (p, p + mu*dp - learning_rate*g) for p, dp, g in zip(self.params, dparams, grads)
        ] + [
            (dp, mu*dp - learning_rate*g) for dp, g in zip(dparams, grads)
        ]

        train_op = theano.function(
            inputs=[thX, thY],
            outputs=[cost, prediction],
            updates=updates,
        )

        self.predict_op = theano.function(
            inputs=[thX],
            outputs=prediction,
        )

        n_batches = N // batch_sz
        costs = []
        for i in range(epochs):
            if n_batches > 1:
              X, Y = shuffle(X, Y)
            for j in range(n_batches):
                Xbatch = X[j*batch_sz:(j*batch_sz+batch_sz)]
                Ybatch = Y[j*batch_sz:(j*batch_sz+batch_sz)]

                c, p = train_op(Xbatch, Ybatch)
                costs.append(c)
                if (j+1) % print_period == 0:
                    print("i:", i, "j:", j, "nb:", n_batches, "cost:", c)
        
        if show_fig:
            plt.plot(costs)
            plt.show()

    def forward(self, X):
        out = X
        for h in self.layers:
            out = h.forward(out)
        return out

    def score(self, X, Y):
        P = self.predict_op(X)
        return np.mean(Y == P)

    def predict(self, X):
        return self.predict_op(X)


================================================
FILE: ann_class2/util.py
================================================
from __future__ import print_function, division
from builtins import range
# Note: you may need to update your version of future
# sudo pip install -U future

# Some utility functions we need for the class.
# For the class Data Science: Practical Deep Learning Concepts in Theano and TensorFlow
# https://deeplearningcourses.com/c/data-science-deep-learning-in-theano-tensorflow
# https://www.udemy.com/data-science-deep-learning-in-theano-tensorflow

# Note: run this from the current folder it is in.

import os
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
from sklearn.decomposition import PCA
from sklearn.linear_model import LogisticRegression


def get_clouds():
    Nclass = 500
    D = 2

    X1 = np.random.randn(Nclass, D) + np.array([0, -2])
    X2 = np.random.randn(Nclass, D) + np.array([2, 2])
    X3 = np.random.randn(Nclass, D) + np.array([-2, 2])
    X = np.vstack([X1, X2, X3])

    Y = np.array([0]*Nclass + [1]*Nclass + [2]*Nclass)
    return X, Y


def get_spiral():
    # Idea: radius -> low...high
    #           (don't start at 0, otherwise points will be "mushed" at origin)
    #       angle = low...high proportional to radius
    #               [0, 2pi/6, 4pi/6, ..., 10pi/6] --> [pi/2, pi/3 + pi/2, ..., ]
    # x = rcos(theta), y = rsin(theta) as usual

    radius = np.linspace(1, 10, 100)
    thetas = np.empty((6, 100))
    for i in range(6):
        start_angle = np.pi*i / 3.0
        end_angle = start_angle + np.pi / 2
        points = np.linspace(start_angle, end_angle, 100)
        thetas[i] = points

    # convert into cartesian coordinates
    x1 = np.empty((6, 100))
    x2 = np.empty((6, 100))
    for i in range(6):
        x1[i] = radius * np.cos(thetas[i])
        x2[i] = radius * np.sin(thetas[i])

    # inputs
    X = np.empty((600, 2))
    X[:,0] = x1.flatten()
    X[:,1] = x2.flatten()

    # add noise
    X += np.random.randn(600, 2)*0.5

    # targets
    Y = np.array([0]*100 + [1]*100 + [0]*100 + [1]*100 + [0]*100 + [1]*100)
    return X, Y



def get_transformed_data():
    print("Reading in and transforming data...")

    if not os.path.exists('../large_files/train.csv'):
        print('Looking for ../large_files/train.csv')
        print('You have not downloaded the data and/or not placed the files in the correct location.')
        print('Please get the data from: https://www.kaggle.com/c/digit-recognizer')
        print('Place train.csv in the folder large_files adjacent to the class folder')
        exit()

    df = pd.read_csv('../large_files/train.csv')
    data = df.values.astype(np.float32)
    np.random.shuffle(data)

    X = data[:, 1:]
    Y = data[:, 0].astype(np.int32)

    Xtrain = X[:-1000]
    Ytrain = Y[:-1000]
    Xtest  = X[-1000:]
    Ytest  = Y[-1000:]

    # center the data
    mu = Xtrain.mean(axis=0)
    Xtrain = Xtrain - mu
    Xtest  = Xtest - mu

    # transform the data
    pca = PCA()
    Ztrain = pca.fit_transform(Xtrain)
    Ztest  = pca.transform(Xtest)

    plot_cumulative_variance(pca)

    # take first 300 cols of Z
    Ztrain = Ztrain[:, :300]
    Ztest = Ztest[:, :300]

    # normalize Z
    mu = Ztrain.mean(axis=0)
    std = Ztrain.std(axis=0)
    Ztrain = (Ztrain - mu) / std
    Ztest = (Ztest - mu) / std

    return Ztrain, Ztest, Ytrain, Ytest


def get_normalized_data():
    print("Reading in and transforming data...")

    if not os.path.exists('../large_files/train.csv'):
        print('Looking for ../large_files/train.csv')
        print('You have not downloaded the data and/or not placed the files in the correct location.')
        print('Please get the data from: https://www.kaggle.com/c/digit-recognizer')
        print('Place train.csv in the folder large_files adjacent to the class folder')
        exit()

    df = pd.read_csv('../large_files/train.csv')
    data = df.values.astype(np.float32)
    np.random.shuffle(data)
    X = data[:, 1:]
    Y = data[:, 0]

    Xtrain = X[:-1000]
    Ytrain = Y[:-1000]
    Xtest  = X[-1000:]
    Ytest  = Y[-1000:]

    # normalize the data
    mu = Xtrain.mean(axis=0)
    std = Xtrain.std(axis=0)
    np.place(std, std == 0, 1)
    Xtrain = (Xtrain - mu) / std
    Xtest = (Xtest - mu) / std
    
    return Xtrain, Xtest, Ytrain, Ytest


def plot_cumulative_variance(pca):
    P = []
    for p in pca.explained_variance_ratio_:
        if len(P) == 0:
            P.append(p)
        else:
            P.append(p + P[-1])
    plt.plot(P)
    plt.show()
    return P


def forward(X, W, b):
    # softmax
    a = X.dot(W) + b
    expa = np.exp(a)
    y = expa / expa.sum(axis=1, keepdims=True)
    return y


def predict(p_y):
    return np.argmax(p_y, axis=1)


def error_rate(p_y, t):
    prediction = predict(p_y)
    return np.mean(prediction != t)
Download .txt
gitextract_oeszk7b8/

├── .gitignore
├── README.md
├── ab_testing/
│   ├── advertisement_clicks.csv
│   ├── bayesian_bandit.py
│   ├── bayesian_normal.py
│   ├── bayesian_starter.py
│   ├── cdfs_and_percentiles.py
│   ├── chisquare.py
│   ├── ci_comparison.py
│   ├── client.py
│   ├── comparing_epsilons.py
│   ├── convergence.py
│   ├── demo.py
│   ├── epsilon_greedy.py
│   ├── epsilon_greedy_starter.py
│   ├── ex_chisq.py
│   ├── ex_ttest.py
│   ├── extra_reading.txt
│   ├── optimistic.py
│   ├── optimistic_starter.py
│   ├── server_solution.py
│   ├── server_starter.py
│   ├── ttest.py
│   ├── ucb1.py
│   └── ucb1_starter.py
├── airline/
│   ├── ann.py
│   ├── international-airline-passengers.csv
│   ├── lr.py
│   └── rnn.py
├── ann_class/
│   ├── backprop.py
│   ├── extra_reading.txt
│   ├── forwardprop.py
│   ├── regression.py
│   ├── sklearn_ann.py
│   ├── tf_example.py
│   └── xor_donut.py
├── ann_class2/
│   ├── __init__.py
│   ├── adam.py
│   ├── batch_norm_tf.py
│   ├── batch_norm_theano.py
│   ├── cntk_example.py
│   ├── dropout_tensorflow.py
│   ├── dropout_theano.py
│   ├── extra_reading.txt
│   ├── grid_search.py
│   ├── keras_example.py
│   ├── keras_functional.py
│   ├── mlp.py
│   ├── momentum.py
│   ├── mxnet_example.py
│   ├── pytorch_batchnorm.py
│   ├── pytorch_dropout.py
│   ├── pytorch_example.py
│   ├── pytorch_example2.py
│   ├── random_search.py
│   ├── rmsprop.py
│   ├── rmsprop_test.py
│   ├── sgd.py
│   ├── tensorflow1.py
│   ├── tensorflow2.py
│   ├── tf_with_save.py
│   ├── theano1.py
│   ├── theano2.py
│   ├── theano_ann.py
│   └── util.py
├── ann_logistic_extra/
│   ├── __init__.py
│   ├── ann_predict.py
│   ├── ann_train.py
│   ├── ecommerce_data.csv
│   ├── logistic_predict.py
│   ├── logistic_softmax_train.py
│   ├── logistic_train.py
│   └── process.py
├── bayesian_ml/
│   ├── 1/
│   │   ├── Q.csv
│   │   ├── README
│   │   ├── Xtest.csv
│   │   ├── Xtrain.csv
│   │   ├── nb.py
│   │   ├── ytest.csv
│   │   └── ytrain.csv
│   ├── 2/
│   │   ├── Q.csv
│   │   ├── README
│   │   ├── Xtest.csv
│   │   ├── Xtrain.csv
│   │   ├── em.py
│   │   ├── probit.py
│   │   ├── ytest.csv
│   │   └── ytrain.csv
│   ├── 3/
│   │   ├── X_set1.csv
│   │   ├── X_set2.csv
│   │   ├── X_set3.csv
│   │   ├── run.py
│   │   ├── y_set1.csv
│   │   ├── y_set2.csv
│   │   ├── y_set3.csv
│   │   ├── z_set1.csv
│   │   ├── z_set2.csv
│   │   └── z_set3.csv
│   └── 4/
│       ├── data.txt
│       ├── emgmm.py
│       ├── npbgmm.py
│       └── vigmm.py
├── best_fit_line.py
├── calculus/
│   ├── WHERE ARE THE NOTEBOOKS.txt
│   └── extra_reading.txt
├── chatgpt_trading/
│   ├── WHERE ARE THE NOTEBOOKS.txt
│   └── extra_reading.txt
├── cnn_class/
│   ├── WHERE ARE THE NOTEBOOKS.txt
│   ├── benchmark.py
│   ├── blur.py
│   ├── cifar.py
│   ├── cnn_tf.py
│   ├── cnn_tf_plot_filters.py
│   ├── cnn_theano.py
│   ├── cnn_theano_plot_filters.py
│   ├── custom_blur.py
│   ├── echo.py
│   ├── edge.py
│   ├── edge_benchmark.py
│   ├── exercises.txt
│   ├── extra_reading.txt
│   └── keras_example.py
├── cnn_class2/
│   ├── WHERE ARE THE NOTEBOOKS.txt
│   ├── class_activation_maps.py
│   ├── extra_reading.txt
│   ├── fashion.py
│   ├── fashion2.py
│   ├── make_limited_datasets.py
│   ├── siamese.py
│   ├── ssd.py
│   ├── style_transfer1.py
│   ├── style_transfer2.py
│   ├── style_transfer3.py
│   ├── test_softmax.py
│   ├── tf_resnet.py
│   ├── tf_resnet_convblock.py
│   ├── tf_resnet_convblock_starter.py
│   ├── tf_resnet_first_layers.py
│   ├── tf_resnet_first_layers_starter.py
│   ├── tf_resnet_identity_block.py
│   ├── tf_resnet_identity_block_starter.py
│   ├── use_pretrained_weights_resnet.py
│   ├── use_pretrained_weights_vgg.py
│   └── util.py
├── data_csv/
│   ├── X.txt
│   ├── X_orig.txt
│   ├── legend.txt
│   ├── readme.txt
│   └── y.txt
├── financial_engineering/
│   └── go_here_instead.txt
├── hmm_class/
│   ├── __init__.py
│   ├── coin_data.txt
│   ├── edgar_allan_poe.txt
│   ├── extra_reading.txt
│   ├── frost.py
│   ├── generate_c.py
│   ├── generate_ht.py
│   ├── hmm_classifier.py
│   ├── hmmc.py
│   ├── hmmc_concat.py
│   ├── hmmc_scaled_concat.py
│   ├── hmmc_scaled_concat_diag.py
│   ├── hmmc_tf.py
│   ├── hmmc_theano.py
│   ├── hmmc_theano2.py
│   ├── hmmd.py
│   ├── hmmd_scaled.py
│   ├── hmmd_tf.py
│   ├── hmmd_theano.py
│   ├── hmmd_theano2.py
│   ├── robert_frost.txt
│   ├── scan1.py
│   ├── scan2.py
│   ├── scan3.py
│   ├── site_data.csv
│   ├── sites.py
│   ├── tf_scan1.py
│   ├── tf_scan2.py
│   └── tf_scan3.py
├── keras_examples/
│   ├── ann.py
│   ├── basic_mlp.py
│   ├── batchnorm.py
│   ├── cnn.py
│   ├── cnn_cifar.py
│   ├── cnn_dropout_batchnorm.py
│   ├── dropout.py
│   ├── sentiment_analysis.py
│   ├── sine.py
│   ├── sine2.py
│   ├── translation.py
│   └── util.py
├── kerascv/
│   ├── extra_reading.txt
│   ├── imagenet_label_names.json
│   ├── makelist.py
│   └── pascal2coco.py
├── linear_algebra/
│   ├── WHERE ARE THE NOTEBOOKS.txt
│   └── extra_reading.txt
├── linear_regression_class/
│   ├── data_1d.csv
│   ├── data_2d.csv
│   ├── data_poly.csv
│   ├── gd.py
│   ├── generate_1d.py
│   ├── generate_2d.py
│   ├── generate_poly.py
│   ├── gradient_descent.py
│   ├── l1_regularization.py
│   ├── l2_regularization.py
│   ├── lr_1d.py
│   ├── lr_2d.py
│   ├── lr_poly.py
│   ├── mlr02.xls
│   ├── moore.csv
│   ├── moore.py
│   ├── overfitting.py
│   └── systolic.py
├── logistic_regression_class/
│   ├── bad_xor.py
│   ├── l1_regularization.py
│   ├── logistic1.py
│   ├── logistic2.py
│   ├── logistic3.py
│   ├── logistic4.py
│   ├── logistic_donut.py
│   ├── logistic_visualize.py
│   └── logistic_xor.py
├── matrix_calculus/
│   └── extra_reading.txt
├── mnist_csv/
│   ├── Q.txt
│   ├── Xtest.txt
│   ├── Xtrain.txt
│   ├── label_test.txt
│   └── label_train.txt
├── naive_bayes/
│   ├── WHERE ARE THE NOTEBOOKS.txt
│   └── extra_reading.txt
├── nlp_class/
│   ├── all_book_titles.txt
│   ├── article_spinner.py
│   ├── cipher_placeholder.py
│   ├── electronics/
│   │   ├── negative.review
│   │   ├── positive.review
│   │   └── unlabeled.review
│   ├── extra_reading.txt
│   ├── lsa.py
│   ├── nb.py
│   ├── sentiment.py
│   ├── spam2.py
│   ├── spambase.data
│   └── stopwords.txt
├── nlp_class2/
│   ├── bow_classifier.py
│   ├── extra_reading.txt
│   ├── glove.py
│   ├── glove_svd.py
│   ├── glove_tf.py
│   ├── glove_theano.py
│   ├── logistic.py
│   ├── markov.py
│   ├── ner.txt
│   ├── ner_baseline.py
│   ├── ner_rnn.py
│   ├── ner_tf.py
│   ├── neural_network.py
│   ├── neural_network2.py
│   ├── pmi.py
│   ├── pos_baseline.py
│   ├── pos_hmm.py
│   ├── pos_ner_keras.py
│   ├── pos_rnn.py
│   ├── pos_tf.py
│   ├── pretrained_glove.py
│   ├── pretrained_w2v.py
│   ├── recursive_tensorflow.py
│   ├── recursive_theano.py
│   ├── rntn_tensorflow.py
│   ├── rntn_tensorflow_rnn.py
│   ├── rntn_theano.py
│   ├── tfidf_tsne.py
│   ├── util.py
│   ├── visualize_countries.py
│   ├── w2v_model.npz
│   ├── w2v_word2idx.json
│   ├── word2vec.py
│   ├── word2vec_tf.py
│   └── word2vec_theano.py
├── nlp_class3/
│   ├── attention.py
│   ├── bilstm_mnist.py
│   ├── bilstm_test.py
│   ├── cnn_toxic.py
│   ├── convert_twitter.py
│   ├── extra_reading.txt
│   ├── lstm_toxic.py
│   ├── memory_network.py
│   ├── poetry.py
│   ├── simple_rnn_test.py
│   └── wseq2seq.py
├── nlp_v2/
│   ├── WHERE ARE THE NOTEBOOKS.txt
│   └── extra_reading.txt
├── numpy_class/
│   ├── classification_example.py
│   ├── dot_for.py
│   ├── exercises/
│   │   ├── ex1.py
│   │   ├── ex2.py
│   │   ├── ex3.py
│   │   ├── ex4.py
│   │   ├── ex5.py
│   │   ├── ex6.py
│   │   ├── ex7.py
│   │   ├── ex8.py
│   │   └── ex9.py
│   ├── manual_data_loading.py
│   ├── python3/
│   │   ├── dot_for.py
│   │   └── manual_data_loading.py
│   ├── regression_example.py
│   ├── table1.csv
│   └── table2.csv
├── openai/
│   ├── extra_reading.txt
│   └── replies.json
├── probability/
│   ├── WHERE ARE THE NOTEBOOKS.txt
│   └── extra_reading.txt
├── prophet/
│   └── extra_reading.txt
├── pytorch/
│   ├── .gitignore
│   ├── WHERE ARE THE NOTEBOOKS.txt
│   ├── aapl_msi_sbux.csv
│   ├── ann_regression.py
│   ├── exercises.txt
│   ├── extra_reading.txt
│   ├── plot_rl_rewards.py
│   └── rl_trader.py
├── recommenders/
│   ├── autorec.py
│   ├── extra_reading.txt
│   ├── itembased.py
│   ├── mf.py
│   ├── mf2.py
│   ├── mf_keras.py
│   ├── mf_keras_deep.py
│   ├── mf_keras_res.py
│   ├── preprocess.py
│   ├── preprocess2dict.py
│   ├── preprocess2sparse.py
│   ├── preprocess_shrink.py
│   ├── rbm_tf_k.py
│   ├── rbm_tf_k_faster.py
│   ├── spark.py
│   ├── spark2.py
│   ├── tfidf.py
│   └── userbased.py
├── rl/
│   ├── approx_control.py
│   ├── approx_prediction.py
│   ├── bayesian_bandit.py
│   ├── bayesian_normal.py
│   ├── bayesian_starter.py
│   ├── cartpole.py
│   ├── cartpole_gym0.19.py
│   ├── comparing_epsilons.py
│   ├── comparing_explore_exploit_methods.py
│   ├── epsilon_greedy.py
│   ├── epsilon_greedy_starter.py
│   ├── extra_reading.txt
│   ├── grid_world.py
│   ├── iterative_policy_evaluation_deterministic.py
│   ├── iterative_policy_evaluation_probabilistic.py
│   ├── linear_rl_trader.py
│   ├── monte_carlo.py
│   ├── monte_carlo_es.py
│   ├── monte_carlo_no_es.py
│   ├── optimistic.py
│   ├── optimistic_initial_values.py
│   ├── optimistic_starter.py
│   ├── plot_rl_rewards.py
│   ├── policy_iteration_deterministic.py
│   ├── policy_iteration_probabilistic.py
│   ├── q_learning.py
│   ├── sarsa.py
│   ├── td0_prediction.py
│   ├── tic_tac_toe.py
│   ├── ucb1.py
│   ├── ucb1_starter.py
│   └── value_iteration.py
├── rl2/
│   ├── a3c/
│   │   ├── main.py
│   │   ├── nets.py
│   │   ├── thread_example.py
│   │   └── worker.py
│   ├── atari/
│   │   ├── dqn_tf.py
│   │   └── dqn_theano.py
│   ├── cartpole/
│   │   ├── dqn_tf.py
│   │   ├── dqn_theano.py
│   │   ├── pg_tf.py
│   │   ├── pg_theano.py
│   │   ├── q_learning.py
│   │   ├── q_learning_bins.py
│   │   ├── random_search.py
│   │   ├── save_a_video.py
│   │   ├── td_lambda.py
│   │   ├── tf_warmup.py
│   │   └── theano_warmup.py
│   ├── extra_reading.txt
│   ├── gym_tutorial.py
│   └── mountaincar/
│       ├── n_step.py
│       ├── pg_tf.py
│       ├── pg_tf_random.py
│       ├── pg_theano.py
│       ├── pg_theano_random.py
│       ├── q_learning.py
│       └── td_lambda.py
├── rl2v2/
│   └── extra_reading.txt
├── rl3/
│   ├── a2c/
│   │   ├── a2c.py
│   │   ├── atari_wrappers.py
│   │   ├── main.py
│   │   ├── neural_network.py
│   │   ├── play.py
│   │   └── subproc_vec_env.py
│   ├── ddpg.py
│   ├── es_flappy.py
│   ├── es_mnist.py
│   ├── es_mujoco.py
│   ├── es_simple.py
│   ├── extra_reading.txt
│   ├── flappy2envs.py
│   ├── gym_review.py
│   ├── plot_ddpg_result.py
│   ├── plot_es_flappy_results.py
│   ├── plot_es_mujoco_results.py
│   └── sample_test.py
├── rl3v2/
│   ├── extra_reading.txt
│   ├── visualize_es.py
│   └── visualize_hill_climbing.py
├── rnn_class/
│   ├── WHERE ARE THE NOTEBOOKS.txt
│   ├── __init__.py
│   ├── batch_gru.py
│   ├── batch_parity.py
│   ├── batch_units.py
│   ├── batch_wiki.py
│   ├── brown.py
│   ├── exercises.txt
│   ├── extra_reading.txt
│   ├── gru.py
│   ├── gru_nonorm_part1_wikipedia_word2idx.json
│   ├── gru_nonorm_part1_word_embeddings.npy
│   ├── lstm.py
│   ├── mlp_parity.py
│   ├── poetry_classifier.py
│   ├── rrnn_language.py
│   ├── srn_language.py
│   ├── srn_language_tf.py
│   ├── srn_parity.py
│   ├── srn_parity_tf.py
│   ├── tf_parity.py
│   ├── util.py
│   ├── visualize_embeddings.py
│   └── wiki.py
├── stats/
│   └── extra_reading.txt
├── supervised_class/
│   ├── app.py
│   ├── app_caller.py
│   ├── app_trainer.py
│   ├── bayes.py
│   ├── dt.py
│   ├── dt_without_recursion.py
│   ├── knn.py
│   ├── knn_donut.py
│   ├── knn_fail.py
│   ├── knn_vectorized.py
│   ├── knn_xor.py
│   ├── multinomialnb.py
│   ├── nb.py
│   ├── perceptron.py
│   ├── regression.py
│   └── util.py
├── supervised_class2/
│   ├── adaboost.py
│   ├── bagging_classification.py
│   ├── bagging_regression.py
│   ├── bias_variance_demo.py
│   ├── bootstrap.py
│   ├── extra_reading.txt
│   ├── knn_dt_demo.py
│   ├── rf_classification.py
│   ├── rf_regression.py
│   ├── rf_vs_bag.py
│   ├── rf_vs_bag2.py
│   └── util.py
├── svm_class/
│   ├── crossval.py
│   ├── extra_reading.txt
│   ├── fake_neural_net.py
│   ├── kernel_svm_gradient_primal.py
│   ├── linear_svm_gradient.py
│   ├── rbfnetwork.py
│   ├── real_neural_net.py
│   ├── regression.py
│   ├── svm_gradient.py
│   ├── svm_medical.py
│   ├── svm_mnist.py
│   ├── svm_smo.py
│   ├── svm_spam.py
│   └── util.py
├── tensorflow/
│   └── input_data.py
├── tf2.0/
│   ├── .gitignore
│   ├── WHERE ARE THE NOTEBOOKS.txt
│   ├── aapl_msi_sbux.csv
│   ├── auto-mpg.data
│   ├── daily-minimum-temperatures-in-me.csv
│   ├── exercises.txt
│   ├── extra_reading.txt
│   ├── fake_util.py
│   ├── keras_trader.py
│   ├── mlp_trader.py
│   ├── moore.csv
│   ├── plot_rl_rewards.py
│   ├── rl_trader.py
│   ├── sbux.csv
│   └── xor3d.py
├── timeseries/
│   ├── WHERE ARE THE NOTEBOOKS.txt
│   └── extra_reading.txt
├── transformers/
│   ├── WHERE ARE THE NOTEBOOKS.txt
│   └── extra_reading.txt
├── unsupervised_class/
│   ├── __init__.py
│   ├── books.py
│   ├── choose_k.py
│   ├── evolution.py
│   ├── gmm.py
│   ├── gmm_mnist.py
│   ├── hcluster.py
│   ├── kmeans.py
│   ├── kmeans_fail.py
│   ├── kmeans_mnist.py
│   ├── kmeans_visualize.py
│   ├── neural_kmeans.py
│   └── tweets.py
├── unsupervised_class2/
│   ├── __init__.py
│   ├── autoencoder.py
│   ├── autoencoder_tf.py
│   ├── compare_pca_svd.py
│   ├── extra_reading.txt
│   ├── gaussian_nb.py
│   ├── pca.py
│   ├── pca_impl.py
│   ├── rbm.py
│   ├── rbm_tf.py
│   ├── sk_mlp.py
│   ├── tsne_books.py
│   ├── tsne_donut.py
│   ├── tsne_mnist.py
│   ├── tsne_visualization.py
│   ├── tsne_xor.py
│   ├── umap_transformer.py
│   ├── unsupervised.py
│   ├── util.py
│   ├── vanishing.py
│   ├── visualize_features.py
│   └── xwing.py
└── unsupervised_class3/
    ├── autoencoder_tf.py
    ├── autoencoder_theano.py
    ├── bayes_classifier_gaussian.py
    ├── bayes_classifier_gmm.py
    ├── dcgan_tf.py
    ├── dcgan_theano.py
    ├── extra_reading.txt
    ├── parameterize_guassian.py
    ├── test_stochastic_tensor.py
    ├── util.py
    ├── vae_tf.py
    ├── vae_theano.py
    └── visualize_latent_space.py
Download .txt
SYMBOL INDEX (2112 symbols across 341 files)

FILE: ab_testing/bayesian_bandit.py
  class Bandit (line 20) | class Bandit:
    method __init__ (line 21) | def __init__(self, p):
    method pull (line 27) | def pull(self):
    method sample (line 30) | def sample(self):
    method update (line 33) | def update(self, x):
  function plot (line 39) | def plot(bandits, trial):
  function experiment (line 49) | def experiment():

FILE: ab_testing/bayesian_normal.py
  class Bandit (line 18) | class Bandit:
    method __init__ (line 19) | def __init__(self, true_mean):
    method pull (line 27) | def pull(self):
    method sample (line 30) | def sample(self):
    method update (line 33) | def update(self, x):
  function plot (line 39) | def plot(bandits, trial):
  function run_experiment (line 49) | def run_experiment():

FILE: ab_testing/bayesian_starter.py
  class Bandit (line 20) | class Bandit:
    method __init__ (line 21) | def __init__(self, p):
    method pull (line 27) | def pull(self):
    method sample (line 30) | def sample(self):
    method update (line 33) | def update(self, x):
  function plot (line 39) | def plot(bandits, trial):
  function experiment (line 49) | def experiment():

FILE: ab_testing/chisquare.py
  class DataGenerator (line 33) | class DataGenerator:
    method __init__ (line 34) | def __init__(self, p1, p2):
    method next (line 38) | def next(self):
  function get_p_value (line 44) | def get_p_value(T):
  function run_experiment (line 52) | def run_experiment(p1, p2, N):

FILE: ab_testing/comparing_epsilons.py
  class BanditArm (line 12) | class BanditArm:
    method __init__ (line 13) | def __init__(self, m):
    method pull (line 18) | def pull(self):
    method update (line 21) | def update(self, x):
  function run_experiment (line 26) | def run_experiment(m1, m2, m3, eps, N):

FILE: ab_testing/convergence.py
  function run_experiment (line 15) | def run_experiment(p1, p2, p3, N):

FILE: ab_testing/demo.py
  function plot (line 14) | def plot(a, b, trial, ctr):

FILE: ab_testing/epsilon_greedy.py
  class BanditArm (line 19) | class BanditArm:
    method __init__ (line 20) | def __init__(self, p):
    method pull (line 26) | def pull(self):
    method update (line 30) | def update(self, x):
  function choose_random_argmax (line 35) | def choose_random_argmax(a):
  function experiment (line 40) | def experiment():

FILE: ab_testing/epsilon_greedy_starter.py
  class BanditArm (line 19) | class BanditArm:
    method __init__ (line 20) | def __init__(self, p):
    method pull (line 26) | def pull(self):
    method update (line 30) | def update(self, x):
  function experiment (line 35) | def experiment():

FILE: ab_testing/ex_chisq.py
  function get_p_value (line 21) | def get_p_value(T):

FILE: ab_testing/optimistic.py
  class Bandit (line 19) | class Bandit:
    method __init__ (line 20) | def __init__(self, p):
    method pull (line 26) | def pull(self):
    method update (line 30) | def update(self, x):
  function experiment (line 35) | def experiment():

FILE: ab_testing/optimistic_starter.py
  class Bandit (line 19) | class Bandit:
    method __init__ (line 20) | def __init__(self, p):
    method pull (line 26) | def pull(self):
    method update (line 30) | def update(self, x):
  function experiment (line 35) | def experiment():

FILE: ab_testing/server_solution.py
  class Bandit (line 21) | class Bandit:
    method __init__ (line 22) | def __init__(self, name):
    method sample (line 27) | def sample(self):
    method add_click (line 33) | def add_click(self):
    method add_view (line 36) | def add_view(self):
  function get_ad (line 51) | def get_ad():
  function click_ad (line 62) | def click_ad():

FILE: ab_testing/server_starter.py
  class Bandit (line 21) | class Bandit:
    method __init__ (line 22) | def __init__(self, name):
    method sample (line 25) | def sample(self):
  function get_ad (line 39) | def get_ad():
  function click_ad (line 45) | def click_ad():

FILE: ab_testing/ucb1.py
  class Bandit (line 18) | class Bandit:
    method __init__ (line 19) | def __init__(self, p):
    method pull (line 25) | def pull(self):
    method update (line 29) | def update(self, x):
  function ucb (line 34) | def ucb(mean, n, nj):
  function run_experiment (line 38) | def run_experiment():

FILE: ab_testing/ucb1_starter.py
  class Bandit (line 18) | class Bandit:
    method __init__ (line 19) | def __init__(self, p):
    method pull (line 25) | def pull(self):
    method update (line 29) | def update(self, x):
  function ucb (line 34) | def ucb(mean, n, nj):
  function run_experiment (line 38) | def run_experiment():

FILE: airline/ann.py
  function init_weight (line 12) | def init_weight(M1, M2):
  function myr2 (line 15) | def myr2(T, Y):
  class HiddenLayer (line 21) | class HiddenLayer(object):
    method __init__ (line 22) | def __init__(self, M1, M2, f, an_id):
    method forward (line 33) | def forward(self, X):
  class ANN (line 37) | class ANN(object):
    method __init__ (line 38) | def __init__(self, hidden_layer_sizes):
    method fit (line 41) | def fit(self, X, Y, activation=T.tanh, learning_rate=1e-3, mu=0.5, reg...
    method forward (line 118) | def forward(self, X):
    method score (line 124) | def score(self, X, Y):
    method predict (line 128) | def predict(self, X):

FILE: airline/rnn.py
  function init_weight (line 19) | def init_weight(M1, M2):
  function myr2 (line 22) | def myr2(T, Y):
  class RNN (line 28) | class RNN(object):
    method __init__ (line 29) | def __init__(self, hidden_layer_sizes):
    method fit (line 32) | def fit(self, X, Y, activation=T.tanh, learning_rate=1e-1, mu=0.5, reg...
    method forward (line 99) | def forward(self, X):
    method score (line 105) | def score(self, X, Y):
    method predict (line 109) | def predict(self, X):

FILE: ann_class/backprop.py
  function forward (line 17) | def forward(X, W1, b1, W2, b2):
  function classification_rate (line 27) | def classification_rate(Y, P):
  function derivative_w2 (line 37) | def derivative_w2(Z, T, Y):
  function derivative_w1 (line 70) | def derivative_w1(X, Z, T, Y, W2):
  function derivative_b2 (line 91) | def derivative_b2(T, Y):
  function derivative_b1 (line 95) | def derivative_b1(T, Y, W2, Z):
  function cost (line 99) | def cost(T, Y):
  function main (line 104) | def main():

FILE: ann_class/forwardprop.py
  function sigmoid (line 39) | def sigmoid(a):
  function forward (line 42) | def forward(X, W1, b1, W2, b2):
  function classification_rate (line 53) | def classification_rate(Y, P):

FILE: ann_class/regression.py
  function forward (line 52) | def forward(X):
  function derivative_V (line 64) | def derivative_V(Z, Y, Yhat):
  function derivative_c (line 67) | def derivative_c(Y, Yhat):
  function derivative_W (line 70) | def derivative_W(X, Z, Y, Yhat, V):
  function derivative_b (line 75) | def derivative_b(Z, Y, Yhat, V):
  function update (line 80) | def update(X, Z, Y, Yhat, W, b, V, c, learning_rate=1e-4):
  function get_cost (line 97) | def get_cost(Y, Yhat):

FILE: ann_class/tf_example.py
  function init_weights (line 41) | def init_weights(shape):
  function forward (line 45) | def forward(X, W1, b1, W2, b2):

FILE: ann_class/xor_donut.py
  function forward (line 19) | def forward(X, W1, b1, W2, b2):
  function predict (line 35) | def predict(X, W1, b1, W2, b2):
  function derivative_w2 (line 40) | def derivative_w2(Z, T, Y):
  function derivative_b2 (line 44) | def derivative_b2(T, Y):
  function derivative_w1 (line 48) | def derivative_w1(X, Z, T, Y, W2):
  function derivative_b1 (line 55) | def derivative_b1(Z, T, Y, W2):
  function get_log_likelihood (line 62) | def get_log_likelihood(T, Y):
  function test_xor (line 67) | def test_xor():
  function test_donut (line 104) | def test_donut():

FILE: ann_class2/adam.py
  function main (line 18) | def main():

FILE: ann_class2/batch_norm_tf.py
  function init_weight (line 15) | def init_weight(M1, M2):
  class HiddenLayerBatchNorm (line 19) | class HiddenLayerBatchNorm(object):
    method __init__ (line 20) | def __init__(self, M1, M2, f):
    method forward (line 37) | def forward(self, X, is_training, decay=0.9):
  class HiddenLayer (line 71) | class HiddenLayer(object):
    method __init__ (line 72) | def __init__(self, M1, M2, f):
    method forward (line 81) | def forward(self, X):
  class ANN (line 85) | class ANN(object):
    method __init__ (line 86) | def __init__(self, hidden_layer_sizes):
    method set_session (line 89) | def set_session(self, session):
    method fit (line 92) | def fit(self, X, Y, Xtest, Ytest, activation=tf.nn.relu, learning_rate...
    method forward (line 168) | def forward(self, X, is_training):
    method score (line 175) | def score(self, X, Y):
    method predict (line 179) | def predict(self, X):
  function main (line 184) | def main():

FILE: ann_class2/batch_norm_theano.py
  function init_weight (line 17) | def init_weight(M1, M2):
  class HiddenLayerBatchNorm (line 21) | class HiddenLayerBatchNorm(object):
    method __init__ (line 22) | def __init__(self, M1, M2, f):
    method forward (line 43) | def forward(self, X, is_training):
  class HiddenLayer (line 83) | class HiddenLayer(object):
    method __init__ (line 84) | def __init__(self, M1, M2, f):
    method forward (line 94) | def forward(self, X):
  function momentum_updates (line 98) | def momentum_updates(cost, params, lr, mu):
  class ANN (line 111) | class ANN(object):
    method __init__ (line 112) | def __init__(self, hidden_layer_sizes):
    method fit (line 115) | def fit(self, X, Y, Xtest, Ytest, activation=T.nnet.relu, learning_rat...
    method forward (line 196) | def forward(self, X, is_training):
    method score (line 203) | def score(self, X, Y):
  function main (line 209) | def main():

FILE: ann_class2/cntk_example.py
  function get_output (line 97) | def get_output(node, X, Y):

FILE: ann_class2/dropout_tensorflow.py
  class HiddenLayer (line 17) | class HiddenLayer(object):
    method __init__ (line 18) | def __init__(self, M1, M2):
    method forward (line 27) | def forward(self, X):
  class ANN (line 31) | class ANN(object):
    method __init__ (line 32) | def __init__(self, hidden_layer_sizes, p_keep):
    method fit (line 36) | def fit(self, X, Y, Xvalid, Yvalid, lr=1e-4, mu=0.9, decay=0.9, epochs...
    method forward (line 110) | def forward(self, X):
    method forward_test (line 120) | def forward_test(self, X):
    method predict (line 126) | def predict(self, X):
  function error_rate (line 131) | def error_rate(p, t):
  function relu (line 135) | def relu(a):
  function main (line 139) | def main():

FILE: ann_class2/dropout_theano.py
  function momentum_updates (line 22) | def momentum_updates(cost, params, lr, mu):
  class HiddenLayer (line 35) | class HiddenLayer(object):
    method __init__ (line 36) | def __init__(self, M1, M2, an_id):
    method forward (line 46) | def forward(self, X):
  class ANN (line 50) | class ANN(object):
    method __init__ (line 51) | def __init__(self, hidden_layer_sizes, p_keep):
    method fit (line 55) | def fit(self, X, Y, Xvalid, Yvalid, learning_rate=1e-2, mu=0.9, decay=...
    method forward_train (line 124) | def forward_train(self, X):
    method forward_predict (line 134) | def forward_predict(self, X):
    method predict (line 140) | def predict(self, X):
  function error_rate (line 145) | def error_rate(p, t):
  function relu (line 149) | def relu(a):
  function main (line 153) | def main():

FILE: ann_class2/grid_search.py
  function grid_search (line 18) | def grid_search():

FILE: ann_class2/mlp.py
  function forward (line 12) | def forward(X, W1, b1, W2, b2):
  function derivative_w2 (line 25) | def derivative_w2(Z, T, Y):
  function derivative_b2 (line 28) | def derivative_b2(T, Y):
  function derivative_w1 (line 31) | def derivative_w1(X, Z, T, Y, W2):
  function derivative_b1 (line 35) | def derivative_b1(Z, T, Y, W2):

FILE: ann_class2/momentum.py
  function main (line 22) | def main():

FILE: ann_class2/pytorch_batchnorm.py
  function train (line 68) | def train(model, loss, optimizer, inputs, labels):
  function get_cost (line 95) | def get_cost(model, loss, inputs, labels):
  function predict (line 113) | def predict(model, inputs):
  function score (line 127) | def score(model, inputs, labels):

FILE: ann_class2/pytorch_dropout.py
  function train (line 69) | def train(model, loss, optimizer, inputs, labels):
  function get_cost (line 96) | def get_cost(model, loss, inputs, labels):
  function predict (line 114) | def predict(model, inputs):
  function score (line 128) | def score(model, inputs, labels):

FILE: ann_class2/pytorch_example.py
  function train (line 66) | def train(model, loss, optimizer, inputs, labels):
  function predict (line 93) | def predict(model, inputs):

FILE: ann_class2/pytorch_example2.py
  function train (line 66) | def train(model, loss, optimizer, inputs, labels):
  function get_cost (line 89) | def get_cost(model, loss, inputs, labels):
  function predict (line 103) | def predict(model, inputs):
  function score (line 113) | def score(model, inputs, labels):

FILE: ann_class2/random_search.py
  function random_search (line 18) | def random_search():

FILE: ann_class2/rmsprop.py
  function main (line 18) | def main():

FILE: ann_class2/rmsprop_test.py
  function rmsprop (line 79) | def rmsprop(cost, params, lr=1e-3, decay=0.9, eps=1e-8):

FILE: ann_class2/sgd.py
  function main (line 24) | def main():

FILE: ann_class2/tensorflow2.py
  function error_rate (line 21) | def error_rate(p, t):
  function main (line 26) | def main():

FILE: ann_class2/tf_with_save.py
  function error_rate (line 18) | def error_rate(p, t):
  class TFLogistic (line 22) | class TFLogistic:
    method __init__ (line 23) | def __init__(self, savefile, D=None, K=None):
    method build (line 29) | def build(self, D, K):
    method fit (line 54) | def fit(self, X, Y, Xtest, Ytest):
    method predict (line 100) | def predict(self, X):
    method score (line 108) | def score(self, X, Y):
    method save (line 111) | def save(self, filename):
    method load (line 121) | def load(filename):
  function main (line 127) | def main():

FILE: ann_class2/theano2.py
  function error_rate (line 21) | def error_rate(p, t):
  function relu (line 25) | def relu(a):
  function main (line 29) | def main():

FILE: ann_class2/theano_ann.py
  function init_weight (line 17) | def init_weight(M1, M2):
  class HiddenLayer (line 21) | class HiddenLayer(object):
    method __init__ (line 22) | def __init__(self, M1, M2, f):
    method forward (line 32) | def forward(self, X):
  class ANN (line 38) | class ANN(object):
    method __init__ (line 39) | def __init__(self, hidden_layer_sizes):
    method fit (line 42) | def fit(self, X, Y, activation=T.nnet.relu, learning_rate=1e-3, mu=0.0...
    method forward (line 118) | def forward(self, X):
    method score (line 124) | def score(self, X, Y):
    method predict (line 128) | def predict(self, X):

FILE: ann_class2/util.py
  function get_clouds (line 21) | def get_clouds():
  function get_spiral (line 34) | def get_spiral():
  function get_transformed_data (line 70) | def get_transformed_data():
  function get_normalized_data (line 117) | def get_normalized_data():
  function plot_cumulative_variance (line 148) | def plot_cumulative_variance(pca):
  function forward (line 160) | def forward(X, W, b):
  function predict (line 168) | def predict(p_y):
  function error_rate (line 172) | def error_rate(p_y, t):
  function cost (line 177) | def cost(p_y, t):
  function gradW (line 182) | def gradW(t, y, X):
  function gradb (line 186) | def gradb(t, y):
  function y2indicator (line 190) | def y2indicator(y):
  function benchmark_full (line 199) | def benchmark_full():
  function benchmark_pca (line 255) | def benchmark_pca():

FILE: ann_logistic_extra/ann_predict.py
  function softmax (line 22) | def softmax(a):
  function forward (line 26) | def forward(X, W1, b1, W2, b2):
  function classification_rate (line 35) | def classification_rate(Y, P):

FILE: ann_logistic_extra/ann_train.py
  function y2indicator (line 13) | def y2indicator(y, K):
  function softmax (line 36) | def softmax(a):
  function forward (line 40) | def forward(X, W1, b1, W2, b2):
  function predict (line 44) | def predict(P_Y_given_X):
  function classification_rate (line 48) | def classification_rate(Y, P):
  function cross_entropy (line 51) | def cross_entropy(Y, pY):

FILE: ann_logistic_extra/logistic_predict.py
  function sigmoid (line 18) | def sigmoid(a):
  function forward (line 21) | def forward(X, W, b):
  function classification_rate (line 28) | def classification_rate(Y, P):

FILE: ann_logistic_extra/logistic_softmax_train.py
  function y2indicator (line 13) | def y2indicator(y, K):
  function softmax (line 33) | def softmax(a):
  function forward (line 37) | def forward(X, W, b):
  function predict (line 40) | def predict(P_Y_given_X):
  function classification_rate (line 44) | def classification_rate(Y, P):
  function cross_entropy (line 47) | def cross_entropy(Y, pY):

FILE: ann_logistic_extra/logistic_train.py
  function sigmoid (line 22) | def sigmoid(a):
  function forward (line 25) | def forward(X, W, b):
  function classification_rate (line 29) | def classification_rate(Y, P):
  function cross_entropy (line 33) | def cross_entropy(T, pY):

FILE: ann_logistic_extra/process.py
  function get_data (line 17) | def get_data():
  function get_binary_data (line 69) | def get_binary_data():

FILE: bayesian_ml/1/nb.py
  class NB (line 11) | class NB:
    method fit (line 12) | def fit(self, X, Y):
    method predict_proba (line 41) | def predict_proba(self, X):
    method predict (line 72) | def predict(self, X):
    method score (line 75) | def score(self, X, Y):
    method confusion_matrix (line 78) | def confusion_matrix(self, X, Y):
    method get_3_misclassified (line 87) | def get_3_misclassified(self, X, Y):
    method get_3_most_ambiguous (line 93) | def get_3_most_ambiguous(self, X, Y):
  function plot_image (line 109) | def plot_image(x, Q, title):

FILE: bayesian_ml/2/em.py
  function loglikelihood (line 25) | def loglikelihood(X, Z, W):

FILE: bayesian_ml/2/probit.py
  class ProbitRegression (line 11) | class ProbitRegression:
    method fit (line 12) | def fit(self, X, Y, sigma=1.5, lam=1, show_w=set(), Q=None):
    method predict_proba (line 51) | def predict_proba(self, X):
    method predict (line 55) | def predict(self, X):
    method score (line 58) | def score(self, X, Y):
    method confusion_matrix (line 61) | def confusion_matrix(self, X, Y):
    method get_3_misclassified (line 70) | def get_3_misclassified(self, X, Y):
    method get_3_most_ambiguous (line 76) | def get_3_most_ambiguous(self, X, Y):
  function plot_image (line 92) | def plot_image(x, Q, title):

FILE: bayesian_ml/3/run.py
  function e_ln_q_gamma (line 14) | def e_ln_q_gamma(a, b):
  function objective (line 17) | def objective(X, Y, C, mu, a, b, e, f, a0, b0, e0, f0):
  function run (line 79) | def run(num=1, T=500):

FILE: bayesian_ml/4/emgmm.py
  function gmm (line 10) | def gmm(X, K, max_iter=20, smoothing=1e-2):
  function main (line 63) | def main():

FILE: bayesian_ml/4/npbgmm.py
  function marginal (line 19) | def marginal(x, c, m, a, B):
  function normalize_phi_hat (line 32) | def normalize_phi_hat(phi_hat):
  function sample_cluster_identity (line 41) | def sample_cluster_identity(phi):
  function sample_from_prior (line 54) | def sample_from_prior(c0, m0, a0, B0):
  function sample_from_X (line 62) | def sample_from_X(X, m0, c0, a0, B0):
  function gmm (line 75) | def gmm(X, T=500):
  function main (line 196) | def main():

FILE: bayesian_ml/4/vigmm.py
  function get_cost (line 11) | def get_cost(X, K, cluster_assignments, phi, alphas, mu_means, mu_covs, ...
  function gmm (line 98) | def gmm(X, K, max_iter=100):
  function main (line 207) | def main():

FILE: cnn_class/benchmark.py
  function error_rate (line 20) | def error_rate(p, t):
  function flatten (line 24) | def flatten(X):
  function get_data (line 50) | def get_data():
  function main (line 63) | def main():

FILE: cnn_class/cifar.py
  function init_weight_and_bias (line 17) | def init_weight_and_bias(M1, M2):
  function init_filter (line 23) | def init_filter(shape, poolsz):
  function error_rate (line 28) | def error_rate(targets, predictions):
  function image2array (line 32) | def image2array(im):
  function getImageData (line 37) | def getImageData():
  class HiddenLayer (line 70) | class HiddenLayer(object):
    method __init__ (line 71) | def __init__(self, M1, M2, an_id):
    method forward (line 80) | def forward(self, X):
  class ConvPoolLayer (line 84) | class ConvPoolLayer(object):
    method __init__ (line 85) | def __init__(self, mi, mo, fw=5, fh=5, poolsz=(2, 2)):
    method forward (line 96) | def forward(self, X):
  class CNN (line 106) | class CNN(object):
    method __init__ (line 107) | def __init__(self, convpool_layer_sizes, hidden_layer_sizes):
    method fit (line 111) | def fit(self, X, Y, lr=1e-4, mu=0.99, reg=1e-6, decay=0.99999, eps=1e-...
    method forward (line 211) | def forward(self, X):
    method predict (line 220) | def predict(self, X):
  function main (line 225) | def main():

FILE: cnn_class/cnn_tf.py
  function convpool (line 26) | def convpool(X, W, b):
  function init_filter (line 34) | def init_filter(shape, poolsz):
  function rearrange (line 39) | def rearrange(X):
  function main (line 51) | def main():

FILE: cnn_class/cnn_tf_plot_filters.py
  function convpool (line 26) | def convpool(X, W, b):
  function init_filter (line 34) | def init_filter(shape, poolsz):
  function rearrange (line 39) | def rearrange(X):

FILE: cnn_class/cnn_theano.py
  function relu (line 24) | def relu(a):
  function convpool (line 28) | def convpool(X, W, b, poolsize=(2, 2)):
  function init_filter (line 46) | def init_filter(shape, poolsz):
  function rearrange (line 51) | def rearrange(X):
  function main (line 63) | def main():

FILE: cnn_class/cnn_theano_plot_filters.py
  function main (line 22) | def main():

FILE: cnn_class/custom_blur.py
  function convolve2d (line 40) | def convolve2d(X, W):

FILE: cnn_class/edge_benchmark.py
  function convolve_flatten (line 34) | def convolve_flatten(X):
  function main (line 50) | def main():

FILE: cnn_class/keras_example.py
  function rearrange (line 31) | def rearrange(X):

FILE: cnn_class2/fashion.py
  function y2indicator (line 18) | def y2indicator(Y):

FILE: cnn_class2/fashion2.py
  function y2indicator (line 18) | def y2indicator(Y):

FILE: cnn_class2/make_limited_datasets.py
  function mkdir (line 5) | def mkdir(p):
  function link (line 9) | def link(src, dst):

FILE: cnn_class2/siamese.py
  function load_img (line 30) | def load_img(filepath):
  function train_generator (line 155) | def train_generator():
  function test_generator (line 196) | def test_generator():
  function euclidean_distance (line 263) | def euclidean_distance(features):
  function contrastive_loss (line 277) | def contrastive_loss(y_true, y_pred):
  function get_train_accuracy (line 291) | def get_train_accuracy(threshold=0.85):
  function get_test_accuracy (line 356) | def get_test_accuracy(threshold=0.85):

FILE: cnn_class2/ssd.py
  function load_image_into_numpy_array (line 67) | def load_image_into_numpy_array(image):

FILE: cnn_class2/style_transfer1.py
  function VGG16_AvgPool (line 32) | def VGG16_AvgPool(shape):
  function VGG16_AvgPool_CutOff (line 56) | def VGG16_AvgPool_CutOff(shape, num_convs):
  function unpreprocess (line 87) | def unpreprocess(img):
  function scale_img (line 95) | def scale_img(x):
  function get_loss_and_grads_wrapper (line 146) | def get_loss_and_grads_wrapper(x_vec):

FILE: cnn_class2/style_transfer2.py
  function gram_matrix (line 30) | def gram_matrix(img):
  function style_loss (line 42) | def style_loss(y, t):
  function minimize (line 47) | def minimize(fn, epochs, batch_shape):
  function get_loss_and_grads_wrapper (line 137) | def get_loss_and_grads_wrapper(x_vec):

FILE: cnn_class2/style_transfer3.py
  function load_img_and_preprocess (line 35) | def load_img_and_preprocess(path, shape=None):
  function get_loss_and_grads_wrapper (line 125) | def get_loss_and_grads_wrapper(x_vec):

FILE: cnn_class2/test_softmax.py
  function custom_softmax (line 13) | def custom_softmax(x):

FILE: cnn_class2/tf_resnet.py
  class AvgPool (line 55) | class AvgPool:
    method __init__ (line 56) | def __init__(self, ksize):
    method forward (line 59) | def forward(self, X):
    method get_params (line 67) | def get_params(self):
  class Flatten (line 70) | class Flatten:
    method forward (line 71) | def forward(self, X):
    method get_params (line 74) | def get_params(self):
  function custom_softmax (line 78) | def custom_softmax(x):
  class DenseLayer (line 85) | class DenseLayer:
    method __init__ (line 86) | def __init__(self, mi, mo):
    method forward (line 90) | def forward(self, X):
    method copyFromKerasLayers (line 97) | def copyFromKerasLayers(self, layer):
    method get_params (line 103) | def get_params(self):
  class TFResNet (line 107) | class TFResNet:
    method __init__ (line 108) | def __init__(self):
    method copyFromKerasLayers (line 147) | def copyFromKerasLayers(self, layers):
    method forward (line 180) | def forward(self, X):
    method predict (line 185) | def predict(self, X):
    method set_session (line 192) | def set_session(self, session):
    method get_params (line 200) | def get_params(self):

FILE: cnn_class2/tf_resnet_convblock.py
  function init_filter (line 14) | def init_filter(d, mi, mo, stride):
  class ConvLayer (line 18) | class ConvLayer:
    method __init__ (line 19) | def __init__(self, d, mi, mo, stride=2, padding='VALID'):
    method forward (line 25) | def forward(self, X):
    method copyFromKerasLayers (line 35) | def copyFromKerasLayers(self, layer):
    method get_params (line 47) | def get_params(self):
  class BatchNormLayer (line 51) | class BatchNormLayer:
    method __init__ (line 52) | def __init__(self, D):
    method forward (line 58) | def forward(self, X):
    method copyFromKerasLayers (line 68) | def copyFromKerasLayers(self, layer):
    method get_params (line 79) | def get_params(self):
  class ConvBlock (line 83) | class ConvBlock:
    method __init__ (line 84) | def __init__(self, mi, fm_sizes, stride=2, activation=tf.nn.relu):
    method forward (line 125) | def forward(self, X):
    method predict (line 142) | def predict(self, X):
    method set_session (line 149) | def set_session(self, session):
    method copyFromKerasLayers (line 162) | def copyFromKerasLayers(self, layers):
    method get_params (line 184) | def get_params(self):

FILE: cnn_class2/tf_resnet_convblock_starter.py
  class ConvBlock (line 14) | class ConvBlock:
    method __init__ (line 15) | def __init__(self):
    method predict (line 18) | def predict(self, X):

FILE: cnn_class2/tf_resnet_first_layers.py
  class ReLULayer (line 47) | class ReLULayer:
    method forward (line 48) | def forward(self, X):
    method get_params (line 51) | def get_params(self):
  class MaxPoolLayer (line 54) | class MaxPoolLayer:
    method __init__ (line 55) | def __init__(self, dim):
    method forward (line 58) | def forward(self, X):
    method get_params (line 66) | def get_params(self):
  class PartialResNet (line 69) | class PartialResNet:
    method __init__ (line 70) | def __init__(self):
    method copyFromKerasLayers (line 83) | def copyFromKerasLayers(self, layers):
    method forward (line 88) | def forward(self, X):
    method predict (line 93) | def predict(self, X):
    method set_session (line 100) | def set_session(self, session):
    method get_params (line 106) | def get_params(self):

FILE: cnn_class2/tf_resnet_first_layers_starter.py
  class PartialResNet (line 26) | class PartialResNet:
    method __init__ (line 27) | def __init__(self):
    method copyFromKerasLayers (line 31) | def copyFromKerasLayers(self, layers):
    method predict (line 35) | def predict(self, X):
    method set_session (line 39) | def set_session(self, session):
    method get_params (line 43) | def get_params(self):

FILE: cnn_class2/tf_resnet_identity_block.py
  class IdentityBlock (line 16) | class IdentityBlock:
    method __init__ (line 17) | def __init__(self, mi, fm_sizes, activation=tf.nn.relu):
    method forward (line 49) | def forward(self, X):
    method predict (line 62) | def predict(self, X):
    method set_session (line 69) | def set_session(self, session):
    method copyFromKerasLayers (line 80) | def copyFromKerasLayers(self, layers):
    method get_params (line 99) | def get_params(self):

FILE: cnn_class2/tf_resnet_identity_block_starter.py
  class IdentityBlock (line 16) | class IdentityBlock:
    method __init__ (line 17) | def __init__(self):
    method predict (line 22) | def predict(self, X):

FILE: cnn_class2/use_pretrained_weights_resnet.py
  function get_confusion_matrix (line 138) | def get_confusion_matrix(data_path, N):

FILE: cnn_class2/use_pretrained_weights_vgg.py
  function get_confusion_matrix (line 136) | def get_confusion_matrix(data_path, N):

FILE: cnn_class2/util.py
  function plot_confusion_matrix (line 14) | def plot_confusion_matrix(cm, classes,
  function y2indicator (line 50) | def y2indicator(Y):

FILE: hmm_class/frost.py
  function remove_punctuation_2 (line 23) | def remove_punctuation_2(s):
  function remove_punctuation_3 (line 26) | def remove_punctuation_3(s):
  function add2dict (line 35) | def add2dict(d, k, v):
  function list2pdict (line 68) | def list2pdict(ts):
  function sample_word (line 86) | def sample_word(d):
  function generate (line 97) | def generate():

FILE: hmm_class/generate_c.py
  function simple_init (line 15) | def simple_init():
  function big_init (line 26) | def big_init():
  function get_signals (line 57) | def get_signals(N=20, T=100, init=big_init):

FILE: hmm_class/generate_ht.py
  function generate_sequence (line 22) | def generate_sequence(N):
  function main (line 33) | def main():

FILE: hmm_class/hmm_classifier.py
  class HMMClassifier (line 21) | class HMMClassifier:
    method __init__ (line 22) | def __init__(self):
    method fit (line 25) | def fit(self, X, Y, V):
    method score (line 40) | def score(self, X, Y):
  function get_tags (line 54) | def get_tags(s):
  function get_data (line 58) | def get_data():
  function main (line 88) | def main():

FILE: hmm_class/hmmc.py
  function random_normalized (line 18) | def random_normalized(d1, d2):
  class HMM (line 23) | class HMM:
    method __init__ (line 24) | def __init__(self, M, K):
    method fit (line 28) | def fit(self, X, max_iter=30, eps=1e0):
    method likelihood (line 176) | def likelihood(self, x):
    method get_state_sequence (line 194) | def get_state_sequence(self, x):
    method likelihood_multi (line 223) | def likelihood_multi(self, X):
    method log_likelihood_multi (line 226) | def log_likelihood_multi(self, X):
    method set (line 229) | def set(self, pi, A, R, mu, sigma):
  function real_signal (line 240) | def real_signal():
  function fake_signal (line 259) | def fake_signal(init=simple_init):

FILE: hmm_class/hmmc_concat.py
  function random_normalized (line 19) | def random_normalized(d1, d2):
  class HMM (line 24) | class HMM:
    method __init__ (line 25) | def __init__(self, M, K):
    method fit (line 29) | def fit(self, X, max_iter=30, eps=1e0):
    method likelihood (line 177) | def likelihood(self, x):
    method likelihood_multi (line 195) | def likelihood_multi(self, X):
    method log_likelihood_multi (line 198) | def log_likelihood_multi(self, X):
    method set (line 201) | def set(self, pi, A, R, mu, sigma):
  function real_signal (line 212) | def real_signal():
  function fake_signal (line 231) | def fake_signal(init=simple_init):

FILE: hmm_class/hmmc_scaled_concat.py
  function random_normalized (line 17) | def random_normalized(d1, d2):
  class HMM (line 21) | class HMM:
    method __init__ (line 22) | def __init__(self, M, K):
    method fit (line 26) | def fit(self, X, max_iter=25, eps=1e-1):
    method log_likelihood (line 179) | def log_likelihood(self, x):
    method log_likelihood_multi (line 200) | def log_likelihood_multi(self, X):
    method set (line 203) | def set(self, pi, A, R, mu, sigma):
  function real_signal (line 214) | def real_signal():
  function fake_signal (line 234) | def fake_signal(init=big_init):

FILE: hmm_class/hmmc_scaled_concat_diag.py
  function random_normalized (line 18) | def random_normalized(d1, d2):
  class HMM (line 22) | class HMM:
    method __init__ (line 23) | def __init__(self, M, K):
    method fit (line 27) | def fit(self, X, max_iter=25, eps=1e-1):
    method log_likelihood (line 177) | def log_likelihood(self, x):
    method get_state_sequence (line 198) | def get_state_sequence(self, x):
    method log_likelihood_multi (line 234) | def log_likelihood_multi(self, X):
    method set (line 237) | def set(self, pi, A, R, mu, sigma):
  function real_signal (line 248) | def real_signal():
  function fake_signal (line 268) | def fake_signal(init=big_init):

FILE: hmm_class/hmmc_tf.py
  class HMM (line 20) | class HMM:
    method __init__ (line 21) | def __init__(self, M, K, D):
    method set_session (line 26) | def set_session(self, session):
    method init_random (line 29) | def init_random(self, X):
    method build (line 46) | def build(self, preSoftmaxPi, preSoftmaxA, preSoftmaxR, mu, logSigma):
    method set (line 133) | def set(self, preSoftmaxPi, preSoftmaxA, preSoftmaxR, mu, logSigma):
    method fit (line 143) | def fit(self, X, max_iter=10):
    method get_cost (line 164) | def get_cost(self, x):
    method get_cost_multi (line 167) | def get_cost_multi(self, X):
  function real_signal (line 171) | def real_signal():
  function fake_signal (line 201) | def fake_signal():

FILE: hmm_class/hmmc_theano.py
  function random_normalized (line 20) | def random_normalized(d1, d2):
  class HMM (line 25) | class HMM:
    method __init__ (line 26) | def __init__(self, M, K):
    method fit (line 30) | def fit(self, X, learning_rate=1e-2, max_iter=10):
    method set (line 94) | def set(self, pi, A, R, mu, sigma):
    method log_likelihood_multi (line 163) | def log_likelihood_multi(self, X):
  function real_signal (line 167) | def real_signal():
  function fake_signal (line 188) | def fake_signal():

FILE: hmm_class/hmmc_theano2.py
  class HMM (line 27) | class HMM:
    method __init__ (line 28) | def __init__(self, M, K):
    method fit (line 32) | def fit(self, X, learning_rate=1e-2, max_iter=10):
    method set (line 84) | def set(self, preSoftmaxPi, preSoftmaxA, preSoftmaxR, mu, sigmaFactor):
    method log_likelihood_multi (line 165) | def log_likelihood_multi(self, X):
  function real_signal (line 169) | def real_signal():
  function fake_signal (line 190) | def fake_signal():

FILE: hmm_class/hmmd.py
  function random_normalized (line 16) | def random_normalized(d1, d2):
  class HMM (line 21) | class HMM:
    method __init__ (line 22) | def __init__(self, M):
    method fit (line 25) | def fit(self, X, max_iter=30):
    method likelihood (line 146) | def likelihood(self, x):
    method likelihood_multi (line 156) | def likelihood_multi(self, X):
    method log_likelihood_multi (line 159) | def log_likelihood_multi(self, X):
    method get_state_sequence (line 162) | def get_state_sequence(self, x):
  function fit_coin (line 181) | def fit_coin():

FILE: hmm_class/hmmd_scaled.py
  function random_normalized (line 15) | def random_normalized(d1, d2):
  class HMM (line 20) | class HMM:
    method __init__ (line 21) | def __init__(self, M):
    method fit (line 24) | def fit(self, X, max_iter=30):
    method log_likelihood (line 117) | def log_likelihood(self, x):
    method log_likelihood_multi (line 132) | def log_likelihood_multi(self, X):
    method get_state_sequence (line 135) | def get_state_sequence(self, x):
  function fit_coin (line 155) | def fit_coin():

FILE: hmm_class/hmmd_tf.py
  class HMM (line 16) | class HMM:
    method __init__ (line 17) | def __init__(self, M):
    method set_session (line 20) | def set_session(self, session):
    method fit (line 23) | def fit(self, X, max_iter=10, print_period=1):
    method get_cost (line 44) | def get_cost(self, x):
    method log_likelihood (line 50) | def log_likelihood(self, x):
    method get_cost_multi (line 53) | def get_cost_multi(self, X):
    method build (line 56) | def build(self, preSoftmaxPi, preSoftmaxA, preSoftmaxB):
    method init_random (line 91) | def init_random(self, V):
    method set (line 98) | def set(self, preSoftmaxPi, preSoftmaxA, preSoftmaxB):
  function fit_coin (line 105) | def fit_coin():

FILE: hmm_class/hmmd_theano.py
  function random_normalized (line 17) | def random_normalized(d1, d2):
  class HMM (line 22) | class HMM:
    method __init__ (line 23) | def __init__(self, M):
    method fit (line 26) | def fit(self, X, learning_rate=0.001, max_iter=10, V=None, p_cost=1.0,...
    method get_cost (line 90) | def get_cost(self, x):
    method log_likelihood (line 95) | def log_likelihood(self, x):
    method get_cost_multi (line 98) | def get_cost_multi(self, X, p_cost=1.0):
    method set (line 102) | def set(self, pi, A, B):
  function fit_coin (line 131) | def fit_coin():

FILE: hmm_class/hmmd_theano2.py
  class HMM (line 24) | class HMM:
    method __init__ (line 25) | def __init__(self, M):
    method fit (line 28) | def fit(self, X, learning_rate=0.001, max_iter=10, V=None, print_perio...
    method get_cost (line 80) | def get_cost(self, x):
    method log_likelihood (line 86) | def log_likelihood(self, x):
    method get_cost_multi (line 89) | def get_cost_multi(self, X):
    method set (line 92) | def set(self, preSoftmaxPi, preSoftmaxA, preSoftmaxB):
  function fit_coin (line 126) | def fit_coin():

FILE: hmm_class/scan1.py
  function square (line 18) | def square(x):

FILE: hmm_class/scan2.py
  function recurrence (line 18) | def recurrence(n, fn_1, fn_2):

FILE: hmm_class/scan3.py
  function recurrence (line 25) | def recurrence(x, last, decay):

FILE: hmm_class/tf_scan1.py
  function square (line 19) | def square(last, current):

FILE: hmm_class/tf_scan2.py
  function recurrence (line 21) | def recurrence(last_output, current_input):

FILE: hmm_class/tf_scan3.py
  function recurrence (line 27) | def recurrence(last, x):

FILE: keras_examples/util.py
  function getKaggleMNIST (line 13) | def getKaggleMNIST():
  function getKaggleFashionMNIST (line 18) | def getKaggleFashionMNIST():
  function getMNISTFormat (line 22) | def getMNISTFormat(path):
  function getKaggleMNIST3D (line 37) | def getKaggleMNIST3D():
  function getKaggleFashionMNIST3D (line 43) | def getKaggleFashionMNIST3D():
  function getCIFAR10 (line 49) | def getCIFAR10():

FILE: kerascv/pascal2coco.py
  function get_label2id (line 12) | def get_label2id(labels_path: str) -> Dict[str, int]:
  function get_annpaths (line 20) | def get_annpaths(ann_dir_path: str = None,
  function get_image_info (line 38) | def get_image_info(annotation_root, extract_num_from_imgid=True):
  function get_coco_annotation_from_obj (line 62) | def get_coco_annotation_from_obj(obj, label2id):
  function convert_xmls_to_cocojson (line 85) | def convert_xmls_to_cocojson(annotation_paths: List[str],
  function main (line 122) | def main():

FILE: linear_regression_class/gd.py
  function J (line 7) | def J(x1, x2):
  function g1 (line 10) | def g1(x1):
  function g2 (line 13) | def g2(x2):

FILE: linear_regression_class/overfitting.py
  function make_poly (line 15) | def make_poly(X, deg):
  function fit (line 23) | def fit(X, Y):
  function fit_and_display (line 27) | def fit_and_display(X, Y, sample, deg):
  function get_mse (line 50) | def get_mse(Y, Yhat):
  function plot_train_vs_test_curves (line 55) | def plot_train_vs_test_curves(X, Y, sample=20, max_deg=20):

FILE: linear_regression_class/systolic.py
  function get_r2 (line 41) | def get_r2(X, Y):

FILE: logistic_regression_class/bad_xor.py
  function sigmoid (line 40) | def sigmoid(z):
  function cross_entropy (line 47) | def cross_entropy(T, Y):

FILE: logistic_regression_class/l1_regularization.py
  function sigmoid (line 15) | def sigmoid(z):

FILE: logistic_regression_class/logistic1.py
  function sigmoid (line 30) | def sigmoid(z):

FILE: logistic_regression_class/logistic2.py
  function sigmoid (line 43) | def sigmoid(z):
  function cross_entropy (line 49) | def cross_entropy(T, Y):

FILE: logistic_regression_class/logistic3.py
  function sigmoid (line 44) | def sigmoid(z):
  function cross_entropy (line 51) | def cross_entropy(T, Y):

FILE: logistic_regression_class/logistic4.py
  function sigmoid (line 43) | def sigmoid(z):
  function cross_entropy (line 50) | def cross_entropy(T, Y):

FILE: logistic_regression_class/logistic_donut.py
  function sigmoid (line 55) | def sigmoid(z):
  function cross_entropy (line 62) | def cross_entropy(T, Y):

FILE: logistic_regression_class/logistic_visualize.py
  function sigmoid (line 37) | def sigmoid(z):

FILE: logistic_regression_class/logistic_xor.py
  function sigmoid (line 42) | def sigmoid(z):
  function cross_entropy (line 49) | def cross_entropy(T, Y):

FILE: nlp_class/article_spinner.py
  function random_sample (line 58) | def random_sample(d):
  function test_spinner (line 68) | def test_spinner():

FILE: nlp_class/lsa.py
  function my_tokenizer (line 36) | def my_tokenizer(s):
  function tokens_to_vector (line 78) | def tokens_to_vector(tokens):
  function main (line 93) | def main():

FILE: nlp_class/sentiment.py
  function my_tokenizer (line 56) | def my_tokenizer(s):
  function tokens_to_vector (line 94) | def tokens_to_vector(tokens, label):

FILE: nlp_class/spam2.py
  function visualize (line 61) | def visualize(label):

FILE: nlp_class2/bow_classifier.py
  class GloveVectorizer (line 28) | class GloveVectorizer:
    method __init__ (line 29) | def __init__(self):
    method fit (line 53) | def fit(self, data):
    method transform (line 56) | def transform(self, data):
    method fit_transform (line 76) | def fit_transform(self, data):
  class Word2VecVectorizer (line 83) | class Word2VecVectorizer:
    method __init__ (line 84) | def __init__(self):
    method fit (line 92) | def fit(self, data):
    method transform (line 95) | def transform(self, data):
    method fit_transform (line 125) | def fit_transform(self, data):

FILE: nlp_class2/glove.py
  class Glove (line 31) | class Glove:
    method __init__ (line 32) | def __init__(self, D, V, context_sz):
    method fit (line 37) | def fit(self, sentences, cc_matrix=None, learning_rate=1e-4, reg=0.1, ...
    method save (line 233) | def save(self, fn):
  function main (line 239) | def main(we_file, w2i_file, use_brown=True, n_files=100):

FILE: nlp_class2/glove_svd.py
  class Glove (line 27) | class Glove:
    method __init__ (line 28) | def __init__(self, D, V, context_sz):
    method fit (line 33) | def fit(self, sentences, cc_matrix=None):
    method save (line 118) | def save(self, fn):
  function main (line 124) | def main(we_file, w2i_file, use_brown=True, n_files=100):

FILE: nlp_class2/glove_tf.py
  class Glove (line 30) | class Glove:
    method __init__ (line 31) | def __init__(self, D, V, context_sz):
    method fit (line 36) | def fit(self, sentences, cc_matrix=None, learning_rate=1e-4, reg=0.1, ...
    method save (line 156) | def save(self, fn):
  function main (line 162) | def main(we_file, w2i_file, use_brown=True, n_files=50):

FILE: nlp_class2/glove_theano.py
  function momentum_updates (line 27) | def momentum_updates(cost, params, lr=1e-4, mu=0.9):
  class Glove (line 41) | class Glove:
    method __init__ (line 42) | def __init__(self, D, V, context_sz):
    method fit (line 47) | def fit(self, sentences, cc_matrix=None, learning_rate=1e-4, reg=0.1, ...
    method save (line 175) | def save(self, fn):
  function main (line 181) | def main(we_file, w2i_file, use_brown=True, n_files=50):

FILE: nlp_class2/logistic.py
  function softmax (line 56) | def softmax(a):
  function smoothed_loss (line 113) | def smoothed_loss(x, decay=0.99):

FILE: nlp_class2/markov.py
  function get_bigram_probs (line 19) | def get_bigram_probs(sentences, V, start_idx, end_idx, smoothing=1):
  function get_score (line 75) | def get_score(sentence):
  function get_words (line 93) | def get_words(sentence):

FILE: nlp_class2/ner_baseline.py
  function get_data (line 18) | def get_data(split_sequences=False):
  function main (line 108) | def main():

FILE: nlp_class2/ner_rnn.py
  function main (line 13) | def main():

FILE: nlp_class2/ner_tf.py
  function get_data (line 27) | def get_data(split_sequences=False):
  function flatten (line 73) | def flatten(l):

FILE: nlp_class2/neural_network.py
  function softmax (line 58) | def softmax(a):
  function smoothed_loss (line 117) | def smoothed_loss(x, decay=0.99):

FILE: nlp_class2/neural_network2.py
  function softmax (line 58) | def softmax(a):
  function smoothed_loss (line 140) | def smoothed_loss(x, decay=0.99):

FILE: nlp_class2/pmi.py
  function remove_punctuation_2 (line 27) | def remove_punctuation_2(s):
  function remove_punctuation_3 (line 30) | def remove_punctuation_3(s):
  function analogy (line 273) | def analogy(pos1, neg1, pos2, neg2):

FILE: nlp_class2/pos_baseline.py
  class LogisticRegression (line 24) | class LogisticRegression:
    method __init__ (line 25) | def __init__(self):
    method fit (line 28) | def fit(self, X, Y, V=None, K=None, D=50, lr=1e-1, mu=0.99, batch_sz=1...
    method score (line 89) | def score(self, X, Y):
    method f1_score (line 93) | def f1_score(self, X, Y):
  function get_data (line 98) | def get_data(split_sequences=False):
  function main (line 171) | def main():

FILE: nlp_class2/pos_hmm.py
  function accuracy (line 24) | def accuracy(T, Y):
  function total_f1_score (line 34) | def total_f1_score(T, Y):
  function main (line 45) | def main(smoothing=1e-1):

FILE: nlp_class2/pos_ner_keras.py
  function get_data_pos (line 33) | def get_data_pos(split_sequences=False):
  function get_data_ner (line 94) | def get_data_ner(split_sequences=False):

FILE: nlp_class2/pos_rnn.py
  class RNN (line 25) | class RNN:
    method __init__ (line 26) | def __init__(self, D, hidden_layer_sizes, V, K):
    method fit (line 32) | def fit(self, X, Y, learning_rate=1e-4, mu=0.99, epochs=30, show_fig=T...
    method score (line 134) | def score(self, X, Y):
    method f1_score (line 142) | def f1_score(self, X, Y):
  function flatten (line 152) | def flatten(l):
  function main (line 156) | def main():

FILE: nlp_class2/pos_tf.py
  function get_data (line 27) | def get_data(split_sequences=False):
  function flatten (line 101) | def flatten(l):

FILE: nlp_class2/pretrained_glove.py
  function dist1 (line 20) | def dist1(a, b):
  function dist2 (line 22) | def dist2(a, b):
  function find_analogies (line 54) | def find_analogies(w1, w2, w3):
  function nearest_neighbors (line 76) | def nearest_neighbors(w, n=5):

FILE: nlp_class2/pretrained_w2v.py
  function find_analogies (line 38) | def find_analogies(w1, w2, w3):
  function nearest_neighbors (line 42) | def nearest_neighbors(w):

FILE: nlp_class2/recursive_tensorflow.py
  function get_labels (line 24) | def get_labels(tree):
  class TNN (line 32) | class TNN:
    method __init__ (line 33) | def __init__(self, V, D, K, activation):
    method fit (line 60) | def fit(self, trees, lr=1e-1, mu=0.9, reg=0.1, epochs=5):
    method get_cost (line 140) | def get_cost(self, logits, labels, reg):
    method get_output_recursive (line 153) | def get_output_recursive(self, tree, list_of_logits, is_root=True):
    method get_output (line 171) | def get_output(self, tree):
    method score (line 180) | def score(self, trees):
  function main (line 218) | def main():

FILE: nlp_class2/recursive_theano.py
  function adagrad (line 21) | def adagrad(cost, params, lr, eps=1e-10):
  class RecursiveNN (line 34) | class RecursiveNN:
    method __init__ (line 35) | def __init__(self, V, D, K):
    method fit (line 40) | def fit(self, trees, learning_rate=3*1e-3, mu=0.99, reg=1e-4, epochs=1...
    method score (line 181) | def score(self, trees, idx2word=None):
  function add_idx_to_tree (line 194) | def add_idx_to_tree(tree, current_idx):
  function tree2list (line 205) | def tree2list(tree, parent_idx, is_binary=False, is_left=False, is_right...
  function print_sentence (line 243) | def print_sentence(words, idx2word):
  function main (line 251) | def main(is_binary=True):

FILE: nlp_class2/rntn_tensorflow.py
  function tensor_mul (line 21) | def tensor_mul(d, x1, A, x2):
  function get_labels (line 33) | def get_labels(tree):
  class RNTN (line 41) | class RNTN:
    method __init__ (line 42) | def __init__(self, V, D, K, activation):
    method fit (line 77) | def fit(self, trees, lr=1e-2, mu=0.9, reg=1e-1, epochs=5):
    method get_cost (line 153) | def get_cost(self, logits, labels, reg):
    method get_output_recursive (line 161) | def get_output_recursive(self, tree, list_of_logits, is_root=True):
    method get_output (line 186) | def get_output(self, tree):
    method score (line 195) | def score(self, trees):
  function main (line 237) | def main():

FILE: nlp_class2/rntn_tensorflow_rnn.py
  class RecursiveNN (line 25) | class RecursiveNN:
    method __init__ (line 26) | def __init__(self, V, D, K, activation=tf.tanh):
    method fit (line 32) | def fit(self, trees, test_trees, reg=1e-3, epochs=8, train_inner_nodes...
    method predict (line 225) | def predict(self, words, left, right, lab):
    method score (line 237) | def score(self, trees):
    method f1_score (line 245) | def f1_score(self, trees):
  function add_idx_to_tree (line 255) | def add_idx_to_tree(tree, current_idx):
  function tree2list (line 266) | def tree2list(tree, parent_idx, is_binary=False):
  function main (line 300) | def main(is_binary=True):

FILE: nlp_class2/rntn_theano.py
  function adagrad (line 71) | def adagrad(cost, params, lr, eps=1e-10):
  class RecursiveNN (line 84) | class RecursiveNN:
    method __init__ (line 85) | def __init__(self, V, D, K, activation=T.tanh):
    method fit (line 91) | def fit(self, trees, test_trees, reg=1e-3, epochs=8, train_inner_nodes...
    method score (line 232) | def score(self, trees):
    method f1_score (line 240) | def f1_score(self, trees):
  function add_idx_to_tree (line 250) | def add_idx_to_tree(tree, current_idx):
  function tree2list (line 261) | def tree2list(tree, parent_idx, is_binary=False):
  function main (line 296) | def main(is_binary=True):

FILE: nlp_class2/tfidf_tsne.py
  function main (line 29) | def main():

FILE: nlp_class2/util.py
  function init_weight (line 17) | def init_weight(Mi, Mo):
  function find_analogies (line 47) | def find_analogies(w1, w2, w3, We, word2idx, idx2word):
  class Tree (line 73) | class Tree:
    method __init__ (line 74) | def __init__(self, word, label):
  function display_tree (line 81) | def display_tree(t, lvl=0):
  function str2tree (line 96) | def str2tree(s, word2idx):
  function get_ptb_data (line 161) | def get_ptb_data():

FILE: nlp_class2/visualize_countries.py
  function main (line 15) | def main(we_file='glove_model_50.npz', w2i_file='glove_word2idx_50.json'):

FILE: nlp_class2/word2vec.py
  function remove_punctuation_2 (line 33) | def remove_punctuation_2(s):
  function remove_punctuation_3 (line 36) | def remove_punctuation_3(s):
  function get_wiki (line 47) | def get_wiki():
  function train_model (line 84) | def train_model(savedir):
  function get_negative_sampling_distribution (line 207) | def get_negative_sampling_distribution(sentences, vocab_size):
  function get_context (line 229) | def get_context(pos, sentence, window_size):
  function sgd (line 246) | def sgd(input_, targets, label, learning_rate, W, V):
  function load_model (line 266) | def load_model(savedir):
  function analogy (line 276) | def analogy(pos1, neg1, pos2, neg2, word2idx, idx2word, W):
  function test_model (line 314) | def test_model(word2idx, W, V):

FILE: nlp_class2/word2vec_tf.py
  function remove_punctuation_2 (line 34) | def remove_punctuation_2(s):
  function remove_punctuation_3 (line 37) | def remove_punctuation_3(s):
  function download_text8 (line 46) | def download_text8(dst):
  function get_text8 (line 50) | def get_text8():
  function get_wiki (line 69) | def get_wiki():
  function train_model (line 106) | def train_model(savedir):
  function get_negative_sampling_distribution (line 322) | def get_negative_sampling_distribution(sentences):
  function get_context (line 350) | def get_context(pos, sentence, window_size):
  function load_model (line 368) | def load_model(savedir):
  function analogy (line 378) | def analogy(pos1, neg1, pos2, neg2, word2idx, idx2word, W):
  function test_model (line 414) | def test_model(word2idx, W, V):

FILE: nlp_class2/word2vec_theano.py
  function remove_punctuation_2 (line 33) | def remove_punctuation_2(s):
  function remove_punctuation_3 (line 36) | def remove_punctuation_3(s):
  function get_wiki (line 46) | def get_wiki():
  function train_model (line 83) | def train_model(savedir):
  function get_negative_sampling_distribution (line 277) | def get_negative_sampling_distribution(sentences, vocab_size):
  function get_context (line 299) | def get_context(pos, sentence, window_size):
  function load_model (line 317) | def load_model(savedir):
  function analogy (line 327) | def analogy(pos1, neg1, pos2, neg2, word2idx, idx2word, W):
  function test_model (line 365) | def test_model(word2idx, W, V):

FILE: nlp_class3/attention.py
  function softmax_over_time (line 31) | def softmax_over_time(x):
  function one_step_attention (line 238) | def one_step_attention(h, st_1):
  function stack_and_transpose (line 311) | def stack_and_transpose(x):
  function custom_loss (line 333) | def custom_loss(y_true, y_pred):
  function acc (line 340) | def acc(y_true, y_pred):
  function decode_sequence (line 440) | def decode_sequence(input_seq):

FILE: nlp_class3/bilstm_mnist.py
  function get_mnist (line 25) | def get_mnist(limit=None):

FILE: nlp_class3/memory_network.py
  function tokenize (line 40) | def tokenize(sent):
  function get_stories (line 51) | def get_stories(f):
  function should_flatten (line 104) | def should_flatten(el):
  function flatten (line 107) | def flatten(l):
  function vectorize_stories (line 123) | def vectorize_stories(data, word2idx, story_maxlen, query_maxlen):
  function stack_inputs (line 142) | def stack_inputs(inputs, story_maxsents, story_maxlen):
  function get_data (line 159) | def get_data(challenge_type):
  function embed_and_sum (line 329) | def embed_and_sum(x, axis=2):
  function hop (line 350) | def hop(query, story):

FILE: nlp_class3/poetry.py
  function sample_line (line 186) | def sample_line():

FILE: nlp_class3/simple_rnn_test.py
  function lstm1 (line 29) | def lstm1():
  function lstm2 (line 41) | def lstm2():
  function gru1 (line 54) | def gru1():
  function gru2 (line 65) | def gru2():

FILE: nlp_class3/wseq2seq.py
  function custom_loss (line 226) | def custom_loss(y_true, y_pred):
  function acc (line 233) | def acc(y_true, y_pred):
  function decode_sequence (line 326) | def decode_sequence(input_seq):

FILE: numpy_class/dot_for.py
  function slow_dot_product (line 10) | def slow_dot_product(a, b):

FILE: numpy_class/exercises/ex2.py
  function sampleY (line 13) | def sampleY(n=1000):

FILE: numpy_class/exercises/ex4.py
  function rotate1 (line 27) | def rotate1(im):
  function rotate2 (line 30) | def rotate2(im):

FILE: numpy_class/exercises/ex5.py
  function is_symmetric1 (line 15) | def is_symmetric1(A):
  function is_symmetric2 (line 19) | def is_symmetric2(A):
  function check (line 32) | def check(A, b):

FILE: numpy_class/exercises/ex7.py
  function get_donut (line 13) | def get_donut():

FILE: numpy_class/exercises/ex8.py
  function get_spiral (line 13) | def get_spiral():

FILE: numpy_class/python3/dot_for.py
  function slow_dot_product (line 17) | def slow_dot_product(a, b):

FILE: pytorch/ann_regression.py
  function full_gd (line 39) | def full_gd(model, criterion, optimizer, X_train, y_train, epochs=1000):

FILE: pytorch/rl_trader.py
  function get_data (line 19) | def get_data():
  class ReplayBuffer (line 31) | class ReplayBuffer:
    method __init__ (line 32) | def __init__(self, obs_dim, act_dim, size):
    method store (line 40) | def store(self, obs, act, rew, next_obs, done):
    method sample_batch (line 49) | def sample_batch(self, batch_size=32):
  function get_scaler (line 61) | def get_scaler(env):
  function maybe_make_dir (line 80) | def maybe_make_dir(directory):
  class MLP (line 87) | class MLP(nn.Module):
    method __init__ (line 88) | def __init__(self, n_inputs, n_action, n_hidden_layers=1, hidden_dim=32):
    method forward (line 103) | def forward(self, X):
    method save_weights (line 106) | def save_weights(self, path):
    method load_weights (line 109) | def load_weights(self, path):
  function predict (line 114) | def predict(model, np_states):
  function train_one_step (line 123) | def train_one_step(model, criterion, optimizer, inputs, targets):
  class MultiStockEnv (line 141) | class MultiStockEnv:
    method __init__ (line 158) | def __init__(self, data, initial_investment=20000):
    method reset (line 191) | def reset(self):
    method step (line 199) | def step(self, action):
    method _get_obs (line 228) | def _get_obs(self):
    method _get_val (line 237) | def _get_val(self):
    method _trade (line 241) | def _trade(self, action):
  class DQNAgent (line 284) | class DQNAgent(object):
    method __init__ (line 285) | def __init__(self, state_size, action_size):
    method update_replay_memory (line 300) | def update_replay_memory(self, state, action, reward, next_state, done):
    method act (line 304) | def act(self, state):
    method replay (line 311) | def replay(self, batch_size=32):
    method load (line 345) | def load(self, name):
    method save (line 349) | def save(self, name):
  function play_one_episode (line 353) | def play_one_episode(agent, env, is_train):

FILE: recommenders/autorec.py
  function custom_loss (line 57) | def custom_loss(y_true, y_pred):
  function generator (line 66) | def generator(A, M):
  function test_generator (line 80) | def test_generator(A, M, A_test, M_test):

FILE: recommenders/itembased.py
  function predict (line 113) | def predict(i, u):
  function mse (line 162) | def mse(p, t):

FILE: recommenders/mf.py
  function get_loss (line 55) | def get_loss(d):

FILE: recommenders/mf2.py
  function get_loss (line 79) | def get_loss(m2u):

FILE: recommenders/preprocess2dict.py
  function update_user2movie_and_movie2user (line 35) | def update_user2movie_and_movie2user(row):
  function update_usermovie2rating_test (line 60) | def update_usermovie2rating_test(row):

FILE: recommenders/preprocess2sparse.py
  function update_train (line 30) | def update_train(row):
  function update_test (line 50) | def update_test(row):

FILE: recommenders/rbm_tf_k.py
  function one_hot_encode (line 29) | def one_hot_encode(X, K):
  function one_hot_mask (line 40) | def one_hot_mask(X, K):
  function convert_probs_to_ratings (line 53) | def convert_probs_to_ratings(probs):
  function dot1 (line 67) | def dot1(V, W):
  function dot2 (line 73) | def dot2(H, W):
  class RBM (line 80) | class RBM(object):
    method __init__ (line 81) | def __init__(self, D, M, K):
    method build (line 88) | def build(self, D, M, K):
    method fit (line 140) | def fit(self, X, mask, X_test, mask_test, epochs=10, batch_sz=256, sho...
    method free_energy (line 207) | def free_energy(self, V):
    method forward_hidden (line 216) | def forward_hidden(self, X):
    method forward_logits (line 219) | def forward_logits(self, X):
    method forward_output (line 223) | def forward_output(self, X):
    method transform (line 226) | def transform(self, X):
    method get_visible (line 232) | def get_visible(self, X):
  function main (line 236) | def main():

FILE: recommenders/rbm_tf_k_faster.py
  function dot1 (line 21) | def dot1(V, W):
  function dot2 (line 27) | def dot2(H, W):
  class RBM (line 34) | class RBM(object):
    method __init__ (line 35) | def __init__(self, D, M, K):
    method build (line 42) | def build(self, D, M, K):
    method fit (line 117) | def fit(self, X, X_test, epochs=10, batch_sz=256, show_fig=True):
    method free_energy (line 173) | def free_energy(self, V):
    method forward_hidden (line 182) | def forward_hidden(self, X):
    method forward_logits (line 185) | def forward_logits(self, X):
    method forward_output (line 189) | def forward_output(self, X):
    method transform (line 192) | def transform(self, X):
    method get_visible (line 198) | def get_visible(self, X):
    method get_sse (line 201) | def get_sse(self, X, Xt):
  function main (line 211) | def main():

FILE: recommenders/tfidf.py
  function genres_and_keywords_to_string (line 15) | def genres_and_keywords_to_string(row):
  function recommend (line 42) | def recommend(title):

FILE: recommenders/userbased.py
  function predict (line 111) | def predict(i, m):
  function mse (line 159) | def mse(p, t):

FILE: rl/approx_control.py
  function epsilon_greedy (line 23) | def epsilon_greedy(model, s, eps=0.1):
  function one_hot (line 34) | def one_hot(k):
  function merge_state_action (line 38) | def merge_state_action(s, a):
  function gather_samples (line 43) | def gather_samples(grid, n_episodes=1000):
  class Model (line 57) | class Model:
    method __init__ (line 58) | def __init__(self, grid):
    method predict (line 69) | def predict(self, s, a):
    method predict_all_actions (line 74) | def predict_all_actions(self, s):
    method grad (line 77) | def grad(self, s, a):

FILE: rl/approx_prediction.py
  function epsilon_greedy (line 20) | def epsilon_greedy(greedy, s, eps=0.1):
  function gather_samples (line 30) | def gather_samples(grid, n_episodes=10000):
  class Model (line 43) | class Model:
    method __init__ (line 44) | def __init__(self, grid):
    method predict (line 55) | def predict(self, s):
    method grad (line 59) | def grad(self, s):

FILE: rl/bayesian_bandit.py
  class Bandit (line 20) | class Bandit:
    method __init__ (line 21) | def __init__(self, p):
    method pull (line 27) | def pull(self):
    method sample (line 30) | def sample(self):
    method update (line 33) | def update(self, x):
  function plot (line 39) | def plot(bandits, trial):
  function experiment (line 49) | def experiment():

FILE: rl/bayesian_normal.py
  class Bandit (line 18) | class Bandit:
    method __init__ (line 19) | def __init__(self, true_mean):
    method pull (line 27) | def pull(self):
    method sample (line 30) | def sample(self):
    method update (line 33) | def update(self, x):
  function plot (line 39) | def plot(bandits, trial):
  function run_experiment (line 49) | def run_experiment():

FILE: rl/bayesian_starter.py
  class Bandit (line 20) | class Bandit:
    method __init__ (line 21) | def __init__(self, p):
    method pull (line 27) | def pull(self):
    method sample (line 30) | def sample(self):
    method update (line 33) | def update(self, x):
  function plot (line 39) | def plot(bandits, trial):
  function experiment (line 49) | def experiment():

FILE: rl/cartpole.py
  function epsilon_greedy (line 18) | def epsilon_greedy(model, s, eps=0.1):
  function gather_samples (line 29) | def gather_samples(env, n_episodes=10000):
  class Model (line 44) | class Model:
    method __init__ (line 45) | def __init__(self, env):
    method predict (line 56) | def predict(self, s, a):
    method predict_all_actions (line 61) | def predict_all_actions(self, s):
    method grad (line 64) | def grad(self, s, a):
  function test_agent (line 70) | def test_agent(model, env, n_episodes=20):
  function watch_agent (line 85) | def watch_agent(model, env, eps):

FILE: rl/cartpole_gym0.19.py
  function epsilon_greedy (line 18) | def epsilon_greedy(model, s, eps=0.1):
  function gather_samples (line 29) | def gather_samples(env, n_episodes=10000):
  class Model (line 43) | class Model:
    method __init__ (line 44) | def __init__(self, env):
    method predict (line 55) | def predict(self, s, a):
    method predict_all_actions (line 60) | def predict_all_actions(self, s):
    method grad (line 63) | def grad(self, s, a):
  function test_agent (line 69) | def test_agent(model, env, n_episodes=20):
  function watch_agent (line 83) | def watch_agent(model, env, eps):

FILE: rl/comparing_epsilons.py
  class Bandit (line 12) | class Bandit:
    method __init__ (line 13) | def __init__(self, m):
    method pull (line 18) | def pull(self):
    method update (line 21) | def update(self, x):
  function run_experiment (line 26) | def run_experiment(m1, m2, m3, eps, N):

FILE: rl/comparing_explore_exploit_methods.py
  class BayesianBandit (line 14) | class BayesianBandit:
    method __init__ (line 15) | def __init__(self, true_mean):
    method pull (line 23) | def pull(self):
    method sample (line 26) | def sample(self):
    method update (line 29) | def update(self, x):
  function run_experiment_decaying_epsilon (line 35) | def run_experiment_decaying_epsilon(m1, m2, m3, N):
  function run_experiment (line 68) | def run_experiment(m1, m2, m3, N):

FILE: rl/epsilon_greedy.py
  class Bandit (line 19) | class Bandit:
    method __init__ (line 20) | def __init__(self, p):
    method pull (line 26) | def pull(self):
    method update (line 30) | def update(self, x):
  function experiment (line 35) | def experiment():

FILE: rl/epsilon_greedy_starter.py
  class Bandit (line 19) | class Bandit:
    method __init__ (line 20) | def __init__(self, p):
    method pull (line 26) | def pull(self):
    method update (line 30) | def update(self, x):
  function experiment (line 35) | def experiment():

FILE: rl/grid_world.py
  class Grid (line 14) | class Grid: # Environment
    method __init__ (line 15) | def __init__(self, rows, cols, start):
    method set (line 21) | def set(self, rewards, actions):
    method set_state (line 27) | def set_state(self, s):
    method current_state (line 31) | def current_state(self):
    method is_terminal (line 34) | def is_terminal(self, s):
    method reset (line 37) | def reset(self):
    method get_next_state (line 43) | def get_next_state(self, s, a):
    method move (line 59) | def move(self, action):
    method undo_move (line 73) | def undo_move(self, action):
    method game_over (line 87) | def game_over(self):
    method all_states (line 92) | def all_states(self):
  function standard_grid (line 99) | def standard_grid():
  function negative_grid (line 126) | def negative_grid(step_cost=-0.1):
  class WindyGrid (line 147) | class WindyGrid:
    method __init__ (line 148) | def __init__(self, rows, cols, start):
    method set (line 154) | def set(self, rewards, actions, probs):
    method set_state (line 161) | def set_state(self, s):
    method current_state (line 165) | def current_state(self):
    method is_terminal (line 168) | def is_terminal(self, s):
    method move (line 171) | def move(self, action):
    method game_over (line 187) | def game_over(self):
    method all_states (line 192) | def all_states(self):
  function windy_grid (line 199) | def windy_grid():
  function windy_grid_no_wind (line 258) | def windy_grid_no_wind():
  function windy_grid_penalized (line 265) | def windy_grid_penalized(step_cost=-0.1):
  function grid_5x5 (line 337) | def grid_5x5(step_cost=-0.1):

FILE: rl/iterative_policy_evaluation_deterministic.py
  function print_values (line 15) | def print_values(V, g):
  function print_policy (line 27) | def print_policy(P, g):

FILE: rl/iterative_policy_evaluation_probabilistic.py
  function print_values (line 15) | def print_values(V, g):
  function print_policy (line 27) | def print_policy(P, g):

FILE: rl/linear_rl_trader.py
  function get_data (line 16) | def get_data():
  function get_scaler (line 29) | def get_scaler(env):
  function maybe_make_dir (line 48) | def maybe_make_dir(directory):
  class LinearModel (line 54) | class LinearModel:
    method __init__ (line 56) | def __init__(self, input_dim, n_action):
    method predict (line 66) | def predict(self, X):
    method sgd (line 71) | def sgd(self, X, Y, learning_rate=0.01, momentum=0.9):
    method load_weights (line 99) | def load_weights(self, filepath):
    method save_weights (line 104) | def save_weights(self, filepath):
  class MultiStockEnv (line 110) | class MultiStockEnv:
    method __init__ (line 127) | def __init__(self, data, initial_investment=20000):
    method reset (line 160) | def reset(self):
    method step (line 168) | def step(self, action):
    method _get_obs (line 197) | def _get_obs(self):
    method _get_val (line 206) | def _get_val(self):
    method _trade (line 210) | def _trade(self, action):
  class DQNAgent (line 253) | class DQNAgent(object):
    method __init__ (line 254) | def __init__(self, state_size, action_size):
    method act (line 263) | def act(self, state):
    method train (line 270) | def train(self, state, action, reward, next_state, done):
    method load (line 286) | def load(self, name):
    method save (line 290) | def save(self, name):
  function play_one_episode (line 294) | def play_one_episode(agent, env, is_train):

FILE: rl/monte_carlo.py
  function play_game (line 17) | def play_game(grid, policy, max_steps=20):

FILE: rl/monte_carlo_es.py
  function play_game (line 21) | def play_game(grid, policy, max_steps=20):
  function max_dict (line 58) | def max_dict(d):

FILE: rl/monte_carlo_no_es.py
  function epsilon_greedy (line 20) | def epsilon_greedy(policy, s, eps=0.1):
  function play_game (line 28) | def play_game(grid, policy, max_steps=20):
  function max_dict (line 61) | def max_dict(d):

FILE: rl/optimistic.py
  class Bandit (line 19) | class Bandit:
    method __init__ (line 20) | def __init__(self, p):
    method pull (line 26) | def pull(self):
    method update (line 30) | def update(self, x):
  function experiment (line 35) | def experiment():

FILE: rl/optimistic_initial_values.py
  class Bandit (line 13) | class Bandit:
    method __init__ (line 14) | def __init__(self, m, upper_limit):
    method pull (line 19) | def pull(self):
    method update (line 22) | def update(self, x):
  function run_experiment (line 27) | def run_experiment(m1, m2, m3, N, upper_limit=10):

FILE: rl/optimistic_starter.py
  class Bandit (line 19) | class Bandit:
    method __init__ (line 20) | def __init__(self, p):
    method pull (line 26) | def pull(self):
    method update (line 30) | def update(self, x):
  function experiment (line 35) | def experiment():

FILE: rl/policy_iteration_deterministic.py
  function get_transition_probs_and_rewards (line 18) | def get_transition_probs_and_rewards(grid):
  function evaluate_deterministic_policy (line 44) | def evaluate_deterministic_policy(grid, policy, initV=None):

FILE: rl/policy_iteration_probabilistic.py
  function get_transition_probs_and_rewards (line 18) | def get_transition_probs_and_rewards(grid):
  function evaluate_deterministic_policy (line 39) | def evaluate_deterministic_policy(grid, policy, initV=None):

FILE: rl/q_learning.py
  function epsilon_greedy (line 20) | def epsilon_greedy(Q, s, eps=0.1):

FILE: rl/sarsa.py
  function epsilon_greedy (line 20) | def epsilon_greedy(Q, s, eps=0.1):

FILE: rl/td0_prediction.py
  function epsilon_greedy (line 20) | def epsilon_greedy(policy, s, eps=0.1):

FILE: rl/tic_tac_toe.py
  class Agent (line 31) | class Agent:
    method __init__ (line 32) | def __init__(self, eps=0.1, alpha=0.5):
    method setV (line 38) | def setV(self, V):
    method set_symbol (line 41) | def set_symbol(self, sym):
    method set_verbose (line 44) | def set_verbose(self, v):
    method reset_history (line 48) | def reset_history(self):
    method take_action (line 51) | def take_action(self, env):
    method update_state_history (line 110) | def update_state_history(self, s):
    method update (line 117) | def update(self, env):
  class Environment (line 135) | class Environment:
    method __init__ (line 136) | def __init__(self):
    method is_empty (line 144) | def is_empty(self, i, j):
    method reward (line 147) | def reward(self, sym):
    method get_state (line 156) | def get_state(self):
    method game_over (line 176) | def game_over(self, force_recalculate=False):
    method is_draw (line 223) | def is_draw(self):
    method draw_board (line 234) | def draw_board(self):
  class Human (line 250) | class Human:
    method __init__ (line 251) | def __init__(self):
    method set_symbol (line 254) | def set_symbol(self, sym):
    method take_action (line 257) | def take_action(self, env):
    method update (line 268) | def update(self, env):
    method update_state_history (line 271) | def update_state_history(self, s):
  function get_state_hash_and_winner (line 280) | def get_state_hash_and_winner(env, i=0, j=0):
  function initialV_x (line 339) | def initialV_x(env, state_winner_triples):
  function initialV_o (line 357) | def initialV_o(env, state_winner_triples):
  function play_game (line 374) | def play_game(p1, p2, env, draw=False):

FILE: rl/ucb1.py
  class Bandit (line 18) | class Bandit:
    method __init__ (line 19) | def __init__(self, p):
    method pull (line 25) | def pull(self):
    method update (line 29) | def update(self, x):
  function ucb (line 34) | def ucb(mean, n, nj):
  function run_experiment (line 38) | def run_experiment():

FILE: rl/ucb1_starter.py
  class Bandit (line 18) | class Bandit:
    method __init__ (line 19) | def __init__(self, p):
    method pull (line 25) | def pull(self):
    method update (line 29) | def update(self, x):
  function ucb (line 34) | def ucb(mean, n, nj):
  function run_experiment (line 38) | def run_experiment():

FILE: rl/value_iteration.py
  function get_transition_probs_and_rewards (line 17) | def get_transition_probs_and_rewards(grid):

FILE: rl2/a3c/main.py
  function Env (line 29) | def Env():
  function smooth (line 41) | def smooth(x):

FILE: rl2/a3c/nets.py
  function build_feature_extractor (line 4) | def build_feature_extractor(input_):
  class PolicyNetwork (line 38) | class PolicyNetwork:
    method __init__ (line 39) | def __init__(self, num_outputs, reg=0.01):
  class ValueNetwork (line 84) | class ValueNetwork:
    method __init__ (line 85) | def __init__(self):
  function create_networks (line 119) | def create_networks(num_outputs):

FILE: rl2/a3c/thread_example.py
  class Worker (line 8) | class Worker:
    method __init__ (line 9) | def __init__(self, id_, global_counter):
    method run (line 14) | def run(self):

FILE: rl2/a3c/worker.py
  class Step (line 10) | class Step:
    method __init__ (line 11) | def __init__(self, state, action, reward, next_state, done):
  class ImageTransformer (line 23) | class ImageTransformer:
    method __init__ (line 24) | def __init__(self):
    method transform (line 35) | def transform(self, state, sess=None):
  function repeat_frame (line 41) | def repeat_frame(frame):
  function shift_frames (line 48) | def shift_frames(state, next_frame):
  function get_copy_params_op (line 53) | def get_copy_params_op(src_vars, dst_vars):
  function make_train_op (line 65) | def make_train_op(local_net, global_net):
  class Worker (line 104) | class Worker:
    method __init__ (line 105) | def __init__(
    method run (line 147) | def run(self, sess, coord, t_max):
    method sample_action (line 171) | def sample_action(self, state, sess):
    method get_value_prediction (line 178) | def get_value_prediction(self, state, sess):
    method run_n_steps (line 185) | def run_n_steps(self, n, sess):
    method update (line 219) | def update(self, steps, sess):

FILE: rl2/atari/dqn_tf.py
  class ImageTransformer (line 45) | class ImageTransformer:
    method __init__ (line 46) | def __init__(self):
    method transform (line 57) | def transform(self, state, sess=None):
  function update_state (line 62) | def update_state(state, obs_small):
  class ReplayMemory (line 67) | class ReplayMemory:
    method __init__ (line 68) | def __init__(self, size=MAX_EXPERIENCES, frame_height=IM_SIZE, frame_w...
    method add_experience (line 99) | def add_experience(self, action, frame, reward, terminal):
    method _get_state (line 116) | def _get_state(self, index):
    method _get_valid_indices (line 123) | def _get_valid_indices(self):
    method get_minibatch (line 136) | def get_minibatch(self):
  class DQN (line 156) | class DQN:
    method __init__ (line 157) | def __init__(self, K, conv_layer_sizes, hidden_layer_sizes, scope):
    method copy_from (line 212) | def copy_from(self, other):
    method save (line 225) | def save(self):
    method load (line 231) | def load(self):
    method set_session (line 240) | def set_session(self, session):
    method predict (line 243) | def predict(self, states):
    method update (line 246) | def update(self, states, actions, targets):
    method sample_action (line 257) | def sample_action(self, x, eps):
  function learn (line 264) | def learn(model, target_model, experience_replay_buffer, gamma, batch_si...
  function play_one (line 278) | def play_one(
  function smooth (line 344) | def smooth(x):

FILE: rl2/atari/dqn_theano.py
  function rgb2gray (line 37) | def rgb2gray(rgb):
  function downsample_image (line 44) | def downsample_image(A):
  function update_state (line 56) | def update_state(state, obs):
  class ReplayMemory (line 61) | class ReplayMemory:
    method __init__ (line 62) | def __init__(self, size=MAX_EXPERIENCES, frame_height=IM_SIZE, frame_w...
    method add_experience (line 93) | def add_experience(self, action, frame, reward, terminal):
    method _get_state (line 110) | def _get_state(self, index):
    method _get_valid_indices (line 117) | def _get_valid_indices(self):
    method get_minibatch (line 130) | def get_minibatch(self):
  function init_filter (line 150) | def init_filter(shape):
  function adam (line 155) | def adam(cost, params, lr0=1e-5, beta1=0.9, beta2=0.999, eps=1e-8):
  class ConvLayer (line 182) | class ConvLayer(object):
    method __init__ (line 183) | def __init__(self, mi, mo, filtsz=5, stride=2, f=T.nnet.relu):
    method forward (line 196) | def forward(self, X):
  class HiddenLayer (line 211) | class HiddenLayer:
    method __init__ (line 212) | def __init__(self, M1, M2, f=T.nnet.relu):
    method forward (line 219) | def forward(self, X):
  class DQN (line 223) | class DQN:
    method __init__ (line 224) | def __init__(self, K, conv_layer_sizes, hidden_layer_sizes):
    method copy_from (line 302) | def copy_from(self, other):
    method predict (line 309) | def predict(self, X):
    method update (line 312) | def update(self, states, actions, targets):
    method sample_action (line 315) | def sample_action(self, x, eps):
  function learn (line 324) | def learn(model, target_model, experience_replay_buffer, gamma, batch_si...
  function play_one (line 338) | def play_one(
  function smooth (line 399) | def smooth(x):

FILE: rl2/cartpole/dqn_tf.py
  class HiddenLayer (line 31) | class HiddenLayer:
    method __init__ (line 32) | def __init__(self, M1, M2, f=tf.nn.tanh, use_bias=True):
    method forward (line 41) | def forward(self, X):
  class DQN (line 49) | class DQN:
    method __init__ (line 50) | def __init__(self, D, K, hidden_layer_sizes, gamma, max_experiences=10...
    method set_session (line 100) | def set_session(self, session):
    method copy_from (line 103) | def copy_from(self, other):
    method predict (line 115) | def predict(self, X):
    method train (line 119) | def train(self, target_network):
    method add_experience (line 146) | def add_experience(self, s, a, r, s2, done):
    method sample_action (line 159) | def sample_action(self, x, eps):
  function play_one (line 167) | def play_one(env, model, tmodel, eps, gamma, copy_period):
  function main (line 197) | def main():

FILE: rl2/cartpole/dqn_theano.py
  function adam (line 30) | def adam(cost, params, lr0=1e-2, beta1=0.9, beta2=0.999, eps=1e-8):
  class HiddenLayer (line 50) | class HiddenLayer:
    method __init__ (line 51) | def __init__(self, M1, M2, f=T.tanh, use_bias=True):
    method forward (line 60) | def forward(self, X):
  class DQN (line 68) | class DQN:
    method __init__ (line 69) | def __init__(self, D, K, hidden_layer_sizes, gamma, max_experiences=10...
    method copy_from (line 128) | def copy_from(self, other):
    method predict (line 135) | def predict(self, X):
    method train (line 139) | def train(self, target_network):
    method add_experience (line 159) | def add_experience(self, s, a, r, s2, done):
    method sample_action (line 172) | def sample_action(self, x, eps):
  function play_one (line 180) | def play_one(env, model, tmodel, eps, gamma, copy_period):
  function main (line 210) | def main():

FILE: rl2/cartpole/pg_tf.py
  class HiddenLayer (line 28) | class HiddenLayer:
    method __init__ (line 29) | def __init__(self, M1, M2, f=tf.nn.tanh, use_bias=True):
    method forward (line 36) | def forward(self, X):
  class PolicyModel (line 45) | class PolicyModel:
    method __init__ (line 46) | def __init__(self, D, K, hidden_layer_sizes):
    method set_session (line 93) | def set_session(self, session):
    method partial_fit (line 96) | def partial_fit(self, X, actions, advantages):
    method predict (line 109) | def predict(self, X):
    method sample_action (line 113) | def sample_action(self, X):
  class ValueModel (line 119) | class ValueModel:
    method __init__ (line 120) | def __init__(self, D, hidden_layer_sizes):
    method set_session (line 149) | def set_session(self, session):
    method partial_fit (line 152) | def partial_fit(self, X, Y):
    method predict (line 157) | def predict(self, X):
  function play_one_td (line 162) | def play_one_td(env, pmodel, vmodel, gamma):
  function play_one_mc (line 193) | def play_one_mc(env, pmodel, vmodel, gamma):
  function main (line 246) | def main():

FILE: rl2/cartpole/pg_theano.py
  class HiddenLayer (line 26) | class HiddenLayer:
    method __init__ (line 27) | def __init__(self, M1, M2, f=T.tanh, use_bias=True):
    method forward (line 36) | def forward(self, X):
  class PolicyModel (line 45) | class PolicyModel:
    method __init__ (line 46) | def __init__(self, D, K, hidden_layer_sizes):
    method partial_fit (line 99) | def partial_fit(self, X, actions, advantages):
    method predict (line 105) | def predict(self, X):
    method sample_action (line 109) | def sample_action(self, X):
  class ValueModel (line 117) | class ValueModel:
    method __init__ (line 118) | def __init__(self, D, hidden_layer_sizes):
    method partial_fit (line 166) | def partial_fit(self, X, Y):
    method predict (line 171) | def predict(self, X):
  function play_one_td (line 176) | def play_one_td(env, pmodel, vmodel, gamma):
  function play_one_mc (line 206) | def play_one_mc(env, pmodel, vmodel, gamma):
  function main (line 259) | def main():

FILE: rl2/cartpole/q_learning.py
  class SGDRegressor (line 28) | class SGDRegressor:
    method __init__ (line 29) | def __init__(self, D):
    method partial_fit (line 33) | def partial_fit(self, X, Y):
    method predict (line 36) | def predict(self, X):
  class FeatureTransformer (line 41) | class FeatureTransformer:
    method __init__ (line 42) | def __init__(self, env):
    method transform (line 63) | def transform(self, observations):
  class Model (line 69) | class Model:
    method __init__ (line 70) | def __init__(self, env, feature_transformer):
    method predict (line 78) | def predict(self, s):
    method update (line 83) | def update(self, s, a, G):
    method sample_action (line 87) | def sample_action(self, s, eps):
  function play_one (line 94) | def play_one(env, model, eps, gamma):
  function main (line 123) | def main():

FILE: rl2/cartpole/q_learning_bins.py
  function build_state (line 26) | def build_state(features):
  function to_bin (line 29) | def to_bin(value, bins):
  class FeatureTransformer (line 33) | class FeatureTransformer:
    method __init__ (line 34) | def __init__(self):
    method transform (line 44) | def transform(self, observation):
  class Model (line 55) | class Model:
    method __init__ (line 56) | def __init__(self, env, feature_transformer):
    method predict (line 64) | def predict(self, s):
    method update (line 68) | def update(self, s, a, G):
    method sample_action (line 72) | def sample_action(self, s, eps):
  function play_one (line 80) | def play_one(model, eps, gamma):
  function plot_running_avg (line 104) | def plot_running_avg(totalrewards):

FILE: rl2/cartpole/random_search.py
  function get_action (line 17) | def get_action(s, w):
  function play_one_episode (line 21) | def play_one_episode(env, params):
  function play_multiple_episodes (line 37) | def play_multiple_episodes(env, T, params):
  function random_search (line 48) | def random_search(env):

FILE: rl2/cartpole/save_a_video.py
  function get_action (line 19) | def get_action(s, w):
  function play_one_episode (line 23) | def play_one_episode(env, params):
  function play_multiple_episodes (line 38) | def play_multiple_episodes(env, T, params):
  function random_search (line 49) | def random_search(env):

FILE: rl2/cartpole/td_lambda.py
  class SGDRegressor (line 24) | class SGDRegressor:
    method __init__ (line 25) | def __init__(self, D):
    method partial_fit (line 28) | def partial_fit(self, x, y, e, lr=1e-1):
    method predict (line 31) | def predict(self, X):
  class Model (line 37) | class Model:
    method __init__ (line 38) | def __init__(self, env, feature_transformer):
    method reset (line 54) | def reset(self):
    method predict (line 57) | def predict(self, s):
    method update (line 62) | def update(self, s, a, G, gamma, lambda_):
    method sample_action (line 77) | def sample_action(self, s, eps):
  function play_one (line 85) | def play_one(model, env, eps, gamma, lambda_):

FILE: rl2/cartpole/tf_warmup.py
  class SGDRegressor (line 14) | class SGDRegressor:
    method __init__ (line 15) | def __init__(self, D):
    method partial_fit (line 40) | def partial_fit(self, X, Y):
    method predict (line 43) | def predict(self, X):

FILE: rl2/cartpole/theano_warmup.py
  class SGDRegressor (line 12) | class SGDRegressor:
    method __init__ (line 13) | def __init__(self, D):
    method partial_fit (line 36) | def partial_fit(self, X, Y):
    method predict (line 39) | def predict(self, X):

FILE: rl2/mountaincar/n_step.py
  class SGDRegressor (line 28) | class SGDRegressor:
    method __init__ (line 29) | def __init__(self, **kwargs):
    method partial_fit (line 33) | def partial_fit(self, X, Y):
    method predict (line 39) | def predict(self, X):
  function play_one (line 56) | def play_one(model, eps, gamma, n=5):

FILE: rl2/mountaincar/pg_tf.py
  class HiddenLayer (line 27) | class HiddenLayer:
    method __init__ (line 28) | def __init__(self, M1, M2, f=tf.nn.tanh, use_bias=True, zeros=False):
    method forward (line 41) | def forward(self, X):
  class PolicyModel (line 50) | class PolicyModel:
    method __init__ (line 51) | def __init__(self, D, ft, hidden_layer_sizes=[]):
    method set_session (line 93) | def set_session(self, session):
    method partial_fit (line 96) | def partial_fit(self, X, actions, advantages):
    method predict (line 111) | def predict(self, X):
    method sample_action (line 116) | def sample_action(self, X):
  class ValueModel (line 122) | class ValueModel:
    method __init__ (line 123) | def __init__(self, D, ft, hidden_layer_sizes=[]):
    method set_session (line 154) | def set_session(self, session):
    method partial_fit (line 157) | def partial_fit(self, X, Y):
    method predict (line 165) | def predict(self, X):
  function play_one_td (line 171) | def play_one_td(env, pmodel, vmodel, gamma):
  function main (line 202) | def main():

FILE: rl2/mountaincar/pg_tf_random.py
  class HiddenLayer (line 27) | class HiddenLayer:
    method __init__ (line 28) | def __init__(self, M1, M2, f=tf.nn.tanh, use_bias=True, zeros=False):
    method forward (line 43) | def forward(self, X):
  class PolicyModel (line 52) | class PolicyModel:
    method __init__ (line 53) | def __init__(self, ft, D, hidden_layer_sizes_mean=[], hidden_layer_siz...
    method set_session (line 111) | def set_session(self, session):
    method init_vars (line 114) | def init_vars(self):
    method predict (line 133) | def predict(self, X):
    method sample_action (line 139) | def sample_action(self, X):
    method copy (line 144) | def copy(self):
    method copy_from (line 151) | def copy_from(self, other):
    method perturb_params (line 163) | def perturb_params(self):
  function play_one (line 178) | def play_one(env, pmodel, gamma):
  function play_multiple_episodes (line 197) | def play_multiple_episodes(env, T, pmodel, gamma, print_iters=False):
  function random_search (line 211) | def random_search(env, pmodel, gamma):
  function main (line 235) | def main():

FILE: rl2/mountaincar/pg_theano.py
  function adam (line 23) | def adam(cost, params, lr0=1e-3, beta1=0.9, beta2=0.999, eps=1e-8):
  class HiddenLayer (line 43) | class HiddenLayer:
    method __init__ (line 44) | def __init__(self, M1, M2, f=T.nnet.relu, use_bias=True, zeros=False):
    method forward (line 57) | def forward(self, X):
  class PolicyModel (line 66) | class PolicyModel:
    method __init__ (line 67) | def __init__(self, D, ft, hidden_layer_sizes=[]):
    method partial_fit (line 131) | def partial_fit(self, X, actions, advantages):
    method predict (line 138) | def predict(self, X):
    method sample_action (line 143) | def sample_action(self, X):
  class ValueModel (line 152) | class ValueModel:
    method __init__ (line 153) | def __init__(self, D, ft, hidden_layer_sizes=[]):
    method partial_fit (line 199) | def partial_fit(self, X, Y):
    method predict (line 205) | def predict(self, X):
  function play_one_td (line 211) | def play_one_td(env, pmodel, vmodel, gamma):
  function main (line 242) | def main():

FILE: rl2/mountaincar/pg_theano_random.py
  class HiddenLayer (line 26) | class HiddenLayer:
    method __init__ (line 27) | def __init__(self, M1, M2, f=T.nnet.relu, use_bias=True, zeros=False):
    method forward (line 40) | def forward(self, X):
  class PolicyModel (line 49) | class PolicyModel:
    method __init__ (line 50) | def __init__(self, ft, D, hidden_layer_sizes_mean=[], hidden_layer_siz...
    method predict (line 111) | def predict(self, X):
    method sample_action (line 116) | def sample_action(self, X):
    method copy (line 123) | def copy(self):
    method copy_from (line 128) | def copy_from(self, other):
    method perturb_params (line 134) | def perturb_params(self):
  function play_one (line 145) | def play_one(env, pmodel, gamma):
  function play_multiple_episodes (line 164) | def play_multiple_episodes(env, T, pmodel, gamma, print_iters=False):
  function random_search (line 178) | def random_search(env, pmodel, gamma):
  function main (line 202) | def main():

FILE: rl2/mountaincar/q_learning.py
  class FeatureTransformer (line 38) | class FeatureTransformer:
    method __init__ (line 39) | def __init__(self, env, n_components=500):
    method transform (line 58) | def transform(self, observations):
  class Model (line 66) | class Model:
    method __init__ (line 67) | def __init__(self, env, feature_transformer, learning_rate):
    method predict (line 76) | def predict(self, s):
    method update (line 82) | def update(self, s, a, G):
    method sample_action (line 87) | def sample_action(self, s, eps):
  function play_one (line 101) | def play_one(model, env, eps, gamma):
  function plot_cost_to_go (line 127) | def plot_cost_to_go(env, estimator, num_tiles=20):
  function plot_running_avg (line 147) | def plot_running_avg(totalrewards):
  function main (line 157) | def main(show_plots=True):

FILE: rl2/mountaincar/td_lambda.py
  class BaseModel (line 31) | class BaseModel:
    method __init__ (line 32) | def __init__(self, D):
    method partial_fit (line 35) | def partial_fit(self, input_, target, eligibility, lr=1e-2):
    method predict (line 38) | def predict(self, X):
  class Model (line 44) | class Model:
    method __init__ (line 45) | def __init__(self, env, feature_transformer):
    method predict (line 56) | def predict(self, s):
    method update (line 63) | def update(self, s, a, G, gamma, lambda_):
    method sample_action (line 70) | def sample_action(self, s, eps):
  function play_one (line 78) | def play_one(model, env, eps, gamma, lambda_):

FILE: rl3/a2c/a2c.py
  function set_global_seeds (line 12) | def set_global_seeds(i):
  function cat_entropy (line 17) | def cat_entropy(logits):
  function find_trainable_variables (line 25) | def find_trainable_variables(key):
  function discount_with_dones (line 30) | def discount_with_dones(rewards, dones, gamma):
  class Agent (line 40) | class Agent:
    method __init__ (line 41) | def __init__(self, Network, ob_space, ac_space, nenvs, nsteps, nstack,
  class Runner (line 102) | class Runner:
    method __init__ (line 103) | def __init__(self, env, agent, nsteps=5, nstack=4, gamma=0.99):
    method update_state (line 119) | def update_state(self, obs):
    method run (line 124) | def run(self):
  function learn (line 168) | def learn(network, env, seed, new_session=True,  nsteps=5, nstack=4, tot...

FILE: rl3/a2c/atari_wrappers.py
  class NoopResetEnv (line 9) | class NoopResetEnv(gym.Wrapper):
    method __init__ (line 10) | def __init__(self, env, noop_max=30):
    method reset (line 20) | def reset(self, **kwargs):
    method step (line 35) | def step(self, ac):
  class FireResetEnv (line 39) | class FireResetEnv(gym.Wrapper):
    method __init__ (line 40) | def __init__(self, env):
    method reset (line 46) | def reset(self, **kwargs):
    method step (line 56) | def step(self, ac):
  class EpisodicLifeEnv (line 60) | class EpisodicLifeEnv(gym.Wrapper):
    method __init__ (line 61) | def __init__(self, env):
    method step (line 69) | def step(self, action):
    method reset (line 83) | def reset(self, **kwargs):
  class MaxAndSkipEnv (line 97) | class MaxAndSkipEnv(gym.Wrapper):
    method __init__ (line 98) | def __init__(self, env, skip=4):
    method step (line 105) | def step(self, action):
    method reset (line 124) | def reset(self, **kwargs):
  class ClipRewardEnv (line 128) | class ClipRewardEnv(gym.RewardWrapper):
    method reward (line 129) | def reward(self, reward):
  class WarpFrame (line 146) | class WarpFrame(gym.ObservationWrapper):
    method __init__ (line 147) | def __init__(self, env, width=84, height=84, grayscale=True):
    method observation (line 160) | def observation(self, frame):
  class FrameStack (line 169) | class FrameStack(gym.Wrapper):
    method __init__ (line 170) | def __init__(self, env, k):
    method reset (line 185) | def reset(self):
    method step (line 191) | def step(self, action):
    method _get_ob (line 196) | def _get_ob(self):
  class LazyFrames (line 201) | class LazyFrames:
    method __init__ (line 202) | def __init__(self, frames):
    method __array__ (line 212) | def __array__(self, dtype=None):
  function make_atari (line 219) | def make_atari(env_id):
  function wrap_deepmind (line 227) | def wrap_deepmind(env, episode_life=True, clip_rewards=True, frame_stack...
  class Monitor (line 246) | class Monitor(gym.Wrapper):
    method __init__ (line 247) | def __init__(self, env, rank=0):
    method reset (line 261) | def reset(self):
    method step (line 274) | def step(self, action):

FILE: rl3/a2c/main.py
  function get_args (line 24) | def get_args():
  function train (line 33) | def train(env_id, num_timesteps, num_cpu):
  function main (line 52) | def main():

FILE: rl3/a2c/neural_network.py
  function sample (line 6) | def sample(logits):
  function conv (line 11) | def conv(inputs, nf, ks, strides, gain=1.0):
  function dense (line 17) | def dense(inputs, n, act=tf.nn.relu, gain=1.0):
  class CNN (line 22) | class CNN:
    method __init__ (line 24) | def __init__(self, sess, ob_space, ac_space, nenv, nsteps, nstack, reu...

FILE: rl3/a2c/play.py
  function get_args (line 12) | def get_args():
  function get_agent (line 19) | def get_agent(env, nsteps=5, nstack=1, total_timesteps=int(80e6),
  function main (line 30) | def main():

FILE: rl3/a2c/subproc_vec_env.py
  function worker (line 6) | def worker(remote, parent_remote, env_fn_wrapper):
  class CloudpickleWrapper (line 33) | class CloudpickleWrapper():
    method __init__ (line 38) | def __init__(self, x):
    method __getstate__ (line 41) | def __getstate__(self):
    method __setstate__ (line 45) | def __setstate__(self, ob):
  class SubprocVecEnv (line 50) | class SubprocVecEnv():
    method __init__ (line 51) | def __init__(self, env_fns):
    method step (line 72) | def step(self, actions):
    method reset (line 84) | def reset(self):
    method reset_task (line 89) | def reset_task(self):
    method close (line 94) | def close(self):
    method num_envs (line 105) | def num_envs(self):

FILE: rl3/ddpg.py
  function ANN (line 25) | def ANN(x, layer_sizes, hidden_activation=tf.nn.relu, output_activation=...
  function get_vars (line 32) | def get_vars(scope):
  function CreateNetworks (line 38) | def CreateNetworks(
  class ReplayBuffer (line 59) | class ReplayBuffer:
    method __init__ (line 60) | def __init__(self, obs_dim, act_dim, size):
    method store (line 68) | def store(self, obs, act, rew, next_obs, done):
    method sample_batch (line 77) | def sample_batch(self, batch_size=32):
  function ddpg (line 87) | def ddpg(
  function smooth (line 299) | def smooth(x):

FILE: rl3/es_flappy.py
  class Env (line 25) | class Env:
    method __init__ (line 26) | def __init__(self):
    method step (line 36) | def step(self, action):
    method reset (line 44) | def reset(self):
    method get_observation (line 48) | def get_observation(self):
    method set_display (line 55) | def set_display(self, boolean_value):
  function softmax (line 70) | def softmax(a):
  function relu (line 75) | def relu(x):
  class ANN (line 78) | class ANN:
    method __init__ (line 79) | def __init__(self, D, M, K, f=relu):
    method init (line 85) | def init(self):
    method forward (line 94) | def forward(self, X):
    method sample_action (line 98) | def sample_action(self, x):
    method get_params (line 107) | def get_params(self):
    method get_params_dict (line 111) | def get_params_dict(self):
    method set_params (line 119) | def set_params(self, params):
  function evolution_strategy (line 129) | def evolution_strategy(
  function reward_function (line 178) | def reward_function(params):

FILE: rl3/es_mnist.py
  function softmax (line 40) | def softmax(a):
  function relu (line 46) | def relu(x):
  function log_likelihood (line 50) | def log_likelihood(Y, P):
  class ANN (line 56) | class ANN:
    method __init__ (line 57) | def __init__(self, D, M, K):
    method init (line 62) | def init(self):
    method forward (line 69) | def forward(self, X):
    method score (line 73) | def score(self, X, Y):
    method get_params (line 77) | def get_params(self):
    method set_params (line 81) | def set_params(self, params):
  function evolution_strategy (line 91) | def evolution_strategy(
  function reward_function (line 129) | def reward_function(params):

FILE: rl3/es_mujoco.py
  function relu (line 37) | def relu(x):
  class ANN (line 43) | class ANN:
    method __init__ (line 44) | def __init__(self, D, M, K, f=relu):
    method init (line 50) | def init(self):
    method forward (line 59) | def forward(self, X):
    method sample_action (line 63) | def sample_action(self, x):
    method get_params (line 70) | def get_params(self):
    method get_params_dict (line 74) | def get_params_dict(self):
    method set_params (line 82) | def set_params(self, params):
  function evolution_strategy (line 92) | def evolution_strategy(
  function reward_function (line 141) | def reward_function(params, display=False):

FILE: rl3/es_simple.py
  function evolution_strategy (line 6) | def evolution_strategy(
  function reward_function (line 36) | def reward_function(params):

FILE: rl3/flappy2envs.py
  class Env (line 19) | class Env:
    method __init__ (line 20) | def __init__(self):
    method step (line 30) | def step(self, action):
    method reset (line 38) | def reset(self):
    method get_observation (line 42) | def get_observation(self):
    method set_display (line 49) | def set_display(self, boolean_value):
  function softmax (line 64) | def softmax(a):
  function relu (line 69) | def relu(x):
  class ANN (line 72) | class ANN:
    method __init__ (line 73) | def __init__(self, D, M, K, f=relu):
    method init (line 79) | def init(self):
    method forward (line 88) | def forward(self, X):
    method sample_action (line 92) | def sample_action(self, x):
    method score (line 101) | def score(self, X, Y):
    method get_params (line 105) | def get_params(self):
    method get_params_dict (line 109) | def get_params_dict(self):
    method set_params (line 117) | def set_params(self, params):
  function reward_function (line 133) | def reward_function(params, env):

FILE: rl3/gym_review.py
  function get_action (line 12) | def get_action(s, w):
  function play_one_episode (line 16) | def play_one_episode(env, params):
  function play_multiple_episodes (line 31) | def play_multiple_episodes(env, T, params):
  function random_search (line 42) | def random_search(env):

FILE: rl3/plot_ddpg_result.py
  function smooth (line 6) | def smooth(x):

FILE: rl3/plot_es_flappy_results.py
  function smooth (line 5) | def smooth(x):

FILE: rl3/plot_es_mujoco_results.py
  function smooth (line 5) | def smooth(x):

FILE: rl3v2/visualize_es.py
  function f (line 5) | def f(x, y):
  function evolution_strategies (line 10) | def evolution_strategies(
  function visualize_es (line 33) | def visualize_es(history, bounds, f, resolution=100):

FILE: rl3v2/visualize_hill_climbing.py
  function f (line 5) | def f(x, y):
  function hill_climb (line 10) | def hill_climb(
  function visualize_es (line 35) | def visualize_es(history, bounds, f, resolution=100):

FILE: rnn_class/batch_gru.py
  class GRU (line 16) | class GRU:
    method __init__ (line 17) | def __init__(self, Mi, Mo, activation):
    method get_ht (line 47) | def get_ht(self, xWxr_t, xWxz_t, xWxh_t, h_t1):
    method recurrence (line 54) | def recurrence(self, xWxr_t, xWxz_t, xWxh_t, is_start, h_t1, h0):
    method output (line 62) | def output(self, Xflat, startPoints):

FILE: rnn_class/batch_parity.py
  class SimpleRNN (line 18) | class SimpleRNN:
    method __init__ (line 19) | def __init__(self, M):
    method fit (line 22) | def fit(self, X, Y, batch_sz=20, learning_rate=1.0, mu=0.99, reg=1.0, ...
  function parity (line 156) | def parity(B=12, learning_rate=1e-3, epochs=3000):

FILE: rnn_class/batch_units.py
  function init_weight (line 13) | def init_weight(Mi, Mo):
  class SimpleRecurrentLayer (line 17) | class SimpleRecurrentLayer:
    method __init__ (line 18) | def __init__(self, Mi, Mo, activation):
    method get_ht (line 36) | def get_ht(self, xWxh_t, h_t1):
    method recurrence (line 39) | def recurrence(self, xWxh_t, is_start, h_t1, h0):
    method output (line 47) | def output(self, Xflat, startPoints):
  class GRU (line 62) | class GRU:
    method __init__ (line 63) | def __init__(self, Mi, Mo, activation):
    method get_ht (line 93) | def get_ht(self, xWxr_t, xWxz_t, xWxh_t, h_t1):
    method recurrence (line 100) | def recurrence(self, xWxr_t, xWxz_t, xWxh_t, is_start, h_t1, h0):
    method output (line 108) | def output(self, Xflat, startPoints):
  class LSTM (line 126) | class LSTM:
    method __init__ (line 127) | def __init__(self, Mi, Mo, activation):
    method get_ht_ct (line 189) | def get_ht_ct(self, xWxi_t, xWxf_t, xWxc_t, xWxo_t, h_t1, c_t1):
    method recurrence (line 197) | def recurrence(self, xWxi_t, xWxf_t, xWxc_t, xWxo_t, is_start, h_t1, c...
    method output (line 205) | def output(self, Xflat, startPoints):

FILE: rnn_class/batch_wiki.py
  class RNN (line 24) | class RNN:
    method __init__ (line 25) | def __init__(self, D, hidden_layer_sizes, V):
    method fit (line 30) | def fit(self, X, learning_rate=1e-4, mu=0.99, epochs=10, batch_sz=100,...
  function train_wikipedia (line 133) | def train_wikipedia(we_file='word_embeddings.npy', w2i_file='wikipedia_w...
  function find_analogies (line 150) | def find_analogies(w1, w2, w3, we_file='word_embeddings.npy', w2i_file='...

FILE: rnn_class/brown.py
  function get_sentences (line 19) | def get_sentences():
  function get_sentences_with_word2idx (line 25) | def get_sentences_with_word2idx():
  function get_sentences_with_word2idx_limit_vocab (line 46) | def get_sentences_with_word2idx_limit_vocab(n_vocab=2000, keep_words=KEE...

FILE: rnn_class/gru.py
  class GRU (line 16) | class GRU:
    method __init__ (line 17) | def __init__(self, Mi, Mo, activation):
    method recurrence (line 47) | def recurrence(self, x_t, h_t1):
    method output (line 54) | def output(self, x):

FILE: rnn_class/lstm.py
  class LSTM (line 15) | class LSTM:
    method __init__ (line 16) | def __init__(self, Mi, Mo, activation):
    method recurrence (line 78) | def recurrence(self, x_t, h_t1, c_t1):
    method output (line 86) | def output(self, x):

FILE: rnn_class/mlp_parity.py
  class HiddenLayer (line 18) | class HiddenLayer(object):
    method __init__ (line 19) | def __init__(self, M1, M2, an_id):
    method forward (line 29) | def forward(self, X):
  class ANN (line 33) | class ANN(object):
    method __init__ (line 34) | def __init__(self, hidden_layer_sizes):
    method fit (line 37) | def fit(self, X, Y, learning_rate=1e-2, mu=0.99, reg=1e-12, epochs=400...
    method forward (line 111) | def forward(self, X):
    method predict (line 117) | def predict(self, X):
  function wide (line 122) | def wide():
  function deep (line 127) | def deep():

FILE: rnn_class/poetry_classifier.py
  class SimpleRNN (line 18) | class SimpleRNN:
    method __init__ (line 19) | def __init__(self, M, V):
    method fit (line 23) | def fit(self, X, Y, learning_rate=1.0, mu=0.99, reg=1.0, activation=T....
    method save (line 93) | def save(self, filename):
    method load (line 97) | def load(filename, activation):
    method set (line 111) | def set(self, Wx, Wh, bh, h0, Wo, bo, activation):
  function train_poetry (line 149) | def train_poetry():

FILE: rnn_class/rrnn_language.py
  class SimpleRNN (line 19) | class SimpleRNN:
    method __init__ (line 20) | def __init__(self, D, M, V):
    method fit (line 25) | def fit(self, X, learning_rate=10., mu=0.9, reg=0., activation=T.tanh,...
    method save (line 99) | def save(self, filename):
    method load (line 103) | def load(filename, activation):
    method set (line 122) | def set(self, We, Wx, Wh, bh, h0, Wxz, Whz, bz, Wo, bo, activation):
    method generate (line 167) | def generate(self, word2idx):
  function train_poetry (line 196) | def train_poetry():
  function generate_poetry (line 203) | def generate_poetry():

FILE: rnn_class/srn_language.py
  class SimpleRNN (line 19) | class SimpleRNN:
    method __init__ (line 20) | def __init__(self, D, M, V):
    method fit (line 25) | def fit(self, X, learning_rate=1., mu=0.99, reg=1.0, activation=T.tanh...
    method save (line 124) | def save(self, filename):
    method load (line 128) | def load(filename, activation):
    method set (line 144) | def set(self, We, Wx, Wh, bh, h0, Wo, bo, activation):
    method generate (line 182) | def generate(self, pi, word2idx):
  function train_poetry (line 211) | def train_poetry():
  function generate_poetry (line 217) | def generate_poetry():
  function wikipedia (line 230) | def wikipedia():

FILE: rnn_class/srn_language_tf.py
  class SimpleRNN (line 18) | class SimpleRNN:
    method __init__ (line 19) | def __init__(self, D, M, V, f, session):
    method set_session (line 26) | def set_session(self, session):
    method build (line 29) | def build(self, We, Wx, Wh, bh, h0, Wo, bo):
    method fit (line 106) | def fit(self, X, epochs=500, show_fig=False):
    method predict (line 161) | def predict(self, prev_words):
    method save (line 169) | def save(self, filename):
    method load (line 174) | def load(filename, activation, session):
    method generate (line 190) | def generate(self, pi, word2idx):
  function train_poetry (line 219) | def train_poetry(session, dims, savefile):
  function generate_poetry (line 226) | def generate_poetry(session, savefile):

FILE: rnn_class/srn_parity.py
  class SimpleRNN (line 18) | class SimpleRNN:
    method __init__ (line 19) | def __init__(self, M):
    method fit (line 22) | def fit(self, X, Y, learning_rate=0.1, mu=0.99, reg=1.0, activation=T....
  function parity (line 105) | def parity(B=12, learning_rate=1e-4, epochs=200):

FILE: rnn_class/srn_parity_tf.py
  class SimpleRNN (line 17) | class SimpleRNN:
    method __init__ (line 18) | def __init__(self, M):
    method fit (line 21) | def fit(self, X, Y, learning_rate=1.0, mu=0.99, reg=1.0, activation=tf...
  function parity (line 99) | def parity(B=12, learning_rate=1e-4, epochs=200):

FILE: rnn_class/tf_parity.py
  function x2sequence (line 29) | def x2sequence(x, T, D, batch_sz):
  class SimpleRNN (line 40) | class SimpleRNN:
    method __init__ (line 41) | def __init__(self, M):
    method fit (line 45) | def fit(self, X, Y, batch_sz=20, learning_rate=0.1, mu=0.9, activation...
  function parity (line 125) | def parity(B=12, learning_rate=1., epochs=1000):

FILE: rnn_class/util.py
  function init_weight (line 17) | def init_weight(Mi, Mo):
  function all_parity_pairs (line 20) | def all_parity_pairs(nbit):
  function all_parity_pairs_with_sequence_labels (line 38) | def all_parity_pairs_with_sequence_labels(nbit):
  function remove_punctuation_2 (line 56) | def remove_punctuation_2(s):
  function remove_punctuation_3 (line 59) | def remove_punctuation_3(s):
  function get_robert_frost (line 68) | def get_robert_frost():
  function my_tokenizer (line 86) | def my_tokenizer(s):
  function get_wikipedia_data (line 91) | def get_wikipedia_data(n_files, n_vocab, by_paragraph=False):
  function get_tags (line 172) | def get_tags(s):
  function get_poetry_classifier_data (line 176) | def get_poetry_classifier_data(samples_per_class, load_cached=True, save...
  function get_stock_data (line 216) | def get_stock_data():

FILE: rnn_class/visualize_embeddings.py
  function main (line 16) | def main(we_file='word_embeddings.npy', w2i_file='wikipedia_word2idx.jso...

FILE: rnn_class/wiki.py
  class RNN (line 25) | class RNN:
    method __init__ (line 26) | def __init__(self, D, hidden_layer_sizes, V):
    method fit (line 31) | def fit(self, X, learning_rate=1e-5, mu=0.99, epochs=10, show_fig=True...
  function train_wikipedia (line 139) | def train_wikipedia(we_file='word_embeddings.npy', w2i_file='wikipedia_w...
  function find_analogies (line 155) | def find_analogies(w1, w2, w3, we_file='word_embeddings.npy', w2i_file='...

FILE: supervised_class/app.py
  class MainHandler (line 22) | class MainHandler(tornado.web.RequestHandler):
    method get (line 23) | def get(self):
  class PredictionHandler (line 26) | class PredictionHandler(tornado.web.RequestHandler):
    method post (line 28) | def post(self):

FILE: supervised_class/bayes.py
  class Bayes (line 18) | class Bayes(object):
    method fit (line 19) | def fit(self, X, Y, smoothing=1e-2):
    method score (line 32) | def score(self, X, Y):
    method predict (line 36) | def predict(self, X):

FILE: supervised_class/dt.py
  function entropy (line 16) | def entropy(y):
  class TreeNode (line 27) | class TreeNode:
    method __init__ (line 28) | def __init__(self, depth=1, max_depth=None):
    method fit (line 35) | def fit(self, X, Y):
    method find_split (line 96) | def find_split(self, X, Y, col):
    method information_gain (line 121) | def information_gain(self, x, y, split):
    method predict_one (line 139) | def predict_one(self, x):
    method predict (line 158) | def predict(self, X):
  class DecisionTree (line 167) | class DecisionTree:
    method __init__ (line 168) | def __init__(self, max_depth=None):
    method fit (line 171) | def fit(self, X, Y):
    method predict (line 175) | def predict(self, X):
    method score (line 178) | def score(self, X, Y):

FILE: supervised_class/dt_without_recursion.py
  function entropy (line 16) | def entropy(y):
  class DecisionTree (line 28) | class DecisionTree:
    method __init__ (line 29) | def __init__(self, depth=0, max_depth=None):
    method fit (line 41) | def fit(self, X, Y):
    method find_split (line 156) | def find_split(self, X, Y, col):
    method information_gain (line 185) | def information_gain(self, x, y, split):
    method predict_one (line 203) | def predict_one(self, x):
    method predict (line 243) | def predict(self, X):
    method score (line 250) | def score(self, X, Y):

FILE: supervised_class/knn.py
  class KNN (line 22) | class KNN(object):
    method __init__ (line 23) | def __init__(self, k):
    method fit (line 26) | def fit(self, X, y):
    method predict (line 30) | def predict(self, X):
    method score (line 62) | def score(self, X, Y):

FILE: supervised_class/knn_fail.py
  function get_data (line 14) | def get_data():

FILE: supervised_class/knn_vectorized.py
  class KNN (line 19) | class KNN(object):
    method __init__ (line 20) | def __init__(self, k):
    method fit (line 23) | def fit(self, X, y):
    method predict (line 27) | def predict(self, X):
    method score (line 58) | def score(self, X, Y):

FILE: supervised_class/multinomialnb.py
  class MultinomialNB (line 15) | class MultinomialNB(object):
    method fit (line 16) | def fit(self, X, Y, smoothing=1.0):
    method score (line 32) | def score(self, X, Y):
    method predict (line 36) | def predict(self, X):

FILE: supervised_class/nb.py
  class NaiveBayes (line 17) | class NaiveBayes(object):
    method fit (line 18) | def fit(self, X, Y, smoothing=1e-2):
    method score (line 30) | def score(self, X, Y):
    method predict (line 34) | def predict(self, X):

FILE: supervised_class/perceptron.py
  function get_data (line 16) | def get_data():
  function get_simple_xor (line 24) | def get_simple_xor():
  class Perceptron (line 30) | class Perceptron:
    method fit (line 31) | def fit(self, X, Y, learning_rate=1.0, epochs=1000):
    method predict (line 63) | def predict(self, X):
    method score (line 66) | def score(self, X, Y):

FILE: supervised_class/util.py
  function get_data (line 12) | def get_data(limit=None):
  function get_xor (line 23) | def get_xor():
  function get_donut (line 32) | def get_donut():

FILE: supervised_class2/adaboost.py
  class AdaBoost (line 15) | class AdaBoost:
    method __init__ (line 16) | def __init__(self, M):
    method fit (line 19) | def fit(self, X, Y):
    method predict (line 40) | def predict(self, X):
    method score (line 49) | def score(self, X, Y):

FILE: supervised_class2/bagging_classification.py
  class BaggedTreeClassifier (line 52) | class BaggedTreeClassifier:
    method __init__ (line 53) | def __init__(self, B):
    method fit (line 56) | def fit(self, X, Y):
    method predict (line 68) | def predict(self, X):
    method score (line 75) | def score(self, X, Y):

FILE: supervised_class2/bagging_regression.py
  class BaggedTreeRegressor (line 38) | class BaggedTreeRegressor:
    method __init__ (line 39) | def __init__(self, B):
    method fit (line 42) | def fit(self, X, Y):
    method predict (line 54) | def predict(self, X):
    method score (line 60) | def score(self, X, Y):

FILE: supervised_class2/bias_variance_demo.py
  function make_poly (line 23) | def make_poly(x, D):
  function f (line 32) | def f(X):

FILE: supervised_class2/knn_dt_demo.py
  function plot_decision_boundary (line 95) | def plot_decision_boundary(X, model):

FILE: supervised_class2/rf_classification.py
  class DataTransformer (line 30) | class DataTransformer:
    method fit (line 31) | def fit(self, df):
    method transform (line 53) | def transform(self, df):
    method fit_transform (line 68) | def fit_transform(self, df):
  function replace_missing (line 73) | def replace_missing(df):
  function get_data (line 87) | def get_data():

FILE: supervised_class2/rf_regression.py
  class DataTransformer (line 43) | class DataTransformer:
    method fit (line 44) | def fit(self, df):
    method transform (line 51) | def transform(self, df):
    method fit_transform (line 64) | def fit_transform(self, df):
  function get_data (line 69) | def get_data():

FILE: supervised_class2/rf_vs_bag2.py
  class NotAsRandomForest (line 36) | class NotAsRandomForest:
    method __init__ (line 37) | def __init__(self, n_estimators):
    method fit (line 40) | def fit(self, X, Y, M=None):
    method predict (line 62) | def predict(self, X):
    method score (line 69) | def score(self, X, Y):

FILE: supervised_class2/util.py
  function plot_decision_boundary (line 13) | def plot_decision_boundary(X, model):
  class BaggedTreeRegressor (line 31) | class BaggedTreeRegressor:
    method __init__ (line 32) | def __init__(self, n_estimators, max_depth=None):
    method fit (line 36) | def fit(self, X, Y):
    method predict (line 48) | def predict(self, X):
    method score (line 54) | def score(self, X, Y):
  class BaggedTreeClassifier (line 60) | class BaggedTreeClassifier:
    method __init__ (line 61) | def __init__(self, n_estimators, max_depth=None):
    method fit (line 65) | def fit(self, X, Y):
    method predict (line 77) | def predict(self, X):
    method score (line 84) | def score(self, X, Y):

FILE: svm_class/fake_neural_net.py
  class SigmoidFeaturizer (line 25) | class SigmoidFeaturizer:
    method __init__ (line 26) | def __init__(self, gamma=1.0, n_components=100, method='random'):
    method _subsample_data (line 32) | def _subsample_data(self, X, Y, n=10000):
    method fit (line 40) | def fit(self, X, Y=None):
    method transform (line 87) | def transform(self, X):
    method fit_transform (line 92) | def fit_transform(self, X, Y=None):

FILE: svm_class/kernel_svm_gradient_primal.py
  function linear (line 18) | def linear(X1, X2, c=0):
  function rbf (line 21) | def rbf(X1, X2, gamma=None):
  function sigmoid (line 34) | def sigmoid(X1, X2, gamma=0.05, c=1):
  class KernelSVM (line 38) | class KernelSVM:
    method __init__ (line 39) | def __init__(self, kernel=linear, C=1.0):
    method _objective (line 43) | def _objective(self, margins):
    method fit (line 47) | def fit(self, X, Y, lr=1e-5, n_iters=400):
    method _decision_function (line 86) | def _decision_function(self, X):
    method predict (line 89) | def predict(self, X):
    method score (line 92) | def score(self, X, Y):
  function plot_decision_boundary (line 97) | def plot_decision_boundary(model, X, Y, resolution=100, colors=('b', 'k'...
  function clouds (line 139) | def clouds():
  function medical (line 145) | def medical():
  function xor (line 151) | def xor():
  function donut (line 157) | def donut():
  function spiral (line 163) | def spiral():

FILE: svm_class/linear_svm_gradient.py
  class LinearSVM (line 19) | class LinearSVM:
    method __init__ (line 20) | def __init__(self, C=1.0):
    method _objective (line 23) | def _objective(self, margins):
    method fit (line 26) | def fit(self, X, Y, lr=1e-5, n_iters=400):
    method _decision_function (line 60) | def _decision_function(self, X):
    method predict (line 63) | def predict(self, X):
    method score (line 66) | def score(self, X, Y):
  function plot_decision_boundary (line 71) | def plot_decision_boundary(model, X, Y, resolution=100, colors=('b', 'k'...
  function clouds (line 113) | def clouds():
  function medical (line 119) | def medical():

FILE: svm_class/svm_gradient.py
  function linear (line 20) | def linear(X1, X2, c=0):
  function rbf (line 23) | def rbf(X1, X2, gamma=None):
  function sigmoid (line 36) | def sigmoid(X1, X2, gamma=0.05, c=1):
  class SVM (line 40) | class SVM:
    method __init__ (line 41) | def __init__(self, kernel, C=1.0):
    method _train_objective (line 45) | def _train_objective(self):
    method fit (line 48) | def fit(self, X, Y, lr=1e-5, n_iters=400):
    method _decision_function (line 82) | def _decision_function(self, X):
    method predict (line 85) | def predict(self, X):
    method score (line 88) | def score(self, X, Y):
  function medical (line 93) | def medical():
  function medical_sigmoid (line 99) | def medical_sigmoid():
  function xor (line 105) | def xor():
  function donut (line 111) | def donut():
  function spiral (line 117) | def spiral():
  function clouds (line 123) | def clouds():

FILE: svm_class/svm_smo.py
  function linear (line 20) | def linear(X1, X2):
  function rbf (line 23) | def rbf(X1, X2):
  function sigmoid (line 34) | def sigmoid(X1, X2, gamma=0.05, c=1):
  class SVM (line 38) | class SVM:
    method __init__ (line 39) | def __init__(self, kernel, C=1.0):
    method _loss (line 43) | def _loss(self, X, Y):
    method _take_step (line 49) | def _take_step(self, i1, i2):
    method _examine_example (line 165) | def _examine_example(self, i2):
    method fit (line 198) | def fit(self, X, Y, tol=0.00001, eps=0.01):
    method _decision_function (line 248) | def _decision_function(self, X):
    method predict (line 252) | def predict(self, X):
    method score (line 255) | def score(self, X, Y):
  function get_data (line 260) | def get_data():

FILE: svm_class/svm_spam.py
  function visualize (line 56) | def visualize(label):

FILE: svm_class/util.py
  function getKaggleMNIST (line 16) | def getKaggleMNIST():
  function get_spiral (line 40) | def get_spiral():
  function get_xor (line 75) | def get_xor():
  function get_donut (line 85) | def get_donut():
  function get_clouds (line 105) | def get_clouds():
  function plot_decision_boundary (line 118) | def plot_decision_boundary(model, resolution=100, colors=('b', 'k', 'r')):

FILE: tensorflow/input_data.py
  function maybe_download (line 11) | def maybe_download(filename, work_directory):
  function _read32 (line 21) | def _read32(bytestream):
  function extract_images (line 24) | def extract_images(filename):
  function dense_to_one_hot (line 40) | def dense_to_one_hot(labels_dense, num_classes=10):
  function extract_labels (line 47) | def extract_labels(filename, one_hot=False):
  class DataSet (line 62) | class DataSet(object):
    method __init__ (line 63) | def __init__(self, images, labels, fake_data=False):
    method images (line 84) | def images(self):
    method labels (line 87) | def labels(self):
    method num_examples (line 90) | def num_examples(self):
    method epochs_completed (line 93) | def epochs_completed(self):
    method next_batch (line 95) | def next_batch(self, batch_size, fake_data=False):
  function read_data_sets (line 118) | def read_data_sets(train_dir, fake_data=False, one_hot=False):

FILE: tf2.0/fake_util.py
  function my_useful_function (line 3) | def my_useful_function():

FILE: tf2.0/keras_trader.py
  function get_data (line 31) | def get_data():
  class ReplayBuffer (line 43) | class ReplayBuffer:
    method __init__ (line 44) | def __init__(self, obs_dim, act_dim, size):
    method store (line 52) | def store(self, obs, act, rew, next_obs, done):
    method sample_batch (line 61) | def sample_batch(self, batch_size=32):
  function get_scaler (line 73) | def get_scaler(env):
  function maybe_make_dir (line 92) | def maybe_make_dir(directory):
  function mlp (line 99) | def mlp(input_dim, n_action, n_hidden_layers=1, hidden_dim=32):
  class MultiStockEnv (line 123) | class MultiStockEnv:
    method __init__ (line 140) | def __init__(self, data, initial_investment=20000):
    method reset (line 173) | def reset(self):
    method step (line 181) | def step(self, action):
    method _get_obs (line 210) | def _get_obs(self):
    method _get_val (line 219) | def _get_val(self):
    method _trade (line 223) | def _trade(self, action):
  class DQNAgent (line 266) | class DQNAgent(object):
    method __init__ (line 267) | def __init__(self, state_size, action_size):
    method update_replay_memory (line 278) | def update_replay_memory(self, state, action, reward, next_state, done):
    method act (line 282) | def act(self, state):
    method replay (line 289) | def replay(self, batch_size=32):
    method load (line 323) | def load(self, name):
    method save (line 327) | def save(self, name):
  function play_one_episode (line 332) | def play_one_episode(agent, env, is_train):

FILE: tf2.0/mlp_trader.py
  function get_data (line 16) | def get_data():
  class ReplayBuffer (line 28) | class ReplayBuffer:
    method __init__ (line 29) | def __init__(self, obs_dim, act_dim, size):
    method store (line 37) | def store(self, obs, act, rew, next_obs, done):
    method sample_batch (line 46) | def sample_batch(self, batch_size=32):
  function get_scaler (line 58) | def get_scaler(env):
  function maybe_make_dir (line 77) | def maybe_make_dir(directory):
  function mlp (line 84) | def mlp(input_dim, n_action, n_hidden_layers=1, hidden_dim=32):
  class MultiStockEnv (line 104) | class MultiStockEnv:
    method __init__ (line 121) | def __init__(self, data, initial_investment=20000):
    method reset (line 154) | def reset(self):
    method step (line 162) | def step(self, action):
    method _get_obs (line 191) | def _get_obs(self):
    method _get_val (line 200) | def _get_val(self):
    method _trade (line 204) | def _trade(self, action):
  class DQNAgent (line 247) | class DQNAgent(object):
    method __init__ (line 248) | def __init__(self, state_size, action_size):
    method update_replay_memory (line 259) | def update_replay_memory(self, state, action, reward, next_state, done):
    method act (line 263) | def act(self, state):
    method replay (line 269) | def replay(self, batch_size=32):
    method load (line 303) | def load(self, name):
    method save (line 308) | def save(self, name):
  function play_one_episode (line 313) | def play_one_episode(agent, env, is_train):

FILE: tf2.0/rl_trader.py
  function get_data (line 24) | def get_data():
  class ReplayBuffer (line 36) | class ReplayBuffer:
    method __init__ (line 37) | def __init__(self, obs_dim, act_dim, size):
    method store (line 45) | def store(self, obs, act, rew, next_obs, done):
    method sample_batch (line 54) | def sample_batch(self, batch_size=32):
  function get_scaler (line 66) | def get_scaler(env):
  function maybe_make_dir (line 85) | def maybe_make_dir(directory):
  function mlp (line 92) | def mlp(input_dim, n_action, n_hidden_layers=1, hidden_dim=32):
  class MultiStockEnv (line 116) | class MultiStockEnv:
    method __init__ (line 133) | def __init__(self, data, initial_investment=20000):
    method reset (line 166) | def reset(self):
    method step (line 174) | def step(self, action):
    method _get_obs (line 203) | def _get_obs(self):
    method _get_val (line 212) | def _get_val(self):
    method _trade (line 216) | def _trade(self, action):
  class DQNAgent (line 259) | class DQNAgent(object):
    method __init__ (line 260) | def __init__(self, state_size, action_size):
    method update_replay_memory (line 271) | def update_replay_memory(self, state, action, reward, next_state, done):
    method act (line 275) | def act(self, state):
    method replay (line 282) | def replay(self, batch_size=32):
    method load (line 316) | def load(self, name):
    method save (line 320) | def save(self, name):
  function play_one_episode (line 325) | def play_one_episode(agent, env, is_train):

FILE: tf2.0/xor3d.py
  function get_label (line 7) | def get_label(x, i1, i2, i3):

FILE: unsupervised_class/books.py
  function my_tokenizer (line 31) | def my_tokenizer(s):
  function tokens_to_vector (line 68) | def tokens_to_vector(tokens):
  function d (line 83) | def d(u, v):
  function cost (line 87) | def cost(X, R, M):
  function plot_k_means (line 100) | def plot_k_means(X, K, index_word_map, max_iter=20, beta=1.0, show_plots...
  function annotate1 (line 207) | def annotate1(X, index_word_map, eps=0.1):

FILE: unsupervised_class/choose_k.py
  function main (line 15) | def main():

FILE: unsupervised_class/evolution.py
  function to_code (line 20) | def to_code(a):
  function dist (line 25) | def dist(a, b):
  function generate_offspring (line 30) | def generate_offspring(parent):
  function maybe_modify (line 35) | def maybe_modify(c):

FILE: unsupervised_class/gmm.py
  function gmm (line 16) | def gmm(X, K, max_iter=20, smoothing=1e-2):
  function main (line 81) | def main():

FILE: unsupervised_class/gmm_mnist.py
  function main (line 25) | def main():

FILE: unsupervised_class/hcluster.py
  function main (line 16) | def main():

FILE: unsupervised_class/kmeans.py
  function d (line 15) | def d(u, v):
  function cost (line 20) | def cost(X, R, M):
  function plot_k_means (line 34) | def plot_k_means(X, K, max_iter=20, beta=3.0, show_plots=False):
  function get_simple_data (line 92) | def get_simple_data():
  function main (line 108) | def main():

FILE: unsupervised_class/kmeans_fail.py
  function donut (line 14) | def donut():
  function main (line 35) | def main():

FILE: unsupervised_class/kmeans_mnist.py
  function get_data (line 22) | def get_data(limit=None):
  function purity2 (line 35) | def purity2(Y, R):
  function purity (line 53) | def purity(Y, R):
  function DBI2 (line 70) | def DBI2(X, R):
  function DBI (line 104) | def DBI(X, M, R):
  function main (line 133) | def main():

FILE: unsupervised_class/kmeans_visualize.py
  function d (line 14) | def d(u, v):
  function cost (line 19) | def cost(X, R, M):
  function plot_k_means (line 27) | def plot_k_means(X, K, max_iter=20, beta=1.0):
  function main (line 65) | def main():

FILE: unsupervised_class/tweets.py
  function filter_tweet (line 54) | def filter_tweet(s):
  function purity (line 123) | def purity(true_labels, cluster_assignments, categories):

FILE: unsupervised_class2/autoencoder.py
  function T_shared_zeros_like32 (line 17) | def T_shared_zeros_like32(p):
  function momentum_updates (line 21) | def momentum_updates(cost, params, mu, learning_rate):
  class AutoEncoder (line 36) | class AutoEncoder(object):
    method __init__ (line 37) | def __init__(self, M, an_id):
    method fit (line 41) | def fit(self, X, learning_rate=0.5, mu=0.99, epochs=1, batch_sz=100, s...
    method forward_hidden (line 112) | def forward_hidden(self, X):
    method forward_output (line 116) | def forward_output(self, X):
    method createFromArrays (line 122) | def createFromArrays(W, bh, bo, an_id):
  class DNN (line 132) | class DNN(object):
    method __init__ (line 133) | def __init__(self, hidden_layer_sizes, UnsupervisedModel=AutoEncoder):
    method fit (line 142) | def fit(self, X, Y, Xtest, Ytest,
    method predict (line 216) | def predict(self, X):
    method forward (line 219) | def forward(self, X):
  function main (line 230) | def main():
  function test_single_autoencoder (line 240) | def test_single_autoencoder():

FILE: unsupervised_class2/autoencoder_tf.py
  class AutoEncoder (line 19) | class AutoEncoder(object):
    method __init__ (line 20) | def __init__(self, D, M, an_id):
    method set_session (line 25) | def set_session(self, session):
    method build (line 28) | def build(self, D, M):
    method fit (line 51) | def fit(self, X, epochs=1, batch_sz=100, show_fig=False):
    method transform (line 70) | def transform(self, X):
    method predict (line 76) | def predict(self, X):
    method forward_hidden (line 82) | def forward_hidden(self, X):
    method forward_logits (line 86) | def forward_logits(self, X):
    method forward_output (line 90) | def forward_output(self, X):
  class DNN (line 94) | class DNN(object):
    method __init__ (line 95) | def __init__(self, D, hidden_layer_sizes, K, UnsupervisedModel=AutoEnc...
    method set_session (line 106) | def set_session(self, session):
    method build_final_layer (line 111) | def build_final_layer(self, D, M, K):
    method fit (line 130) | def fit(self, X, Y, Xtest, Ytest, pretrain=True, epochs=1, batch_sz=100):
    method forward (line 169) | def forward(self, X):
  function test_pretraining_dnn (line 180) | def test_pretraining_dnn():
  function test_single_autoencoder (line 197) | def test_single_autoencoder():

FILE: unsupervised_class2/gaussian_nb.py
  class GaussianNB (line 18) | class GaussianNB(object):
    method fit (line 19) | def fit(self, X, Y, smoothing=1e-2):
    method score (line 31) | def score(self, X, Y):
    method predict (line 35) | def predict(self, X):

FILE: unsupervised_class2/pca.py
  function main (line 14) | def main():

FILE: unsupervised_class2/rbm.py
  class RBM (line 19) | class RBM(object):
    method __init__ (line 20) | def __init__(self, M, an_id):
    method fit (line 25) | def fit(self, X, learning_rate=0.1, epochs=1, batch_sz=100, show_fig=F...
    method free_energy (line 89) | def free_energy(self, V):
    method sample_h_given_v (line 92) | def sample_h_given_v(self, V):
    method sample_v_given_h (line 97) | def sample_v_given_h(self, H):
    method forward_hidden (line 102) | def forward_hidden(self, X):
    method forward_output (line 105) | def forward_output(self, X):
    method createFromArrays (line 111) | def createFromArrays(W, c, b, an_id):
  function main (line 121) | def main():

FILE: unsupervised_class2/rbm_tf.py
  class RBM (line 16) | class RBM(object):
    method __init__ (line 17) | def __init__(self, D, M, an_id):
    method set_session (line 23) | def set_session(self, session):
    method build (line 26) | def build(self, D, M):
    method fit (line 73) | def fit(self, X, epochs=1, batch_sz=100, show_fig=False):
    method free_energy (line 92) | def free_energy(self, V):
    method forward_hidden (line 105) | def forward_hidden(self, X):
    method forward_logits (line 108) | def forward_logits(self, X):
    method forward_output (line 112) | def forward_output(self, X):
    method transform (line 115) | def transform(self, X):
  function main (line 122) | def main():

FILE: unsupervised_class2/tsne_books.py
  function my_tokenizer (line 27) | def my_tokenizer(s):
  function tokens_to_vector (line 62) | def tokens_to_vector(tokens):
  function main (line 78) | def main():

FILE: unsupervised_class2/tsne_donut.py
  function get_donut_data (line 14) | def get_donut_data():
  function main (line 34) | def main():

FILE: unsupervised_class2/tsne_mnist.py
  function main (line 21) | def main():

FILE: unsupervised_class2/tsne_xor.py
  function get_xor_data (line 14) | def get_xor_data():
  function main (line 24) | def main():

FILE: unsupervised_class2/unsupervised.py
  class DBN (line 22) | class DBN(object):
    method __init__ (line 23) | def __init__(self, hidden_layer_sizes, UnsupervisedModel=AutoEncoder):
    method fit (line 31) | def fit(self, X, pretrain_epochs=1):
    method forward (line 44) | def forward(self, X):
    method fit_to_input (line 50) | def fit_to_input(self, k, learning_rate=1.0, mu=0.99, epochs=100000):
    method save (line 100) | def save(self, filename):
    method load (line 105) | def load(filename, UnsupervisedModel=AutoEncoder):
  function main (line 124) | def main():

FILE: unsupervised_class2/util.py
  function relu (line 13) | def relu(x):
  function error_rate (line 17) | def error_rate(p, t):
  function getKaggleMNIST (line 21) | def getKaggleMNIST():
  function init_weights (line 37) | def init_weights(shape):

FILE: unsupervised_class2/vanishing.py
  class HiddenLayer (line 18) | class HiddenLayer(object):
    method __init__ (line 19) | def __init__(self, D, M):
    method forward (line 26) | def forward(self, X):
  class ANN (line 32) | class ANN(object):
    method __init__ (line 33) | def __init__(self, hidden_layer_sizes):
    method fit (line 36) | def fit(self, X, Y, learning_rate=0.01, mu=0.99, epochs=30, batch_sz=1...
    method predict (line 110) | def predict(self, X):
    method forward (line 113) | def forward(self, X):
  function main (line 121) | def main():

FILE: unsupervised_class2/visualize_features.py
  function main (line 20) | def main(loadfile=None, savefile=None):

FILE: unsupervised_class2/xwing.py
  class Layer (line 24) | class Layer(object):
    method __init__ (line 25) | def __init__(self, m1, m2):
    method forward (line 34) | def forward(self, X):
    method forwardT (line 37) | def forwardT(self, X):
  class DeepAutoEncoder (line 41) | class DeepAutoEncoder(object):
    method __init__ (line 42) | def __init__(self, hidden_layer_sizes):
    method fit (line 45) | def fit(self, X, learning_rate=0.5, mu=0.99, epochs=50, batch_sz=100, ...
    method forward (line 92) | def forward(self, X):
  function main (line 108) | def main():

FILE: unsupervised_class3/autoencoder_tf.py
  class Autoencoder (line 14) | class Autoencoder:
    method __init__ (line 15) | def __init__(self, D, M):
    method fit (line 48) | def fit(self, X, epochs=30, batch_sz=64):
    method predict (line 65) | def predict(self, X):
  function main (line 69) | def main():

FILE: unsupervised_class3/autoencoder_theano.py
  class Autoencoder (line 15) | class Autoencoder:
    method __init__ (line 16) | def __init__(self, D, M):
    method fit (line 71) | def fit(self, X, epochs=30, batch_sz=64):
  function main (line 89) | def main():

FILE: unsupervised_class3/bayes_classifier_gaussian.py
  function clamp_sample (line 14) | def clamp_sample(x):
  class BayesClassifier (line 20) | class BayesClassifier:
    method fit (line 21) | def fit(self, X, Y):
    method sample_given_y (line 37) | def sample_given_y(self, y):
    method sample (line 41) | def sample(self):

FILE: unsupervised_class3/bayes_classifier_gmm.py
  function clamp_sample (line 14) | def clamp_sample(x):
  class BayesClassifier (line 20) | class BayesClassifier:
    method fit (line 21) | def fit(self, X, Y):
    method sample_given_y (line 37) | def sample_given_y(self, y):
    method sample (line 50) | def sample(self):

FILE: unsupervised_class3/dcgan_tf.py
  function lrelu (line 30) | def lrelu(x, alpha=0.2):
  class ConvLayer (line 34) | class ConvLayer:
    method __init__ (line 35) | def __init__(self, name, mi, mo, apply_batch_norm, filtersz=5, stride=...
    method forward (line 57) | def forward(self, X, reuse, is_training):
  class FractionallyStridedConvLayer (line 82) | class FractionallyStridedConvLayer:
    method __init__ (line 83) | def __init__(self, name, mi, mo, output_shape, apply_batch_norm, filte...
    method forward (line 107) | def forward(self, X, reuse, is_training):
  class DenseLayer (line 132) | class DenseLayer(object):
    method __init__ (line 133) | def __init__(self, name, M1, M2, apply_batch_norm, f=tf.nn.relu):
    method forward (line 149) | def forward(self, X, reuse, is_training):
  class DCGAN (line 167) | class DCGAN:
    method __init__ (line 168) | def __init__(self, img_length, num_colors, d_sizes, g_sizes):
    method build_discriminator (line 258) | def build_discriminator(self, X, d_sizes):
    method d_forward (line 301) | def d_forward(self, X, reuse=None, is_training=True):
    method build_generator (line 313) | def build_generator(self, Z, g_sizes):
    method g_forward (line 375) | def g_forward(self, Z, reuse=None, is_training=True):
    method fit (line 407) | def fit(self, X):
    method sample (line 498) | def sample(self, n):
  function celeb (line 505) | def celeb():
  function mnist (line 542) | def mnist():

FILE: unsupervised_class3/dcgan_theano.py
  function lrelu (line 38) | def lrelu(x, alpha=0.2):
  function adam (line 43) | def adam(params, grads):
  function batch_norm (line 62) | def batch_norm(
  class ConvLayer (line 101) | class ConvLayer:
    method __init__ (line 102) | def __init__(self, mi, mo, apply_batch_norm, filtersz=5, stride=2, f=T...
    method forward (line 124) | def forward(self, X, is_training):
  class FractionallyStridedConvLayer (line 159) | class FractionallyStridedConvLayer:
    method __init__ (line 160) | def __init__(self, mi, mo, output_shape, apply_batch_norm, filtersz=5,...
    method forward (line 184) | def forward(self, X, is_training):
  class DenseLayer (line 214) | class DenseLayer(object):
    method __init__ (line 215) | def __init__(self, M1, M2, apply_batch_norm, f=T.nnet.relu):
    method forward (line 234) | def forward(self, X, is_training):
  class DCGAN (line 257) | class DCGAN:
    method __init__ (line 258) | def __init__(self, img_length, num_colors, d_sizes, g_sizes):
    method build_discriminator (line 336) | def build_discriminator(self, X, d_sizes):
    method d_forward (line 382) | def d_forward(self, X, is_training):
    method build_generator (line 394) | def build_generator(self, Z, g_sizes):
    method g_forward (line 468) | def g_forward(self, Z, is_training):
    method fit (line 506) | def fit(self, X):
    method sample (line 587) | def sample(self, n):
  function celeb (line 593) | def celeb():
  function mnist (line 630) | def mnist():

FILE: unsupervised_class3/parameterize_guassian.py
  function softplus (line 13) | def softplus(x):
  function forward (line 31) | def forward(x, W1, W2):

FILE: unsupervised_class3/util.py
  function imread (line 20) | def imread(fn):
  function imsave (line 24) | def imsave(fn, arr):
  function imresize (line 28) | def imresize(arr, sz):
  function get_mnist (line 39) | def get_mnist(limit=None):
  function get_celeb (line 59) | def get_celeb(limit=None):
  function crop_and_resave (line 104) | def crop_and_resave(inputfile, outputdir):
  function scale_image (line 120) | def scale_image(im):
  function files2images_theano (line 125) | def files2images_theano(filenames):
  function files2images (line 132) | def files2images(filenames):
  function save_response_content (line 137) | def save_response_content(r, dest):
  function get_confirm_token (line 153) | def get_confirm_token(response):
  function download_file (line 160) | def download_file(file_id, dest):

FILE: unsupervised_class3/vae_tf.py
  class DenseLayer (line 27) | class DenseLayer(object):
    method __init__ (line 28) | def __init__(self, M1, M2, f=tf.nn.relu):
    method forward (line 36) | def forward(self, X):
  class VariationalAutoencoder (line 40) | class VariationalAutoencoder:
    method __init__ (line 41) | def __init__(self, D, hidden_layer_sizes):
    method fit (line 202) | def fit(self, X, epochs=30, batch_sz=64):
    method transform (line 219) | def transform(self, X):
    method prior_predictive_with_input (line 225) | def prior_predictive_with_input(self, Z):
    method posterior_predictive_sample (line 231) | def posterior_predictive_sample(self, X):
    method prior_predictive_sample_with_probs (line 235) | def prior_predictive_sample_with_probs(self):
  function main (line 240) | def main():

FILE: unsupervised_class3/vae_theano.py
  class DenseLayer (line 17) | class DenseLayer(object):
    method __init__ (line 18) | def __init__(self, M1, M2, f=T.nnet.relu):
    method forward (line 24) | def forward(self, X):
  class VariationalAutoencoder (line 28) | class VariationalAutoencoder:
    method __init__ (line 29) | def __init__(self, D, hidden_layer_sizes):
    method fit (line 197) | def fit(self, X, epochs=30, batch_sz=64):
  function main (line 215) | def main():
Copy disabled (too large) Download .json
Condensed preview — 556 files, each showing path, character count, and a content snippet. Download the .json file for the full structured content (14,310K chars).
[
  {
    "path": ".gitignore",
    "chars": 65,
    "preview": "*.DS_Store\n*.pyc\nlarge_files\nlarge_files/*\nnlp_class2/chunking/*\n"
  },
  {
    "path": "README.md",
    "chars": 7311,
    "preview": "machine_learning_examples\n=========================\n\nA collection of machine learning examples and tutorials.\n\nFind asso"
  },
  {
    "path": "ab_testing/advertisement_clicks.csv",
    "chars": 8024,
    "preview": "advertisement_id,action\nB,1\nB,1\nA,0\nB,0\nA,1\nA,0\nB,0\nA,1\nB,0\nA,0\nB,1\nA,0\nB,1\nB,0\nB,1\nB,1\nB,0\nA,0\nB,0\nB,0\nB,0\nA,0\nA,1\nA,1\n"
  },
  {
    "path": "ab_testing/bayesian_bandit.py",
    "chars": 1937,
    "preview": "# From the course: Bayesin Machine Learning in Python: A/B Testing\n# https://deeplearningcourses.com/c/bayesian-machine-"
  },
  {
    "path": "ab_testing/bayesian_normal.py",
    "chars": 2122,
    "preview": "# https://deeplearningcourses.com/c/artificial-intelligence-reinforcement-learning-in-python\n# https://www.udemy.com/art"
  },
  {
    "path": "ab_testing/bayesian_starter.py",
    "chars": 1925,
    "preview": "# From the course: Bayesin Machine Learning in Python: A/B Testing\n# https://deeplearningcourses.com/c/bayesian-machine-"
  },
  {
    "path": "ab_testing/cdfs_and_percentiles.py",
    "chars": 639,
    "preview": "import numpy as np\nimport matplotlib.pyplot as plt\nfrom scipy.stats import norm\n\n\nmu = 170\nsd = 7\n\n\n# generate samples f"
  },
  {
    "path": "ab_testing/chisquare.py",
    "chars": 1973,
    "preview": "# From the course: Bayesin Machine Learning in Python: A/B Testing\n# https://deeplearningcourses.com/c/bayesian-machine-"
  },
  {
    "path": "ab_testing/ci_comparison.py",
    "chars": 1201,
    "preview": "# From the course: Bayesin Machine Learning in Python: A/B Testing\n# https://deeplearningcourses.com/c/bayesian-machine-"
  },
  {
    "path": "ab_testing/client.py",
    "chars": 1302,
    "preview": "# From the course: Bayesin Machine Learning in Python: A/B Testing\n# https://deeplearningcourses.com/c/bayesian-machine-"
  },
  {
    "path": "ab_testing/comparing_epsilons.py",
    "chars": 2156,
    "preview": "# https://deeplearningcourses.com/c/artificial-intelligence-reinforcement-learning-in-python\n# https://www.udemy.com/art"
  },
  {
    "path": "ab_testing/convergence.py",
    "chars": 1056,
    "preview": "# From the course: Bayesin Machine Learning in Python: A/B Testing\n# https://deeplearningcourses.com/c/bayesian-machine-"
  },
  {
    "path": "ab_testing/demo.py",
    "chars": 982,
    "preview": "# From the course: Bayesin Machine Learning in Python: A/B Testing\n# https://deeplearningcourses.com/c/bayesian-machine-"
  },
  {
    "path": "ab_testing/epsilon_greedy.py",
    "chars": 2458,
    "preview": "# From the course: Bayesin Machine Learning in Python: A/B Testing\n# https://deeplearningcourses.com/c/bayesian-machine-"
  },
  {
    "path": "ab_testing/epsilon_greedy_starter.py",
    "chars": 2220,
    "preview": "# From the course: Bayesin Machine Learning in Python: A/B Testing\n# https://deeplearningcourses.com/c/bayesian-machine-"
  },
  {
    "path": "ab_testing/ex_chisq.py",
    "chars": 1182,
    "preview": "# From the course: Bayesin Machine Learning in Python: A/B Testing\n# https://deeplearningcourses.com/c/bayesian-machine-"
  },
  {
    "path": "ab_testing/ex_ttest.py",
    "chars": 1229,
    "preview": "# From the course: Bayesin Machine Learning in Python: A/B Testing\n# https://deeplearningcourses.com/c/bayesian-machine-"
  },
  {
    "path": "ab_testing/extra_reading.txt",
    "chars": 792,
    "preview": "The Unbiased Estimate of the Covariance Matrix\nhttps://lazyprogrammer.me/covariance-matrix-divide-by-n-or-n-1/\n\nAlgorith"
  },
  {
    "path": "ab_testing/optimistic.py",
    "chars": 1914,
    "preview": "# From the course: Bayesin Machine Learning in Python: A/B Testing\n# https://deeplearningcourses.com/c/bayesian-machine-"
  },
  {
    "path": "ab_testing/optimistic_starter.py",
    "chars": 1812,
    "preview": "# From the course: Bayesin Machine Learning in Python: A/B Testing\n# https://deeplearningcourses.com/c/bayesian-machine-"
  },
  {
    "path": "ab_testing/server_solution.py",
    "chars": 1728,
    "preview": "# From the course: Bayesin Machine Learning in Python: A/B Testing\n# https://deeplearningcourses.com/c/bayesian-machine-"
  },
  {
    "path": "ab_testing/server_starter.py",
    "chars": 1306,
    "preview": "# From the course: Bayesin Machine Learning in Python: A/B Testing\n# https://deeplearningcourses.com/c/bayesian-machine-"
  },
  {
    "path": "ab_testing/ttest.py",
    "chars": 1020,
    "preview": "# From the course: Bayesin Machine Learning in Python: A/B Testing\n# https://deeplearningcourses.com/c/bayesian-machine-"
  },
  {
    "path": "ab_testing/ucb1.py",
    "chars": 2198,
    "preview": "# https://deeplearningcourses.com/c/artificial-intelligence-reinforcement-learning-in-python\n# https://www.udemy.com/art"
  },
  {
    "path": "ab_testing/ucb1_starter.py",
    "chars": 2113,
    "preview": "# https://deeplearningcourses.com/c/artificial-intelligence-reinforcement-learning-in-python\n# https://www.udemy.com/art"
  },
  {
    "path": "airline/ann.py",
    "chars": 5556,
    "preview": "# The corresponding tutorial for this code was released EXCLUSIVELY as a bonus\n# If you want to learn about future bonus"
  },
  {
    "path": "airline/international-airline-passengers.csv",
    "chars": 2334,
    "preview": "\"Month\",\"International airline passengers: monthly totals in thousands. Jan 49 ? Dec 60\"\r\n\"1949-01\",112\r\n\"1949-02\",118\r\n"
  },
  {
    "path": "airline/lr.py",
    "chars": 1799,
    "preview": "# The corresponding tutorial for this code was released EXCLUSIVELY as a bonus\n# If you want to learn about future bonus"
  },
  {
    "path": "airline/rnn.py",
    "chars": 5069,
    "preview": "# The corresponding tutorial for this code was released EXCLUSIVELY as a bonus\n# If you want to learn about future bonus"
  },
  {
    "path": "ann_class/backprop.py",
    "chars": 4203,
    "preview": "# backpropagation example for deep learning in python class.\n# with sigmoid activation\n#\n# the notes for this class can "
  },
  {
    "path": "ann_class/extra_reading.txt",
    "chars": 485,
    "preview": "The Chain Rule of Calculus\nhttp://tutorial.math.lamar.edu/Classes/CalcI/ChainRule.aspx\n\nYes you should understand backpr"
  },
  {
    "path": "ann_class/forwardprop.py",
    "chars": 1828,
    "preview": "# forward propagation example for deep learning in python class.\n#\n# the notes for this class can be found at: \n# https:"
  },
  {
    "path": "ann_class/regression.py",
    "chars": 3035,
    "preview": "from __future__ import print_function, division\nfrom builtins import range\n# Note: you may need to update your version o"
  },
  {
    "path": "ann_class/sklearn_ann.py",
    "chars": 961,
    "preview": "# Train a neural network in just 3 lines of code!\n#\n# the notes for this class can be found at: \n# https://deeplearningc"
  },
  {
    "path": "ann_class/tf_example.py",
    "chars": 2459,
    "preview": "# neural network in TensorFlow very simple example.\n#\n# the notes for this class can be found at: \n# https://deeplearnin"
  },
  {
    "path": "ann_class/xor_donut.py",
    "chars": 4513,
    "preview": "# revisiting the XOR and donut problems to show how features\n# can be learned automatically using neural networks.\n#\n# t"
  },
  {
    "path": "ann_class2/__init__.py",
    "chars": 0,
    "preview": ""
  },
  {
    "path": "ann_class2/adam.py",
    "chars": 6076,
    "preview": "# Compare RMSprop with momentum vs. Adam\n# For the class Data Science: Practical Deep Learning Concepts in Theano and Te"
  },
  {
    "path": "ann_class2/batch_norm_tf.py",
    "chars": 5711,
    "preview": "from __future__ import print_function, division\nfrom builtins import range\n# Note: you may need to update your version o"
  },
  {
    "path": "ann_class2/batch_norm_theano.py",
    "chars": 5905,
    "preview": "from __future__ import print_function, division\nfrom builtins import range\n# Note: you may need to update your version o"
  },
  {
    "path": "ann_class2/cntk_example.py",
    "chars": 3277,
    "preview": "# https://deeplearningcourses.com/c/data-science-deep-learning-in-theano-tensorflow\n# https://www.udemy.com/data-science"
  },
  {
    "path": "ann_class2/dropout_tensorflow.py",
    "chars": 5004,
    "preview": "from __future__ import print_function, division\nfrom builtins import range\n# Note: you may need to update your version o"
  },
  {
    "path": "ann_class2/dropout_theano.py",
    "chars": 5111,
    "preview": "# A 1-hidden-layer neural network in Theano.\n# This code is not optimized for speed.\n# It's just to get something workin"
  },
  {
    "path": "ann_class2/extra_reading.txt",
    "chars": 1240,
    "preview": "The Marginal Value of Adaptive Gradient Methods in Machine Learning\nhttps://arxiv.org/abs/1705.08292\n\nAsynchronous Stoch"
  },
  {
    "path": "ann_class2/grid_search.py",
    "chars": 2605,
    "preview": "# For the class Data Science: Practical Deep Learning Concepts in Theano and TensorFlow\n# https://deeplearningcourses.co"
  },
  {
    "path": "ann_class2/keras_example.py",
    "chars": 2334,
    "preview": "# https://deeplearningcourses.com/c/data-science-deep-learning-in-theano-tensorflow\n# https://www.udemy.com/data-science"
  },
  {
    "path": "ann_class2/keras_functional.py",
    "chars": 2207,
    "preview": "# https://deeplearningcourses.com/c/data-science-deep-learning-in-theano-tensorflow\n# https://www.udemy.com/data-science"
  },
  {
    "path": "ann_class2/mlp.py",
    "chars": 1207,
    "preview": "# Simple multi-layer preceptron / neural network in Python and Numpy\n# For the class Data Science: Practical Deep Learni"
  },
  {
    "path": "ann_class2/momentum.py",
    "chars": 6320,
    "preview": "# Compare momentum with regular gradient descent\n# For the class Data Science: Practical Deep Learning Concepts in Thean"
  },
  {
    "path": "ann_class2/mxnet_example.py",
    "chars": 2621,
    "preview": "# https://deeplearningcourses.com/c/data-science-deep-learning-in-theano-tensorflow\n# https://www.udemy.com/data-science"
  },
  {
    "path": "ann_class2/pytorch_batchnorm.py",
    "chars": 4990,
    "preview": "# https://deeplearningcourses.com/c/data-science-deep-learning-in-theano-tensorflow\n# https://www.udemy.com/data-science"
  },
  {
    "path": "ann_class2/pytorch_dropout.py",
    "chars": 5078,
    "preview": "# https://deeplearningcourses.com/c/data-science-deep-learning-in-theano-tensorflow\n# https://www.udemy.com/data-science"
  },
  {
    "path": "ann_class2/pytorch_example.py",
    "chars": 3740,
    "preview": "# https://deeplearningcourses.com/c/data-science-deep-learning-in-theano-tensorflow\n# https://www.udemy.com/data-science"
  },
  {
    "path": "ann_class2/pytorch_example2.py",
    "chars": 4598,
    "preview": "# https://deeplearningcourses.com/c/data-science-deep-learning-in-theano-tensorflow\n# https://www.udemy.com/data-science"
  },
  {
    "path": "ann_class2/random_search.py",
    "chars": 2306,
    "preview": "# For the class Data Science: Practical Deep Learning Concepts in Theano and TensorFlow\n# https://deeplearningcourses.co"
  },
  {
    "path": "ann_class2/rmsprop.py",
    "chars": 4666,
    "preview": "# Compare RMSprop vs. constant learning rate\n# For the class Data Science: Practical Deep Learning Concepts in Theano an"
  },
  {
    "path": "ann_class2/rmsprop_test.py",
    "chars": 2953,
    "preview": "# https://deeplearningcourses.com/c/data-science-deep-learning-in-theano-tensorflow\n# https://www.udemy.com/data-science"
  },
  {
    "path": "ann_class2/sgd.py",
    "chars": 5387,
    "preview": "# In this file we compare the progression of the cost function vs. iteration\n# for 3 cases:\n# 1) full gradient descent\n#"
  },
  {
    "path": "ann_class2/tensorflow1.py",
    "chars": 3129,
    "preview": "# This tutorial is analogous to theano1.py\n# It introduces basic variables and functions\n# and shows how you can optimiz"
  },
  {
    "path": "ann_class2/tensorflow2.py",
    "chars": 3784,
    "preview": "# 2-hidden layer NN in TensorFlow\n# This code is not optimized for speed.\n# It's just to get something working, using th"
  },
  {
    "path": "ann_class2/tf_with_save.py",
    "chars": 4268,
    "preview": "# https://deeplearningcourses.com/c/data-science-deep-learning-in-theano-tensorflow\n# https://www.udemy.com/data-science"
  },
  {
    "path": "ann_class2/theano1.py",
    "chars": 1841,
    "preview": "# Theano basics.\n# For the class Data Science: Practical Deep Learning Concepts in Theano and TensorFlow\n# https://deepl"
  },
  {
    "path": "ann_class2/theano2.py",
    "chars": 3543,
    "preview": "# A 1-hidden-layer neural network in Theano.\n# This code is not optimized for speed.\n# It's just to get something workin"
  },
  {
    "path": "ann_class2/theano_ann.py",
    "chars": 3859,
    "preview": "# For the class Data Science: Practical Deep Learning Concepts in Theano and TensorFlow\n# https://deeplearningcourses.co"
  },
  {
    "path": "ann_class2/util.py",
    "chars": 8067,
    "preview": "from __future__ import print_function, division\nfrom builtins import range\n# Note: you may need to update your version o"
  },
  {
    "path": "ann_logistic_extra/__init__.py",
    "chars": 0,
    "preview": ""
  },
  {
    "path": "ann_logistic_extra/ann_predict.py",
    "chars": 867,
    "preview": "from __future__ import print_function, division\nfrom builtins import range\n# Note: you may need to update your version o"
  },
  {
    "path": "ann_logistic_extra/ann_train.py",
    "chars": 2214,
    "preview": "from __future__ import print_function, division\nfrom builtins import range\n# Note: you may need to update your version o"
  },
  {
    "path": "ann_logistic_extra/ecommerce_data.csv",
    "chars": 12381,
    "preview": "is_mobile,n_products_viewed,visit_duration,is_returning_visitor,time_of_day,user_action\n1,0,0.657509946224,0,3,0\n1,1,0.5"
  },
  {
    "path": "ann_logistic_extra/logistic_predict.py",
    "chars": 663,
    "preview": "from __future__ import print_function, division\nfrom builtins import range\n# Note: you may need to update your version o"
  },
  {
    "path": "ann_logistic_extra/logistic_softmax_train.py",
    "chars": 1848,
    "preview": "from __future__ import print_function, division\nfrom builtins import range\n# Note: you may need to update your version o"
  },
  {
    "path": "ann_logistic_extra/logistic_train.py",
    "chars": 1524,
    "preview": "from __future__ import print_function, division\nfrom builtins import range\n# Note: you may need to update your version o"
  },
  {
    "path": "ann_logistic_extra/process.py",
    "chars": 1831,
    "preview": "from __future__ import print_function, division\nfrom builtins import range\n# Note: you may need to update your version o"
  },
  {
    "path": "bayesian_ml/1/Q.csv",
    "chars": 121984,
    "preview": "-7.4867e-19,5.6315e-19,1.973e-19,-6.7717e-19,2.373e-18,-1.4162e-19,-5.6552e-19,-1.2146e-19,7.3088e-19,5.5656e-19,1.2218e"
  },
  {
    "path": "bayesian_ml/1/README",
    "chars": 822,
    "preview": "This data set contains the following:\n\nXtrain: 11791 training examples of 4's and 9's, each 15 dimensions in length\nytra"
  },
  {
    "path": "bayesian_ml/1/Xtest.csv",
    "chars": 240996,
    "preview": "3.7638,-0.92236,-0.53047,1.233,-0.71848,1.525,-1.3769,-1.551,-0.17975,-1.1499,-0.11756,-1.5398,0.34207,-0.60512,0.065985"
  },
  {
    "path": "bayesian_ml/1/Xtrain.csv",
    "chars": 1425178,
    "preview": "2.5666,-1.3258,-2.3719,-0.81289,-1.7141,0.7571,-1.3487,1.482,1.7461,-0.623,1.1981,-0.059047,0.71099,-0.091248,-0.80487\n-"
  },
  {
    "path": "bayesian_ml/1/nb.py",
    "chars": 4428,
    "preview": "# Naive Bayes with prior on mean and precision of Gaussian\n# mean | precision ~ N(0, c / precision)\n# precision ~ Gamma("
  },
  {
    "path": "bayesian_ml/1/ytest.csv",
    "chars": 3982,
    "preview": "0\n0\n0\n0\n0\n0\n0\n0\n0\n0\n0\n0\n0\n0\n0\n0\n0\n0\n0\n0\n0\n0\n0\n0\n0\n0\n0\n0\n0\n0\n0\n0\n0\n0\n0\n0\n0\n0\n0\n0\n0\n0\n0\n0\n0\n0\n0\n0\n0\n0\n0\n0\n0\n0\n0\n0\n0\n0\n0\n0\n"
  },
  {
    "path": "bayesian_ml/1/ytrain.csv",
    "chars": 23582,
    "preview": "0\n0\n0\n0\n0\n0\n0\n0\n0\n0\n0\n0\n0\n0\n0\n0\n0\n0\n0\n0\n0\n0\n0\n0\n0\n0\n0\n0\n0\n0\n0\n0\n0\n0\n0\n0\n0\n0\n0\n0\n0\n0\n0\n0\n0\n0\n0\n0\n0\n0\n0\n0\n0\n0\n0\n0\n0\n0\n0\n0\n"
  },
  {
    "path": "bayesian_ml/2/Q.csv",
    "chars": 121984,
    "preview": "-7.4867e-19,5.6315e-19,1.973e-19,-6.7717e-19,2.373e-18,-1.4162e-19,-5.6552e-19,-1.2146e-19,7.3088e-19,5.5656e-19,1.2218e"
  },
  {
    "path": "bayesian_ml/2/README",
    "chars": 822,
    "preview": "This data set contains the following:\n\nXtrain: 11791 training examples of 4's and 9's, each 15 dimensions in length\nytra"
  },
  {
    "path": "bayesian_ml/2/Xtest.csv",
    "chars": 240996,
    "preview": "3.7638,-0.92236,-0.53047,1.233,-0.71848,1.525,-1.3769,-1.551,-0.17975,-1.1499,-0.11756,-1.5398,0.34207,-0.60512,0.065985"
  },
  {
    "path": "bayesian_ml/2/Xtrain.csv",
    "chars": 1425178,
    "preview": "2.5666,-1.3258,-2.3719,-0.81289,-1.7141,0.7571,-1.3487,1.482,1.7461,-0.623,1.1981,-0.059047,0.71099,-0.091248,-0.80487\n-"
  },
  {
    "path": "bayesian_ml/2/em.py",
    "chars": 1623,
    "preview": "# expectation-maximization for the model:\n# x(n) ~ N(Wz(n), sigma**2 I) (observed variables)\n# z(n) ~ N(0, I) (latent va"
  },
  {
    "path": "bayesian_ml/2/probit.py",
    "chars": 3706,
    "preview": "# probit regression\n\nimport numpy as np\nimport pandas as pd\nimport matplotlib.pyplot as plt\nfrom scipy.stats import norm"
  },
  {
    "path": "bayesian_ml/2/ytest.csv",
    "chars": 3982,
    "preview": "0\n0\n0\n0\n0\n0\n0\n0\n0\n0\n0\n0\n0\n0\n0\n0\n0\n0\n0\n0\n0\n0\n0\n0\n0\n0\n0\n0\n0\n0\n0\n0\n0\n0\n0\n0\n0\n0\n0\n0\n0\n0\n0\n0\n0\n0\n0\n0\n0\n0\n0\n0\n0\n0\n0\n0\n0\n0\n0\n0\n"
  },
  {
    "path": "bayesian_ml/2/ytrain.csv",
    "chars": 23582,
    "preview": "0\n0\n0\n0\n0\n0\n0\n0\n0\n0\n0\n0\n0\n0\n0\n0\n0\n0\n0\n0\n0\n0\n0\n0\n0\n0\n0\n0\n0\n0\n0\n0\n0\n0\n0\n0\n0\n0\n0\n0\n0\n0\n0\n0\n0\n0\n0\n0\n0\n0\n0\n0\n0\n0\n0\n0\n0\n0\n0\n0\n"
  },
  {
    "path": "bayesian_ml/3/X_set1.csv",
    "chars": 97266,
    "preview": "1,1,0.99994,0.99458,0.97123,0.95636,0.9371,0.83213,0.82584,0.79677,0.78811,0.78538,0.76095,0.71788,0.67846,0.66621,0.476"
  },
  {
    "path": "bayesian_ml/3/X_set2.csv",
    "chars": 610112,
    "preview": "1,1,0.99977,0.99944,0.99721,0.99195,0.99179,0.98993,0.98053,0.95135,0.94458,0.94179,0.94169,0.93015,0.90749,0.90432,0.88"
  },
  {
    "path": "bayesian_ml/3/X_set3.csv",
    "chars": 2438434,
    "preview": "1,1,0.99878,0.99866,0.99778,0.99695,0.99567,0.99253,0.98174,0.97249,0.97091,0.96204,0.96049,0.95403,0.93645,0.93041,0.92"
  },
  {
    "path": "bayesian_ml/3/run.py",
    "chars": 3993,
    "preview": "# variational-inference for linear regression\n# y(i) ~ N( x(i).dot(w), 1/lambda )\n# w ~ N( 0, diag(alpha_1, alpha_2, ..."
  },
  {
    "path": "bayesian_ml/3/y_set1.csv",
    "chars": 784,
    "preview": "-1.9807\n1.3977\n0.39627\n0.11422\n0.59669\n1.6194\n1.9997\n0.27002\n-1.2381\n0.71259\n1.1677\n-0.56528\n-0.16157\n1.4075\n1.3544\n-0.0"
  },
  {
    "path": "bayesian_ml/3/y_set2.csv",
    "chars": 1961,
    "preview": "1.0719\n0.11583\n0.93244\n0.57572\n0.31694\n0.83109\n-1.0948\n-0.96628\n-0.70567\n-0.42234\n0.50279\n-0.62063\n1.4233\n0.15329\n0.2971"
  },
  {
    "path": "bayesian_ml/3/y_set3.csv",
    "chars": 3901,
    "preview": "1.2493\n0.34608\n-0.65115\n-0.65096\n0.43414\n1.8492\n-0.4061\n-0.38145\n-1.2443\n1.1232\n-0.20684\n0.39811\n2.4196\n0.030414\n0.81584"
  },
  {
    "path": "bayesian_ml/3/z_set1.csv",
    "chars": 772,
    "preview": "-4.9721\n-4.9644\n-4.8984\n-4.8012\n-4.7609\n-4.7172\n-4.5434\n-4.5347\n-4.4954\n-4.4841\n-4.4806\n-4.4494\n-4.3964\n-4.3493\n-4.3348\n"
  },
  {
    "path": "bayesian_ml/3/z_set2.csv",
    "chars": 1903,
    "preview": "-4.9765\n-4.9613\n-4.9528\n-4.9236\n-4.8866\n-4.8857\n-4.8759\n-4.8363\n-4.7532\n-4.7377\n-4.7316\n-4.7314\n-4.7074\n-4.6649\n-4.6594\n"
  },
  {
    "path": "bayesian_ml/3/z_set3.csv",
    "chars": 3796,
    "preview": "-4.9868\n-4.9519\n-4.9502\n-4.9396\n-4.9315\n-4.9209\n-4.9002\n-4.8511\n-4.8198\n-4.815\n-4.7901\n-4.786\n-4.7699\n-4.7306\n-4.7182\n-4"
  },
  {
    "path": "bayesian_ml/4/data.txt",
    "chars": 3771,
    "preview": "4.1018,2.7755\n-2.7843,-2.5328\n4.9085,1.7415\n-0.98785,-0.096165\n5.0936,1.4871\n-3.2698,-2.0138\n4.322,1.7522\n1.1916,1.0932\n"
  },
  {
    "path": "bayesian_ml/4/emgmm.py",
    "chars": 1916,
    "preview": "# GMM using Expectation-Maximization\n\nimport numpy as np\nimport pandas as pd\nimport matplotlib.pyplot as plt\n\nfrom scipy"
  },
  {
    "path": "bayesian_ml/4/npbgmm.py",
    "chars": 6509,
    "preview": "# GMM using Bayesian Nonparametric Clustering\n# Gaussian Mixture Model\n# Dirichlet Process\n# Gibbs Sampling\n\nimport nump"
  },
  {
    "path": "bayesian_ml/4/vigmm.py",
    "chars": 6380,
    "preview": "# GMM using Variational Inference\n\nimport numpy as np\nimport pandas as pd\nimport matplotlib.pyplot as plt\n\nfrom scipy im"
  },
  {
    "path": "best_fit_line.py",
    "chars": 1100,
    "preview": "from pulp import *\n\n### remove variable b because it is unconstrained\n### it's just a linear combination of the others\n#"
  },
  {
    "path": "calculus/WHERE ARE THE NOTEBOOKS.txt",
    "chars": 299,
    "preview": "As stated in the \"where to get the code\" / \"where to get the notebooks\" lecture, the notebooks are NOT on Github.\n\nIf yo"
  },
  {
    "path": "calculus/extra_reading.txt",
    "chars": 55,
    "preview": "Calculus: Early Transcendentals\nhttps://amzn.to/3Kwmabe"
  },
  {
    "path": "chatgpt_trading/WHERE ARE THE NOTEBOOKS.txt",
    "chars": 299,
    "preview": "As stated in the \"where to get the code\" / \"where to get the notebooks\" lecture, the notebooks are NOT on Github.\n\nIf yo"
  },
  {
    "path": "chatgpt_trading/extra_reading.txt",
    "chars": 149,
    "preview": "ARIMA (for mean reversion)\nhttps://deeplearningcourses.com/c/time-series-analysis\n\nFinancial Engineering\nhttps://deeplea"
  },
  {
    "path": "cnn_class/WHERE ARE THE NOTEBOOKS.txt",
    "chars": 299,
    "preview": "As stated in the \"where to get the code\" / \"where to get the notebooks\" lecture, the notebooks are NOT on Github.\n\nIf yo"
  },
  {
    "path": "cnn_class/benchmark.py",
    "chars": 4615,
    "preview": "# Vanilla deep network\n# https://deeplearningcourses.com/c/deep-learning-convolutional-neural-networks-theano-tensorflow"
  },
  {
    "path": "cnn_class/blur.py",
    "chars": 1602,
    "preview": "# https://deeplearningcourses.com/c/deep-learning-convolutional-neural-networks-theano-tensorflow\n# https://udemy.com/de"
  },
  {
    "path": "cnn_class/cifar.py",
    "chars": 7120,
    "preview": "# https://deeplearningcourses.com/c/deep-learning-convolutional-neural-networks-theano-tensorflow\nimport os\nimport numpy"
  },
  {
    "path": "cnn_class/cnn_tf.py",
    "chars": 7243,
    "preview": "# New concepts and differences from Theano:\n# - stride is the interval at which to apply the convolution\n# - unlike prev"
  },
  {
    "path": "cnn_class/cnn_tf_plot_filters.py",
    "chars": 6822,
    "preview": "# New concepts and differences from Theano:\n# - stride is the interval at which to apply the convolution\n# - unlike prev"
  },
  {
    "path": "cnn_class/cnn_theano.py",
    "chars": 5684,
    "preview": "# https://deeplearningcourses.com/c/deep-learning-convolutional-neural-networks-theano-tensorflow\n# https://udemy.com/de"
  },
  {
    "path": "cnn_class/cnn_theano_plot_filters.py",
    "chars": 6915,
    "preview": "# https://deeplearningcourses.com/c/deep-learning-convolutional-neural-networks-theano-tensorflow\n# https://udemy.com/de"
  },
  {
    "path": "cnn_class/custom_blur.py",
    "chars": 2744,
    "preview": "# https://deeplearningcourses.com/c/deep-learning-convolutional-neural-networks-theano-tensorflow\n# https://udemy.com/de"
  },
  {
    "path": "cnn_class/echo.py",
    "chars": 1689,
    "preview": "# https://deeplearningcourses.com/c/deep-learning-convolutional-neural-networks-theano-tensorflow\n# https://udemy.com/de"
  },
  {
    "path": "cnn_class/edge.py",
    "chars": 1138,
    "preview": "# https://deeplearningcourses.com/c/deep-learning-convolutional-neural-networks-theano-tensorflow\n# https://udemy.com/de"
  },
  {
    "path": "cnn_class/edge_benchmark.py",
    "chars": 4091,
    "preview": "# Vanilla deep network\n# https://deeplearningcourses.com/c/deep-learning-convolutional-neural-networks-theano-tensorflow"
  },
  {
    "path": "cnn_class/exercises.txt",
    "chars": 760,
    "preview": "Logistic Regression\nhttps://www.kaggle.com/uciml/pima-indians-diabetes-database\nhttps://lazyprogrammer.me/course_files/e"
  },
  {
    "path": "cnn_class/extra_reading.txt",
    "chars": 695,
    "preview": "Gradient-Based Learning Applied to Document Recognition\nhttp://yann.lecun.com/exdb/publis/pdf/lecun-01a.pdf\n\nImageNet Cl"
  },
  {
    "path": "cnn_class/keras_example.py",
    "chars": 2912,
    "preview": "# https://deeplearningcourses.com/c/advanced-computer-vision\n# https://www.udemy.com/advanced-computer-vision\n\nfrom __fu"
  },
  {
    "path": "cnn_class2/WHERE ARE THE NOTEBOOKS.txt",
    "chars": 299,
    "preview": "As stated in the \"where to get the code\" / \"where to get the notebooks\" lecture, the notebooks are NOT on Github.\n\nIf yo"
  },
  {
    "path": "cnn_class2/class_activation_maps.py",
    "chars": 2392,
    "preview": "# https://deeplearningcourses.com/c/advanced-computer-vision\n# https://www.udemy.com/advanced-computer-vision\n\nfrom __fu"
  },
  {
    "path": "cnn_class2/extra_reading.txt",
    "chars": 622,
    "preview": "A Neural Algorithm of Artistic Style\nhttps://arxiv.org/abs/1508.06576\n\nSSD: Single Shot MultiBox Detector\nhttps://arxiv."
  },
  {
    "path": "cnn_class2/fashion.py",
    "chars": 2728,
    "preview": "# https://deeplearningcourses.com/c/advanced-computer-vision\n# https://www.udemy.com/advanced-computer-vision\n\nfrom __fu"
  },
  {
    "path": "cnn_class2/fashion2.py",
    "chars": 2486,
    "preview": "# https://deeplearningcourses.com/c/advanced-computer-vision\n# https://www.udemy.com/advanced-computer-vision\n\nfrom __fu"
  },
  {
    "path": "cnn_class2/make_limited_datasets.py",
    "chars": 928,
    "preview": "# https://deeplearningcourses.com/c/advanced-computer-vision\n# https://www.udemy.com/advanced-computer-vision\nimport os\n"
  },
  {
    "path": "cnn_class2/siamese.py",
    "chars": 11838,
    "preview": "# https://deeplearningcourses.com/c/advanced-computer-vision\nfrom __future__ import print_function, division\nfrom builti"
  },
  {
    "path": "cnn_class2/ssd.py",
    "chars": 4654,
    "preview": "# https://deeplearningcourses.com/c/advanced-computer-vision\n# https://www.udemy.com/advanced-computer-vision\n\n# simple "
  },
  {
    "path": "cnn_class2/style_transfer1.py",
    "chars": 4792,
    "preview": "# https://deeplearningcourses.com/c/advanced-computer-vision\n# https://www.udemy.com/advanced-computer-vision\n\nfrom __fu"
  },
  {
    "path": "cnn_class2/style_transfer2.py",
    "chars": 4117,
    "preview": "# https://deeplearningcourses.com/c/advanced-computer-vision\n# https://www.udemy.com/advanced-computer-vision\n\nfrom __fu"
  },
  {
    "path": "cnn_class2/style_transfer3.py",
    "chars": 3932,
    "preview": "# https://deeplearningcourses.com/c/advanced-computer-vision\n# https://www.udemy.com/advanced-computer-vision\n\nfrom __fu"
  },
  {
    "path": "cnn_class2/test_softmax.py",
    "chars": 832,
    "preview": "# https://deeplearningcourses.com/c/advanced-computer-vision\n# https://www.udemy.com/advanced-computer-vision\nfrom __fut"
  },
  {
    "path": "cnn_class2/tf_resnet.py",
    "chars": 7690,
    "preview": "# https://deeplearningcourses.com/c/advanced-computer-vision\n# https://www.udemy.com/advanced-computer-vision\nfrom __fut"
  },
  {
    "path": "cnn_class2/tf_resnet_convblock.py",
    "chars": 6240,
    "preview": "# https://deeplearningcourses.com/c/advanced-computer-vision\n# https://www.udemy.com/advanced-computer-vision\nfrom __fut"
  },
  {
    "path": "cnn_class2/tf_resnet_convblock_starter.py",
    "chars": 765,
    "preview": "# https://deeplearningcourses.com/c/advanced-computer-vision\n# https://www.udemy.com/advanced-computer-vision\nfrom __fut"
  },
  {
    "path": "cnn_class2/tf_resnet_first_layers.py",
    "chars": 4631,
    "preview": "# https://deeplearningcourses.com/c/advanced-computer-vision\n# https://www.udemy.com/advanced-computer-vision\nfrom __fut"
  },
  {
    "path": "cnn_class2/tf_resnet_first_layers_starter.py",
    "chars": 2344,
    "preview": "# https://deeplearningcourses.com/c/advanced-computer-vision\n# https://www.udemy.com/advanced-computer-vision\nfrom __fut"
  },
  {
    "path": "cnn_class2/tf_resnet_identity_block.py",
    "chars": 3736,
    "preview": "# https://deeplearningcourses.com/c/advanced-computer-vision\n# https://www.udemy.com/advanced-computer-vision\nfrom __fut"
  },
  {
    "path": "cnn_class2/tf_resnet_identity_block_starter.py",
    "chars": 872,
    "preview": "# https://deeplearningcourses.com/c/advanced-computer-vision\n# https://www.udemy.com/advanced-computer-vision\nfrom __fut"
  },
  {
    "path": "cnn_class2/use_pretrained_weights_resnet.py",
    "chars": 4847,
    "preview": "# https://deeplearningcourses.com/c/advanced-computer-vision\n# https://www.udemy.com/advanced-computer-vision\n\nfrom __fu"
  },
  {
    "path": "cnn_class2/use_pretrained_weights_vgg.py",
    "chars": 4833,
    "preview": "# https://deeplearningcourses.com/c/advanced-computer-vision\n# https://www.udemy.com/advanced-computer-vision\nfrom __fut"
  },
  {
    "path": "cnn_class2/util.py",
    "chars": 1552,
    "preview": "# https://deeplearningcourses.com/advanced-computer-vision\n# https://www.udemy.com/advanced-computer-vision\n\nfrom __futu"
  },
  {
    "path": "data_csv/X.txt",
    "chars": 20057,
    "preview": "1,1.4821,1.0759,0.66329,0.61975,-1.2836,-1.6232\n1,1.4821,1.4868,1.5726,0.84226,-1.4649,-1.6232\n1,1.4821,1.181,1.1829,0.5"
  },
  {
    "path": "data_csv/X_orig.txt",
    "chars": 9240,
    "preview": "1,8,307,130,3504,12,70\n1,8,350,165,3693,11.5,70\n1,8,318,150,3436,11,70\n1,8,304,150,3433,12,70\n1,8,302,140,3449,10.5,70\n1"
  },
  {
    "path": "data_csv/legend.txt",
    "chars": 6713,
    "preview": "chevrolet chevelle malibu\nbuick skylark 320\nplymouth satellite\namc rebel sst\nford torino\nford galaxie 500\nchevrolet impa"
  },
  {
    "path": "data_csv/readme.txt",
    "chars": 590,
    "preview": "___Data information___\n\n- The output we wish to predict is in \"y\":\n\ty: miles per gallon\n\n- Each row in \"X\" contains seve"
  },
  {
    "path": "data_csv/y.txt",
    "chars": 1447,
    "preview": "18\n15\n18\n16\n17\n15\n14\n14\n14\n15\n15\n14\n15\n14\n24\n22\n18\n21\n27\n26\n25\n24\n25\n26\n21\n10\n10\n11\n9\n27\n28\n25\n19\n16\n17\n19\n18\n14\n14\n14\n1"
  },
  {
    "path": "financial_engineering/go_here_instead.txt",
    "chars": 56,
    "preview": "https://github.com/lazyprogrammer/financial_engineering\n"
  },
  {
    "path": "hmm_class/__init__.py",
    "chars": 0,
    "preview": ""
  },
  {
    "path": "hmm_class/coin_data.txt",
    "chars": 1550,
    "preview": "THTHTHHHTTTTTHTHTHTHTTHHHHTTTH\nHHHHHHHHHTTHHTHHTTTTTHTTTTHTHT\nTTHHTTHTTTHTTTHHTTTTHTHHTHTHTT\nTHHHTHHHHTHHHTTHTTTTHHTHHTH"
  },
  {
    "path": "hmm_class/edgar_allan_poe.txt",
    "chars": 26612,
    "preview": "LO! Death hath rear'd himself a throne\nIn a strange city, all alone,\nFar down within the dim west\nWhere the good, and th"
  },
  {
    "path": "hmm_class/extra_reading.txt",
    "chars": 396,
    "preview": "A Tutorial on Hidden Markov Models and Selected Applications in Speech Recognition\nhttps://www.ece.ucsb.edu/Faculty/Rabi"
  },
  {
    "path": "hmm_class/frost.py",
    "chars": 3137,
    "preview": "# https://deeplearningcourses.com/c/unsupervised-machine-learning-hidden-markov-models-in-python\n# https://udemy.com/uns"
  },
  {
    "path": "hmm_class/generate_c.py",
    "chars": 2255,
    "preview": "# https://deeplearningcourses.com/c/unsupervised-machine-learning-hidden-markov-models-in-python\n# https://udemy.com/uns"
  },
  {
    "path": "hmm_class/generate_ht.py",
    "chars": 1271,
    "preview": "# https://deeplearningcourses.com/c/unsupervised-machine-learning-hidden-markov-models-in-python\n# https://udemy.com/uns"
  },
  {
    "path": "hmm_class/hmm_classifier.py",
    "chars": 3121,
    "preview": "# https://deeplearningcourses.com/c/unsupervised-machine-learning-hidden-markov-models-in-python\n# https://udemy.com/uns"
  },
  {
    "path": "hmm_class/hmmc.py",
    "chars": 9959,
    "preview": "# https://deeplearningcourses.com/c/unsupervised-machine-learning-hidden-markov-models-in-python\n# https://udemy.com/uns"
  },
  {
    "path": "hmm_class/hmmc_concat.py",
    "chars": 8830,
    "preview": "# https://deeplearningcourses.com/c/unsupervised-machine-learning-hidden-markov-models-in-python\n# https://udemy.com/uns"
  },
  {
    "path": "hmm_class/hmmc_scaled_concat.py",
    "chars": 9035,
    "preview": "# https://deeplearningcourses.com/c/unsupervised-machine-learning-hidden-markov-models-in-python\n# https://udemy.com/uns"
  },
  {
    "path": "hmm_class/hmmc_scaled_concat_diag.py",
    "chars": 10252,
    "preview": "# https://deeplearningcourses.com/c/unsupervised-machine-learning-hidden-markov-models-in-python\n# https://udemy.com/uns"
  },
  {
    "path": "hmm_class/hmmc_tf.py",
    "chars": 7842,
    "preview": "# https://deeplearningcourses.com/c/unsupervised-machine-learning-hidden-markov-models-in-python\n# https://udemy.com/uns"
  },
  {
    "path": "hmm_class/hmmc_theano.py",
    "chars": 6490,
    "preview": "# https://deeplearningcourses.com/c/unsupervised-machine-learning-hidden-markov-models-in-python\n# https://udemy.com/uns"
  },
  {
    "path": "hmm_class/hmmc_theano2.py",
    "chars": 7018,
    "preview": "# https://deeplearningcourses.com/c/unsupervised-machine-learning-hidden-markov-models-in-python\n# https://udemy.com/uns"
  },
  {
    "path": "hmm_class/hmmd.py",
    "chars": 7291,
    "preview": "# https://deeplearningcourses.com/c/unsupervised-machine-learning-hidden-markov-models-in-python\n# https://udemy.com/uns"
  },
  {
    "path": "hmm_class/hmmd_scaled.py",
    "chars": 6254,
    "preview": "# https://deeplearningcourses.com/c/unsupervised-machine-learning-hidden-markov-models-in-python\n# https://udemy.com/uns"
  },
  {
    "path": "hmm_class/hmmd_tf.py",
    "chars": 4802,
    "preview": "# https://deeplearningcourses.com/c/unsupervised-machine-learning-hidden-markov-models-in-python\n# https://udemy.com/uns"
  },
  {
    "path": "hmm_class/hmmd_theano.py",
    "chars": 4632,
    "preview": "# https://deeplearningcourses.com/c/unsupervised-machine-learning-hidden-markov-models-in-python\n# https://udemy.com/uns"
  },
  {
    "path": "hmm_class/hmmd_theano2.py",
    "chars": 4851,
    "preview": "# https://deeplearningcourses.com/c/unsupervised-machine-learning-hidden-markov-models-in-python\n# https://udemy.com/uns"
  },
  {
    "path": "hmm_class/robert_frost.txt",
    "chars": 56259,
    "preview": "Two roads diverged in a yellow wood,\nAnd sorry I could not travel both\nAnd be one traveler, long I stood\nAnd looked down"
  },
  {
    "path": "hmm_class/scan1.py",
    "chars": 732,
    "preview": "# https://deeplearningcourses.com/c/unsupervised-machine-learning-hidden-markov-models-in-python\n# https://udemy.com/uns"
  },
  {
    "path": "hmm_class/scan2.py",
    "chars": 773,
    "preview": "# https://deeplearningcourses.com/c/unsupervised-machine-learning-hidden-markov-models-in-python\n# https://udemy.com/uns"
  },
  {
    "path": "hmm_class/scan3.py",
    "chars": 1023,
    "preview": "# https://deeplearningcourses.com/c/unsupervised-machine-learning-hidden-markov-models-in-python\n# https://udemy.com/uns"
  },
  {
    "path": "hmm_class/site_data.csv",
    "chars": 419857,
    "preview": "-1,8\n4,8\n-1,2\n1,B\n-1,5\n7,5\n7,C\n-1,9\n-1,0\n0,3\n2,B\n-1,6\n3,C\n-1,5\n0,5\n1,0\n-1,7\n3,4\n8,B\n-1,5\n3,0\n-1,9\n4,6\n7,2\n3,2\n6,B\n3,2\n9,"
  },
  {
    "path": "hmm_class/sites.py",
    "chars": 973,
    "preview": "# https://deeplearningcourses.com/c/unsupervised-machine-learning-hidden-markov-models-in-python\n# https://udemy.com/uns"
  },
  {
    "path": "hmm_class/tf_scan1.py",
    "chars": 948,
    "preview": "# https://deeplearningcourses.com/c/unsupervised-machine-learning-hidden-markov-models-in-python\n# https://udemy.com/uns"
  },
  {
    "path": "hmm_class/tf_scan2.py",
    "chars": 949,
    "preview": "# https://deeplearningcourses.com/c/unsupervised-machine-learning-hidden-markov-models-in-python\n# https://udemy.com/uns"
  },
  {
    "path": "hmm_class/tf_scan3.py",
    "chars": 1207,
    "preview": "# https://deeplearningcourses.com/c/unsupervised-machine-learning-hidden-markov-models-in-python\n# https://udemy.com/uns"
  },
  {
    "path": "keras_examples/ann.py",
    "chars": 1781,
    "preview": "from __future__ import print_function, division\nfrom builtins import range\n# Note: you may need to update your version o"
  },
  {
    "path": "keras_examples/basic_mlp.py",
    "chars": 1072,
    "preview": "from __future__ import print_function, division\nfrom builtins import range\n# Note: you may need to update your version o"
  },
  {
    "path": "keras_examples/batchnorm.py",
    "chars": 1871,
    "preview": "from __future__ import print_function, division\nfrom builtins import range\n# Note: you may need to update your version o"
  },
  {
    "path": "keras_examples/cnn.py",
    "chars": 2003,
    "preview": "# https://deeplearningcourses.com/c/data-science-deep-learning-in-theano-tensorflow\n# https://www.udemy.com/data-science"
  },
  {
    "path": "keras_examples/cnn_cifar.py",
    "chars": 2261,
    "preview": "# https://deeplearningcourses.com/c/data-science-deep-learning-in-theano-tensorflow\n# https://www.udemy.com/data-science"
  },
  {
    "path": "keras_examples/cnn_dropout_batchnorm.py",
    "chars": 2120,
    "preview": "# https://deeplearningcourses.com/c/data-science-deep-learning-in-theano-tensorflow\n# https://www.udemy.com/data-science"
  },
  {
    "path": "keras_examples/dropout.py",
    "chars": 1850,
    "preview": "from __future__ import print_function, division\nfrom builtins import range\n# Note: you may need to update your version o"
  },
  {
    "path": "keras_examples/sentiment_analysis.py",
    "chars": 2653,
    "preview": "# https://deeplearningcourses.com/c/deep-learning-advanced-nlp\nfrom __future__ import print_function, division\nfrom buil"
  },
  {
    "path": "keras_examples/sine.py",
    "chars": 1416,
    "preview": "# https://lazyprogrammer.me\nimport numpy as np\nimport pandas as pd\nimport matplotlib.pyplot as plt\n\nfrom keras.models im"
  },
  {
    "path": "keras_examples/sine2.py",
    "chars": 1456,
    "preview": "# https://lazyprogrammer.me\nimport numpy as np\nimport pandas as pd\nimport matplotlib.pyplot as plt\n\nfrom keras.models im"
  },
  {
    "path": "keras_examples/translation.py",
    "chars": 4108,
    "preview": "# https://deeplearningcourses.com/c/deep-learning-advanced-nlp\nfrom __future__ import print_function, division\nfrom buil"
  },
  {
    "path": "keras_examples/util.py",
    "chars": 2197,
    "preview": "from __future__ import print_function, division\nfrom builtins import range\n# Note: you may need to update your version o"
  },
  {
    "path": "kerascv/extra_reading.txt",
    "chars": 210,
    "preview": "KerasCV List of Models\nhttps://keras.io/api/keras_cv/models/\n\nFast R-CNN (Ross Girshick)\nhttps://arxiv.org/pdf/1504.0808"
  },
  {
    "path": "kerascv/imagenet_label_names.json",
    "chars": 14193,
    "preview": "[\"tench\",\n\"goldfish\",\n\"great white shark\",\n\"tiger shark\",\n\"hammerhead shark\",\n\"electric ray\",\n\"stingray\",\n\"cock\",\n\"hen\","
  },
  {
    "path": "kerascv/makelist.py",
    "chars": 199,
    "preview": "'''\nUse this script to generate a list of all XML files in a folder.\n'''\n\nfrom glob import glob\n\nfiles = glob('*.xml')\nw"
  },
  {
    "path": "kerascv/pascal2coco.py",
    "chars": 5499,
    "preview": "# adapted from https://blog.roboflow.com/how-to-convert-annotations-from-voc-xml-to-coco-json/\n\nimport os\nimport argpars"
  },
  {
    "path": "linear_algebra/WHERE ARE THE NOTEBOOKS.txt",
    "chars": 299,
    "preview": "As stated in the \"where to get the code\" / \"where to get the notebooks\" lecture, the notebooks are NOT on Github.\n\nIf yo"
  },
  {
    "path": "linear_algebra/extra_reading.txt",
    "chars": 291,
    "preview": "Introduction to Linear Algebra by Gilbert Strang\nhttps://amzn.to/2G3bvW1\n\nStill Don't Understand Gravity? This Will Help"
  },
  {
    "path": "linear_regression_class/data_1d.csv",
    "chars": 2770,
    "preview": "95.724162408,197.179636092\n35.7576189281,67.5906695414\n28.8168474238,60.8541328206\n99.9584813087,196.907396981\n66.809748"
  },
  {
    "path": "linear_regression_class/data_2d.csv",
    "chars": 4164,
    "preview": "17.9302012052,94.5205919533,320.259529602\n97.1446971852,69.5932819844,404.634471526\n81.7759007845,5.73764809688,181.4851"
  },
  {
    "path": "linear_regression_class/data_poly.csv",
    "chars": 2778,
    "preview": "76.7007086033,663.797275569\n95.2735441552,1014.3622816\n73.0957232493,618.938826916\n46.9516354572,288.012877367\n33.313748"
  }
]

// ... and 356 more files (download for full content)

About this extraction

This page contains the full source code of the lazyprogrammer/machine_learning_examples GitHub repository, extracted and formatted as plain text for AI agents and large language models (LLMs). The extraction includes 556 files (27.1 MB), approximately 3.5M tokens, and a symbol index with 2112 extracted functions, classes, methods, constants, and types. Use this with OpenClaw, Claude, ChatGPT, Cursor, Windsurf, or any other AI tool that accepts text input. You can copy the full output to your clipboard or download it as a .txt file.

Extracted by GitExtract — free GitHub repo to text converter for AI. Built by Nikandr Surkov.

Copied to clipboard!