- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
假设,我有一个维度为 n
xn
的二元(邻接)矩阵 A
和另一个矩阵 U
> 尺寸为 n
xl
。我使用以下代码来计算我需要的新矩阵。
import numpy as np
from numpy import linalg as LA
new_U = np.zeros_like(U)
for idx, a in np.ndenumerate(A):
diff = U[idx[0], :] - U[idx[1], :]
if a == 1.0:
new_U[idx[0], :] += 2 * diff
elif a == 0.0:
norm_diff = LA.norm(U[idx[0], :] - U[idx[1], :])
new_U[idx[0], :] += -2 * diff * np.exp(-norm_diff**2)
return new_U
即使 n
和 l
很小,这也需要相当多的时间来运行。有没有更好的方法来重写(向量化)此代码以减少运行时间?
编辑 1:示例输入和输出。
A = np.array([[0,1,0], [1,0,1], [0,1,0]], dtype='float64')
U = np.array([[2,3], [4,5], [6,7]], dtype='float64')
new_U = np.array([[-4.,-4.], [0,0],[4,4]], dtype='float64')
其中u_ik = U[i, k]
、u_jk = U[j, k]
和u_i = U[i, :]
.另外,(i,j)\in E
对应于代码中的a == 1.0
。
最佳答案
利用broadcasting
和 np.einsum
用于求和 -
# Get pair-wise differences between rows for all rows in a vectorized manner
Ud = U[:,None,:]-U
# Compute norm L1 values with those differences
L = LA.norm(Ud,axis=2)
# Compute 2 * diff values for all rows and mask it with ==0 condition
# and sum along axis=1 to simulate the accumulating behaviour
p1 = np.einsum('ijk,ij->ik',2*Ud,A==1.0)
# Similarly, compute for ==1 condition and finally sum those two parts
p2 = np.einsum('ijk,ij,ij->ik',-2*Ud,np.exp(-L**2),A==0.0)
out = p1+p2
或者,使用 einsum
计算平方范数值并使用这些值来获取 p2
-
Lsq = np.einsum('ijk,ijk->ij',Ud,Ud)
p2 = np.einsum('ijk,ij,ij->ik',-2*Ud,np.exp(-Lsq),A==0.0)
关于python-3.x - 如何在 numpy 中向量化两个矩阵的函数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56663783/
我想使用Tensorflow的transform_graph工具优化图形。我尝试优化 MultiNet 中的图表(以及其他具有类似编码器-解码器架构的)。然而,优化后的图在使用 quantize_we
我试图在 C# 中将图像量化为 10 种颜色,但在绘制量化图像时遇到问题,我已经制作了映射表并且它是正确的,我已经制作了原始图像的副本并且正在更改基于映射表的像素颜色,我使用下面的代码: bm = n
我需要降低UIImage的颜色深度,但是我不知道该怎么做。结果应与在Photoshop中应用索引颜色相同。 我的目标是要具有较低的色深(32色或更低)。也许这是我的错,但我没有找到解决方法。令人惊讶的
我有大量的 numpy 向量,每个形状 (3,) 都有 8 位整数值: vec = np.random.randint(2**8, size=3) 我想通过一些已知的缩减因子将这些向量量化到更小的空间
我正在用 jQuery 编写一个心理学应用程序。我的项目的一部分需要测量用户对声音的 react 时间(用户按下一个键)。因此,我需要在调用(&时间戳)声音文件和实际开始播放之间的延迟尽可能小地播放声
最近,我开始使用 Tensorflow + Keras 创建神经网络,我想尝试 Tensorflow 中提供的量化功能。到目前为止,使用 TF 教程中的示例进行试验效果很好,我有这个基本的工作示例(来
使用当前的 Tensorflow quantization ops ,我将如何在推理过程中模拟每 channel 量化?这paper将每层量化定义为 We can specify a single q
我已经卡住了我的模型并获得了 .pb 文件。然后我在 Linux 上使用 tocoConverter 量化我的模型,因为 Windows 不支持它。我有 quantized_model.tflite。
我将 git 用于一个稍微不寻常的目的——它在我写小说时存储我的文本。 (我知道,我知道......令人讨厌。) 我正在尝试跟踪生产力,并想衡量后续提交之间的差异程度。作家代表“作品”的是“文字”,至
quantization有什么区别和 simplification ? 量化是另一种简化方式吗? 在某些情况下使用量化更好吗? 或者我应该同时使用两者? 最佳答案 几何体的总大小由两个因素控制:点数和
扎克伯格说,Llama3-8B还是太大了,不适合放到手机中,有什么办法? 量化、剪枝、蒸馏,如果你经常关注大语言模型,一定会看到这几个词,单看这几个字,我们很难理解它们都干了些什么,但
相对于
我正在将一些我无法控制的 XML 转换为 XHTML。 XML 模式定义了一个 段落标记和 和 用于列表。 我经常在这个 XML 中找到嵌套在段落中的列表。因此,直接转换会导致 s 嵌套在 中s,
我看到过这样的说法:CNN 的更深层次可以学习识别更复杂的特征。这通常附带一张早期过滤器识别直线/简单曲线的图片,以及后期过滤器识别更复杂图案的图片。它具有直观意义:您距离数据越远,您对数据的理解就越
在使用 C++ 的带有 tensorflow lite 的树莓派上,对象检测无法正常工作。我的代码编译并运行,但输出似乎从未得到正确填充。我是否会遗漏任何依赖项或错误地访问结果? 我遵循了以下教程:
如何衡量/量化 Corona SDK 游戏应用中的“迟缓”? 我在我构建的基于 Corona SDK 的物理游戏(使用 Box2D)上寻找旧手机(例如 iPhone 4、Samsung GT-I900
我正在尝试创建一个 Tensorflow 量化模型,以便使用 Coral USB 加速器进行推理。这是我的问题的一个最小的独立示例: import sys import tensorflow as t
我有一个分位数回归模型,其中包含 1 个回归变量和 1 个回归变量。我想假设检验回归量在每个分位数上都相等。我想到的一种方法是在 {0.01,0.02,....,0.99} 上测试所有 tau。但是,
要求做,在 PGM 文件上使用 KMeans 进行 vector 量化(或图像压缩) 图像是 PMG 文件,其中 b = block 大小,k = 次数,t = 迭代,-g = 初始质心 图像是这样的
我是一名优秀的程序员,十分优秀!