Deep learning notes (based on pytorch) - Numpy Foundation

Array deformation

reshape

Definition: change the vector arr dimension again without modifying the vector itself. That is, it does not affect the content under the original address, creates a copy result and outputs it.

import numpy as np

arr = np.arange(10)
print(arr)
# Transform the vector arrr dimension into 2 rows and 5 columns
print(arr.reshape(2, 5))
# When specifying dimensions, you can only specify the number of rows or columns, and - 1 can be used for others
print(arr.reshape(5,-1))
print(arr.reshape(-1, 5))

Output results

resize

Definition: modify the vector itself while changing the vector dimension, that is, modify the content of the original address.

import numpy as np
arr = np.arange(10)
print(arr)
# Transform the vector arr dimension into 2 rows and 5 columns
arr.resize(2, 5)
print(arr)

Output results

T

Definition: vector transpose will not affect the content of the original address.

import numpy as np

arr = np.arange(12).reshape(3, 4)
# The vector arr is 3 rows and 4 columns
print(arr)
# Transpose the vector arr to 4 rows and 3 columns
print(arr.T)

Output results

ravel

Definition: flatten the original vector, that is, turn the multi-dimensional array into a one-dimensional array, and the content under the original address will not be modified.

import numpy as np

arr = np.arange(6).reshape(2, -1)
print(arr)
# Flatten by column priority
print("Flatten by column priority")
print(arr.ravel('F'))
# Flatten by row
print("Flatten by row")
print(arr.ravel())

Output results

flatten

Definition: flatten the original vector, that is, turn the multi-dimensional array into a one-dimensional array. The contents under the original address will not be modified, but a copy of the original array will be returned.

import numpy as np
a = np.floor(10*np.random.random((3, 4)))
print(a)
print(a.flatten())

Output results

squeeze

Definition: at this time, a function mainly used to reduce the dimension, which removes the dimension containing 1 from the matrix.

import numpy as np

arr = np.arange(3).reshape(3, 1)
print(arr.shape)
print(arr.squeeze().shape)
arr1 = np.arange(6).reshape([3, 1, 2, 1])
print(arr1.shape)
print(arr1.squeeze().shape)

Output results

transpose

Definition: axisymmetry of high-dimensional matrix.

import numpy as np

arr2 = np.arange(24).reshape([2, 3, 4])
print(arr2.shape)
print(arr2.transpose(1, 2, 0).shape)

Output results

Merge array

Notes:
(1) append, concatenate and stack all have an axis parameter to control whether the array is merged by row or column.
(2) For append and concatenate, the array to be merged must have the same number of rows or columns (one can be satisfied).
(3) stack requires that the arrays to be merged must have the same shape.

append

Merge multidimensional arrays:

import numpy as np
a = np.arange(4).reshape(2, 2)
b = np.arange(4).reshape(2, 2)
# Merge by row
c = np.append(a, b, axis=0)
print('Results merged by row')
print(c)
print('Data dimension after consolidation', c.shape)
# Merge by column
d = np.append(a, b, axis=1)
print('Consolidated results by column')
print(d)
print('Data dimension after consolidation', d.shape)

Output results

concatenate

Joins an array or matrix along a specified axis.

import numpy as np

a = np.array([[1, 2], [3, 4]])
b = np.array([[5, 6]])
c = np.concatenate((a, b), axis=0)
print(c)
d = np.concatenate((a, b.T), axis=1)
print(d)

Output results

stack

Stacks an array or matrix along a specified axis.

import numpy as np

a = np.array([[1, 2], [3, 4]])
b = np.array([[5, 6], [7, 8]])
print(np.stack((a, b), axis=0))

Output results

Batch processing

The calculation of deep learning is generally complex and the amount of data is generally large. In order to calculate more effectively, the whole data set is generally processed in batches.
How to split big data into multiple batches? The following steps can be used:
(1) Get data set
(2) Randomly scramble data
(3) Define batch size
(4) Batch dataset

import numpy as np

# Generate 10000 matrices with shape of 2 * 3
data_train = np.random.randn(10000, 2, 3)
# This is a three-dimensional matrix. The first dimension is the number of samples, and the last two are data shapes
print(data_train.shape)
# Disrupt these 10000 data
np.random.shuffle(data_train)
# Define batch size
batch_size=100
# Batch processing
for i in range(0, len(data_traiin), batch_size):
	x_batch_sum=np.sum(data_train[i:i+batch_size])
	print('The first{}Batch, the sum of the batch's data:{}'.format(i, x_batch_sum))

Broadcasting mechanism

Adjust the array so that the shape satisfies certain rules:
(1) Let all input arrays align with the array with the longest shape, and the insufficient part is supplemented by adding 1 in front.
(2) The shape of the output array is the maximum value on each axis of the input array shape.
(3) If the length of an axis of the input array is the same as that of the output array, or if the length of an axis is 1, this array can be used for calculation, otherwise an error will occur.
(4) When the length of an axis of the input array is 1, the first set of values on this axis are used (or copied) when operating along this axis.
Example:

import numpy as np

A = np.arange(0, 40, 10).reshape(4, 1)
B = np.arange(0, 3)
print("A Shape of matrix:{},B Shape of matrix:{}".format(A.shape,B.shape))
C = A+B
print("C Shape of matrix:{}".format(C.shape))
print(C)

Supplement: usage of choice function in random module

import numpy as np
from numpy import random as nr

a = np.arange(1, 25, dtype=float)
c1 = nr.choice(a, size=(3, 4)) # size specifies the shape of the output array
c2 = nr.choice(a, size=(3, 4), replace=False)  # replace defaults to True to repeat extraction
# In the following formula, parameter p specifies the extraction probability corresponding to each element. By default, the probability of each element being extracted is the same
c3 = nr.choice(a, size=(3, 4), p = a / np.sum(a))
print('Random repeatable extraction')
print(c1)
print('Random but not repeated extraction')
print(c2)
print('Random but according to institutional probability')
print(c3)

reference

Wu Maogui, Yu Mingmin, Yang benfa, Li Tao, Zhang Yuelei. Python deep learning (based on pytoch). Beijing: Machinery Industry Press, 2019

Tags: Python Pytorch Deep Learning

Posted on Wed, 01 Dec 2021 08:33:17 -0500 by dhvani