1. 轉置操作 (Transpose)
概念:將矩陣的行和列互換 應用場景: 在卷積神經網絡中轉換特征圖維度 矩陣乘法運算前的維度調整 數據預處理過程中的特征重排
原始矩陣 A = [[1, 2, 3], 轉置后 A^T = [[1, 4],[4, 5, 6]] [2, 5],[3, 6]]
import numpy as np
import tensorflow as tf
import torch
import pandas as pd
import scipy. sparse as sp
matrix = np. array( [ [ 1 , 2 , 3 ] , [ 4 , 5 , 6 ] ] )
print ( "原始矩陣:" )
print ( matrix)
print ( "形狀:" , matrix. shape)
print ( "\n1. NumPy 轉置:" )
np_transpose = matrix. T
print ( np_transpose)
print ( "形狀:" , np_transpose. shape)
np_transpose2 = np. transpose( matrix)
print ( np_transpose2)
print ( "\n2. TensorFlow 轉置:" )
tf_matrix = tf. constant( matrix)
tf_transpose = tf. transpose( tf_matrix)
print ( tf_transpose. numpy( ) )
print ( "\n3. PyTorch 轉置:" )
torch_matrix = torch. tensor( matrix)
torch_transpose = torch. transpose( torch_matrix, 0 , 1 )
print ( torch_transpose)
torch_transpose2 = torch_matrix. T
print ( torch_transpose2)
print ( "\n4. pandas 轉置:" )
df = pd. DataFrame( matrix)
pd_transpose = df. T
print ( pd_transpose)
print ( "\n5. SciPy 轉置 (稀疏矩陣):" )
sparse_matrix = sp. csr_matrix( matrix)
scipy_transpose = sparse_matrix. transpose( )
print ( scipy_transpose. toarray( ) )
2. 矩陣乘法 (Matrix Multiplication)
概念:兩個矩陣相乘,要求第一個矩陣的列數等于第二個矩陣的行數 應用場景: Transformer 架構中的注意力計算和全連接層 線性層的權重和輸入相乘 嵌入向量與查詢向量的相似度計算
矩陣 A = [[1, 2], 矩陣 B = [[5, 6], A×B = [[19, 22], [3, 4]] [7, 8]] [43, 50]]
import numpy as np
import tensorflow as tf
import torch
import pandas as pd
import scipy. sparse as sp
A = np. array( [ [ 1 , 2 ] , [ 3 , 4 ] ] )
B = np. array( [ [ 5 , 6 ] , [ 7 , 8 ] ] )
print ( "矩陣A:" )
print ( A)
print ( "矩陣B:" )
print ( B)
print ( "\n1. NumPy 矩陣乘法:" )
np_matmul = np. matmul( A, B)
print ( np_matmul)
np_matmul2 = A @ B
print ( np_matmul2)
np_dot = np. dot( A, B)
print ( np_dot)
print ( "\n2. TensorFlow 矩陣乘法:" )
tf_A = tf. constant( A)
tf_B = tf. constant( B)
tf_matmul = tf. matmul( tf_A, tf_B)
print ( tf_matmul. numpy( ) )
print ( "\n3. PyTorch 矩陣乘法:" )
torch_A = torch. tensor( A)
torch_B = torch. tensor( B)
torch_matmul = torch. matmul( torch_A, torch_B)
print ( torch_matmul)
torch_matmul2 = torch_A @ torch_B
print ( torch_matmul2)
torch_mm = torch. mm( torch_A, torch_B)
print ( torch_mm)
print ( "\n4. pandas 矩陣乘法:" )
df_A = pd. DataFrame( A)
df_B = pd. DataFrame( B)
pd_matmul = df_A. values @ df_B. values
print ( pd_matmul)
print ( "\n5. SciPy 矩陣乘法 (稀疏矩陣):" )
sparse_A = sp. csr_matrix( A)
sparse_B = sp. csr_matrix( B)
scipy_matmul = sparse_A @ sparse_B
print ( scipy_matmul. toarray( ) )
3. 逐元素乘法 (Element-wise Multiplication / Hadamard Product)
概念:兩個相同形狀的矩陣對應位置元素相乘 應用場景: 門控機制(如LSTM、GRU中的門控操作) 特征選擇和加權 掩碼操作(如注意力掩碼)
矩陣 A = [[1, 2], 矩陣 B = [[5, 6], A⊙B = [[5, 12], [3, 4]] [7, 8]] [21, 32]]
import numpy as np
import tensorflow as tf
import torch
import pandas as pd
import scipy. sparse as sp
A = np. array( [ [ 1 , 2 ] , [ 3 , 4 ] ] )
B = np. array( [ [ 5 , 6 ] , [ 7 , 8 ] ] )
print ( "矩陣A:" )
print ( A)
print ( "矩陣B:" )
print ( B)
print ( "\n1. NumPy 逐元素乘法:" )
np_element_wise = A * B
print ( np_element_wise)
np_multiply = np. multiply( A, B)
print ( np_multiply)
print ( "\n2. TensorFlow 逐元素乘法:" )
tf_A = tf. constant( A)
tf_B = tf. constant( B)
tf_element_wise = tf_A * tf_B
print ( tf_element_wise. numpy( ) )
tf_multiply = tf. multiply( tf_A, tf_B)
print ( tf_multiply. numpy( ) )
print ( "\n3. PyTorch 逐元素乘法:" )
torch_A = torch. tensor( A)
torch_B = torch. tensor( B)
torch_element_wise = torch_A * torch_B
print ( torch_element_wise)
torch_mul = torch. mul( torch_A, torch_B)
print ( torch_mul)
print ( "\n4. pandas 逐元素乘法:" )
df_A = pd. DataFrame( A)
df_B = pd. DataFrame( B)
pd_element_wise = df_A * df_B
print ( pd_element_wise)
print ( "\n5. SciPy 逐元素乘法 (稀疏矩陣):" )
sparse_A = sp. csr_matrix( A)
sparse_B = sp. csr_matrix( B)
scipy_element_wise = sparse_A. multiply( sparse_B)
print ( scipy_element_wise. toarray( ) )
4. 矩陣求逆 (Matrix Inversion)
概念:求一個方陣的逆矩陣,滿足 A × A?1 = A?1 × A = I(單位矩陣) 應用場景:
矩陣 A = [[4, 7], A?1 = [[-8/19, 7/19],[2, 6]] [2/19, -4/19]]
import numpy as np
import tensorflow as tf
import torch
import pandas as pd
import scipy. linalg as spla
A = np. array( [ [ 4 , 7 ] , [ 2 , 6 ] ] )
print ( "原始矩陣:" )
print ( A)
print ( "\n1. NumPy 矩陣求逆:" )
np_inv = np. linalg. inv( A)
print ( np_inv)
print ( "驗證 A × A?1 ≈ I:" )
print ( A @ np_inv)
print ( "\n2. TensorFlow 矩陣求逆:" )
tf_A = tf. constant( A, dtype= tf. float32)
tf_inv = tf. linalg. inv( tf_A)
print ( tf_inv. numpy( ) )
print ( "\n3. PyTorch 矩陣求逆:" )
torch_A = torch. tensor( A, dtype= torch. float32)
torch_inv = torch. inverse( torch_A)
print ( torch_inv)
print ( "\n4. pandas 矩陣求逆:" )
df_A = pd. DataFrame( A)
pd_inv = pd. DataFrame( np. linalg. inv( df_A. values) )
print ( pd_inv)
print ( "\n5. SciPy 矩陣求逆:" )
scipy_inv = spla. inv( A)
print ( scipy_inv)
5. 矩陣分解 - SVD (奇異值分解)
概念:將矩陣分解為 A = U × Σ × V^T,其中U和V是正交矩陣,Σ是對角矩陣 應用場景: 降維處理(如PCA的實現) 推薦系統中的矩陣分解 圖像壓縮和噪聲過濾 潛在語義分析(LSA)
矩陣 A = [[1, 2], [3, 4], [5, 6]] 分解為:
U = [[-0.2298, 0.8835, 0.4082],[-0.5247, 0.2408, -0.8165],[-0.8196, -0.4019, 0.4082]]Σ = [[9.5255, 0],[0, 0.5144],[0, 0]]V^T = [[-0.6196, -0.7849],[-0.7849, 0.6196]]
import numpy as np
import tensorflow as tf
import torch
import pandas as pd
import scipy. linalg as spla
A = np. array( [ [ 1 , 2 ] , [ 3 , 4 ] , [ 5 , 6 ] ] )
print ( "原始矩陣:" )
print ( A)
print ( "\n1. NumPy SVD分解:" )
U, s, Vh = np. linalg. svd( A, full_matrices= True )
print ( "U 矩陣:" )
print ( U)
print ( "奇異值:" )
print ( s)
print ( "V^T 矩陣:" )
print ( Vh)
S = np. zeros( ( A. shape[ 0 ] , A. shape[ 1 ] ) )
S[ : len ( s) , : len ( s) ] = np. diag( s)
A_reconstructed = U @ S @ Vh
print ( "重構矩陣:" )
print ( A_reconstructed)
print ( "\n2. TensorFlow SVD分解:" )
tf_A = tf. constant( A, dtype= tf. float32)
s_tf, u_tf, v_tf = tf. linalg. svd( tf_A)
print ( "奇異值:" )
print ( s_tf. numpy( ) )
print ( "U 矩陣:" )
print ( u_tf. numpy( ) )
print ( "V 矩陣 (注意:這是V不是V^T):" )
print ( v_tf. numpy( ) )
print ( "\n3. PyTorch SVD分解:" )
torch_A = torch. tensor( A, dtype= torch. float32)
U_torch, s_torch, V_torch = torch. svd( torch_A)
print ( "U 矩陣:" )
print ( U_torch)
print ( "奇異值:" )
print ( s_torch)
print ( "V 矩陣 (注意:這是V不是V^T):" )
print ( V_torch)
print ( "\n4. pandas SVD分解:" )
df_A = pd. DataFrame( A)
U_pd, s_pd, Vh_pd = np. linalg. svd( df_A. values)
print ( "通過pandas數據,使用NumPy的SVD實現" )
print ( "\n5. SciPy SVD分解:" )
U_scipy, s_scipy, Vh_scipy = spla. svd( A)
print ( "U 矩陣:" )
print ( U_scipy)
print ( "奇異值:" )
print ( s_scipy)
print ( "V^T 矩陣:" )
print ( Vh_scipy)
6. 矩陣reshape操作
概念:改變矩陣的形狀,同時保持元素總數不變 應用場景: 神經網絡層間維度變換(如展平操作) 批處理數據的維度調整 特征圖轉換(如在CNN和RNN之間)
矩陣 A = [[1, 2, 3], reshape后 = [[1, 2],[4, 5, 6]] [3, 4],[5, 6]]
import numpy as np
import tensorflow as tf
import torch
import pandas as pd
import scipy. sparse as sp
A = np. array( [ [ 1 , 2 , 3 ] , [ 4 , 5 , 6 ] ] )
print ( "原始矩陣:" )
print ( A)
print ( "形狀:" , A. shape)
print ( "\n1. NumPy reshape:" )
np_reshaped = A. reshape( 3 , 2 )
print ( np_reshaped)
print ( "新形狀:" , np_reshaped. shape)
np_reshaped_auto = A. reshape( - 1 , 2 )
print ( "自動計算行數:" )
print ( np_reshaped_auto)
print ( "\n2. TensorFlow reshape:" )
tf_A = tf. constant( A)
tf_reshaped = tf. reshape( tf_A, [ 3 , 2 ] )
print ( tf_reshaped. numpy( ) )
print ( "\n3. PyTorch reshape:" )
torch_A = torch. tensor( A)
torch_reshaped = torch_A. reshape( 3 , 2 )
print ( torch_reshaped)
torch_viewed = torch_A. view( 3 , 2 )
print ( "使用view:" )
print ( torch_viewed)
print ( "\n4. pandas reshape:" )
df_A = pd. DataFrame( A)
pd_reshaped = df_A. values. reshape( 3 , 2 )
print ( pd_reshaped)
pd_reshaped_df = pd. DataFrame( pd_reshaped)
print ( pd_reshaped_df)
print ( "\n5. SciPy reshape (稀疏矩陣):" )
sparse_A = sp. csr_matrix( A)
scipy_reshaped = sparse_A. reshape( 3 , 2 )
print ( scipy_reshaped. toarray( ) )
7. 矩陣正則化 (Normalization)
概念:對矩陣進行縮放,使其滿足特定的范數約束 應用場景: 批歸一化(Batch Normalization) 權重正則化減少過擬合 特征縮放提高訓練效率 梯度裁剪防止梯度爆炸
矩陣 A = [[1, 2], [3, 4]]L2行歸一化后 = [[0.4472, 0.8944],[0.6, 0.8]]
import numpy as np
import tensorflow as tf
import torch
import pandas as pd
import scipy. sparse as sp
from sklearn. preprocessing import normalize
A = np. array( [ [ 1 , 2 ] , [ 3 , 4 ] ] )
print ( "原始矩陣:" )
print ( A)
print ( "\n1. NumPy 矩陣行歸一化:" )
row_norms = np. sqrt( np. sum ( A** 2 , axis= 1 , keepdims= True ) )
np_normalized = A / row_norms
print ( np_normalized)
np_normalized_sklearn = normalize( A, norm= 'l2' , axis= 1 )
print ( "使用sklearn:" )
print ( np_normalized_sklearn)
print ( "\n2. TensorFlow 矩陣歸一化:" )
tf_A = tf. constant( A, dtype= tf. float32)
tf_normalized = tf. nn. l2_normalize( tf_A, axis= 1 )
print ( tf_normalized. numpy( ) )
tf_batch_norm = tf. keras. layers. BatchNormalization( ) ( tf. reshape( tf_A, [ 1 , 2 , 2 , 1 ] ) )
print ( "Batch Normalization (要求4D輸入):" )
print ( tf. reshape( tf_batch_norm, [ 2 , 2 ] ) . numpy( ) )
print ( "\n3. PyTorch 矩陣歸一化:" )
torch_A = torch. tensor( A, dtype= torch. float32)
torch_normalized = torch. nn. functional. normalize( torch_A, p= 2 , dim= 1 )
print ( torch_normalized)
batch_norm = torch. nn. BatchNorm1d( 2 )
torch_batch_norm = batch_norm( torch_A)
print ( "Batch Normalization:" )
print ( torch_batch_norm)
print ( "\n4. pandas 矩陣歸一化:" )
df_A = pd. DataFrame( A)
pd_normalized = df_A. apply ( lambda x: x / np. sqrt( np. sum ( x** 2 ) ) , axis= 1 )
print ( pd_normalized)
print ( "\n5. SciPy 矩陣歸一化 (稀疏矩陣):" )
sparse_A = sp. csr_matrix( A)
row_norms = sp. linalg. norm( sparse_A, axis= 1 )
row_norms_inv = 1.0 / row_norms
diag_inv = sp. spdiags( row_norms_inv, 0 , sparse_A. shape[ 0 ] , sparse_A. shape[ 0 ] )
scipy_normalized = diag_inv @ sparse_A
print ( scipy_normalized. toarray( ) )
8. 矩陣拼接 (Concatenation)
概念:沿指定軸將多個矩陣連接在一起 應用場景: 特征融合(多模態學習) 批處理數據合并 序列數據拼接(如在RNN中) 層級特征組合(如在Skip Connection中)
矩陣 A = [[1, 2], 矩陣 B = [[5, 6], [3, 4]] [7, 8]]橫向拼接 = [[1, 2, 5, 6],[3, 4, 7, 8]]縱向拼接 = [[1, 2],[3, 4],[5, 6],[7, 8]]
import numpy as np
import tensorflow as tf
import torch
import pandas as pd
import scipy. sparse as sp
A = np. array( [ [ 1 , 2 ] , [ 3 , 4 ] ] )
B = np. array( [ [ 5 , 6 ] , [ 7 , 8 ] ] )
print ( "矩陣A:" )
print ( A)
print ( "矩陣B:" )
print ( B)
print ( "\n1. NumPy 矩陣拼接:" )
np_hconcat = np. concatenate( [ A, B] , axis= 1 )
print ( "橫向拼接 (axis=1):" )
print ( np_hconcat)
np_vconcat = np. concatenate( [ A, B] , axis= 0 )
print ( "縱向拼接 (axis=0):" )
print ( np_vconcat)
np_hstack = np. hstack( [ A, B] )
np_vstack = np. vstack( [ A, B] )
print ( "使用hstack:" )
print ( np_hstack)
print ( "使用vstack:" )
print ( np_vstack)
print ( "\n2. TensorFlow 矩陣拼接:" )
tf_A = tf. constant( A)
tf_B = tf. constant( B)
tf_hconcat = tf. concat( [ tf_A, tf_B] , axis= 1 )
print ( "橫向拼接 (axis=1):" )
print ( tf_hconcat. numpy( ) )
tf_vconcat = tf. concat( [ tf_A, tf_B] , axis= 0 )
print ( "縱向拼接 (axis=0):" )
print ( tf_vconcat. numpy( ) )
print ( "\n3. PyTorch 矩陣拼接:" )
torch_A = torch. tensor( A)
torch_B = torch. tensor( B)
torch_hconcat = torch. cat( [ torch_A, torch_B] , dim= 1 )
print ( "橫向拼接 (dim=1):" )
print ( torch_hconcat)
torch_vconcat = torch. cat( [ torch_A, torch_B] , dim= 0 )
print ( "縱向拼接 (dim=0):" )
print ( torch_vconcat)
torch_hstack = torch. hstack( [ torch_A, torch_B] )
torch_vstack = torch. vstack( [ torch_A, torch_B] )
print ( "使用hstack:" )
print ( torch_hstack)
print ( "使用vstack:" )
print ( torch_vstack)
print ( "\n4. pandas 矩陣拼接:" )
df_A = pd. DataFrame( A)
df_B = pd. DataFrame( B)
pd_hconcat = pd. concat( [ df_A, df_B] , axis= 1 )
print ( "橫向拼接 (axis=1):" )
print ( pd_hconcat)
pd_vconcat = pd. concat( [ df_A, df_B] , axis= 0 )
print ( "縱向拼接 (axis=0):" )
print ( pd_vconcat)
print ( "\n5. SciPy 矩陣拼接 (稀疏矩陣):" )
sparse_A = sp. csr_matrix( A)
sparse_B = sp. csr_matrix( B)
scipy_hconcat = sp. hstack( [ sparse_A, sparse_B] )
print ( "橫向拼接:" )
print ( scipy_hconcat. toarray( ) )
scipy_vconcat = sp. vstack( [ sparse_A, sparse_B] )
print ( "縱向拼接:" )
print ( scipy_vconcat. toarray( ) )
9. 矩陣求和與歸約操作
概念:沿指定軸對矩陣進行求和或歸約操作 應用場景: 池化操作(平均池化、最大池化) 注意力權重的歸一化 批處理統計量的計算 損失函數中的歸約
矩陣 A = [ [ 1 , 2 , 3 ] , [ 4 , 5 , 6 ] ] 按行求和 = [ 6 , 15 ]
按列求和 = [ 5 , 7 , 9 ]
全局求和 = 21
```python
import numpy as np
import tensorflow as tf
import torch
import pandas as pd
import scipy. sparse as sp
A = np. array( [ [ 1 , 2 , 3 ] , [ 4 , 5 , 6 ] ] )
print ( "原始矩陣:" )
print ( A)
print ( "\n1. NumPy 矩陣歸約操作:" )
np_row_sum = np. sum ( A, axis= 1 )
print ( "按行求和 (axis=1):" )
print ( np_row_sum)
np_col_sum = np. sum ( A, axis= 0 )
print ( "按列求和 (axis=0):" )
print ( np_col_sum)
np_total_sum = np. sum ( A)
print ( "全局求和:" )
print ( np_total_sum)
np_max = np. max ( A, axis= 1 )
np_min = np. min ( A, axis= 0 )
np_mean = np. mean( A)
print ( "按行最大值:" , np_max)
print ( "按列最小值:" , np_min)
print ( "全局平均值:" , np_mean)
print ( "\n2. TensorFlow 矩陣歸約操作:" )
tf_A = tf. constant( A)
tf_row_sum = tf. reduce_sum( tf_A, axis= 1 )
print ( "按行求和 (axis=1):" )
print ( tf_row_sum. numpy( ) )
tf_col_sum = tf. reduce_sum( tf_A, axis= 0 )
print ( "按列求和 (axis=0):" )
print ( tf_col_sum. numpy( ) )
tf_total_sum = tf. reduce_sum( tf_A)
print ( "全局求和:" )
print ( tf_total_sum. numpy( ) )
tf_max = tf. reduce_max( tf_A, axis= 1 )
tf_min = tf. reduce_min( tf_A, axis= 0 )
tf_mean = tf. reduce_mean( tf_A)
print ( "按行最大值:" , tf_max. numpy( ) )
print ( "按列最小值:" , tf_min. numpy( ) )
print ( "全局平均值:" , tf_mean. numpy( ) )
print ( "\n3. PyTorch 矩陣歸約操作:" )
torch_A = torch. tensor( A)
torch_row_sum = torch. sum ( torch_A, dim= 1 )
print ( "按行求和 (dim=1):" )
print ( torch_row_sum)
torch_col_sum = torch. sum ( torch_A, dim= 0 )
print ( "按列求和 (dim=0):" )
print ( torch_col_sum)
torch_total_sum = torch. sum ( torch_A)
print ( "全局求和:" )
print ( torch_total_sum)
torch_max = torch. max ( torch_A, dim= 1 ) . values
torch_min = torch. min ( torch_A, dim= 0 ) . values
torch_mean = torch. mean( torch_A)
print ( "按行最大值:" , torch_max)
print ( "按列最小值:" , torch_min)
print ( "全局平均值:" , torch_mean)
print ( "\n4. pandas 矩陣歸約操作:" )
df_A = pd. DataFrame( A)
pd_row_sum = df_A. sum ( axis= 1 )
print ( "按行求和 (axis=1):" )
print ( pd_row_sum)
pd_col_sum = df_A. sum ( axis= 0 )
print ( "按列求和 (axis=0):" )
print ( pd_col_sum)
pd_total_sum = df_A. values. sum ( )
print ( "全局求和:" )
print ( pd_total_sum)
pd_max = df_A. max ( axis= 1 )
pd_min = df_A. min ( axis= 0 )
pd_mean = df_A. mean( ) . mean( )
print ( "按行最大值:" , pd_max. values)
print ( "按列最小值:" , pd_min. values)
print ( "全局平均值:" , pd_mean)
print ( "\n5. SciPy 矩陣歸約操作 (稀疏矩陣):" )
sparse_A = sp. csr_matrix( A)
scipy_row_sum = sparse_A. sum ( axis= 1 )
print ( "按行求和 (axis=1):" )
print ( scipy_row_sum)
scipy_col_sum = sparse_A. sum ( axis= 0 )
print ( "按列求和 (axis=0):" )
print ( scipy_col_sum)
scipy_total_sum = sparse_A. sum ( )
print ( "全局求和:" )
print ( scipy_total_sum)
10. 矩陣廣播 (Broadcasting)
概念:自動擴展較小的矩陣以匹配較大矩陣的形狀,用于不同形狀矩陣間的運算 應用場景: 批處理中的權重共享 添加偏置項到特征矩陣 批量數據縮放 注意力機制的掩碼應用
矩陣 A = [ [ 1 , 2 , 3 ] , 向量 b = [ 10 , 20 , 30 ] [ 4 , 5 , 6 ] ] A + b = [ [ 11 , 22 , 33 ] , [ 14 , 25 , 36 ] ]
import numpy as np
import tensorflow as tf
import torch
import pandas as pd
import scipy. sparse as sp
A = np. array( [ [ 1 , 2 , 3 ] , [ 4 , 5 , 6 ] ] )
b = np. array( [ 10 , 20 , 30 ] )
print ( "矩陣A:" )
print ( A)
print ( "向量b:" )
print ( b)
print ( "\n1. NumPy 廣播操作:" )
np_broadcast = A + b
print ( "A + b (廣播):" )
print ( np_broadcast)
c = np. array( [ [ 100 ] , [ 200 ] ] )
np_broadcast2 = A + c
print ( "A + c (列向量廣播):" )
print ( np_broadcast2)
print ( "\n2. TensorFlow 廣播操作:" )
tf_A = tf. constant( A)
tf_b = tf. constant( b)
tf_broadcast = tf_A + tf_b
print ( "A + b (廣播):" )
print ( tf_broadcast. numpy( ) )
tf_c = tf. constant( [ [ 100 ] , [ 200 ] ] )
tf_broadcast2 = tf_A + tf_c
print ( "A + c (列向量廣播):" )
print ( tf_broadcast2. numpy( ) )
print ( "\n3. PyTorch 廣播操作:" )
torch_A = torch. tensor( A)
torch_b = torch. tensor( b)
torch_broadcast = torch_A + torch_b
print ( "A + b (廣播):" )
print ( torch_broadcast)
torch_c = torch. tensor( [ [ 100 ] , [ 200 ] ] )
torch_broadcast2 = torch_A + torch_c
print ( "A + c (列向量廣播):" )
print ( torch_broadcast2)
torch_b_expanded = torch. broadcast_to( torch_b, ( 2 , 3 ) )
print ( "顯式廣播b:" )
print ( torch_b_expanded)
print ( "\n4. pandas 廣播操作:" )
df_A = pd. DataFrame( A)
s_b = pd. Series( b)
pd_broadcast = df_A + s_b. values
print ( "使用numpy值進行廣播:" )
print ( pd_broadcast)
pd_broadcast2 = df_A. values + b
print ( "直接使用numpy操作:" )
print ( pd_broadcast2)
print ( "\n5. SciPy 廣播操作 (與NumPy類似):" )
sparse_A = sp. csr_matrix( A)
scipy_broadcast = sp. csr_matrix( sparse_A. toarray( ) + b)
print ( "稀疏矩陣廣播 (通過轉換):" )
print ( scipy_broadcast. toarray( ) )
11. 矩陣掩碼操作 (Masking)
概念:使用布爾矩陣選擇性地保留或修改矩陣中的特定元素 應用場景: 注意力掩碼(Transformer中的掩碼機制) 序列填充處理(處理變長序列) 條件計算(選擇性激活) 梯度掩碼(控制反向傳播)
矩陣 A = [ [ 1 , 2 , 3 ] , 掩碼 M = [ [ True , False , True ] , [ 4 , 5 , 6 ] ] [ False , True , False ] ] 應用掩碼后 = [ [ 1 , 0 , 3 ] , [ 0 , 5 , 0 ] ]
import numpy as np
import tensorflow as tf
import torch
import pandas as pd
import scipy. sparse as sp
A = np. array( [ [ 1 , 2 , 3 ] , [ 4 , 5 , 6 ] ] )
mask = np. array( [ [ True , False , True ] , [ False , True , False ] ] )
print ( "原始矩陣A:" )
print ( A)
print ( "掩碼矩陣mask:" )
print ( mask)
print ( "\n1. NumPy 掩碼操作:" )
np_masked = A * mask
print ( "應用掩碼 (A * mask):" )
print ( np_masked)
np_masked_specific = np. where( mask, A, - 1 )
print ( "應用掩碼 (特定值替換):" )
print ( np_masked_specific)
condition_mask = A > 3
np_conditional = np. where( condition_mask, A, 0 )
print ( "條件掩碼 (A > 3):" )
print ( np_conditional)
print ( "\n2. TensorFlow 掩碼操作:" )
tf_A = tf. constant( A)
tf_mask = tf. constant( mask)
tf_masked = tf_A * tf. cast( tf_mask, tf. int32)
print ( "應用掩碼 (A * mask):" )
print ( tf_masked. numpy( ) )
tf_masked_specific = tf. where( tf_mask, tf_A, - 1 )
print ( "應用掩碼 (特定值替換):" )
print ( tf_masked_specific. numpy( ) )
tf_condition_mask = tf_A > 3
tf_conditional = tf. where( tf_condition_mask, tf_A, 0 )
print ( "條件掩碼 (A > 3):" )
print ( tf_conditional. numpy( ) )
print ( "\n3. PyTorch 掩碼操作:" )
torch_A = torch. tensor( A)
torch_mask = torch. tensor( mask)
torch_masked = torch_A * torch_mask
print ( "應用掩碼 (A * mask):" )
print ( torch_masked)
torch_masked_specific = torch. where( torch_mask, torch_A, torch. tensor( - 1 ) )
print ( "應用掩碼 (特定值替換):" )
print ( torch_masked_specific)
torch_condition_mask = torch_A > 3
torch_conditional = torch. where( torch_condition_mask, torch_A, torch. tensor( 0 ) )
print ( "條件掩碼 (A > 3):" )
print ( torch_conditional)
seq_length = 4
causal_mask = torch. tril( torch. ones( seq_length, seq_length) )
print ( "因果掩碼 (下三角矩陣):" )
print ( causal_mask)
print ( "\n4. pandas 掩碼操作:" )
df_A = pd. DataFrame( A)
pd_mask = pd. DataFrame( mask)
pd_masked = df_A. mask( ~ pd_mask, 0 )
print ( "應用掩碼 (A.mask(~mask)):" )
print ( pd_masked)
pd_masked_specific = df_A. where( pd_mask, - 1 )
print ( "應用掩碼 (特定值替換):" )
print ( pd_masked_specific)
pd_condition_mask = df_A > 3
pd_conditional = df_A. where( pd_condition_mask, 0 )
print ( "條件掩碼 (A > 3):" )
print ( pd_conditional)
print ( "\n5. SciPy 掩碼操作 (稀疏矩陣):" )
sparse_A = sp. csr_matrix( A)
sparse_mask = sp. csr_matrix( mask)
scipy_masked = sparse_A. multiply( sparse_mask)
print ( "應用掩碼 (稀疏矩陣):" )
print ( scipy_masked. toarray( ) )
12. 矩陣切片和索引
概念:提取矩陣的子集或特定元素 應用場景: 特征選擇 批處理數據的提取 注意力機制中的頭部分割 隱藏狀態的選擇性訪問
矩陣 A = [ [ 1 , 2 , 3 , 4 ] , [ 5 , 6 , 7 , 8 ] , [ 9 , 10 , 11 , 12 ] ] 子矩陣 ( 行1 - 2 , 列1 - 3 ) = [ [ 6 , 7 ] , [ 10 , 11 ] ]
import numpy as np
import tensorflow as tf
import torch
import pandas as pd
import scipy. sparse as sp
A = np. array( [ [ 1 , 2 , 3 , 4 ] , [ 5 , 6 , 7 , 8 ] , [ 9 , 10 , 11 , 12 ] ] )
print ( "原始矩陣:" )
print ( A)
print ( "\n1. NumPy 切片和索引:" )
np_slice = A[ 1 : 3 , 1 : 3 ]
print ( "子矩陣 (行1-2, 列1-3):" )
print ( np_slice)
np_row = A[ 1 , : ]
np_col = A[ : , 2 ]
print ( "第二行:" )
print ( np_row)
print ( "第三列:" )
print ( np_col)
np_specific = A[ [ 0 , 2 ] , [ 1 , 3 ] ]
print ( "特定元素 (A[0,1] 和 A[2,3]):" )
print ( np_specific)
np_bool_idx = A[ A > 5 ]
print ( "所有大于5的元素:" )
print ( np_bool_idx)
print ( "\n2. TensorFlow 切片和索引:" )
tf_A = tf. constant( A)
tf_slice = tf_A[ 1 : 3 , 1 : 3 ]
print ( "子矩陣 (行1-2, 列1-3):" )
print ( tf_slice. numpy( ) )
tf_row = tf_A[ 1 , : ]
tf_col = tf_A[ : , 2 ]
print ( "第二行:" )
print ( tf_row. numpy( ) )
print ( "第三列:" )
print ( tf_col. numpy( ) )
tf_specific = tf. gather_nd( tf_A, [ [ 0 , 1 ] , [ 2 , 3 ] ] )
print ( "特定元素 (A[0,1] 和 A[2,3]):" )
print ( tf_specific. numpy( ) )
tf_bool_mask = tf_A > 5
tf_bool_idx = tf. boolean_mask( tf_A, tf_bool_mask)
print ( "所有大于5的元素:" )
print ( tf_bool_idx. numpy( ) )
print ( "\n3. PyTorch 切片和索引:" )
torch_A = torch. tensor( A)
torch_slice = torch_A[ 1 : 3 , 1 : 3 ]
print ( "子矩陣 (行1-2, 列1-3):" )
print ( torch_slice)
torch_row = torch_A[ 1 , : ]
torch_col = torch_A[ : , 2 ]
print ( "第二行:" )
print ( torch_row)
print ( "第三列:" )
print ( torch_col)
torch_specific = torch_A[ [ 0 , 2 ] , [ 1 , 3 ] ]
print ( "特定元素 (A[0,1] 和 A[2,3]):" )
print ( torch_specific)
torch_bool_mask = torch_A > 5
torch_bool_idx = torch_A[ torch_bool_mask]
print ( "所有大于5的元素:" )
print ( torch_bool_idx)
print ( "\n4. pandas 切片和索引:" )
df_A = pd. DataFrame( A)
pd_slice = df_A. iloc[ 1 : 3 , 1 : 3 ]
print ( "子矩陣 (行1-2, 列1-3):" )
print ( pd_slice)
pd_row = df_A. iloc[ 1 , : ]
pd_col = df_A. iloc[ : , 2 ]
print ( "第二行:" )
print ( pd_row)
print ( "第三列:" )
print ( pd_col)
pd_bool_idx = df_A[ df_A > 5 ]
print ( "所有大于5的元素 (保留NaN):" )
print ( pd_bool_idx)
print ( "\n5. SciPy 切片和索引 (稀疏矩陣):" )
sparse_A = sp. csr_matrix( A)
scipy_slice = sparse_A[ 1 : 3 , 1 : 3 ]
print ( "子矩陣 (行1-2, 列1-3):" )
print ( scipy_slice. toarray( ) )
scipy_row = sparse_A[ 1 , : ] . toarray( )
scipy_col = sparse_A[ : , 2 ] . toarray( )
print ( "第二行:" )
print ( scipy_row)
print ( "第三列:" )
print ( scipy_col)
13. 維度壓縮與擴展 (Squeeze/Unsqueeze)
概念 : squeeze : 移除tensor中所有大小為1的維度unsqueeze : 在指定位置添加一個大小為1的維度 應用場景 : 添加批處理維度進行批量操作 在CNN中調整通道維度 適配不同網絡層之間的輸入輸出維度 使用廣播機制進行計算前的維度準備
原始tensor: shape=[2, 1, 3] squeeze后: shape=[2, 3]
[[[ 1, 2, 3]], [[ 1, 2, 3],[[ 4, 5, 6]]] [ 4, 5, 6]]原始tensor: shape=[2, 3] unsqueeze(1)后: shape=[2, 1, 3]
[[ 1, 2, 3], [[[ 1, 2, 3]],[ 4, 5, 6]] [[ 4, 5, 6]]]
import numpy as np
import tensorflow as tf
import torch
import pandas as pd
x = np. array( [ [ [ 1 , 2 , 3 ] ] , [ [ 4 , 5 , 6 ] ] ] )
print ( "原始數組:" )
print ( x)
print ( "形狀:" , x. shape)
print ( "\n1. NumPy squeeze/expand_dims:" )
np_squeezed = np. squeeze( x, axis= 1 )
print ( "squeeze后:" )
print ( np_squeezed)
print ( "形狀:" , np_squeezed. shape)
y = np. array( [ [ 1 , 2 , 3 ] , [ 4 , 5 , 6 ] ] )
np_unsqueezed = np. expand_dims( y, axis= 1 )
print ( "\nexpand_dims后:" )
print ( np_unsqueezed)
print ( "形狀:" , np_unsqueezed. shape)
print ( "\n2. TensorFlow squeeze/expand_dims:" )
tf_x = tf. constant( x)
tf_squeezed = tf. squeeze( tf_x, axis= 1 )
print ( "squeeze后:" )
print ( tf_squeezed. numpy( ) )
print ( "形狀:" , tf_squeezed. shape)
tf_y = tf. constant( y)
tf_unsqueezed = tf. expand_dims( tf_y, axis= 1 )
print ( "\nexpand_dims后:" )
print ( tf_unsqueezed. numpy( ) )
print ( "形狀:" , tf_unsqueezed. shape)
print ( "\n3. PyTorch squeeze/unsqueeze:" )
torch_x = torch. tensor( x)
torch_squeezed = torch_x. squeeze( 1 )
print ( "squeeze后:" )
print ( torch_squeezed)
print ( "形狀:" , torch_squeezed. size( ) )
torch_y = torch. tensor( y)
torch_unsqueezed = torch_y. unsqueeze( 1 )
print ( "\nunsqueeze后:" )
print ( torch_unsqueezed)
print ( "形狀:" , torch_unsqueezed. size( ) )
print ( "\n4. Pandas 沒有直接對應的squeeze/unsqueeze函數,需轉換為NumPy處理" )
print ( "\n5. SciPy 稀疏矩陣不直接支持多維squeeze/unsqueeze,通常需轉為密集數組處理" )
14. 內存連續性 (Contiguous)
概念 :確保張量在內存中連續存儲,某些操作(如轉置、切片)會導致內存非連續應用場景 : 執行需要連續內存的操作前調用 視圖操作后提高后續計算效率 CUDA操作優化 與要求連續內存的外部庫交互
# 視覺表示(簡化):
非連續內存: [1, 2, 3, 4, 5, 6] <-- 內存中的實際存儲↑ ↑ ↑ 但邏輯訪問順序是: 1,4,2,5,3,6| | |
連續內存: [1, 4, 2, 5, 3, 6] <-- 重排后的內存存儲
import numpy as np
import tensorflow as tf
import torch
matrix = np. array( [ [ 1 , 2 , 3 ] , [ 4 , 5 , 6 ] ] )
print ( "1. PyTorch contiguous:" )
torch_matrix = torch. tensor( matrix)
print ( "原始張量是否連續:" , torch_matrix. is_contiguous( ) )
torch_transposed = torch_matrix. T
print ( "\n轉置后是否連續:" , torch_transposed. is_contiguous( ) )
torch_cont = torch_transposed. contiguous( )
print ( "應用contiguous后是否連續:" , torch_cont. is_contiguous( ) )
print ( "數據內容是否相同:" , torch. all ( torch_transposed == torch_cont) . item( ) )
print ( "\n2. NumPy ascontiguousarray:" )
np_transposed = matrix. T
print ( "轉置后是否C連續:" , np_transposed. flags. c_contiguous)
np_cont = np. ascontiguousarray( np_transposed)
print ( "應用ascontiguousarray后是否C連續:" , np_cont. flags. c_contiguous)
print ( "\n3. TensorFlow 沒有直接對應的contiguous函數,但可使用tf.identity創建副本" )
tf_matrix = tf. constant( matrix)
tf_transposed = tf. transpose( tf_matrix)
tf_copy = tf. identity( tf_transposed)
print ( "\n4. Pandas 沒有直接對應的contiguous函數" )
print ( "\n5. SciPy 稀疏矩陣使用特殊的內存表示方式,可通過格式轉換重新排列數據" )
15. 獲取張量維度 (Size)
概念 :獲取張量的維度信息(形狀)應用場景 : 調試和驗證張量維度 動態構建神經網絡層 數據處理前的維度檢查 動態調整批量大小
3D張量表示: shape=[2, 3, 4]
[[[值, 值, 值, 值],[值, 值, 值, 值],[值, 值, 值, 值]],[[值, 值, 值, 值],[值, 值, 值, 值],[值, 值, 值, 值]]]
import numpy as np
import tensorflow as tf
import torch
import pandas as pd
import scipy. sparse as sp
array = np. random. rand( 2 , 3 , 4 )
print ( "1. NumPy shape:" )
print ( "shape屬性:" , array. shape)
print ( "維度數量:" , array. ndim)
print ( "元素總數:" , array. size)
print ( "\n2. TensorFlow shape:" )
tf_tensor = tf. constant( array)
print ( "shape屬性:" , tf_tensor. shape)
print ( "維度數量:" , tf. rank( tf_tensor) )
print ( "元素總數:" , tf. size( tf_tensor) )
print ( "\n3. PyTorch size:" )
torch_tensor = torch. tensor( array)
print ( "size():" , torch_tensor. size( ) )
print ( "shape屬性:" , torch_tensor. shape)
print ( "指定維度大小 size(0):" , torch_tensor. size( 0 ) )
print ( "維度數量:" , torch_tensor. dim( ) )
print ( "\n4. Pandas shape:" )
df = pd. DataFrame( np. random. rand( 3 , 4 ) )
print ( "shape屬性:" , df. shape)
print ( "行數:" , df. shape[ 0 ] )
print ( "列數:" , df. shape[ 1 ] )
print ( "元素總數:" , df. size)
print ( "\n5. SciPy shape:" )
sparse_matrix = sp. csr_matrix( np. random. rand( 3 , 4 ) )
print ( "shape屬性:" , sparse_matrix. shape)
print ( "維度數量:" , sparse_matrix. ndim)
print ( "非零元素數量:" , sparse_matrix. nnz)
16. 張量重復 (Repeat)
概念 :沿指定維度重復張量的元素應用場景 : 生成批量數據 構建注意力掩碼 擴展特征向量以匹配其他張量 圖像處理中的上采樣
原始矩陣: [[1, 2], repeat(2,1) → [[1, 2, 1, 2],[3, 4]] [3, 4, 3, 4]]原始矩陣: [[1, 2], repeat(2,2) → [[1, 2, 1, 2],[3, 4]] [3, 4, 3, 4],[1, 2, 1, 2],[3, 4, 3, 4]]
import numpy as np
import tensorflow as tf
import torch
import pandas as pd
import scipy. sparse as sp
matrix = np. array( [ [ 1 , 2 ] , [ 3 , 4 ] ] )
print ( "原始矩陣:" )
print ( matrix)
print ( "形狀:" , matrix. shape)
print ( "\n1. NumPy tile/repeat:" )
np_tiled = np. tile( matrix, ( 2 , 2 ) )
print ( "np.tile((2, 2))后:" )
print ( np_tiled)
print ( "形狀:" , np_tiled. shape)
np_repeat_0 = np. repeat( matrix, 2 , axis= 0 )
print ( "\nnp.repeat(2, axis=0)后:" )
print ( np_repeat_0)
print ( "形狀:" , np_repeat_0. shape) np_repeat_1 = np. repeat( matrix, 2 , axis= 1 )
print ( "\nnp.repeat(2, axis=1)后:" )
print ( np_repeat_1)
print ( "形狀:" , np_repeat_1. shape)
print ( "\n2. TensorFlow tile/repeat:" )
tf_matrix = tf. constant( matrix)
tf_tiled = tf. tile( tf_matrix, [ 2 , 2 ] )
print ( "tf.tile([2, 2])后:" )
print ( tf_tiled. numpy( ) )
print ( "形狀:" , tf_tiled. shape)
tf_repeat_0 = tf. repeat( tf_matrix, 2 , axis= 0 )
print ( "\ntf.repeat(2, axis=0)后:" )
print ( tf_repeat_0. numpy( ) )
print ( "形狀:" , tf_repeat_0. shape)
print ( "\n3. PyTorch repeat:" )
torch_matrix = torch. tensor( matrix)
torch_repeat = torch_matrix. repeat( 2 , 2 )
print ( "torch.repeat(2, 2)后:" )
print ( torch_repeat)
print ( "形狀:" , torch_repeat. size( ) )
vector = torch. tensor( [ 1 , 2 , 3 ] )
vector_expanded = vector. unsqueeze( 0 )
vector_repeated = vector_expanded. repeat( 3 , 1 )
print ( "\n向量經unsqueeze(0)和repeat(3, 1)后:" )
print ( vector_repeated)
print ( "形狀:" , vector_repeated. size( ) )
print ( "\n4. Pandas repeat:" )
df = pd. DataFrame( { 'A' : [ 1 , 3 ] , 'B' : [ 2 , 4 ] } )
df_repeat_index = pd. DataFrame( np. repeat( df. values, 2 , axis= 0 ) , columns= df. columns)
print ( "行重復2次:" )
print ( df_repeat_index)
print ( "\n5. SciPy 稀疏矩陣沒有直接的repeat函數,可轉換為密集數組后處理" )
17. 上三角矩陣 (Triu)
概念 :提取或創建上三角矩陣,即只保留主對角線和主對角線以上的元素應用場景 : 生成注意力掩碼(自回歸模型、Transformer解碼器) 矩陣分解 線性方程組求解 避免重復計算(如距離矩陣)
原始矩陣: triu操作后: triu(k=1)后:
[[1, 2, 3], [[1, 2, 3], [[0, 2, 3],[4, 5, 6], → [0, 5, 6], → [0, 0, 6],[7, 8, 9]] [0, 0, 9]] [0, 0, 0]]
import numpy as np
import tensorflow as tf
import torch
import pandas as pd
import scipy. sparse as sp
matrix = np. array( [ [ 1 , 2 , 3 ] , [ 4 , 5 , 6 ] , [ 7 , 8 , 9 ] ] )
print ( "原始矩陣:" )
print ( matrix)
print ( "\n1. NumPy triu:" )
np_triu = np. triu( matrix)
print ( "np.triu(matrix):" )
print ( np_triu)
np_triu_k1 = np. triu( matrix, k= 1 )
print ( "\nnp.triu(matrix, k=1):" )
print ( np_triu_k1)
print ( "\n2. TensorFlow 上三角:" )
tf_matrix = tf. constant( matrix)
tf_triu = tf. linalg. band_part( tf_matrix, 0 , - 1 )
print ( "tf上三角矩陣:" )
print ( tf_triu. numpy( ) )
tf_triu_k1 = tf. linalg. band_part( tf_matrix, - 1 , - 1 ) - tf. linalg. band_part( tf_matrix, 0 , - 1 )
print ( "\ntf主對角線上方:" )
print ( tf_triu_k1. numpy( ) )
print ( "\n3. PyTorch triu:" )
torch_matrix = torch. tensor( matrix)
torch_triu = torch. triu( torch_matrix)
print ( "torch.triu():" )
print ( torch_triu)
torch_triu_k1 = torch. triu( torch_matrix, diagonal= 1 )
print ( "\ntorch.triu(diagonal=1):" )
print ( torch_triu_k1)
print ( "\n4. Pandas 沒有直接的triu函數,可通過NumPy實現:" )
df = pd. DataFrame( matrix)
df_triu = pd. DataFrame( np. triu( df. values) )
print ( df_triu)
print ( "\n5. SciPy triu:" )
sparse_matrix = sp. csr_matrix( matrix)
scipy_triu = sp. triu( sparse_matrix)
print ( "sp.triu():" )
print ( scipy_triu. toarray( ) )
scipy_triu_k1 = sp. triu( sparse_matrix, k= 1 )
print ( "\nsp.triu(k=1):" )
print ( scipy_triu_k1. toarray( ) )