Tensorflow (7) 图解 NumPy

1、Creating Arrays

Tensorflow (7) 图解 NumPy_第1张图片

Tensorflow (7) 图解 NumPy_第2张图片

2、Array Arithmetic

Tensorflow (7) 图解 NumPy_第3张图片

 

Tensorflow (7) 图解 NumPy_第4张图片

data * 1.6:

3、Indexing

 

Tensorflow (7) 图解 NumPy_第5张图片

4、Aggregation

 

Tensorflow (7) 图解 NumPy_第6张图片

min, max, and sum, plenty of others.

5、Creating Matrices

np.array([[1,2],[3,4]])

Tensorflow (7) 图解 NumPy_第7张图片

 

Tensorflow (7) 图解 NumPy_第8张图片

6、Matrix Arithmetic

Tensorflow (7) 图解 NumPy_第9张图片

Tensorflow (7) 图解 NumPy_第10张图片

7、Dot Product

matrix multiplication

Tensorflow (7) 图解 NumPy_第11张图片

 

Tensorflow (7) 图解 NumPy_第12张图片

 8、Matrix Indexing

Tensorflow (7) 图解 NumPy_第13张图片

9、Matrix Aggregation

Tensorflow (7) 图解 NumPy_第14张图片

 

Tensorflow (7) 图解 NumPy_第15张图片

10、Transposing and Reshaping

 

Tensorflow (7) 图解 NumPy_第16张图片

 

 

Tensorflow (7) 图解 NumPy_第17张图片

 

11、Yet More Dimensions

Tensorflow (7) 图解 NumPy_第18张图片

Tensorflow (7) 图解 NumPy_第19张图片

np.ones((4,3,2))

array([[[1., 1.],
        [1., 1.],
        [1., 1.]],

       [[1., 1.],
        [1., 1.],
        [1., 1.]],

       [[1., 1.],
        [1., 1.],
        [1., 1.]],

       [[1., 1.],
        [1., 1.],
        [1., 1.]]])

 

 

12、Formulas应用

 

Tensorflow (7) 图解 NumPy_第20张图片

NumPy表示:

Tensorflow (7) 图解 NumPy_第21张图片

Tensorflow (7) 图解 NumPy_第22张图片

Tensorflow (7) 图解 NumPy_第23张图片

Tensorflow (7) 图解 NumPy_第24张图片

13、Data Representation 

13.1 Tables and Spreadsheets

  • A spreadsheet or a table of values is a two dimensional matrix. Each sheet in a spreadsheet can be its own variable. The most popular abstraction in python for those is the pandas dataframe, which actually uses NumPy and builds on top of it.

Tensorflow (7) 图解 NumPy_第25张图片

13.2 Audio and Timeseries

  • An audio file is a one-dimensional array of samples. Each sample is a number representing a tiny chunk of the audio signal. CD-quality audio may have 44,100 samples per second and each sample is an integer between -32767 and 32768. Meaning if you have a ten-seconds WAVE file of CD-quality, you can load it in a NumPy array with length 10 * 44,100 = 441,000 samples. Want to extract the first second of audio? simply load the file into a NumPy array that we’ll call audio, and get audio[:44100].

Here’s a look at a slice of an audio file:

 

Tensorflow (7) 图解 NumPy_第26张图片

The same goes for time-series data (for example, the price of a stock over time).

13.3 Images

  • An image is a matrix of pixels of size (height x width).

    • If the image is black and white (a.k.a. grayscale), each pixel can be represented by a single number (commonly between 0 (black) and 255 (white)). Want to crop the top left 10 x 10 pixel part of the image? Just tell NumPy to get you image[:10,:10].

Here’s a look at a slice of an image file:

Tensorflow (7) 图解 NumPy_第27张图片

  • If the image is colored, then each pixel is represented by three numbers - a value for each of red, green, and blue. In that case we need a 3rd dimension (because each cell can only contain one number). So a colored image is represented by an ndarray of dimensions: (height x width x 3).

  • Tensorflow (7) 图解 NumPy_第28张图片

 13.4 Language

  • If we’re dealing with text, the story is a little different. The numeric representation of text requires a step of building a vocabulary (an inventory of all the unique words the model knows) and an embedding step. Let us see the steps of numerically representing this (translated) quote by an ancient spirit:

    “Have the bards who preceded me left any theme unsung?”

    A model needs to look at a large amount of text before it can numerically represent the anxious words of this warrior poet. We can proceed to have it process a small dataset and use it to build a vocabulary (of 71,290 words):

Tensorflow (7) 图解 NumPy_第29张图片

The sentence can then be broken into an array of tokens (words or parts of words based on common rules):

We then replace each word by its id in the vocabulary table:

These ids still don’t provide much information value to a model. So before feeding a sequence of words to a model, the tokens/words need to be replaced with their embeddings (50 dimension word2vec embedding in this case):

Tensorflow (7) 图解 NumPy_第30张图片

You can see that this NumPy array has the dimensions [embedding_dimension x sequence_length]. In practice these would be the other way around, but I’m presenting it this way for visual consistency. For performance reasons, deep learning models tend to preserve the first dimension for batch size (because the model can be trained faster if multiple examples are trained in parallel). This is a clear case where reshape() becomes super useful. A model like BERT, for example, would expect its inputs in the shape: [batch_size, sequence_length, embedding_size].

Tensorflow (7) 图解 NumPy_第31张图片

 

 

你可能感兴趣的:(神经网络,深度学习,Python,NumPy)