关于张量分解的文献很多,我向感兴趣的读者推荐 Kolda 和 Balder 的 综述[2]。特别是,Tucker 分解具有许多应用,例如张量回归,将张量用作 目标[3]或 预测[4]变量。关键的一点是,它允许提取一个核张量,即原始数据的压缩版本。如果这让你想起了 PCA,那就对了: Tucker 分解的步骤之一实际上是 SVD 的扩展即 高阶奇异值分解。
现有的算法允许提取核张量以及分解矩阵(在我们的应用程序中未使用)。超参数是秩 n。无需赘述,主要思想是 n 值越高,分解越精确。秩 n 也决定了核张量的大小。如果 n 较小,则重构的张量可能与原始张量不完全匹配,但是数据维度降得越低:如何权衡取决于当前应用。
A,B 和 C 是分解矩阵,而 G 是核张量,其维数由 n 指定。图像来源:Kolda,Tamara G. 和 Brett W. Bader。
提取此核张量会具有很大用处,下面的实际应用例子中将看到这一点。
# Import libraries import cv2 import numpy as np import random import tensorly as tl from tensorly.decomposition import tucker # Create VideoCapture objects parking_lot = cv2.VideoCapture('parking_lot.MOV') patio = cv2.VideoCapture('patio.MOV') commute = cv2.VideoCapture('commute.MOV') # Get number of frames in each video parking_lot_frames = int(parking_lot.get(cv2.CAP_PROP_FRAME_COUNT)) patio_frames = int(patio.get(cv2.CAP_PROP_FRAME_COUNT)) commute_frames = int(commute.get(cv2.CAP_PROP_FRAME_COUNT))
从这些张量中随机采样 50 帧以加快后面操作
# Set the seed for reproducibility random.seed(42) random_frames = random.sample(range(0, commute_frames), 50) # Use these random frames to subset the tensors subset_parking_lot = parking_lot_tensor[random_frames,:,:,:] subset_patio = patio_tensor[random_frames,:,:,:] subset_commute = commute_tensor[random_frames, :, :, :] # Convert three tensors to double subset_parking_lot = subset_parking_lot.astype('d') subset_patio = subset_patio.astype('d') subset_commute = subset_commute.astype('d')
# Get core tensor for the parking lot video core_parking_lot, factors_parking_lot = tucker(subset_parking_lot, ranks = [2,2,2,2]) # Get core tensor for the patio video core_patio, factors_patio = tucker(subset_patio, ranks = [2,2,2,2]) # Get core tensor for the commute video core_commute, factors_commute = tucker(subset_commute, ranks = [2,2,2,2]) # Compare core parking lot and patio parking_patio_diff = tl.norm(core_parking_lot - core_patio) int(parking_patio_diff) # Compare core parking lot and commute parking_commute_diff= tl.norm(core_parking_lot - core_commute) int(parking_commute_diff) # Compare core patio and commute patio_commute_diff = tl.norm(core_patio - core_commute) int(patio_commute_diff)
Nacos /nɑ:kəʊs/ 是 Dynamic Naming and Configuration Service 的首字母简称,一个易于构建 AI Agent 应用的动态服务发现、配置管理和AI智能体管理平台。Nacos 致力于帮助您发现、配置和管理微服务及AI智能体应用。Nacos 提供了一组简单易用的特性集,帮助您快速实现动态服务发现、服务配置、服务元数据、流量管理。Nacos 帮助您更敏捷和容易地构建、交付和管理微服务平台。
Sublime Text具有漂亮的用户界面和强大的功能,例如代码缩略图,Python的插件,代码段等。还可自定义键绑定,菜单和工具栏。Sublime Text 的主要功能包括:拼写检查,书签,完整的 Python API , Goto 功能,即时项目切换,多选择,多窗口等等。Sublime Text 是一个跨平台的编辑器,同时支持Windows、Linux、Mac OS X等操作系统。