Deep (learning) like Jacques Cousteau – Part 2 – Scalars

[This article was first published on Embracing the Random | R, and kindly contributed to R-bloggers]. (You can report issue about the content on this page here)
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

(TL;DR: Scalars are single numbers.)


I have so many scalars!
Opie, the open source snake

I’m sorry…that was lame…

we covered some basic concepts regarding sets on our journey to
understanding vectors and matrices.

Let’s do this!

Today’s topic: Scalars

What’s a scalar?

A scalar is a single number! This seems very simple (and it is). But we
need to know this to understand operations like scalar multiplication
or statements like “The result of multiplying this by that is a

We will use the notation from Goodfellow, Ian, et
and depict them in lower case,
italicised letters like this:

How can we define the types of numbers our scalars should represent?

Say, we define our arbitrary scalar, , as a number from the set of
We would show this set membership like this:

The ‘’ symbol means
‘is a member/is an element of/belongs to (some set)’ Pick the one you like most!
However, the whole statement is often read as ’ is a natural

The symbol ‘’ means
‘is not a member/is not an element of/does not belongs to (some set)’. Easy!

Let’s bring this back to machine learning

What are the implications of defining our scalars as natural numbers?
Let’s start with an abstract example!

  • Let’s say we start with the number , and we want to add
    some arbitrary number, , to it.
  • Let’s define as a natural number. That is,
    belongs to the set of ‘whole’, positive numbers starting with 1 and increasing with no upper bound.

Here are some of the implications of our definition of

  • cannot equal because , and
    therefore, cannot take on the value of .
  • We can never get an answer where the first decimal place is
    something other than zero. For example, there is no natural
    number, , where .

Now here is my (crappy) attempt at intuitively bringing this back to machine

  • Let’s say that our scalar, , is the value used to update the
    parameters in our model
    after some iteration of training.
  • Then we are restricted to making crude updates of at least one
  • Our algorithm may never converge and we might see the values of our
    evaluation metric jumping about erratically as training progresses.

This might not be an academically rigorous explanation, but it’s
hopefully good enough to build some intuition.

We’ll define our scalars as real numbers

We’ll make our universe of numbers into something larger where our scalars can take on more than just whole, positive values. We will
define our arbitrary scalars, , as coming from the set of real
numbers. That is:

How can we represent scalars in R?

R technically has no scalar data type! From Hadley Wickham’s ‘Advanced
R’, 1st edition
, we can find this
in the ‘Data Structures’

Note that R has no 0-dimensional, or scalar types. Individual numbers
or strings, which you might think would be scalars, are actually
vectors of length one.

But in practice, we can emulate our real number scalar by doing something like this:

<span class="n">x</span><span class="w"> </span><span class="o"><-</span><span class="w"> </span><span class="m">123.532</span><span class="w">
</span><span class="n">print</span><span class="p">(</span><span class="n">x</span><span class="p">)</span><span class="w">
## [1] 123.532

In the same section, we also find out that to test whether something is
a vector in R, one must use is.atomic():

<span class="n">print</span><span class="p">(</span><span class="nf">is.atomic</span><span class="p">(</span><span class="n">x</span><span class="p">))</span><span class="w">
## [1] TRUE

Yes, we have ourselves a vector! How many elements do we have?

<span class="n">print</span><span class="p">(</span><span class="nf">length</span><span class="p">(</span><span class="n">x</span><span class="p">))</span><span class="w">
## [1] 1

Hooray! We have ourselves a proxy for our scalar. Now what is the data
type of our scalar?

From the numeric help page in the R documentation, we find this:

numeric is identical to double (and real). It creates a
double-precision vector of the specified length with each element
equal to 0.

Then from the double help page, we find this:

All real numbers are stored in double precision format.

Let’s test it out!

<span class="nf">class</span><span class="p">(</span><span class="n">x</span><span class="p">)</span><span class="w">
## [1] "numeric"
<span class="n">typeof</span><span class="p">(</span><span class="n">x</span><span class="p">)</span><span class="w">
## [1] "double"



We now know that scalars are members of sets. We have defined our scalars
as coming from the set of real numbers.

On to vectors!


To leave a comment for the author, please follow the link and comment on their blog: Embracing the Random | R. offers daily e-mail updates about R news and tutorials about learning R and many other topics. Click here if you're looking to post or find an R/data-science job.
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

Never miss an update!
Subscribe to R-bloggers to receive
e-mails with the latest R posts.
(You will not see this message again.)

Click here to close (This popup will not appear again)