<commit_before>---
layout: post
title:  "Machine learning, but not understanding"
date:   2021-04-10
categories: post
---

{% newthought 'In the expression _machine learning_' %}, are the machines actually learning anything?

In the book "Artificial Intelligence, a guide for thinking humans" <NAME> explains that

{% epigraph
"Learning in neural networks simply consists in gradually modifying the weights on connections so that each output’s error gets as close to 0 as possible on all training examples."
"<NAME>" "Artificial Intelligence, a guide for thinking humans" %}

Let's explore what "learning" means for machine learning, guided by Mitchell's book. More specifically, we will concentrate on "deep learning", a branch of machine learning that has powered most of the recent advances in artificial intelligence.

<!--more-->

# An extremely short explanation of deep learning

{% newthought 'Deep learning uses layers of "units"'%}{% sidenote 'mn-id-units' 'Also called _neurons_, but some people, including Mitchell and I, prefer the more generic _units_ term, to not confuse with biological neurons.' %} to extract patterns from labeled data. The internal layers are called "hidden layers". The last layer is called the "output layer", or the classification layer.

In the following figure (from Mitchell's book), a neural network comprised of several hidden layers (only one shown) was trained to classify handwritten digits. The output layer has ten units, one for each possible digit.

{% maincolumn 'images/2021-04-10/neural-network.png' 'From Mitchell, _Artificial Intelligence_, chapter 2' %}

How does a neural network learn? Back to Mitchell's quote:

{% epigraph "Learning in neural networks simply consists in gradually modifying the weights on connections so that each output’s error gets as close to 0 as possible on all training examples."
"<NAME>" "Artificial Intelligence, a guide for thinking humans" %}

Going through the sentence pieces:

- _training examples_: The labeled examples we present to the network to train it. For example, we present a picture of a square or a triangle and its corresponding label, "square" or "triangle".
- _output's error_: How far the network's prediction is from the correct label of the example picture.
- _weights on connections_: A large-precision decimal number that adjusts the output of a unit in one layer to the input of a unit in the next layer. The weights are where the "knowledge" of the neural network is encoded.
- _gradually modifying_: This is the neural network learning process. An algorithm carefully modifies the weights on the connections to get closer to the expected output. Repeating the adjustment step over time (many, many times) allows the network to learn from the training examples.

# An important consequence of this process

{% epigraph "The machine learns what it observes in the data rather than what you (the human) might observe. If there are statistical associations in the training data, even if irrelevant to the task at hand, the machine will happily learn those instead of what you wanted it to learn." "<NAME>" "Artificial Intelligence, a guide for thinking humans" %}

Thus, neural networks are not "learning" in the sense that we would understand the term. They are not learning higher-level concepts from the samples used to train them. They are extracting patterns from the data presented to them during training (and they assume that the labels are correct). That's all.

Or, as Mitchell puts more eloquently:

{% epigraph  "The phrase “barrier of meaning” perfectly captures an idea that has permeated this book: humans, in some deep and essential way, understand the situations they encounter, whereas no AI system yet possesses such understanding. While state-of-the-art AI systems have nearly equaled (and in some cases surpassed) humans on certain narrowly defined tasks, these systems all lack a grasp of the rich meanings humans bring to bear in perception, language, and reasoning. This lack of understanding is clearly revealed by the un-humanlike errors these systems can make; by their difficulties with abstracting and transferring what they have learned; by their lack of commonsense knowledge; ... The barrier of meaning between AI and human-level intelligence still stands today."
"<NAME>" "Artificial Intelligence, a guide for thinking humans" %}

Should we be concerned that deep learning is not "learning"? We should, if we don't understand what it implies for real-life applications.

In the next sections we will explore how neural networks lack the grasp of "rich meanings we humans bring to bear in perception", illustrating it with some "un-humanlike errors these systems can make; by their difficulties with abstracting and transferring what they have learned; by their lack of commonsense knowledge".{% sidenote '' 'You can run the examples used in the text with the Jupyter notebook on [this GitHub repository](https://github.com/fau-masters-collected-works-cgarbin/machine-learning-but-not-understanding). The examples use small pictures ro tun quickly on any computer.' %}

# Telling squares and triangles apart

{% newthought 'We will see how a neural network' %} trained to tell squares and triangles apart behaves.

For human beings, the pictures below show squares and triangles. Some are small, some are large, some are in a light background, some are in a darker background. But they are all clearly either a square or a triangle in a frame.

{% maincolumn 'images/2021-04-10/squares-triangles.png' ''%}

In this section we will go through the typical process of training a neural network to classify squares and triangles:

1. Get a dataset with labeled pictures of squares and triangles
1. Split the dataset into a training set and a test set
1. Train the network with the training set
1. Validate the neural network accuracy with the test set

After we are done with that, we will predict similar images to see how the network handles them.

## The "squares vs. triangles" training examples

This is how some of the training images look like. Each picture is a square or a triangle in different positions. The dataset has hundreds of these pictures.

{% maincolumn 'images/2021-04-10/output_12_0.png' ''%}

## The "squares vs. triangles" neural network

{% marginfigure 'mf-id-cnn' 'images/2021-04-10/cnn.png' 'Typical representation of a CNN. From the excellent _[Stanford CS231n: Convolutional Neural Networks for Visual Recognition](https://cs231n.github.io/convolutional-networks/)_ class.' %}<commit_msg>Fix typo<commit_after>---
layout: post
title:  "Machine learning, but not understanding"
date:   2021-04-10
categories: post
---

{% newthought 'In the expression _machine learning_' %}, are the machines actually learning anything?

In the book "Artificial Intelligence, a guide for thinking humans" <NAME> explains that

{% epigraph
"Learning in neural networks simply consists in gradually modifying the weights on connections so that each output’s error gets as close to 0 as possible on all training examples."
"<NAME>" "Artificial Intelligence, a guide for thinking humans" %}

Let's explore what "learning" means for machine learning, guided by Mitchell's book. More specifically, we will concentrate on "deep learning", a branch of machine learning that has powered most of the recent advances in artificial intelligence.

<!--more-->

# An extremely short explanation of deep learning

{% newthought 'Deep learning uses layers of "units"'%}{% sidenote 'mn-id-units' 'Also called _neurons_, but some people, including Mitchell and I, prefer the more generic _units_ term, to not confuse with biological neurons.' %} to extract patterns from labeled data. The internal layers are called "hidden layers". The last layer is called the "output layer", or the classification layer.

In the following figure (from Mitchell's book), a neural network comprised of several hidden layers (only one shown) was trained to classify handwritten digits. The output layer has ten units, one for each possible digit.

{% maincolumn 'images/2021-04-10/neural-network.png' 'From Mitchell, _Artificial Intelligence_, chapter 2' %}

How does a neural network learn? Back to Mitchell's quote:

{% epigraph "Learning in neural networks simply consists in gradually modifying the weights on connections so that each output’s error gets as close to 0 as possible on all training examples."
"<NAME>" "Artificial Intelligence, a guide for thinking humans" %}

Going through the sentence pieces:

- _training examples_: The labeled examples we present to the network to train it. For example, we present a picture of a square or a triangle and its corresponding label, "square" or "triangle".
- _output's error_: How far the network's prediction is from the correct label of the example picture.
- _weights on connections_: A large-precision decimal number that adjusts the output of a unit in one layer to the input of a unit in the next layer. The weights are where the "knowledge" of the neural network is encoded.
- _gradually modifying_: This is the neural network learning process. An algorithm carefully modifies the weights on the connections to get closer to the expected output. Repeating the adjustment step over time (many, many times) allows the network to learn from the training examples.

# An important consequence of this process

{% epigraph "The machine learns what it observes in the data rather than what you (the human) might observe. If there are statistical associations in the training data, even if irrelevant to the task at hand, the machine will happily learn those instead of what you wanted it to learn." "<NAME>" "Artificial Intelligence, a guide for thinking humans" %}

Thus, neural networks are not "learning" in the sense that we would understand the term. They are not learning higher-level concepts from the samples used to train them. They are extracting patterns from the data presented to them during training (and they assume that the labels are correct). That's all.

Or, as Mitchell puts more eloquently:

{% epigraph  "The phrase “barrier of meaning” perfectly captures an idea that has permeated this book: humans, in some deep and essential way, understand the situations they encounter, whereas no AI system yet possesses such understanding. While state-of-the-art AI systems have nearly equaled (and in some cases surpassed) humans on certain narrowly defined tasks, these systems all lack a grasp of the rich meanings humans bring to bear in perception, language, and reasoning. This lack of understanding is clearly revealed by the un-humanlike errors these systems can make; by their difficulties with abstracting and transferring what they have learned; by their lack of commonsense knowledge; ... The barrier of meaning between AI and human-level intelligence still stands today."
"<NAME>" "Artificial Intelligence, a guide for thinking humans" %}

Should we be concerned that deep learning is not "learning"? We should, if we don't understand what it implies for real-life applications.

In the next sections we will explore how neural networks lack the grasp of "rich meanings we humans bring to bear in perception", illustrating it with some "un-humanlike errors these systems can make; by their difficulties with abstracting and transferring what they have learned; by their lack of commonsense knowledge".{% sidenote '' 'You can run the examples used in the text with the Jupyter notebook on [this GitHub repository](https://github.com/fau-masters-collected-works-cgarbin/machine-learning-but-not-understanding). The examples use small pictures to run quickly on any computer.' %}

# Telling squares and triangles apart

{% newthought 'We will see how a neural network' %} trained to tell squares and triangles apart behaves.

For human beings, the pictures below show squares and triangles. Some are small, some are large, some are in a light background, some are in a darker background. But they are all clearly either a square or a triangle in a frame.

{% maincolumn 'images/2021-04-10/squares-triangles.png' ''%}

In this section we will go through the typical process of training a neural network to classify squares and triangles:

1. Get a dataset with labeled pictures of squares and triangles
1. Split the dataset into a training set and a test set
1. Train the network with the training set
1. Validate the neural network accuracy with the test set

After we are done with that, we will predict similar images to see how the network handles them.

## The "squares vs. triangles" training examples

This is how some of the training images look like. Each picture is a square or a triangle in different positions. The dataset has hundreds of these pictures.

{% maincolumn 'images/2021-04-10/output_12_0.png' ''%}

## The "squares vs. triangles" neural network

{% marginfigure 'mf-id-cnn' 'images/2021-04-10/cnn.png' 'Typical representation of a CNN. From the excellent _[Stanford CS231n: Convolutional Neural Networks for Visual Recognition](https://cs231n.github.io/convolutional-networks/)_ class.' %}
{'fraction_non_alphanumeric': 0.057822869784117784, 'fraction_numerical': 0.008543748569684949, 'mean_word_length': 4.729895104895105, 'pattern_counts': {'":': 0, '<': 15, '<?xml version=': 0, '>': 15, 'https://': 4, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '135251', 'n_tokens_mistral': 3253, 'n_tokens_neox': 2988, 'n_words': 1905}