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
.
- Work with Deep Learning networks and related packages in R
- Create Natural Language Processing models
- And much more
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.
Leave a Reply