@ -1,11 +1,11 @@
|
||||
# Assignment
|
||||
# Interview a data scientist
|
||||
|
||||
## Instructions
|
||||
|
||||
|
||||
In your company, in a user group, or among your friends or fellow students, talk to someone who works professionally as a data scientist. Write a short paper (500 words) about their daily occupations. Are they specialists, or do they work 'full stack'?
|
||||
|
||||
## Rubric
|
||||
|
||||
| Criteria | Exemplary | Adequate | Needs Improvement |
|
||||
| -------- | --------- | -------- | ----------------- |
|
||||
| | 1 | 2 | 3 |
|
||||
| Criteria | Exemplary | Adequate | Needs Improvement |
|
||||
| -------- | ------------------------------------------------------------------------------------ | ------------------------------------------------------------------ | --------------------- |
|
||||
| | An essay of the correct length, with attributed sources, is presented as a .doc file | The essay is poorly attributed or shorter than the required length | No essay is presented |
|
||||
|
After Width: | Height: | Size: 99 KiB |
After Width: | Height: | Size: 348 KiB |
@ -1,13 +1,13 @@
|
||||
# Regression with Scikit-Learn
|
||||
# Regression with Scikit-learn
|
||||
|
||||
## Instructions
|
||||
|
||||
Take a look at the [Linnerud dataset](https://scikit-learn.org/stable/modules/generated/sklearn.datasets.load_linnerud.html#sklearn.datasets.load_linnerud) in Scikit-Learn. This dataset has multiple [targets](https://scikit-learn.org/stable/datasets/toy_dataset.html#linnerrud-dataset): 'It consists of three excercise (data) and three physiological (target) variables collected from twenty middle-aged men in a fitness club'.
|
||||
Take a look at the [Linnerud dataset](https://scikit-learn.org/stable/modules/generated/sklearn.datasets.load_linnerud.html#sklearn.datasets.load_linnerud) in Scikit-learn. This dataset has multiple [targets](https://scikit-learn.org/stable/datasets/toy_dataset.html#linnerrud-dataset): 'It consists of three excercise (data) and three physiological (target) variables collected from twenty middle-aged men in a fitness club'.
|
||||
|
||||
In your own words, describe how to create a Regression model that would plot the relationship between the waistline and how many situps are accomplished. Do the same for the other datapoints in this dataset.
|
||||
|
||||
## Rubric
|
||||
|
||||
| Criteria | Exemplary | Adequate | Needs Improvement |
|
||||
| -------- | --------- | -------- | ----------------- |
|
||||
| Submit a descriptive paragraph | Well-written paragraph is submitted | A few sentences are submitted | No description is supplied |
|
||||
| Criteria | Exemplary | Adequate | Needs Improvement |
|
||||
| ------------------------------ | ----------------------------------- | ----------------------------- | -------------------------- |
|
||||
| Submit a descriptive paragraph | Well-written paragraph is submitted | A few sentences are submitted | No description is supplied |
|
||||
|
Before Width: | Height: | Size: 219 KiB |
Before Width: | Height: | Size: 1.1 MiB After Width: | Height: | Size: 1.1 MiB |
After Width: | Height: | Size: 790 KiB |
@ -1,22 +1,21 @@
|
||||
# Getting Started with Natural Language Processing
|
||||
# Getting started with natural language processing
|
||||
|
||||
## Regional topic: European literature and Romantic Hotels of Europe ❤️
|
||||
## Regional topic: European languages and literature and romantic hotels of Europe ❤️
|
||||
|
||||
In this section of the curriculum, you will be introduced to one of the most widespread uses of machine learning: Natural Language Processing (NLP). Derived from Computational Linguistics, this category of Artificial Intelligence is the bridge between humans and machines via voice or textual communication.
|
||||
In this section of the curriculum, you will be introduced to one of the most widespread uses of machine learning: natural language processing (NLP). Derived from computational linguistics, this category of artificial intelligence is the bridge between humans and machines via voice or textual communication.
|
||||
|
||||
In these lessons we'll learn the basics of NLP by building small conversational bots to learn how Machine Learning aids in making these conversations more and more 'smart'. You'll travel back in time, chatting with Elizabeth Bennett and Mr. Darcy from Jane Austen's classic novel, **Pride and Prejudice**, published in 1813. Then, you'll further your knowledge by learning about sentiment analysis via hotel reviews in Europe.
|
||||
In these lessons we'll learn the basics of NLP by building small conversational bots to learn how machine learning aids in making these conversations more and more 'smart'. You'll travel back in time, chatting with Elizabeth Bennett and Mr. Darcy from Jane Austen's classic novel, **Pride and Prejudice**, published in 1813. Then, you'll further your knowledge by learning about sentiment analysis via hotel reviews in Europe.
|
||||
|
||||

|
||||
> Photo by <a href="https://unsplash.com/@elaineh?utm_source=unsplash&utm_medium=referral&utm_content=creditCopyText">Elaine Howlin</a> on <a href="https://unsplash.com/s/photos/pride-and-prejudice?utm_source=unsplash&utm_medium=referral&utm_content=creditCopyText">Unsplash</a>
|
||||
|
||||
## Lessons
|
||||
|
||||
1. [Introduction to Natural Language Processing](1-Introduction-to-NLP/README.md)
|
||||
2. [Common NLP Tasks and Techniques](2-Tasks/README.md)
|
||||
3. [Translation and Sentiment Analysis with Machine Learning](3-Translation-Sentiment/README.md)
|
||||
1. [Introduction to natural language processing](1-Introduction-to-NLP/README.md)
|
||||
2. [Common NLP tasks and techniques](2-Tasks/README.md)
|
||||
3. [Translation and sentiment analysis with machine learning](3-Translation-Sentiment/README.md)
|
||||
4. TBD
|
||||
5. TBD
|
||||
|
||||
## Credits
|
||||
|
||||
These Natural Language Processing lessons were written with ☕ by [Stephen Howell]([Twitter](https://twitter.com/Howell_MSFT))
|
||||
These natural language processing lessons were written with ☕ by [Stephen Howell](https://twitter.com/Howell_MSFT)
|
||||
|
@ -1,21 +1,18 @@
|
||||
# Time Series Forecasting
|
||||
# Introduction to time series forecasting
|
||||
## Regional topic: worldwide electricity usage ✨
|
||||
|
||||
## Regional topic: Worldwide Electricity Usage ✨
|
||||
|
||||
In these two lessons, you will be introduced to Time Series Forecasting, a somewhat lesser known area of Machine Learning that is nevertheless extremely valuable for industry and business applications, among other fields. While neural networks can be used to enhance the utility of these models, we will study them in the context of classical machine learning as models help predict future performance based on the past.
|
||||
In these two lessons, you will be introduced to time series forecasting, a somewhat lesser known area of machine learning that is nevertheless extremely valuable for industry and business applications, among other fields. While neural networks can be used to enhance the utility of these models, we will study them in the context of classical machine learning as models help predict future performance based on the past.
|
||||
|
||||
Our regional focus is electrical usage in the world, an interesting dataset to learn about forecasting future power usage based on patterns of past load. You can see how this kind of forecasting can be extremely helpful in a business environment.
|
||||
|
||||

|
||||
|
||||
Photo by <a href="https://unsplash.com/@shutter_log?utm_source=unsplash&utm_medium=referral&utm_content=creditCopyText">Peddi Sai hrithik</a> of electrical towers on a road in Rajasthan on <a href="https://unsplash.com/s/photos/electric-india?utm_source=unsplash&utm_medium=referral&utm_content=creditCopyText">Unsplash</a>
|
||||
|
||||
|
||||
## Lessons
|
||||
|
||||
1. [Introduction to Time Series Forecasting](1-Introduction/README.md)
|
||||
2. [Building ARIMA Time Series Models](2-ARIMA/README.md)
|
||||
1. [Introduction to time series forecasting](1-Introduction/README.md)
|
||||
2. [Building ARIMA time series models](2-ARIMA/README.md)
|
||||
|
||||
## Credits
|
||||
|
||||
"Time Series Forecasting" was written with ⚡️ by [Francesca Lazzeri](https://twitter.com/frlazzeri) and [Jen Looper](https://twitter.com/jenlooper)
|
||||
"Introduction to time series forecasting" was written with ⚡️ by [Francesca Lazzeri](https://twitter.com/frlazzeri) and [Jen Looper](https://twitter.com/jenlooper)
|
||||
|
@ -1,55 +1,316 @@
|
||||
# [Lesson Topic]
|
||||
# CartPole Skating
|
||||
|
||||
Add a sketchnote if possible/appropriate
|
||||
The problem we have been solving in the previous lesson might seem like a toy problem, not really applicable for real life scenarios. This is not the case, because many real world problems also share this scenario - including playing Chess or Go. They are similar, because we also have a board with given rules and a **discrete state**.
|
||||
|
||||

|
||||
In this lesson we will apply the same principles of Q-Learning to a problem with **continuous state**, i.e. a state that is given by one or more real numbers. We will deal with the following problem:
|
||||
|
||||
## [Pre-lecture quiz](link-to-quiz-app) 45
|
||||
> **Problem**: If Peter wants to escape from the wolf, he needs to be able to move faster. We will see how Peter can learn to skate, in particular, to keep balance, using Q-Learning.
|
||||
|
||||
Describe what we will learn
|
||||
We will use a simplified version of balancing known as a **CartPole** problem. In the cartpole world, we have a horizontal slider that can move left or right, and the goal is to balance a vertical pole on top of the slider.
|
||||
|
||||
### Introduction
|
||||
<img alt="a cartpole" src="images/cartpole.png" width="200"/>
|
||||
|
||||
Describe what will be covered
|
||||
## Prerequisites
|
||||
|
||||
> Notes
|
||||
In this lesson, we will be using a library called **OpenAI Gym** to simulate different **environments**. You can run this lesson's code locally (eg. from Visual Studio Code), in which case the simulation will open in a new window. When running the code online, you may need to make some tweaks to the code, as described [here](https://towardsdatascience.com/rendering-openai-gym-envs-on-binder-and-google-colab-536f99391cc7).
|
||||
## OpenAI Gym
|
||||
|
||||
### Prerequisite
|
||||
In the previous lesson, the rules of the game and the state were given by the `Board` class which we defined ourselves. Here we will use a special **sumulation environment**, which will simulate the physics behind the balancing pole. One of the most popular simulation environments for training reinforcement learning algorithms is called a [Gym](https://gym.openai.com/), which is maintained by [OpenAI](https://openai.com/). By using this gym we can create difference **environments** from a cartpole simulation to Atari games.
|
||||
|
||||
What steps should have been covered before this lesson?
|
||||
> **Note**: You can see other environments available from OpenAI Gym [here](https://gym.openai.com/envs/#classic_control).
|
||||
|
||||
### Preparation
|
||||
First, let's install the gym and import required libraries (code block 1):
|
||||
|
||||
Preparatory steps to start this lesson
|
||||
```python
|
||||
import sys
|
||||
!{sys.executable} -m pip install gym
|
||||
|
||||
---
|
||||
import gym
|
||||
import matplotlib.pyplot as plt
|
||||
import numpy as np
|
||||
import random
|
||||
```
|
||||
|
||||
## A cartpole environment
|
||||
|
||||
To work with a cartpole balancing problem, we need to initialize corresponding environment. Each environment is associated with an:
|
||||
* **Observation space** that defines the structure of information that we receive from the environment. For cartpole problem, we receive position of the pole, velocity and some other values.
|
||||
* **Action space** that defines possible actions. In our case the action space is discrete, and consists of two actions - **left** and **right**. (code block 2)
|
||||
|
||||
```python
|
||||
env = gym.make("CartPole-v1")
|
||||
print(env.action_space)
|
||||
print(env.observation_space)
|
||||
print(env.action_space.sample())
|
||||
```
|
||||
|
||||
To see how the environment works, let's run a short simulation for 100 steps. At each step, we provide one of the actions to be taken - in this simulation we just randomly select an action from `action_space`. Run the code below and see what it leads to.
|
||||
|
||||
> **Note**: Remember that it is preferred to run this code on local Python installation! (code block 3)
|
||||
|
||||
```python
|
||||
env.reset()
|
||||
|
||||
for i in range(100):
|
||||
env.render()
|
||||
env.step(env.action_space.sample())
|
||||
env.close()
|
||||
```
|
||||
|
||||
You should be seeing something similar to this one:
|
||||
|
||||

|
||||
|
||||
During simulation, we need to get observations in order to decide how to act. In fact, `step` function returns us back current observations, reward function, and the `done` flag that indicates whether it makes sense to continue the simulation or not: (code block 4)
|
||||
|
||||
```python
|
||||
env.reset()
|
||||
|
||||
done = False
|
||||
while not done:
|
||||
env.render()
|
||||
obs, rew, done, info = env.step(env.action_space.sample())
|
||||
print(f"{obs} -> {rew}")
|
||||
env.close()
|
||||
```
|
||||
You will end up seeing something like this in the notebook output:
|
||||
```text
|
||||
[ 0.03403272 -0.24301182 0.02669811 0.2895829 ] -> 1.0
|
||||
[ 0.02917248 -0.04828055 0.03248977 0.00543839] -> 1.0
|
||||
[ 0.02820687 0.14636075 0.03259854 -0.27681916] -> 1.0
|
||||
[ 0.03113408 0.34100283 0.02706215 -0.55904489] -> 1.0
|
||||
[ 0.03795414 0.53573468 0.01588125 -0.84308041] -> 1.0
|
||||
...
|
||||
[ 0.17299878 0.15868546 -0.20754175 -0.55975453] -> 1.0
|
||||
[ 0.17617249 0.35602306 -0.21873684 -0.90998894] -> 1.0
|
||||
```
|
||||
|
||||
The observation vector that is returned at each step of the simulation contains the following values:
|
||||
* Position of cart
|
||||
* Velocity of cart
|
||||
* Angle of pole
|
||||
* Rotation rate of pole
|
||||
|
||||
We can get min and max value of those numbers: (code block 5)
|
||||
|
||||
```python
|
||||
print(env.observation_space.low)
|
||||
print(env.observation_space.high)
|
||||
```
|
||||
|
||||
You may also notice that reward value on each simulation step is always 1. This is because our goal is to survive as long as possible, i.e. keep the pole to a reasonably vertical position for the longest period of time.
|
||||
|
||||
> In fact, CartPole simulation is considered solved if we manage to get the average reward of 195 over 100 consecutive trials.
|
||||
|
||||
## State discretization
|
||||
|
||||
In Q=Learning, we need to build Q-Table that defines what to do at each state. To be able to do this, we need state to be **discreet**, more precisely, it should contain finite number of discrete values. Thus, we need somehow to **discretize** our observations, mapping them to a finite set of states.
|
||||
|
||||
There are a few ways we can do this:
|
||||
|
||||
* If we know the interval of a certain value, we can divide this interval into a number of **bins**, and then replace the value by the bin number that it belongs to. This can be done using the numpy [`digitize`](https://numpy.org/doc/stable/reference/generated/numpy.digitize.html) method. In this case, we will precisely know the state size, because it will depend on the number of bins we select for digitalization.
|
||||
|
||||
✅ We can use linear interpolation to bring values to some finite interval (say, from -20 to 20), and then convert numbers to integers by rounding them. This gives us a bit less control on the size of the state, especially if we do not know the exact ranges of input values. For example, in our case 2 out of 4 values do not have upper/lower bounds on their values, which may result in the infinite number of states.
|
||||
|
||||
In our example, we will go with the second approach. As you may notice later, despite undefined upper/lower bounds, those value rarely take values outside of certain finite intervals, thus those states with extreme values will be very rare.
|
||||
|
||||
Here is the function that will take the observation from our model, and produces a tuple of 4 integer values: (code block 6)
|
||||
|
||||
```python
|
||||
def discretize(x):
|
||||
return tuple((x/np.array([0.25, 0.25, 0.01, 0.1])).astype(np.int))
|
||||
```
|
||||
Let's also explore another discretization method using bins: (code block 7)
|
||||
|
||||
```python
|
||||
def create_bins(i,num):
|
||||
return np.arange(num+1)*(i[1]-i[0])/num+i[0]
|
||||
|
||||
print("Sample bins for interval (-5,5) with 10 bins\n",create_bins((-5,5),10))
|
||||
|
||||
ints = [(-5,5),(-2,2),(-0.5,0.5),(-2,2)] # intervals of values for each parameter
|
||||
nbins = [20,20,10,10] # number of bins for each parameter
|
||||
bins = [create_bins(ints[i],nbins[i]) for i in range(4)]
|
||||
|
||||
def discretize_bins(x):
|
||||
return tuple(np.digitize(x[i],bins[i]) for i in range(4))
|
||||
```
|
||||
|
||||
Let's now run a short simulation and observe those discrete environment values. Feel free to try both `discretize` and `discretize_bins` and see if there is a difference.
|
||||
|
||||
> **Note**: `discretize_bins` returns the bin number, which is 0-based, thus for values of input variable around 0 it returns the number from the middle of the interval (10). In `discretize`, we did not care about the range of output values, allowing them to be negative, thus the state values are not shifted, and 0 corresponds to 0. (code block 8)
|
||||
|
||||
```python
|
||||
env.reset()
|
||||
|
||||
done = False
|
||||
while not done:
|
||||
#env.render()
|
||||
obs, rew, done, info = env.step(env.action_space.sample())
|
||||
#print(discretize_bins(obs))
|
||||
print(discretize(obs))
|
||||
env.close()
|
||||
```
|
||||
> **Note**: Uncomment the line starting with `env.render` if you want to see how the environment executes. Otherwise you can execute it in the background, which is faster. We will use this "invisible" execution during our Q-Learning process.
|
||||
|
||||
[Step through content in blocks]
|
||||
## The Q-Table structure
|
||||
|
||||
## [Topic 1]
|
||||
In our previous lesson, the state was a simple pair of numbers from 0 to 8, and thus it was convenient to represent Q-Table by a numpy tensor with a shape of 8x8x2. If we use bins discretization, the size of our state vector is also known, so we can use the same approach and represent state by an array of shape 20x20x10x10x2 (here 2 is the dimension of action space, and first dimensions correspond to the number of bins we have selected to use for each of the parameters in observation space).
|
||||
|
||||
### Task:
|
||||
However, sometimes precise dimensions of the observation space are not known. In case of the `discretize` function, we may never be sure that our state stays within certain limits, because some of the original values are not bound. Thus, we will use a slightly different approach and represent Q-Table by a dictionary.
|
||||
|
||||
Work together to progressively enhance your codebase to build the project with shared code:
|
||||
We will use the pair *(state,action)* as the dictionary key, and the value would correspond to Q-Table entry value. (code block 9)
|
||||
|
||||
```html
|
||||
code blocks
|
||||
```python
|
||||
Q = {}
|
||||
actions = (0,1)
|
||||
|
||||
def qvalues(state):
|
||||
return [Q.get((state,a),0) for a in actions]
|
||||
```
|
||||
|
||||
Here we also define a function `qvalues`, which returns a list of Q-Table values for a given state that corresponds to all possible actions. If the entry is not present in the Q-Table, we will return 0 as the default.
|
||||
|
||||
## Let's start Q-Learning!
|
||||
|
||||
Now we are ready to teach Peter to balance! First, let's set some hyperparameters: (code block 10)
|
||||
|
||||
```python
|
||||
# hyperparameters
|
||||
alpha = 0.3
|
||||
gamma = 0.9
|
||||
epsilon = 0.90
|
||||
```
|
||||
|
||||
Here, `alpha` is the **learning rate** that defines to which extent we should adjust the current values of Q-Table at each step. In the previous lesson we started with 1, and then decreased `alpha` to lower values during training. In this example we will keep it constant just for simplicity, and you can experiment with adjusting `alpha` values later.
|
||||
|
||||
`gamma` is the **discount factor** that shows to which extent we should prioritize future reward over current reward.
|
||||
|
||||
`epsilon` is the **exploration/exploitation factor** that determines whether we should prefer exploration to exploitation or vice versa. In our algorithm, we will in `epsilon` percent of the cases select the next action according to Q-Table values, and in the remaining number of cases we will execute a random action. This will allow us to explore areas of the search space that we have never seen before.
|
||||
|
||||
✅ In terms of balancing - choosing random action (exploration) would act as a random punch in the wrong direction, and the pole would have to learn how to recover the balance from those "mistakes"
|
||||
|
||||
We can also make two improvements to our algorithm from the previous lesson:
|
||||
|
||||
* Calculating average cumulative reward over a number of simulations. We will print the progress each 5000 iterations, and we will average out our cumulative reward over that period of time. It means that if we get more than 195 point - we can consider the problem solved, with even higher quality than required.
|
||||
|
||||
* We will calculate maximum average cumulative result `Qmax`, and we will store the Q-Table corresponding to that result. When you run the training you will notice that sometimes the average cumulative result starts to drop, and we want to keep the values of Q-Table that correspond to the best model observed during training.
|
||||
|
||||
We will also collect all cumulative rewards at each simulation at `rewards` vector for further plotting. (code block 11)
|
||||
|
||||
```python
|
||||
def probs(v,eps=1e-4):
|
||||
v = v-v.min()+eps
|
||||
v = v/v.sum()
|
||||
return v
|
||||
|
||||
Qmax = 0
|
||||
cum_rewards = []
|
||||
rewards = []
|
||||
for epoch in range(100000):
|
||||
obs = env.reset()
|
||||
done = False
|
||||
cum_reward=0
|
||||
# == do the simulation ==
|
||||
while not done:
|
||||
s = discretize(obs)
|
||||
if random.random()<epsilon:
|
||||
# exploitation - chose the action according to Q-Table probabilities
|
||||
v = probs(np.array(qvalues(s)))
|
||||
a = random.choices(actions,weights=v)[0]
|
||||
else:
|
||||
# exploration - randomly chose the action
|
||||
a = np.random.randint(env.action_space.n)
|
||||
|
||||
obs, rew, done, info = env.step(a)
|
||||
cum_reward+=rew
|
||||
ns = discretize(obs)
|
||||
Q[(s,a)] = (1 - alpha) * Q.get((s,a),0) + alpha * (rew + gamma * max(qvalues(ns)))
|
||||
cum_rewards.append(cum_reward)
|
||||
rewards.append(cum_reward)
|
||||
# == Periodically print results and calculate average reward ==
|
||||
if epoch%5000==0:
|
||||
print(f"{epoch}: {np.average(cum_rewards)}, alpha={alpha}, epsilon={epsilon}")
|
||||
if np.average(cum_rewards) > Qmax:
|
||||
Qmax = np.average(cum_rewards)
|
||||
Qbest = Q
|
||||
cum_rewards=[]
|
||||
```
|
||||
|
||||
What you may notice from those results:
|
||||
|
||||
* We are very close to achieving the goal of getting 195 cumulative rewards over 100+ consecutive runs of the simulation, or we may have actually achieved it! Even if we get smaller numbers, we still do not know, because we average over 5000 runs, and only 100 runs is required in the formal criteria.
|
||||
|
||||
* Sometimes the reward start to drop, which means that we can "destroy" already learnt values in the Q-Table with the ones that make the situation worse.
|
||||
|
||||
This is more clearly visible if we plot training progress.
|
||||
|
||||
## Plotting Training Progress
|
||||
|
||||
During training, we have collected the cumulative reward value at each of the iterations into `rewards` vector. Here is how it looks when we plot it against the iteration number:
|
||||
|
||||
```python
|
||||
plt.plot(rewards)
|
||||
```
|
||||
|
||||
✅ Knowledge Check - use this moment to stretch students' knowledge with open questions
|
||||

|
||||
|
||||
From this graph, it is not possible to tell anything, because due to the nature of stochastic training process the length of training sessions varies greatly. To make more sense of this graph, we can calculate the **running average** over a series of experiments, let's say 100. This can be done conveniently using `np.convolve`: (code block 12)
|
||||
|
||||
```python
|
||||
def running_average(x,window):
|
||||
return np.convolve(x,np.ones(window)/window,mode='valid')
|
||||
|
||||
plt.plot(running_average(rewards,100))
|
||||
```
|
||||
|
||||

|
||||
|
||||
## Varying hyperparameters
|
||||
|
||||
To make learning more stable, it makes sense to adjust some of our hyperparameters during training. In particular:
|
||||
|
||||
* For **learning rate**, `alpha`, we may start with values close to 1, and then keep decreasing the parameter. With time, we will be getting good probability values in the Q-Table, and thus we should be adjusting them slightly, and not overwriting completely with new values.
|
||||
|
||||
## [Topic 2]
|
||||
* We may want to increase the `epsilon` slowly, in order to explore less and exploit more. It probably makes sense to start with lower value of `epsilon`, and move up to almost 1.
|
||||
|
||||
## [Topic 3]
|
||||
> **Task 1**: Play with hyperparameter values and see if you can achieve higher cumulative reward. Are you getting above 195?
|
||||
|
||||
> **Task 2**: To formally solve the problem, you need to get 195 average reward across 100 consecutive runs. Measure that during training and make sure that you have formally solved the problem!
|
||||
|
||||
## Seeing the result in action
|
||||
|
||||
It would be interesting to actually see how the trained model behaves. Let's run the simulation and follow the same action selection strategy as during training, sampling according to the probability distribution in Q-Table: (code block 13)
|
||||
|
||||
```python
|
||||
obs = env.reset()
|
||||
done = False
|
||||
while not done:
|
||||
s = discretize(obs)
|
||||
env.render()
|
||||
v = probs(np.array(qvalues(s)))
|
||||
a = random.choices(actions,weights=v)[0]
|
||||
obs,_,done,_ = env.step(a)
|
||||
env.close()
|
||||
```
|
||||
|
||||
You should see something like this:
|
||||
|
||||

|
||||
|
||||
---
|
||||
## 🚀Challenge
|
||||
|
||||
Add a challenge for students to work on collaboratively in class to enhance the project
|
||||
> **Task 3**: Here, we were using the final copy of Q-Table, which may not be the best one. Remember that we have stored the best-performing Q-Table into `Qbest` variable! Try the same example with the best-performing Q-Table by copying `Qbest` over to `Q` and see if you notice the difference.
|
||||
|
||||
> **Task 4**: Here we were not selecting the best action on each step, but rather sampling with corresponding probability distribution. Would it make more sense to always select the best action, with the highest Q-Table value? This can be done by using `np.argmax` function to find out the action number corresponding to highers Q-Table value. Implement this strategy and see if it improves the balancing.
|
||||
|
||||
## [Post-lecture quiz](link-to-quiz-app)
|
||||
|
||||
Optional: add a screenshot of the completed lesson's UI if appropriate
|
||||
## Assignment: [Train a Mountain Car](assignment.md)
|
||||
|
||||
## [Post-lecture quiz](link-to-quiz-app) 46
|
||||
## Conclusion
|
||||
|
||||
## Review & Self Study
|
||||
We have now learned how to train agents to achieve good results just by providing them a reward function that defines the desired state of the game, and by giving them an opportunity to intelligently explore the search space. We have successfully applied the Q-Learning algorithm in the cases of discrete and continuous environments, but with discrete actions.
|
||||
|
||||
## Assignment [Assignment Name](assignment.md)
|
||||
It's important to also study situations where action state is also continuous, and when observation space is much more complex, such as the image from the Atari game screen. In those problems we often need to use more powerful machine learning techniques, such as neural networks, in order to achieve good results. Those more advanced topics are the subject of our forthcoming more advanced AI course.
|
@ -1,9 +1,43 @@
|
||||
# [Assignment Name]
|
||||
# Train Mountain Car
|
||||
|
||||
[OpenAI Gym](http://gym.openai.com) has been designed in such a way that all environments provide the same API - i.e. the same methods `reset`, `step` and `render`, and the same abstractions of **action space** and **observation space**. Thus is should be possible to adapt the same reinforcement learning algorithms to different environments with minimal code changes.
|
||||
|
||||
## A Mountain Car Environment
|
||||
|
||||
[Mountain Car environment](https://gym.openai.com/envs/MountainCar-v0/) contains a car stuck in a valley:
|
||||
|
||||
<img src="images/mountaincar.png" width="300"/>
|
||||
|
||||
The goal is to get out of the valley and capture the flag, by doing at each step one of the following actions:
|
||||
|
||||
| Value | Meaning |
|
||||
|---|---|
|
||||
| 0 | Accelerate to the left |
|
||||
| 1 | Do not accelerate |
|
||||
| 2 | Accelerate to the right |
|
||||
|
||||
The main trick of this problem is, however, that the car's engine is not strong enough to scale the mountain in a single pass. Therefore, the only way to succeed is to drive back and forth to build up momentum.
|
||||
|
||||
Observation space consists of just two values:
|
||||
|
||||
| Num | Observation | Min | Max |
|
||||
|-----|--------------|-----|-----|
|
||||
| 0 | Car Position | -1.2| 0.6 |
|
||||
| 1 | Car Velocity | -0.07 | 0.07 |
|
||||
|
||||
Reward system for the mountain car is rather tricky:
|
||||
|
||||
* Reward of 0 is awarded if the agent reached the flag (position = 0.5) on top of the mountain.
|
||||
* Reward of -1 is awarded if the position of the agent is less than 0.5.
|
||||
|
||||
Episode terminates if the car position is more than 0.5, or episode length is greater than 200.
|
||||
## Instructions
|
||||
|
||||
Adapt our reinforcement learning algorithm to solve the mountain car problem. Start with existing [notebook.ipynb](notebook.ipynb) code, substitute new environment, change state discretization functions, and try to make existing algorithm to train with minimal code modifications. Optimize the result by adjusting hyperparameters.
|
||||
|
||||
> **Note**: Hyperparameters adjustment is likely to be needed to make algorithm converge.
|
||||
## Rubric
|
||||
|
||||
| Criteria | Exemplary | Adequate | Needs Improvement |
|
||||
| -------- | --------- | -------- | ----------------- |
|
||||
| | | | |
|
||||
| | Q-Learning algorithm is successfully adapted from CartPole example, with minimal code modifications, which is able to solve the problem of capturing the flag under 200 steps. | A new Q-Learning algorithm has been adopted from the Internet, but is well-documented; or existing algorithm adopted, but does not reach desired results | Student was not able to successfully adopt any algorithm, but has mede substantial steps towards solution (implemented state discretization, Q-Table data structure, etc.) |
|
||||
|
After Width: | Height: | Size: 383 KiB |
After Width: | Height: | Size: 43 KiB |
After Width: | Height: | Size: 926 B |
After Width: | Height: | Size: 8.0 KiB |
After Width: | Height: | Size: 3.8 KiB |
After Width: | Height: | Size: 7.3 KiB |
After Width: | Height: | Size: 456 KiB |
@ -0,0 +1,39 @@
|
||||
- Introduction
|
||||
- [Introduction to Machine Learning](../1-Introduction/1-intro-to-ML/README.md)
|
||||
- [History of Machine Learning](../1-Introduction/2-history-of-ML/README.md)
|
||||
- [ML and Fairness](../1-Introduction/3-fairness/README.md)
|
||||
- [Techniques of ML](../1-Introduction/4-techniques-of-ML/README.md)
|
||||
|
||||
- Regression
|
||||
- [Tools of the Trade](../2-Regression/1-Tools/README.md)
|
||||
- [Data](../2-Regression/2-Data/README.md)
|
||||
- [Linear Regression](../2-Regression/3-Linear/README.md)
|
||||
- [Logistic Regression](../2-Regression/4-Logistic/README.md)
|
||||
|
||||
- Build a Web App
|
||||
- [Web App](../3-Web-App/1-Web-App/README.md)
|
||||
|
||||
- Classification
|
||||
- [Intro to Classification](../4-Classification/1-Introduction/README.md)
|
||||
- [Classifiers 1](../4-Classification/2-Classifiers-1/README.md)
|
||||
- [Classifiers 2](../4-Classification/3-Classifiers-2/README.md)
|
||||
- [Applied ML](../4-Classification/4-Applied/README.md)
|
||||
|
||||
- Clustering
|
||||
- [Visualize your Data](../5-Clustering/1-Visualize/README.md)
|
||||
- [K-Means](../5-Clustering/2-K-Means/README.md)
|
||||
|
||||
- NLP
|
||||
- [Introduction to NLP](../6-NLP/1-Introduction-to-NLP/README.md)
|
||||
- [NLP Tasks](../6-NLP/2-Tasks/README.md)
|
||||
- [Translation and Sentiment](../6-NLP/3-Translation-Sentiment/README.md)
|
||||
|
||||
- Time Series Forecasting
|
||||
- [Introduction to Time Series Forecasting](../7-TimeSeries/1-Introduction/README.md)
|
||||
- [ARIMA](../7-TimeSeries/2-ARIMA/README.md)
|
||||
|
||||
- Reinforcement Learning
|
||||
- [Q-Learning](../8-Reinforcement/1-QLearning/README.md)
|
||||
|
||||
- Real World ML
|
||||
- [Applications](../9-Real-World/1-Applications/README.md)
|
@ -0,0 +1,9 @@
|
||||
module.exports = {
|
||||
contents: ['docs/_sidebar.md'], // array of "table of contents" files path
|
||||
pathToPublic: 'pdf/readme.pdf', // path where pdf will stored
|
||||
pdfOptions: {
|
||||
margin: { top: '100px', bottom: '100px' }
|
||||
}, // reference: https://github.com/GoogleChrome/puppeteer/blob/master/docs/api.md#pagepdfoptions
|
||||
removeTemp: true, // remove generated .md and .html or not
|
||||
emulateMedia: 'print', // mediaType, emulating by puppeteer for rendering pdf, 'print' by default (reference: https://github.com/GoogleChrome/puppeteer/blob/master/docs/api.md#pageemulatemediamediatype)
|
||||
};
|
@ -0,0 +1,29 @@
|
||||
{
|
||||
"name": "ml-for-beginners",
|
||||
"version": "1.0.0",
|
||||
"description": "Machine Learning for Beginners - A Curriculum",
|
||||
"main": "index.js",
|
||||
"scripts": {
|
||||
"convert": "node_modules/.bin/docsify-to-pdf"
|
||||
},
|
||||
"repository": {
|
||||
"type": "git",
|
||||
"url": "git+https://github.com/microsoft/ML-For-Beginners.git"
|
||||
},
|
||||
"keywords": [
|
||||
"machine",
|
||||
"learning",
|
||||
"ml",
|
||||
"ai",
|
||||
"curriculum"
|
||||
],
|
||||
"author": "Jen Looper and team",
|
||||
"license": "MIT",
|
||||
"bugs": {
|
||||
"url": "https://github.com/microsoft/ML-For-Beginners/issues"
|
||||
},
|
||||
"homepage": "https://github.com/microsoft/ML-For-Beginners#readme",
|
||||
"devDependencies": {
|
||||
"docsify-to-pdf": "0.0.5"
|
||||
}
|
||||
}
|
@ -1,3 +1,3 @@
|
||||
All the curriculum's sketchnotes can be downloaded here.
|
||||
|
||||
Credits:
|
||||
Credits: Tomomi Imura
|