You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
As a first step, let's focus on getting Python installed and running. I am going to focus on a Windows-targeted installation; if you're using any other OS please see the OS-specific installation instructions [here](https://realpython.com/installing-python/).
3
-
For a Windows installation, navigate your browser to the official python [Releases for Windows](https://www.python.org/downloads/windows/) page. Near the top of the page there should be a link to the *Latest Python 3 Release - Python 3.X.X*; click it! The simplest installation method involves downloading the executable installer and merely executing it.
4
-
<divstyle="border:1pxsolidblack">
5
-
BRIEF NOTE ON VERSIONS AND ARCHITECTURES
6
-
7
-
Python is presently operating under version 3 (Python 3), yet still provides downloads and some support for Python 2. There are few linguistic differences between the versions, yet several of the more modern ML packages specifically support Python 3. Please download Python 3 for this tutorial. If you require the legacy Python 2 for any reason, both versions can be installed side-by-side.
8
-
9
-
Python 3 supports both 32- and 64-bit architectures, yet some of the more important ML packages only support 64-bit. So, let's focus on the 64-bit version.
10
-
</div>
11
-
12
-
## Setting environment variables
13
-
14
-
Let's make an admission, Windows is stupid. And it's not just Windows, it's all operating systems! They facilitate you in finding and organizing files and executables on your machine, yet they rarely fix themselves or spontaneously learn things. This is a long way of saying that we need to manually teach Windows the command we will use to start python (and pip), and what Windows should do with that command. This is deemed 'setting environmental variables' or adding to a search path. [These](https://datatofish.com/add-python-to-windows-path/) instructions should help you teach Windows how to do what you want.
1
+
*Correct!*
15
2
16
-
## Testing Python
17
-
Let's quickly verify that python is installed and that the environmental variables are set. Launch your Command Line, which can be done in Windows by search "CMD" and executing it.
18
-
This will give you an "old school" DOS prompt, that black screen that no one under the age of 30 remembers, and no one under 45 remembers how to use. As a good habit, before launching Python, I set the directory to the root, which typically for Windows is the C:\ drive. This can be done by typing "cd\", meaning change director to nothing.
3
+
Glad you are paying attention. 😉
19
4
20
-
```console
21
-
C:\Users\ross.hoehn>cd\
22
-
C:\>
23
-
```
24
-
After achieving the root directory, and thanks to us setting the environmental variables above, merely typing "python" at the c-prompt will launch the Python environment. You will immediately be greeted with the version information of your Python install (mine is 3.6.7 and is 64 bit).
5
+
## Install Python
6
+
As a first step, let's make sure Python is installed and running. To test if it is installed and configured already, type `python` into your terminal. If it isn't installed yet, it should say something like "python is a unknown command". If it is installed, it will open the python environment, and should look something like this:
25
7
26
8
```console
27
-
C:\>python
28
9
Python 3.6.7 (v3.6.7:6ec5cf24b7, Oct 20 2018, 13:35:33) [MSC v.1900 64 bit (AMD64)] on win32
29
10
Type "help", "copyright", "credits" or "license" for more information.
30
11
>>>
31
12
```
13
+
If you see this type `exit()` to return to the command prompt. If not, you need install the latest Python 3 release for your OS: [Windows](https://www.python.org/downloads/windows/), [Mac](https://www.python.org/downloads/mac-osx/), [Linux/Unix](https://www.python.org/downloads/source/). After you install python, you need to add it to your PATH variable to use the `python` command shortcut. You can find directions for how to do this [here](https://www.tutorialspoint.com/python/python_environment.htm)
32
14
33
-
You will also know that you are in the Python environment as the prompt at which you type is represented by ">>>". Now, to install some packages for our project, we cannot do this from the Python environment, so we will need the means to leave the environment. This command is conveniently "exit()".
34
-
```console
35
-
>>> exit()
36
-
C:\>
37
-
```
15
+
You will need to close and reopen you command prompt before the new environmental variable is recognized. Type `python` to check if it is set up correctly. You will also know that you are in the Python environment as the prompt at which you type is represented by ">>>". Now, to install some packages for our project, we cannot do this from the Python environment, so we will need the means to leave the environment. This command is conveniently `exit()`.
38
16
39
17
*close this issue if you correctly installed Python*
Copy file name to clipboardExpand all lines: responses/02-import.md
+12-5Lines changed: 12 additions & 5 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -1,20 +1,27 @@
1
1
## Pip Installing Packages
2
2
3
-
To complete this project we will need a few packages; these are add-ons available to Python but not included in the base install. Very luckily, we installed the latest Python 3, which includes the Pip Installation module of Python. Pip is a fast and easy way to install packages and their dependencies. As we are doing an NN-based project, we will need to use TensorFlow. For those who only recognize TensorFlow by its association with NNs, it may be shocking to learn that TensorFlow is a more general tool for the manipulation of mathematical entities called tensors. NNs are just a single use of tenor mechanics, and therefore TensorFlow.
3
+
To complete this project we will need a few packages; these are add-ons available to Python but not included in the base install. Very luckily, we installed the latest Python 3, which includes the Pip Installation module of Python. Pip is a fast and easy way to install packages and their dependencies. As we are doing an NN-based project, we will need to use TensorFlow. For those who only recognize TensorFlow by its association with NNs, it may be shocking to learn that TensorFlow is a more general tool for the manipulation of mathematical entities called tensors. NNs are just a single use of tenor mechanics, and therefore TensorFlow.
4
4
5
5
As tensors and TensorFlow can be fairly complex to manage, there exists another package named Keras that acts as a high-level API (Application Programming Interface), allowing users to easily generation, define and manipulate the structures within TensorFlow.
6
+
6
7
Finally, as we wish to visualize aspects of our dataset and generate some informational plots, we will want Python's Mathematical Plotting Library, MatPlotLib. Additionally, MatPlotLib facilitates the generation of graphical plots in new windows, even when executed from the Command Line. And finally, to handle numerical computations and array-based operations we will import Numpy.
7
8
8
9
Starting from the c-prompt (where we left off above), you will need to tell the easy Python package manager (a program that helps you install, uninstall, update and upgrade ancillary features to a larger application) that we want to install Numpy, MatPlotLib, and Tensorflow.
10
+
9
11
```console
10
-
C:\>pip install Numpy
11
-
C:\>pip install MatPlotLib
12
-
C:\>pip install Tensorflow
12
+
pip install Numpy
13
+
pip install MatPlotLib
14
+
pip install Tensorflow
13
15
```
16
+
17
+
If you encounter a permissions error, you may need to add `--user` to the end of each install. For example `pip install Numpy --user`.
18
+
14
19
After each of these actions, pip will display that it is downloading and installing the package. If you have an existing (but not up-to-date) version, pip will first uninstall the old version before installing the new. If you have previously installed the current version of any package, pip will inform you that the 'requirement already exists'.
20
+
15
21
Now we can test if Python has installed our Tensorflow correctly (this being the testier of the three packages), by returning to the Python Environment and printing the version of our Tensorflow:
22
+
16
23
```console
17
-
C:\>python
24
+
python
18
25
Python 3.6.7 (v3.6.7:6ec5cf24b7, Oct 20 2018, 13:35:33) [MSC v.1900 64 bit (AMD64)] on win32
19
26
Type "help", "copyright", "credits" or "license" for more information.
*That's right!* Remember that TensorFlow is a more general tool for working with *tensors*. Therefore, it is not only used for Neural Networks.
2
+
1
3
## Import Packages
2
-
We will be executing all of our Python code from the Window's Command Line (CMD). Although CMD doesn't afford us syntactic color coding, it provides us with a degree of immediate responsiveness and the ability to see all messages passed during operations instead of just fail or pass state messages. Note that we have provided you syntactic color coding in this tutorial as it, generally, makes the code more readable.
4
+
We will be executing all of our Python code using the command line interface and a Python interpretor. This provides us with a degree of immediate responsiveness and the ability to see all messages passed during operations instead of just fail or pass state messages.
5
+
3
6
To import packaged into a specific Python environment, you need only use the 'import' command.
7
+
4
8
```console
5
-
C:\>python
9
+
python
6
10
Python 3.6.7 (v3.6.7:6ec5cf24b7, Oct 20 2018, 13:35:33) [MSC v.1900 64 bit (AMD64)] on win32
7
11
Type "help", "copyright", "credits" or "license" for more information.
8
12
>>> import numpy
9
13
>>>
10
14
```
15
+
11
16
We are also able to give packages nicknames, really useful so we don't have to type out matplotlib or tensorflow thousands of times. We assign these nicknames by "importing as".
17
+
12
18
```console
13
19
>>> import matplotlib.pyplot as plt
14
20
>>>
15
21
```
22
+
16
23
And finally, we import Tensorflow and its Keras interface. Note that we can always call Keras as a Tensorflow object (i.e. tf.keras.command() ), but that takes too much typing. To save us time, we will import Keras from Tensorflow.
24
+
17
25
```console
18
26
>>> import tensorflow as tf
19
27
>>> from tensorflow import keras
20
28
>>>
21
29
```
30
+
22
31
All of our prerequisite packages are now installed!
23
32
24
33
*Leave a comment after you have imported the libraries above for the next step.*
Copy file name to clipboardExpand all lines: responses/02-test-image.md
+6-1Lines changed: 6 additions & 1 deletion
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -2,6 +2,7 @@
2
2
Now we are ready to roll! First, we must admit that it takes a lot of data to train a NN, and 70,000 examples is an anemic dataset. So instead of doing a more traditional 70/20/10 or 80/10/10 percent split between training/validating/testing, we will do a simple 6:1 ratio of training:testing (note that this is not best practices, but when there is limited data it may be your only recourse).
3
3
4
4
We first load in the dataset from the Keras package:
The first line merely assigns the name fashion_mnist to a particular dataset located within Keras' dataset library. The second line defines four arrays containing the training and testing data, cleaved again into separate structures for images and labels, and then loads all of that data into our standup of Python. The training data arrays will be used to (duh) train the model, and the testing arrays will allow us to evaluate the performance of our model.
19
+
20
+
The first line merely assigns the name fashion_mnist to a particular dataset located within Keras' dataset library. The second line defines four arrays containing the training and testing data, cleaved again into separate structures for images and labels, and then loads all of that data into our standup of Python. The training data arrays will be used to --you guessed it-- train the model, and the testing arrays will allow us to evaluate the performance of our model.
19
21
20
22
### Look at data
21
23
It's always nice to be able to show that we've actually done something; ever since kindergarten there has been no better way than with a picture! You'll note that we pip installed and imported MatPlotLib, a library for plots and graphs. Here we'll use it to visualize an example of the Fashion MNIST dataset.
24
+
22
25
```console
23
26
>>> plt.figure()
24
27
<Figure size 640x480 with 0 Axes>
@@ -29,7 +32,9 @@ It's always nice to be able to show that we've actually done something; ever sin
29
32
>>> plt.grid(False)
30
33
>>> plt.show()
31
34
```
35
+
32
36
The first command basically generates a figure object that will be manipulated by commands 2 through 4. Command 2 specifies what it is that we shall be plotting: the first element from the train_images array. *NOTE*: Recall that python is an inclusive counting language, meaning that it numbers/indexes things starting from zero, not one! And the final command, "show()", tells Python to generate this figure in an external (from CMD) window.
37
+
33
38
Your window should contain a plot that looks similar to Figure 3. Also, be aware that after plt.show(), Python will not return you to a command line until the newly generated window (containing our super nice picture) is closed. Upon closing the window, you will be able to continue entering Python commands.
34
39
35
40

This is why we used the train_labels array as the second argument.
4
+
1
5
## Evaluating Our Model
2
6
Now we are working with a functional and trained NN model. Following our logic from the top, we have built a NN that intakes a (28,28) array, flattens the data into a (784) array, compiled and trained 2 dense layers, and the softmax activation function of the final output layer will provide a probability that the image belongs to each of the 10 label categories.
7
+
3
8
Our model can be evaluated by using the model.evaluate command, that takes in the images and labels so that it can compare its predictions to the ground truth provided by the labels. Model.evaluate provides two outputs, the value of the loss function over the testing examples, and the accuracy of the model over this testing population. The important output for us is the model's accuracy.
This is great! Our model performs at an accuracy of 87.21%. As good as that is, it is lower than the model accuracy promised above (89.01%). This lower performance is due to the model overfitting on the training data. Overfitting occurs when there are too many parameters within the model when compared to the number of training instances; this allows the model to over learn on those limited examples. Overfitting leads to better model performance over non-training data.
12
19
13
20
That said, 87.21% is a decent number! Let's finally learn how you can feed our model the series of test examples from the test_images array, and have it provide its predictions.
As we can see, most of the entries in our prediction array are very close to 0. The entry that stands out is predictions[0][9] at .8658, or 86.58%, certainty that this image should be classified as a boot! This happens to be true and can be check by you if you repeat the code above to visualize test_images[0].
29
+
As we can see, most of the entries in our prediction array are very close to 0. They are written in scientific notation--the value after the *e* being the number decimal places to adjust the value (for example 5.1 e-04 is actually 0.00051). The entry that stands out is predictions[0][9] at .8658, or 86.58%, certainty that this image should be classified as a boot!
22
30
23
31
If you prefer to not look through a list to determine the class label, we can simplify the output by:
32
+
24
33
```console
25
34
>>> numpy.argmax(predictions[0])
26
35
9
27
36
>>>
28
37
```
38
+
29
39
Finally, we can verify this prediction by looking at the label ourselves:
40
+
30
41
```console
31
42
>>> test_labels[0]
32
43
9
@@ -36,16 +47,18 @@ Finally, we can verify this prediction by looking at the label ourselves:
36
47
*To complete this course, leave a comment with the letter (A,B,C,D) that best answers the following question:*
Copy file name to clipboardExpand all lines: responses/03-model.md
+5Lines changed: 5 additions & 0 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -1,9 +1,14 @@
1
+
*Nailed it!*
2
+
3
+
Remember, the label arrays are only used to associate images with their lables.
4
+
1
5
## Model Generation
2
6
Every NN is constructed from a series of connected layers that are full of connection nodes. Simple mathematical operations are undertaken at each node in each layer, yet through the volume of connections and operations, these ML models can perform impressive and complex tasks.
3
7
4
8
Our model will be constructed from 3 layers. The first layer – often referred to as the Input Layer – will intake an image and format the data structure in a method acceptable for the subsequent layers. In our case, this first layer will be a Flatten layer that intakes a multi-dimensional array and produces an array of a single dimension, this places all the pixel data on an equal depth during input. Both of the next layers will be simple fully connected layers, referred to as Dense layers, with 128 and 10 nodes respectively. These fully connected layers are the simplest layer in the sense of understanding, yet allow for the greatest number of layer-to-layer connections and relationships.
5
9
6
10
The final bit of hyper-technical knowledge you'll need to learn is that each layer can have its own particular mathematical operation. These activation functions determine the form and relationship between the information provided by the layer. The first dense layer will feature a Rectified Linear Unit (ReLU) Activation Function that outputs values between zero and 1; mathematically, the activation function behaves like f(x)=max(0,x). The final layer uses the softmax activation function. This function also produces values in the 0-1 range, BUT generates these values such that the sum of the outputs will be 1! This makes the softmax a layer that is excellent at outputting probabilities.
11
+
7
12
```console
8
13
>>> model = keras.Sequential([ keras.layers.Flatten(input_shape=(28,28)), keras.layers.Dense(128, activation=tf.nn.relu), keras.layers.Dense(10, activation=tf.nn.softmax)])
0 commit comments