liveBook · Manning

Array

As explained in Deep Learning with R, Third Edition

An R array is R's built-in multidimensional array data structure for storing values across one or more dimensions.

An R array stores values along one or more axes. It is R’s built-in implementation of a tensor, the basic data structure used to represent data in neural networks. A vector has one axis; a matrix has two. More axes let an array represent sequences, images, and batches of data. section 2.2 section 2.2.2 section 2.2.3

Make a two-axis array

Run `x <- array(seq(2 * 3), dim = c(2, 3))`. The result has two rows and three columns: its first row is `c(1, 3, 5)`, and its first column is `c(1, 2)`. Check its rank with `length(dim(x))`, which returns `2`. section 2.2.3

Three attributes help describe any array: rank is its number of axes, shape gives the size of each axis, and data type identifies the kind of values stored. For an R array `x`, inspect these with `length(dim(x))`, `dim(x)`, and `typeof(x)`. A shape of `(2, 3)` means two entries along the first axis and three along the second; it does not mean rank 3. section 2.2.5 section 2.2.3

Choose axes to match what each entry represents:

  • Tabular vector data: `(samples, features)`. A dataset of 100,000 people described by three attributes has shape `(100000, 3)`. section 2.2.8
  • Sequences: `(samples, timesteps, features)`; images: `(samples, height, width, channels)`; video: `(samples, frames, height, width, channels)`. section 2.2.8

The MNIST training images illustrate why shape matters: 60,000 grayscale images of 28 × 28 pixels form a rank-3 array with shape `(60000, 28, 28)`. Before feeding them to a Dense-layer network, reshape them to `(60000, 28 * 28)` and divide their pixel values by `255`. Their labels are a separate array, with one label per image. section 2.1 section 2.2.5

Listing 2.4 Preparing the image data
train_images <- array_reshape(train_images, c(60000, 28 * 28))
train_images <- train_images / 255
test_images <- array_reshape(test_images, c(10000, 28 * 28))
test_images <- test_images / 255

The preprocessing code turns each image grid into a row of values for the network and scales its pixel values. section 2.1

R arrays can be passed to Keras and its deep learning backends; they are automatically converted to NumPy arrays at that boundary. NumPy arrays offer additional data types and array-operation behaviors, so the two representations are useful in different parts of a workflow. section 2.2

How Deep Learning with R, Third Edition covers it

How deep it goes
Works through array construction, axes, rank, shape, data type, dataset shapes, and image reshaping; does not detail array storage internals.
Examples
Uses a 2 × 3 R array, MNIST image arrays, and shapes for tabular, sequence, image, and video data.
What it assumes
Starts with numbers and familiar rows and columns while defining tensors, axes, rank, and shape.
What you can do after
Practice/perform array construction, inspect rank and shape, and reshape image arrays for a Dense-layer model.
How it connects
Connects arrays to tensors, vectors, matrices, NumPy arrays, and Dense layers.

Also explained in

Compare how 40 books explain array →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Deep Learning with R, Third Edition. The section links point to the parts of the book it draws on.