"““光子存内计算核心运算模拟:Y = W @ X””" import numpy as np class OpticalPIM: "““模拟光子存内计算:强度编码 + 光域矩阵乘””“ def init(self, weights, noise_level=0.05): self.weights = np.array(weights, dtype=float) self.noise_level = noise_level # 模拟光学噪声比例 self.gc_count = 0 # 光域计算次数 def encode(self, x): ”““强度编码:数据映射为光强,限制在 0~1””" x = np.clip(x, 0.0, 1.0) return x def forward(self, x): "““并行乘加:光传播过程就是 W 与 x 的矩阵乘法””“ x_enc = self.encode(x) y = self.weights @ x_enc if self.noise_level > 0: # 散粒噪声:强度与信号成正比 noise = np.random.normal( loc=0.0, scale=self.noise_level * np.abs(y) ) y = y + noise self.gc_count += 1 return y def main(): w = [ [0.7, 0.2, 0.5], [0.4, 0.9, 0.3], [0.6, 0.5, 0.8], ] pim = OpticalPIM(w, noise_level=0.08) x = np.array([0.8, 0.6, 0.4]) y_opt = pim.forward(x) y_ele = np.asarray(w) @ x print(“输入 x :”, x) print(“光学输出:”, np.round(y_opt, 3)) print(“理想输出:”, np.round(y_ele, 3)) print(“误差:”, np.round(np.abs(y_opt - y_ele), 3)) print(“计算次数:”, pim.gc_count) if name == ”main": main() ```

岗位关联

代码用途:用 Python 模拟光子存内计算的核心数学过程——输入强度编码、光域传播加权、光电探测器累加,并与理想电子计算结果对比,观察噪声对精度的影响。