Tensorflow is an open source, software library for numerical computation using **data flow graphs**. Nodes in the graph are called ops (short for operations), while the graph edges represent the R multidimensional data arrays (tensors) communicated between them. An op takes zero or more Tensors, performs some computation, and produces zero or more Tensors.

In this set of exercises, we will go through the basics of Tensorflow. By the end of this post, you will be able to perform the basic numerical operations between tensors and array operations. Before start solving the exercises, it is recommended to check out the tutorial that this post is based on.

Before proceeding, it might be helpful to look over the help pages for the `tf$add`

, `tf$div`

, `tf$multiply`

, `tf$matmul`

, `tf$shape`

, `tf$split`

, `tf$concat`

.

Moreover, please run the following commands:

`X <- tf$constant(matrix(c(as.integer(rnorm(200, mean = 10, sd = 2)), as.integer(rnorm(200, mean = 50, sd = 10))), nrow = 200, ncol = 2))`

`a <- tf$constant(matrix(c(3, 12), nrow = 1, ncol = 2), dtype = tf$float32, name = "a")`

`b <- tf$constant(matrix(c(5, 15), nrow = 1, ncol = 2), dtype = tf$float32, name = "b")`

`weights <- tf$constant(matrix(c(0.015, 0.02, 0.025, 0.03), nrow = 2, ncol = 2), dtype = tf$float32, name = "weights")`

Answers to the exercises are available here. If you obtained a different (correct) answer than those listed on the solutions page, please feel free to post your answer as a comment on that page.

Exercise 1

Print out the value of the tensor `a`

.

Exercise 2

Add the two tensors `a`

and `b`

.

Exercise 3

Subtract the two tensors `a`

and `b`

.

Exercise 4

Divide the two tensors `a`

and `b`

.

Exercise 5

Conduct element-wise multiplication between the two tensors `a`

and `b`

.

Exercise 6

Conduct a matrix multiplication between the tensors `a`

and `b`

.

Hint: You need to transpose `b`

.

Exercise 7

Calculate the inner product of `a`

and `weights`

. Then add the `bias`

. Assign this calculation to the object `alpha`

.

Exercise 8

Print out the shape of `X`

.

Exercise 9

Split the tensor X into `train_X`

, `validation_X`

and `test_x`

.

The dimensions should be:

train_X – (160, 2)

validation_X – (20, 2)

test_X – (20, 2)

Print out the dimensions of the splits to make sure you got it right.

Exercise 10

Concatenate the tensors `train_X`

, `validation_X`

and `test_x`

,assign it to the object `concatenated_X`

Print out the dimensions of the concatenated tensor to make sure you got it right.

R has a rich set of options for including this mathematical text on plots. We previously discussed this in a tutorial.

Answers to the exercises are available here.

**Exercise 1**

Load the *LakeHuron* data set using the `data()`

function. This data includes the lake’s water depth in feet each year from 1875—1972.

**Exercise 2**

Plot Lake Huron water depth over time. Also use `lm()`

to run a linear model and plot a best fit line. Also, use `summary()`

to look at the model fit.

**Exercise 3**

Use `str()`

on the previous `summary()`

object to see everything you can extract from the `summary()`

object. Also, be able to extract the model coefficients specifically.

**Exercise 4**

Add text for a linear model (y[i] = alpha + beta*X[i]) on your plot using the `text()`

function combined with `expression()`

and `paste()`

**Exercise 5**

Again, add text detailing a linear model, but use the model coefficients in place of alpha and beta in the previous exercise. You will need to use `substitute()`

here.

**Exercise 6**

Add the R^2 symbol to the plot. Use `expression()`

.

**Exercise 7**

Use `substitute()`

to include the model estimated R^2 on the plot.

**Exercise 8**

In bold text, include R’s notation for a p-value, Pr(>|t|), in the title of the plot.

**Exercise 9**

Use the model estimated p-value for the significance of the slope of the line in the title of the plot.

**Exercise 10**

Use demo(plotmath) or ?plotmath to learn more about using mathematical notation in R

]]>- Spatial Data Analysis: Introduction to Raster Processing (Part 1)
- Spatial Data Analysis: Introduction to Raster Processing: Part-3
- Advanced Techniques With Raster Data: Part 1 – Unsupervised Classification
- Become a Top R Programmer Fast with our Individual Coaching Program
- Explore all our (>4000) R exercises
- Find an R course using our R Course Finder directory

In this tutorial, we will examine how mathematical expressions can be included into R graphics.

We will use the

The following code loads the data and then creates a simple plot of CO2 versus time.

data("co2") plot(co2)

This is a bit ugly and there is room to clean up the plot.

We can use `expression()`

to include mathematical expressions in the plot labels.

plot(co2,ylab = expression("Atmospheric concentration of CO"[2]),las=1)

We now see that CO2 on the Y-label has a subscript for the number 2. We can include more advanced mathematical expressions as well. For example, let’s add a title to the plot.

Notice that here we used `expression()`

in combination with `paste()`

to combine multiple pieces of information.

If you want to include a variable within a mathematical expression, you need to use `substitute()`

instead of `expression()`

.

The plot shows how `text()`

can be used with `substitute()`

to insert mathematical notation with a variable `CO2_level`

that can be altered or updated.

As you can imagine, there are lots of possible mathematical expressions, depending on the specific context. You can use `?plotmath`

or `demo(plotmath)`

to explore more options.

Here are some possibilities which can be found by using `?plotmath`

.

Syntax |
Meaning |

`x + y` |
x plus y |

`x - y` |
x minus y |

`x*y` |
juxtapose x and y |

`x/y` |
x forwardslash y |

`x %+-% y` |
x plus or minus y |

`x %/% y` |
x divided by y |

`x %*% y` |
x times y |

`x %.% y` |
x cdot y |

`x[i]` |
x subscript i |

`x^2` |
x superscript 2 |

`paste(x, y, z)` |
juxtapose x, y, and z |

`sqrt(x)` |
square root of x |

`sqrt(x, y)` |
yth root of x |

`x == y` |
x equals y |

`x != y` |
x is not equal to y |

`x < y` |
x is less than y |

`x <= y` |
x is less than or equal to y |

`x > y` |
x is greater than y |

`x >= y` |
x is greater than or equal to y |

`x %~~% y` |
x is approximately equal to y |

`x %=~% y` |
x and y are congruent |

`x %==% y` |
x is defined as y |

`x %prop% y` |
x is proportional to y |

`plain(x)` |
draw x in normal font |

`bold(x)` |
draw x in bold font |

`italic(x)` |
draw x in italic font |

`bolditalic(x)` |
draw x in bolditalic font |

`list(x, y, z)` |
comma-separated list |

`...` |
ellipsis (height varies) |

`cdots` |
ellipsis (vertically centred) |

`ldots` |
ellipsis (at baseline) |

`x %subset% y` |
x is a proper subset of y |

`x %subseteq% y` |
x is a subset of y |

`x %notsubset% y` |
x is not a subset of y |

`x %supset% y` |
x is a proper superset of y |

`x %supseteq% y` |
x is a superset of y |

`x %in% y` |
x is an element of y |

`x %notin% y` |
x is not an element of y |

`hat(x)` |
x with a circumflex |

`tilde(x)` |
x with a tilde |

`dot(x)` |
x with a dot |

`ring(x)` |
x with a ring |

`bar(xy)` |
xy with bar |

`widehat(xy)` |
xy with a wide circumflex |

`widetilde(xy)` |
xy with a wide tilde |

`x %<->% y` |
x double-arrow y |

`x %->% y` |
x right-arrow y |

`x %<-% y` |
x left-arrow y |

`x %up% y` |
x up-arrow y |

`x %down% y` |
x down-arrow y |

`x %<=>% y` |
x is equivalent to y |

`x %=>% y` |
x implies y |

`x %<=% y` |
y implies x |

`x %dblup% y` |
x double-up-arrow y |

`x %dbldown% y` |
x double-down-arrow y |

`alpha` – `omega` |
Greek symbols |

`Alpha` – `Omega` |
uppercase Greek symbols |

`theta1, phi1, sigma1, omega1` |
cursive Greek symbols |

`Upsilon1` |
capital upsilon with hook |

`infinity` |
infinity symbol |

`partialdiff` |
partial differential symbol |

`32*degree` |
32 degrees |

`60*minute` |
60 minutes of angle |

`30*second` |
30 seconds of angle |

`displaystyle(x)` |
draw x in normal size (extra spacing) |

`textstyle(x)` |
draw x in normal size |

`scriptstyle(x)` |
draw x in small size |

`scriptscriptstyle(x)` |
draw x in very small size |

`underline(x)` |
draw x underlined |

`x ~~ y` |
put extra space between x and y |

`x + phantom(0) + y` |
leave gap for “0”, but don’t draw it |

`x + over(1, phantom(0))` |
leave vertical gap for “0” (don’t draw) |

`frac(x, y)` |
x over y |

`over(x, y)` |
x over y |

`atop(x, y)` |
x over y (no horizontal bar) |

`sum(x[i], i==1, n)` |
sum x[i] for i equals 1 to n |

`prod(plain(P)(X==x), x)` |
product of P(X=x) for all values of x |

`integral(f(x)*dx, a, b)` |
definite integral of f(x) wrt x |

`union(A[i], i==1, n)` |
union of A[i] for i equals 1 to n |

`intersect(A[i], i==1, n)` |
intersection of A[i] |

`lim(f(x), x %->% 0)` |
limit of f(x) as x tends to 0 |

`min(g(x), x > 0)` |
minimum of g(x) for x greater than 0 |

`inf(S)` |
infimum of S |

`sup(S)` |
supremum of S |

`x^y + z` |
normal operator precedence |

`x^(y + z)` |
visible grouping of operands |

`x^{y + z}` |
invisible grouping of operands |

`group("(",list(a, b),"]")` |
specify left and right delimiters |

`bgroup("(",atop(x,y),")")` |
use scalable delimiters |

`group(lceil, x, rceil)` |
special delimiters |