2. Mathematical Building Blocks Flashcards
What are Tensors?
At its core, a tensor is a container for data—almost always numerical data. So, it’s a container for numbers. You may be already familiar with matrices, which are 2D tensors: tensors are a generalization of matrices to an arbitrary number of dimensions (note that in the context of tensors, a dimension is often called an axis).
Scalars
Vectors
Matrices (2D tensors)
An array of vectors is a matrix, or 2D tensor. A matrix has two axes (often referred to rows and columns). You can visually interpret a matrix as a rectangular grid of numbers. This is a Numpy matrix:
3D tensors and higher-dimensional tensors
A tensor is defined by three key attributes:
Displaying mnist digit images
Data Batches
Real World Tensor Examples
Vector for: “An actuarial dataset of people, where we consider each person’s age, ZIP code, and income.”
Each person can be characterized as a vector of 3 values, and thus an entire dataset of 100,000 people can be stored in a 2D tensor of shape (100000, 3).
Vector for: “A dataset of text documents, where we represent each document by the counts of how many times each word appears in it (out of a dictionary of 20,000 common words).”
Each document can be encoded as a vector of 20,000 values (one count per word in the dictionary), and thus an entire dataset of 500 documents can be stored in a tensor of shape (500, 20000).
Vector for: “A dataset of stock prices. Every minute, we store the current price of the stock, the highest price in the past minute, and the lowest price in the past minute.”
Thus every minute is encoded as a 3D vector, an entire day of trading is encoded as a 2D tensor of shape (390, 3) (there are 390 minutes in a trading day), and 250 days’ worth of data can be stored in a 3D tensor of shape (250, 390,3). Here, each sample would be one day’s worth of data.
Vector for: “A dataset of tweets, where we encode each tweet as a sequence of 280 characters out of an alphabet of 128 unique characters.”
In this setting, each character can be encoded as a binary vector of size 128 (an all-zeros vector except for a 1 entry at the index corresponding to the character). Then each tweet can be encoded as a 2D tensor of shape (280, 128), and a dataset of 1 million tweets can be stored in a tensor of shape (1000000, 280, 128).
Vector for: “A batch of 128 grayscale images of size 256 × 256”
A batch of 128 grayscale images of size 256 × 256 could thus be stored in a tensor of shape (128, 256, 256, 1), and a batch of 128 color images could be stored in a tensor of shape (128, 256, 256, 3)
Describe as a function: “keras.layers.Dense(512, activation=’relu’)”
output = relu(dot(W, input) + b)