[Repost] Tensor is a god horse? Why still Flow?

Tensor is a god horse? Why still Flow?

internet lover

Baijiahao 17-05-23 10:03

Big data abstract works, please refer to the end of the article for reprint requirements

Compilation | Shao Pangpang, Jiang Fan, Da Jieqiong, Aileen

Maybe you've downloaded TensorFlow and are ready to start working on deep learning. But you will wonder: what the hell is Tensor in TensorFlow, that is, "tensor"? Maybe you looked up Wikipedia and got even more confused now. Maybe you saw it in a NASA tutorial and still don't know what it's talking about? The problem is that most guides on tensors assume that you already know all the terms they use to describe mathematics.

do not worry!

I hate math like a kid, so if I can get it, so can you! We just need to explain it all in simple terms. So, what is a tensor, and why does it flow?

content

0-dimensional tensor/scalar

A scalar is a number

1D tensor/vector

A 1-dimensional tensor is called a "vector".

2-dimensional tensor

2-dimensional tensors are called matrices

3-dimensional tensor

Common data is stored in tensors

time series data

share price

text data

picture

color pictures

5D Tensor

in conclusion

Let's first take a look at what is a tensor?

Tensor = container

Tensors are the foundation of modern machine learning. Its core is a data container, in most cases it contains numbers, sometimes it also contains strings, but this is rare. So think of it as a digital bucket.

Tensors come in many forms, first let's look at the most basic forms, which you'll come across in deep learning, which are between 0 and 5 dimensions. We can think of the various types of tensors as this (to the friends who are attracted by the cat in the title, don't worry! The cat will appear later!):

0-dimensional tensor/scalar

Each number packed in a tensor/container bucket is called a "scalar".

A scalar is a number.

Why not just call them a number, you ask?

I don't know, maybe mathematicians just like to sound cool? Scalars do sound cooler than numbers.

In fact, you can use a tensor of numbers, we call it a 0-dimensional tensor, that is, a tensor with only 0 dimensions. It's just a bucket with a number. Imagine that there is only one drop of water in the bucket, that is a 0-dimensional tensor.

In this tutorial, I will use Python, Keras, TensorFlow and the Python library Numpy. In Python, tensors are usually stored in Nunpy arrays. Numpy is a very frequently used data package for scientific computing in most AI frameworks.

You will often see Jupyter Notebooks on Kaggle (the website for data science competitions) (for installation, see the link at the end of the article, "Learn AI if you suck at math: Take you to build an economic trial version of the ultimate AI killer") about turning data into Numpy arrays. Jupyter notebooks are essentially embedded by working code markup. It can be thought of as a fusion of interpretation and procedure.

Why do we want to convert data to Numpy array?

Simple. Because we need to convert all input data, such as string text, image, stock price, or video, into a unified standard so that it can be easily processed.

This way we turn the data into digital buckets that we can process with TensorFlow.

It simply organizes the data into a usable format. In web applications, you might represent them in XML, so you can define their characteristics and quickly manipulate them. Likewise, in deep learning, we use tensor buckets as basic Lego bricks.

1D tensor/vector

If you're a programmer, you already know that it's similar to 1-dimensional tensors: arrays.

Every programming language has an array, which is just a set of data blocks in a single column or row. In deep learning it is called a 1-dimensional tensor. A tensor is defined in terms of how many axes it has in total. A 1D tensor has only one axis.

A 1-dimensional tensor is called a "vector".

We can think of a vector as a single column or row of numbers.

If you want to get this result in Numpy, follow the method below:

We can use NumPy's ndim function to see that a tensor has multiple axes. We can try 1-dimensional tensors.

2-dimensional tensor

You may already know another form of tensors, matrices - 2-dimensional tensors are called matrices

No, this is not the Keanu Reeves movie The Matrix, imagine an excel sheet.

We can think of it as a grid of numbers with rows and columns.

This row and column represent two coordinate axes, and a matrix is ​​a two-dimensional tensor, which means that it has two dimensions, that is, a tensor with two coordinate axes.

In Numpy, we can represent it as follows:

x = np.array([[5,10,15,30,25],

[20,30,65,70,90],

[7,80,95,20,30]])

We can store person features in a 2D tensor. A typical example is mailing lists.

For example we have 10000 people, we have the following characteristics and characteristics of each person:

First Name(名)

Last Name(姓)

Street Address

City

State (state/province)

Country

Zip (Postal Code)

That means we have seven characteristics of 10,000 people.

A tensor has a "shape", its shape is a bucket, which holds our data and defines the maximum size of the tensor. We can put everyone's data into a 2D tensor, which is (10000, 7).

You might want to say it has 10,000 columns and 7 rows.

Do not.

Tensors can be transformed and manipulated so that columns become rows or rows become columns.

3-dimensional tensor

That's when tensors really start to become useful, and we often need to store a series of 2D tensors in buckets, which results in a 3D tensor.

In NumPy, we can express as follows:

x = np.array([[[5,10,15,30,25],

[20,30,65,70,90],

[7,80,95,20,30]]

[[3,0,5,0,45],

[12,-2,6,7,90],

[18,-9,95,120,30]]

[[17,13,25,30,15],

[23,36,9,7,80],

[1,-7,-5,22,3]]])

As you have guessed, a 3D tensor has three axes, which can be seen like this:

x.ndim

The output is:

3

Let's look at the above mailing list again, now we have 10 mailing lists, we will store the 2-dimensional tensor in another bucket, creating a 3-dimensional tensor with the following shape:

(number_of_mailing_lists, number_of_people, number_of_characteristics_per_person)

(10,10000,7)

You might have guessed it, but a 3D tensor is a cube of numbers.

We can keep stacking the cubes, creating a larger and larger tensor to edit different types of data, i.e. 4-dimensional tensors, 5-dimensional tensors, and so on, up to N-dimensional tensors. N is a mathematician-defined unknown, which is an additional unit that lasts into an infinite set. It can be 5, 10 or infinite.

In fact, a 3D tensor is best viewed as a layer of grid, which looks a bit like the image below:

formula stored in tensor data

Here are some common dataset types stored in various types of tensors:

3 dimensions = time series

4 dimensions = image

5 dimensions = video

What almost all of these tensors have in common is the sample size. The sample size is the number of elements in the collection, it can be some images, some videos, some files or some tweets.

Usually, the real data is at least a data volume.

Think of the different dimensions in the shape as fields. We find the minimum value of a field to describe the data.

So even though 4-dimensional tensors usually store images, that's because the sample size occupies the 4th field of the tensor.

For example, an image can be represented by three fields:

(width, height, color_depth) = 3D

However, in machine learning work, we often deal with more than one image or one document - we deal with a collection. We may have 10,000 pictures of tulips, which means, we will use 4D tensors like this:

(sample_size, width, height, color_depth) = 4D

Let's take a look at some examples of multidimensional tensor storage models:

time series data

Modeling time series with 3D tensors can be very efficient!

Medical scans - We can encode the brainwave (EEG) signal into a 3D tensor because it can be described by these three parameters:

(time, frequency, channel)

This transformation looks like this:

If we have multiple patient brain wave scans, that forms a 4D tensor:

(sample_size, time, frequency, channel)

Stock Prices

In trading, stocks have high, low and final prices every minute. As shown in the candlestick chart below:

The NYSE is open from 9:30am to 4:00pm, or 6.5 hours, for a total of 6.5 x 60 = 390 minutes. In this way, we can store the high, low and final stock price for each minute into a 2D tensor (390,3). If we track transactions for a week (five days), we will get a 3D tensor like this:

(week_of_data, minutes, high_low_price)

ie: (5,390,3)

Similarly, if we observe 10 different stocks for a week, we will get a 4D tensor

(10,5,390,3)

Suppose we are observing a mutual fund consisting of 25 stocks, each of which is represented by our 4D tensor. Then, this mutual fund can be represented by a 5D tensor:

(25,10,5,390,3)

text data

We can also use 3D tensors to store text data, let's take the example of Twitter.

First, Twitter has a 140-word limit. Second, Twitter uses the UTF-8 encoding standard, which can represent millions of characters, but in reality we are only interested in the first 128 characters because they are the same as ASCII. So, a tweet can be packed into a 2D vector:

(140,128)

If we download a million tweets from Trump (I think he can tweet that many in a week), we will use a 3D tensor to store:

(number_of_tweets_captured, tweet, character)

This means that our collection of Trump tweets will look like this:

(1000000,140,128)

picture

4D tensors are great for storing image files such as JPEGs. We mentioned before that an image has three parameters: height, width and color depth. An image is a 3D tensor, an image set is 4D, and the fourth dimension is the sample size.

The well-known MNIST dataset is a sequence of handwritten digits that has plagued many data scientists for decades as an image recognition problem. Today, computers can solve this problem with 99 percent or better accuracy. Even so, this dataset can be used as an excellent benchmark for testing new machine learning algorithm applications, or for doing your own experiments.

Keras can even help us automatically import the MNIST dataset with the following statement:

from keras.datasets import mnist

(train_images, train_labels), (test_images, test_labels) = mnist.load_data()

This dataset is divided into two parts: training set and test set. Each image in the dataset has a label. This label is written with the correct reading, such as 3, 7 or 9, and these labels are manually judged and filled in.

The training set is used to train the neural network learning algorithm, and the test set is used to verify the learning algorithm.

MNIST images are black and white, which means they can be encoded in 2D tensors, but we are used to encoding all images in 3D tensors, with a third dimension representing the color depth of the image.

The MNIST dataset has 60,000 images, they are all 28 x 28 pixels, and they have a color depth of 1, i.e. only grayscale.

TensorFlow stores image data like this:

(sample_size, height, width, color_depth).

So we can think that the 4D tensor of the MNIST dataset is like this:

(60000,28,28,1)

color pictures

Color images have different color depths, depending on their color (note: resolution independent) encoding. A typical JPG image uses RGB encoding, so its color depth is 3, representing red, green, and blue respectively.

Here is a photo of my beautiful boundless cat (Dove), 750 x 750 pixels, which means we can represent it as a 3D tensor:

(750,750,3)

My beautiful cat Dove (750 x 750 pixels)

In this way, my lovely Dove will be reduced to a string of frosty figures, as if it were deformed or flowed.

Then, if we had a whole bunch of cat pictures of different types (although none of them as beautiful as Dove), maybe 100,000, not DOVE's, 750x750 pixels. We can use a 4D tensor in Keras to define it like this:

(10000,750,750,3)

5D Tensor

5D tensors can be used to store video data. In TensorFlow, the video data will be encoded like this:

(sample_size, frames, width, height, color_depth)

If we consider a video of 5 minutes (300 seconds), 1080pHD (1920 x 1080 pixels), 15 frames per second (4500 frames total), and a color depth of 3, we can store it in a 4D tensor:

(4500,1920,1080,3)

The fifth dimension in the tensor will be used when we have multiple videos. If we have 10 such videos, we will get a 5D tensor:

(10,4500,1920,1080,3)

Actually this example is crazy!

This tensor is ridiculously large, over 1TB. Let's consider this example to illustrate a problem: in the real world, we sometimes need to reduce the sample data as much as possible to facilitate processing calculations, unless you have endless time.

The number of values ​​in this 5D tensor is:

10 x 4500 x 1920 x 1080 x 3 = 279,936,000,000

In Keras, we can use a data type called dype to store 32bits or 64bits floating point numbers

Each value in our 5D tensor will be stored in 32 bits. Now, let's convert it in terabytes:

279,936,000,000 x 32 = 8,957,952,000,000

This is just a conservative estimate, maybe using 32bit to store is not enough at all (who will calculate what will happen if you use 64bit to store), so reduce your sample friend.

In fact, I have this last crazy example for a special purpose. We just learned about data preprocessing and data compression. You can't just throw a bunch of data at your AI model without doing anything. You have to clean and reduce that data to make subsequent work easier and more efficient.

Lowering the resolution, removing unnecessary data (aka deduplication), which greatly reduces the framerate, etc. This is also the job of a data scientist. If you can't do these preprocessing on the data well, then you can hardly do anything meaningful.

in conclusion

Well, now you have a better understanding of tensors and how to use tensors to interface with different types of data.

In the next article "Learn AI even if you're bad at math", we'll learn how to do various transformations on tensors, which is known as mathematics. In other words, we will let the tensors "flow".

Original link: https://hackernoon.com/learning-ai-if-you-suck-at-math-p4-tensors-illustrated-with-cats-27f0002c9b32

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325225949&siteId=291194637