New to ML? Don't know where to start?

Machine learning may seem complex at first, given the math, background understanding, and code involved. However, if you truly want to learn, the best place to start is by building and messing around with a model. FastiAI makes it super easy to create and modify models to best solve your problem! Don't worry too much if you don't understand, we will get there.

Our First Model

As I have said above, the best way to learn is by actually creating your first model

from fastai.vision.all import * #IMPORT 
path = untar_data(URLs.PETS)/'images' #DATA SET 

def is_cat(x): return x[0].isupper() #Labels for the dataset (This dataset cat labels begin w/ uppercase letter)

#Create dataset (Training data, test data) and correctly gets imgs w/ labels
dls = ImageDataLoaders.from_name_func( 
    path, get_image_files(path), valid_pct=0.2, seed=42,
    label_func=is_cat, item_tfms=Resize(224))

learn = cnn_learner(dls, resnet34, metrics=error_rate) #Creating architecture
learn.fine_tune(1) #Training
epoch train_loss valid_loss error_rate time
0 0.154482 0.022808 0.007442 00:56
epoch train_loss valid_loss error_rate time
0 0.057639 0.017561 0.006089 01:09

Look at that, we created our first model and all with a few lines of code.

Why don't we test our cat classifier?

img = PILImage.create(image_cat())
img.to_thumb(192)
uploader = widgets.FileUpload()
uploader
img = PILImage.create(uploader.data[0])
img.to_thumb(192)
is_cat,_,probs = learn.predict(img)
print(f"Is this a cat?: {is_cat}.")
print(f"Probability it's a cat: {probs[1].item():.6f}")
Is this a cat?: True.
Probability it's a cat: 0.999998

Fantastic, you can now classify cats!

Deep Learning Is Not Just for Image Classification

Often people think machine learning model are used only for images, this is not true at all!
Below you will see numerous other types of models, each with its benifits!

A segmentation model

path = untar_data(URLs.CAMVID_TINY)
dls = SegmentationDataLoaders.from_label_func( #Segmentation
    path, bs=8, fnames = get_image_files(path/"images"),
    label_func = lambda o: path/'labels'/f'{o.stem}_P{o.suffix}',
    codes = np.loadtxt(path/'codes.txt', dtype=str)
)

learn = unet_learner(dls, resnet34)
learn.fine_tune(8)
epoch train_loss valid_loss time
0 2.821857 3.201599 00:05
epoch train_loss valid_loss time
0 2.189840 1.802189 00:04
1 1.831589 1.741264 00:04
2 1.633543 1.287880 00:04
3 1.459935 1.189295 00:04
4 1.315327 1.003694 00:04
5 1.188064 0.926959 00:04
6 1.080196 0.872466 00:04
7 0.997371 0.871319 00:04
learn.show_results(max_n=2, figsize=(10,12))

A natural language model

from fastai.text.all import *

dls = TextDataLoaders.from_folder(untar_data(URLs.IMDB), valid='test')
learn = text_classifier_learner(dls, AWD_LSTM, drop_mult=0.5, metrics=accuracy)
learn.fine_tune(2, 1e-2)
learn.predict("I really liked that movie!")
('pos', tensor(1), tensor([0.0228, 0.9772]))

A salary prediction model (Regression)

from fastai.tabular.all import *
path = untar_data(URLs.ADULT_SAMPLE)

dls = TabularDataLoaders.from_csv(path/'adult.csv', path=path, y_names="salary",
    cat_names = ['workclass', 'education', 'marital-status', 'occupation',
                 'relationship', 'race'],
    cont_names = ['age', 'fnlwgt', 'education-num'],
    procs = [Categorify, FillMissing, Normalize])

learn = tabular_learner(dls, metrics=accuracy)
learn.fit_one_cycle(3)
epoch train_loss valid_loss accuracy time
0 0.372949 0.361306 0.833077 00:05
1 0.354939 0.348455 0.841830 00:05
2 0.349614 0.347378 0.840756 00:05

The below is a reccomendation model (AKA Regression model)

from fastai.collab import *
path = untar_data(URLs.ML_SAMPLE)
dls = CollabDataLoaders.from_csv(path/'ratings.csv')
learn = collab_learner(dls, y_range=(0.5,5.5))
learn.fine_tune(10)
epoch train_loss valid_loss time
0 1.523757 1.424118 00:00
epoch train_loss valid_loss time
0 1.376706 1.363828 00:00
1 1.282471 1.173324 00:00
2 1.034186 0.848724 00:00
3 0.805283 0.694374 00:00
4 0.709625 0.654900 00:00
5 0.652975 0.645875 00:00
6 0.634861 0.639299 00:00
7 0.611313 0.637229 00:00
8 0.617857 0.636715 00:00
9 0.612095 0.636567 00:00
learn.show_results()
userId movieId rating rating_pred
0 87.0 48.0 5.0 4.045295
1 73.0 92.0 4.0 4.072179
2 66.0 26.0 3.0 4.015839
3 66.0 30.0 3.0 3.367572
4 4.0 46.0 3.5 3.269851
5 82.0 84.0 4.0 3.817361
6 90.0 79.0 4.0 4.012848
7 61.0 65.0 4.0 3.507185
8 88.0 7.0 4.5 4.166433

Conclusion

I hope you feel more comfortable with machine learning and recognize the many benefits it can serve you :)

Questionnaire

  1. Do you need these for deep learning?

    • Lots of math T / F
    • Lots of data T / F
    • Lots of expensive computers T / F
    • A PhD T / F
  2. Name five areas where deep learning is now the best in the world.
    Vision, Natural language processing, Medicine, Robotics, and Games

  3. What was the name of the first device that was based on the principle of the artificial neuron?
    Mark I Perceptron
  4. Based on the book of the same name, what are the requirements for parallel distributed processing (PDP)?
    Processing units, State of activation, Output function, Pattern of connectivity, Propagation rule, Activation rule, Learning rule, Environment
  5. What were the two theoretical misunderstandings that held back the field of neural networks?
    Single layer network unable to learn simple mathimatical functions. More layers make network too big and slow to be useful.
  6. What is a GPU?
    A graphics card is a processor that can handle 1000's of tasks at the same time. Particularly great for deep learning.
  7. Open a notebook and execute a cell containing: 1+1. What happens?
    2
  8. Follow through each cell of the stripped version of the notebook for this chapter. Before executing each cell, guess what will happen.
  9. Complete the Jupyter Notebook online appendix.
  10. Why is it hard to use a traditional computer program to recognize images in a photo?
    They are missing the weight assignment needed to recognize patterns within images to accomplish the task.
  11. What did Samuel mean by "weight assignment"?
    The weight is another form of input that has direct influence on the model's performance.
  12. What term do we normally use in deep learning for what Samuel called "weights"?
    Parameters
  13. Draw a picture that summarizes Samuel's view of a machine learning model.
    https://vikramriyer.github.io/assets/images/machine_learning/fastai/model.jpeg
  14. Why is it hard to understand why a deep learning model makes a particular prediction?
    There are many layers, each with numerous neurons. Therefore, it gets complex really fast what each neuron is looking for when viewing an image, and how that impacts the perediction.
  15. What is the name of the theorem that shows that a neural network can solve any mathematical problem to any level of accuracy?
    Universal approximation theorem
  16. What do you need in order to train a model?
    Data with labels
  17. How could a feedback loop impact the rollout of a predictive policing model?
    The more the model is used the more biased the data becomes, and therefore, the more bias the model becomes.
  18. Do we always have to use 224×224-pixel images with the cat recognition model?
    No.
  19. What is the difference between classification and regression?
    Classification is about categorizing/labeling objects. Regression is about predicting numerical quantities, such as temp.
  20. What is a validation set? What is a test set? Why do we need them?
    The validation set measures the accuracy of the model during training.
    The test set is used during the final evaluation to test the accuracy of the model.

    We need both of them because the validation set could cause some bias in the model as we would are fitting the model towards it during training. However, the test set removes this and evaluates the model on unseen data, thereby, giving an accurate metric of accuracy.

  21. What will fastai do if you don't provide a validation set?
    Fastai will automatically create a validation dataset for us.
  22. Can we always use a random sample for a validation set? Why or why not?
    It is not reccomended where order is neccessary, example ordered by time.
  23. What is overfitting? Provide an example.
    This is when the model begins to fit to the training data rather than generalizing for similar unseen datasets. For example a model that does amazing on the training data, but performs poorly on test data: Good indication that model may have overfitted.
  24. What is a metric? How does it differ from "loss"?
    The loss is the value calculated by the model to determine the impact each neuron has on the end result: Therefore, the value is used by models to measure its performance. The metric gives us, humans, an overall value of how accurate the model was: Therefore, a value we use to understand the models performance.
  25. How can pretrained models help?
    A pretrained model already has the fundementals. Therefore, it can use this prior knowledge to learn faster and perform better on similer datasets.
  26. What is the "head" of a model?
    The final layers from the pretrained model that have been replaced with new layers (w/ randomized weights) to better align with our dataset. These final layers are often the only thing trained while the rest of the model is frozen.
  27. What kinds of features do the early layers of a CNN find? How about the later layers?
    The early layers often extract simple features like edges.
    The later layers are more complex and can identify advanced features like faces.
  28. Are image models only useful for photos?
    No. Lots of other forms of data can be converted into images that can be used to solve such non-photo data problems.
  29. What is an "architecture"?
    This is the structure of the model we use to solve the problem.
  30. What is segmentation?
    Method of labeling all pixels within an image and masking it.
  31. What is y_range used for? When do we need it?
    Specifies the range of values that can be perdicted by model. For example, movie rating's 0-5.
  32. What are "hyperparameters"?
    These are the parameters that we can adjust to help the model perform better (Ex: Epochs).
  33. What's the best way to avoid failures when using AI in an organization?
    Begin with the most simplest model and then slowly building up to more complexity. This way you have something working and don't get lost as you add onto the model.

Further Research

Each chapter also has a "Further Research" section that poses questions that aren't fully answered in the text, or gives more advanced assignments. Answers to these questions aren't on the book's website; you'll need to do your own research!

  1. Why is a GPU useful for deep learning? How is a CPU different, and why is it less effective for deep learning? </br> Modern GPUs provide a far superior processing power, memory bandwidth, and efficiency over the CPU.

  2. Try to think of three areas where feedback loops might impact the use of machine learning. See if you can find documented examples of that happening in practice. </br> I believe feedback loops are primarly great for recommendation models. This is because the feedback loops create a bias model. For example, if a viewer like a movie, he/she will like similer movies. Being bias here towards particular types of movie is the best way to keep the viewer engaged.