#!/usr/bin/env python # coding: utf-8 # # Welcome to CS545: Machine Learning # # ## Instructors # # - [Chuck Anderson](http://www.cs.colostate.edu/~anderson) and [Dejan Markovikj](https://www.linkedin.com/in/markovikj/) # ## Lectures # - Tuesday and Thursday: 11:00 - 12:15, Computer Science Building, Room 130. See "Overview" web page. # - All students can watch recorded lecture videos through Echo360 in Canvas # - Presentation and discussions of lecture notes and assignments and all other topics of interest to you! # ## Grading # # - 6 or 7 Assignments: combinations of python implementations and applications of machine learning algorithms with text, math and visualizations that explain the algorithms and results. # - Final assignment will be a project you design yourself. It will count about as much as two regular assignments. # - Check in solutions through Canvas as jupyter notebooks # - Possibly some quizzes. # - No exams! # ## Questions? # - What is [python](https://www.python.org/)? # - What is a [jupyter notebook](https://jupyter.org/)? This [tutorial](https://www.youtube.com/watch?v=3jZYC9rGrNg) is helpful. # - And what do you mean by [machine learning](https://mitsloan.mit.edu/ideas-made-to-matter/machine-learning-explained)? # # Machine Learning # # ## Supervised Learning # - Given samples of inputs and correct outputs, find a computational model that takes an input and produces approximately correct outputs, even for other inputs. # - For example, predict stock prices, or classify a patient's symptoms into likely diagnoses. # ## Unsupervised Learning # - Given samples of input data, find similarities, dissimilarities, anomolies, trends, and low-dimensional representations of the data. # - For example, identify network attacks, or a faulty sensor. # ## Reinforcement Learning # - Find sequences of decisions, or actions, that optimize some measure of success over time. # - Requires trying many actions to see which ones produce good results. # - For example, learn to take shortest paths, or learn to play chess. # ## Examples of algorithms that we will cover: # - Regression (supervised learning) # - Classification (supervised learning) # - Clustering (unsupervised learning) # - Q-Learning (reinforcement learning) # # All using deep learning (artificial neural networks), implemented ourselves mostly using `numpy` and sometimes using public frameworks, such as `pytorch` and `jax`. # # Python # # If you have no or just a little experience with python, it may be difficult for you to gain the necessary experience fast enough. # # 1. True or False: Python code must be compiled before being run. # 2. True or False: Variables in python can be assigned values of any type. # 3. True or False: Python is to be avoided for big machine learning tasks because execution is so slow. # 4. True or False: Python is free and available on all platforms. # 5. True of False: Knowing python will not help you get a job. # # Easy to install on your own laptop. The [Anaconda distribution](https://www.anaconda.com/products/individual) is recommended. # [Popularity of Python](http://pypl.github.io/PYPL.html) judged by accesses of tutorials on github. # # [Popularity of Python, and other languages, for Deep Learning](https://iglu.net/machine-learning-programming/) # ## Python Packages We Will Use # # numpy # matplotlib # pandas # pytorch # jax # # and others # In[ ]: import numpy as np import matplotlib.pyplot as plt plt.plot(np.sin(0.1 * np.arange(100))); # ## jupyter notebook # # Finally, we have # - literate programming # - executable documents # - reproducible results # - super tutorials # - excellent lab notebook for computer scientists # - executable lecture notes! # # Github, and some IDEs ([VSCode](https://code.visualstudio.com/docs/datascience/jupyter-notebooks)), can render them in browsers. # ## Let's see what you know. # # How would you do these? # # 1. Assign to variable $W$ a 5 x 2 matrix (`numpy` array) of random values between -0.1 and 0.1 # 2. Assign to variable a $X$ 10 x 5 matrix of random values between 0 and 10. # 3. Multiply two matrices, $X$ and $W$. # 4. Take the transpose of $XW$, which is $(XW)^T$. # 5. Multiply all elements of matrix $X$ by 2 and subtract 1 from all elements. # 6. Iteratively make small adjustments to an initial matrix $W$ to make it converge on values in matrix $Z$. # In[ ]: # 1. Assign to variable W a 5 x 2 matrix (numpy array) of random values between -0.1 and 0.1 # In[ ]: # 2. Assign to variable X a 10 x 5 matrix of random values between 0 and 10. # In[ ]: # 3. Multiply two matrices, X and W. # In[ ]: # 4. Take the transpose of XW. # In[ ]: # 5. Multiply all elements of matrix X by 2 and subtract 1 from all elements. # In[ ]: # Iteratively make small adjustments to an initial matrix `W` to # make it converge on values in matrix `Z`. # In[ ]: # Iteratively make small adjustments to an initial matrix `W` to # make it converge on values in matrix `Z`. Z = np.array([1,2,3]) W = np.random.uniform(-10, 10, 3) for step in range(300): W += 0.1 * (Z - W) if step % 30 == 0: print(f'{step=} {W=} {Z=}') # # A Curious Story of High-Dimensional Spaces # # Here is a wild statement. In a high-dimensional space, most of the points lie on the surface of the hypercube shell! # # Huh? As discussed at [this stackexchange post](https://datascience.stackexchange.com/questions/27388/what-does-it-mean-when-we-say-most-of-the-points-in-a-hypercube-are-at-the-bound), consider the volume of the hypercube in $d$-dimensional space with each side of the hypercube being of length 1. # # $$ 1 \cdot 1 \cdot 1 \cdot \ldots \cdot 1 = 1^d$$ # # Nothing surprising here. # Now, let's look at the volume of the "interior" of this hypercube. Let's define the interior as the space from 0.01 to 0.99 along each dimension. This would be # # $$ 0.98 \cdot 0.98 \cdot 0.98 \ldots \cdot 0.98 = 0.98^d$$ # # So what? Well, let's look at the results of these calculations. First let's consider two dimensions. # In[ ]: 0.98**2 # In[ ]: d = 2 print(f'Total volume {1**d}. Interior volume {0.98**d}') # How about 10 dimensions, or 50? # In[ ]: d = 10 print(f'Total volume {1**d}. Interior volume {0.98**d}') # In[ ]: d = 50 print(f'Total volume {1**d}. Interior volume {0.98**d}') # Okay, this is getting tedious. Let's automate this and calculate interior volume for a range of dimensions up to 100, and plot it. # In[ ]: def interior_volume(d): return (1 - 0.01 * 2) ** d dims = np.arange(1, 100, 1) plt.plot(dims, interior_volume(dims)) plt.xlabel('dimensions') plt.ylabel('interior volume') plt.grid('on') # Keep going, up to 1000 dimensions. # In[ ]: dims = np.arange(1, 1000, 1) plt.plot(dims, interior_volume(dims)) plt.xlabel('dimensions') plt.ylabel('interior volume') plt.grid('on') plt.text(500, 0.6, 'WHOA!', fontsize=40); # Go back to definition of `interior_volume` and try a thinner shell. # # *It's the questions that drive us, Mr. Anderson.* # # - Agent Smith from The Matrix # 1. What are the general categories of machine learning? # # 1. What will I learn in this class? # 1. Why are we using python and jupyter notebooks, and what are jupyter notebooks? # 1. Will we be using GPUs? # 1. What deep learning software frameworks are there, and which ones will we be using? # 6. Why do you think you can teach this stuff? # 1. How can I get a job in machine learning? # 1. My math background is weak. Will I do okay in this course? # 1. My python coding and debugging is weak. Will I do okay in this course? # 1. Why are there no exams?