🫐Python光电光对光神经网络非相干光图像低维映射模拟
🎯要点
🎯光学神经网络非相干光图像处理 | 🎯光电光对光处理多层光学神经网络 | 🎯光图像传感器构建两层神经网络 | 🎯非相干光输入图像映射到低维空间 | 🎯多层非线性对比浅层线性光神经网络 | 🎯模拟算法图像分类评估:手绘图形、生物细胞图像、实景三维对象。
🍪语言内容分比
🍇Python矩阵矢量乘法优化
数学定义
矩阵乘矩阵
如果A是矩阵并且B是矩阵
矩阵乘积 (不带乘号或点表示)被定义为 矩阵
于是
对于 和 。
也就是说,乘积的条目是通过将A的第i行和B的第j列的条目逐项相乘,然后求和得到的这些 乘积。换句话说, 是A 的第i 行和B 的第j 列的点积。
因此, 也可以写成
因此,当且仅当 A 中的列数等于 B 中的行数(在本例中为 n)时,才定义乘积 A B。
矩阵乘矢量
长度为 的向量 可以被视为列向量,对应于 矩阵 X,其条目由 给出 如果 A 是一个 矩阵, 表示的矩阵乘以向量乘积就是向量 ,将其视为列向量,等于矩阵。在索引表示法中,这相当于:
看待这个问题的一种方法是假设从“普通”向量到列向量以及返回的变化是隐式的。
类似地,长度为 n 的向量 x 可以被视为行向量,对应于 矩阵。为了清楚地表明行向量的含义,在这种情况下通常将其表示为列向量的转置;因此,人们会看到诸如 之类的符号。
恒等式 成立。在索引表示法中,如果 是 矩阵,则 相当于: 。
代码优化示例
许多数值计算库都具有高效的矢量化操作实现。矩阵乘法、查找点积等操作非常高效。这些操作的实现是为了利用 CPU 中的多个核心,并将计算卸载到 GPU(如果可用)。通常,矩阵和向量的操作由 BLAS(基本线性代数子程序)提供。一些示例是 Intel MKL、OpenBLAS、cuBLAS 等。
首先我们创建两个矩阵,以便我们可以用它来检查我们的实现是否正确。
import tensorflow as tf
import numpy as np
tf.__version__ # 2.0.0
a = np.random.normal(size=(200, 784)).astype('float32')
b = np.random.normal(size=(784, 10)).astype('float32')
expected = np.matmul(a, b)
不使用外部库实现功能,
def py_matmul1(a, b):
ra, ca = a.shape
rb, cb = b.shape
assert ca == rb, f"{ca} != {rb}"
output = np.zeros(shape=(ra, cb))
for i in range(ra):
for j in range(cb):
for k in range(rb):
output[i, j] += a[i, k] * b[k, j]
return output
%time result = py_matmul1(a, b)
assert result.shape == expected.shape
assert np.allclose(result, expected, rtol=1e-02), (result, expected)
执行时,在我的计算机上需要 1.38 秒。它相当慢,可以大大改进。如果您注意到,最内层循环基本上是在计算两个向量的点积。在这种情况下,两个向量分别是 a 和 b 的第 i 行和第 j 列。所以让我们用点积实现删除最内层循环。
矢量优化一:
def py_matmul2(a, b):
ra, ca = a.shape
rb, cb = b.shape
assert ca == rb, f"{ca} != {rb}"
output = np.zeros(shape=(ra, cb))
for i in range(ra):
for j in range(cb):
# we replaced the loop with dot product
output[i, j] = np.dot(a[i], b[:,j])
return output
%time result = py_matmul2(a, b)
assert result.shape == expected.shape
assert np.allclose(result, expected, rtol=1e-02), (result, expected)
此实现仅需 6 毫秒。与原始实现相比,这是一个巨大的改进。由于内部循环本质上是在计算点积,我们将其替换为 np.dot
函数,并传递矩阵 a 中的第 i 行和矩阵 b 中的第 j 列。
矢量优化二:
现在让我们删除迭代矩阵 b 的列的 for 循环。
def py_matmul3(a, b):
ra, ca = a.shape
rb, cb = b.shape
assert ca == rb, f"{ca} != {rb}"
output = np.zeros(shape=(ra, cb))
for i in range(ra):
output[i] = np.dot(a[i], b)
return output
%time result = py_matmul3(a, b)
assert result.shape == expected.shape
assert np.allclose(result, expected, rtol=1e-02), (result, expected)
此实现耗时 2.97 毫秒。使用称为广播的技术,我们可以从本质上消除循环,仅使用一行 output[i] = np.dot(a[i], b)
,我们就可以计算输出矩阵第 i 行的整个值。numpy 所做的是广播向量 a[i],使其与矩阵 b 的形状相匹配。然后它计算每对向量的点积。广播规则在 numpy、tensorflow、pytorch 等主要库中几乎相同。
库优化三:
现在让我们使用 numpy 的内置 matmul 函数。
def py_matmul4(a, b):
ra, ca = a.shape
rb, cb = b.shape
assert ca == rb, f"{ca} != {rb}"
return np.matmul(a, b)
%time result = py_matmul4(a, b)
assert result.shape == expected.shape
assert np.allclose(result, expected, rtol=1e-02), (result, expected)
使用numpy的内置matmul函数,需要999 μ 𝜇 s。这是迄今为止我们实施的最快的。
库优化四:
在张量流中,它也与 numpy 非常相似。我们只需要调用 matmul 函数即可。
def py_matmul5(a, b):
ra, ca = a.shape
rb, cb = b.shape
assert ca == rb, f"{ca} != {rb}"
return tf.matmul(a, b)
tf_a = tf.constant(a)
tf_b = tf.constant(b)
%time result = py_matmul5(tf_a, tf_b)
assert result.shape == expected.shape
assert np.allclose(result, expected, rtol=1e-02), (result, expected)
TensorFlow 计算结果大约需要 999 μ s。我们可以直接传递 numpy 数组,而不必转换为 TensorFlow 张量,但执行速度会慢一些。在我的实验中,如果我只调用 py_matmul5(a, b),大约需要 10 毫秒,但使用 tf.constant 函数将 numpy 数组转换为 tf.Tensor 可以获得更好的性能。
Last updated
Was this helpful?