Skip to content

Latest commit

 

History

History
112 lines (79 loc) · 10 KB

File metadata and controls

112 lines (79 loc) · 10 KB

核心算法与底层推导日志 (From-Scratch Algorithm Notes)

这份文档专门为您准备,用于应对期末答辩和 Presentation 时可能面对的最强硬的学术提问,即:“你们在项目中真正手写的底层数学代数和机器学习算法到底有哪些?”


🇨🇳 中文解析 (Chinese with English Terminology)

一、L2 归一化代数推导 (L2 Normalization)

发生于 normalize_vectors() 函数。

1. 遇到的问题: 无论 HuggingFace 的 Transformer 模型转录出了多少维(比如 384维 或者 768维)的稠密向量矩阵,不同的文本最终转换出的向量**模长(大小/标量绝对长度)**都不一样。由于文本有长有短,它的高维折射射程也就有远有近。这会使得我们仅仅寻找“相似性质(特质方向角度)”的主任务变得极其受干扰且造成比较运算的不公。

2. 数学手段与公式: 我们为此特地加入了 L2 距离范数的强力归一化惩罚操作,把高维宇宙中所有不同长短乱飞的粗细词向量,统统强行拍压成一条长度标尺仅仅为 1 的单向标准利箭(即数学定义的:单位向量 Unit Vector)。 如果将输入大模型产出的张量视为多维空间点的位移 V,其 L2 原生模长自然即为自身内部每个维度的欧几里得距离: ||V|| = √(V₁² + V₂² + ... + Vₙ²)

之后我们使代码矩阵中每一个维度的分量数值,强制除以自己的这个总欧氏模长标量,便达成了归一化收束重写: V_norm = V / ||V||

在我们的 Python 真实工程代码中,通过严格执行 norms = np.linalg.norm(vectors, axis=1) 找寻该个体的长短分母项,随后配合 Numpy 的全局广播机制(Broadcasting)完美的批量完成了这个退化收敛过程。


二、点积降维提速思想 (Cosine to Dot-Product Simplification)

发生于 cosine_similarity()batch_cosine_similarity() 函数。

1. 理论基础: 判定两个文本像不像,在 NLP 中就是纯粹客观地计算它们两条高维向量在 N 维空间上岔开夹角的余弦偏射数值 cos(θ)。岔开得越小(趋向于 0度平行贴合),那么算出的 cos(0) ≈ 1(也意味着性质最接近);反之若是方向垂直甚至南辕北辙 cos(90) = 0(说明两者毫无关联)。 但请注意,原始的余弦代数相似度方程本应当是无比繁复且耗费 CPU 算力的除法: cos(θ) = (A · B) / (||A|| × ||B||)

2. 实战中的工程思维体现: 我们将这一步精明地简化了。因为我们在上一步强制要求游戏池库里的所有矩阵必须无脑经历先遣归一化洗礼(即硬性赋予属性使得 ||A|| = 1||B|| = 1)。 此时再从代数逻辑上往前进行推导演算,公式底段那巨大的除法计算分母就被完美等切变成了常数 1 并彻底隐身消失了。 原本笨重的原始空间余弦比较方程,历经我们架构处理后,极简地完美剥离并退化成了干练的纯向量内积乘法操作 (Vector Dot Product),这毫无疑问极大地避免和省去了计算机底层浮点数除法所需要的算力! cos(θ) = A_norm · B_norm 在我们工程代码的内核体现,就是极度干练迅速的一行核心 np.dot(d_norm, q_norm) 批量相乘返回全部的结果比较矩阵列。这也就是为什么哪怕系统抛弃调包,转而要求纯 Python 手写遍历五千甚至数万个游戏矩阵比较,我们的实时前端系统延迟也被无情地死死压在了极窄的毫秒级水平。


三、排雷惩罚系统的空间偏移反向推力 (Vector Algebra / Dealbreakers Penalty)

发生于 rank_games_for_query() 函数。

1. 机制痛点: 传统过滤搜索一般基于 SQL 化或死板的布尔树形操作。但在真正落地的现实自然口语化搜索体验中,用户的厌恶意图往往是重叠、潜意识且非常模糊的高纬度化情绪(例如抱怨:“这不能有氪金,也不能有挂机成分”),这就无情地要求我们必须超越硬性排重,转而进行在高维几何层面上的软性干预措施。

2. 代数分离向外排斥推力原理: 当感知捕捉到用户要求不要存在某种游戏体验和氛围时,我们在代码内截取这股排斥情绪段落,将其生成独立负向牵引向量意图 Q_negative。 随后基于权重经验,系统将它附加挂载为一个十分强而有力的偏移推力拉扯因子参数 α (惩罚权重常数推荐设定为 0.5)。 它在多维空间起到的直观作用是:与原本追求的最完美心仪定点目标 Q_positive 相背驰,系统将利用这股因子矩阵对最初始坐标点发起偏移折射反向推移: Q_optimized = Q_positive - (α × Q_negative) 代码物理实现:q_vec = q_vec - (alpha * neg_vec)

向教授进行口述汇报的总结:在做 Dealbreaker(避雷防坑机制)展示时,您不必向全库进行低级的枚举筛选。告诉他们,我们直接将“避开游戏致命缺陷”的口语意境,完全实现了绝对无标签化(Zero Boolean Hard-Filter)情况下的泛维度物理坐标轴点直接偏移运算。这就是 Embeddings 最本质的作用与突破口!


🇺🇸 English Version

1. L2 Normalization

Executed in the normalize_vectors() function.

1. The Problem Encountered: Regardless of whether the HuggingFace Transformer model outputs dense vector matrices possessing 384 or 768 dimensions, the absolute magnitude (Length/Scalar) of the generated vectors varies drastically depending on the length of the source text. Longer descriptions yield farther spatial trajectories in high-dimensional projections. This causes severe interference and unfair computational bias when our primary goal is to solely identify "Semantic Similarities" (directional angles).

2. Mathematical Approach & Formulas: To compensate, we deliberately introduced a forceful L2 Normalization penalty operation. We compress these wildly varying vector lengths down into uniform, standard arrows precisely 1 unit long (mathematically defined as a Unit Vector). If an input embedding tensor is viewed as a spatial displacement V, its raw L2 norm is naturally the Euclidean distance across all dimensions: ||V|| = √(V₁² + V₂² + ... + Vₙ²)

Afterward, we force the component value of every single dimension inside the code matrix to be divided by its total Euclidean norm scalar, achieving a converged, normalized rewrite: V_norm = V / ||V||

In our Python engineering environment, this degradation process is mass-executed by combining norms = np.linalg.norm(vectors, axis=1) to locate the denominator, paired seamlessly with Numpy's global Broadcasting mechanics.


2. Cosine to Dot-Product Simplification (Dimensionality Acceleration)

Executed in cosine_similarity() & batch_cosine_similarity() functions.

1. Theoretical Foundation: In NLP, determining whether two Texts are alike boils down to objectively calculating the spatial Cosine angle cos(θ) between their two high-dimensional trajectories. The smaller the diverging angle (approaching 0 degrees parallel), the closer cos(0) ≈ 1 becomes (signifying maximum similarity). However, the original cosine similarity algebraic equation mandates an intensely expensive division calculation: cos(θ) = (A · B) / (||A|| × ||B||)

2. Engineering Implementation: We cleverly simplified this step. Because we mandated in the previous step that all matrices within the game pool must ruthlessly undergo normalization (enforcing ||A|| = 1 and ||B|| = 1). Deriving the algebra forward, the massive division denominator at the bottom of the formula perfectly equates to a constant 1, thus vanishing completely. The traditionally heaviest spatial cosine equation, having passed through our architecture, is perfectly reduced into a swift, pure Vector Dot Product operation, vastly bypassing the computational resources demanded by floating-point division! cos(θ) = A_norm · B_norm Within the core of our engineering code, this is manifested in a single incredibly concise line: np.dot(d_norm, q_norm). This explains why, even when abandoning Scikit-Learn libraries to manually traverse thousands of arrays, our real-time latency remains relentlessly pinned to mere milliseconds.


3. Dealbreakers Penalty via Vector Algebra Offset (Repulsion Thrust)

Executed in rank_games_for_query() function.

1. Mechanism Pain Points: Traditional filter searching generally relies on SQL-style or rigid Boolean methodologies. However, in authentic natural-language search experiences, user aversions are frequently overlapping, subconscious, and highly dimensionally ambiguous. This demands that we transcend rigid boolean exclusion, shifting instead toward soft intervention directly within the high-dimensional geometric layout.

2. Algebraic Separation and Outward Repulsion Theory: When the system senses a user demanding the absence of a specific gameplay atmosphere, we intercept that aversive emotional string and generate an isolated, negative-pull intent vector Q_negative. Following weighted empirical logic, the system continuously appends this as an aggressively strong offset repulsion factor, denoted by α (the recommended penalty weight constant is 0.5). In multi-dimensional space, its direct visual effect operates strictly opposite to the user's primary ideal target location Q_positive. The system utilizes this factor matrix to actively initiate a repulsive trajectory shift upon the initial starting coordinate: Q_optimized = Q_positive - (α × Q_negative) Physical code implementation: q_vec = q_vec - (alpha * neg_vec)

Presentation Summary for the Professor: When demonstrating the Dealbreaker defense algorithm, assert that we fully achieved Boolean Label-less multi-dimensional direct physical axis point offset modeling. We discarded obsolete brute-force keyword exclusion, choosing instead to mathematically manipulate the spatial gravity of NLP Embeddings—the absolute pinnacle breakthrough for achieving a premier academic grade.