我试图找出最快的矩阵乘法方法,尝试了3种不同的方法:
numpy.dot(a, b)ctypesPython中的模块与C连接.这是转换为共享库的C代码:
#include <stdio.h>
#include <stdlib.h>
void matmult(float* a, float* b, float* c, int n) {
int i = 0;
int j = 0;
int k = 0;
/*float* c = malloc(nay * sizeof(float));*/
for (i = 0; i < n; i++) {
for (j = 0; j < n; j++) {
int sub = 0;
for (k = 0; k < n; k++) {
sub = sub + a[i * n + …Run Code Online (Sandbox Code Playgroud) 我需要获取包含对数概率的两个NumPy矩阵(或其他2d数组)的矩阵乘积.np.log(np.dot(np.exp(a), np.exp(b)))出于显而易见的原因,天真的方式不是优选的.
运用
from scipy.misc import logsumexp
res = np.zeros((a.shape[0], b.shape[1]))
for n in range(b.shape[1]):
# broadcast b[:,n] over rows of a, sum columns
res[:, n] = logsumexp(a + b[:, n].T, axis=1)
Run Code Online (Sandbox Code Playgroud)
工作但运行速度比慢100倍 np.log(np.dot(np.exp(a), np.exp(b)))
运用
logsumexp((tile(a, (b.shape[1],1)) + repeat(b.T, a.shape[0], axis=0)).reshape(b.shape[1],a.shape[0],a.shape[1]), 2).T
Run Code Online (Sandbox Code Playgroud)
或者其他瓦片和重塑的组合也起作用,但是比上面的循环运行得更慢,因为实际大小的输入矩阵需要非常大量的存储器.
我目前正在考虑在C中编写一个NumPy扩展来计算它,但当然我宁愿避免这种情况.是否有既定的方法来执行此操作,或者是否有人知道执行此计算的内存密集程度较低的方法?
编辑: 感谢larsmans提供此解决方案(参见下面的推导):
def logdot(a, b):
max_a, max_b = np.max(a), np.max(b)
exp_a, exp_b = a - max_a, b - max_b
np.exp(exp_a, out=exp_a)
np.exp(exp_b, out=exp_b)
c = np.dot(exp_a, exp_b)
np.log(c, out=c)
c += max_a + …Run Code Online (Sandbox Code Playgroud) 我想尽可能快地计算相同维度的两个矩阵的行方点积.这就是我这样做的方式:
import numpy as np
a = np.array([[1,2,3], [3,4,5]])
b = np.array([[1,2,3], [1,2,3]])
result = np.array([])
for row1, row2 in a, b:
result = np.append(result, np.dot(row1, row2))
print result
Run Code Online (Sandbox Code Playgroud)
当然输出是:
[ 26. 14.]
Run Code Online (Sandbox Code Playgroud) 什么是倍增(逐元素)2D张量(矩阵)的最有效方法:
x11 x12 .. x1N
...
xM1 xM2 .. xMN
Run Code Online (Sandbox Code Playgroud)
通过垂直向量:
w1
...
wN
Run Code Online (Sandbox Code Playgroud)
获得一个新的矩阵:
x11*w1 x12*w2 ... x1N*wN
...
xM1*w1 xM2*w2 ... xMN*wN
Run Code Online (Sandbox Code Playgroud)
为了给出一些上下文,我们M在批处理中可以并行处理数据样本,并且每个N元素样本必须乘以w存储在变量中的权重,以最终Xij*wj为每行选择最大值i.
python linear-algebra matrix-multiplication tensorflow tensor
我知道在1-d情况下,两个矢量之间的卷积a可以被计算为
b,也可作为在之间的乘积conv(a, b)和T_a,其中b是用于相应的托普利兹矩阵T_a.
是否有可能将这个想法扩展到2-D?
给定a和a = [5 1 3; 1 1 2; 2 1 3]是否有可能b=[4 3; 1 2]在Toeplitz矩阵中进行转换a并T_a在1-D情况下计算矩阵矩阵乘积?
convolution matrix-multiplication neural-network deep-learning conv-neural-network
我正在尝试使用多维数组([2][2])进行简单的矩阵乘法.我对此有点新意见,而我却无法找到它我做错了什么.我非常感谢能告诉我它是什么的任何帮助.我宁愿不使用库或类似的东西,我主要是这样做以了解它是如何工作的.非常感谢你提前.
我在主方法中声明我的arays如下:
Double[][] A={{4.00,3.00},{2.00,1.00}};
Double[][] B={{-0.500,1.500},{1.000,-2.0000}};
Run Code Online (Sandbox Code Playgroud)
A*B应该返回单位矩阵.它没有.
public static Double[][] multiplicar(Double[][] A, Double[][] B){
//the method runs and returns a matrix of the correct dimensions
//(I actually changed the .length function to a specific value to eliminate
//it as a possible issue), but not the correct values
Double[][] C= new Double[2][2];
int i,j;
////I fill the matrix with zeroes, if I don't do this it gives me an error
for(i=0;i<2;i++) {
for(j=0;j<2;j++){
C[i][j]=0.00000;
}
}
///this is where …Run Code Online (Sandbox Code Playgroud) java arrays matrix multidimensional-array matrix-multiplication
在R中,矩阵乘法非常优化,即实际上只是对BLAS/LAPACK的调用.但是,我很惊讶这个非常天真的C++代码用于矩阵向量乘法似乎可靠地快了30%.
library(Rcpp)
# Simple C++ code for matrix multiplication
mm_code =
"NumericVector my_mm(NumericMatrix m, NumericVector v){
int nRow = m.rows();
int nCol = m.cols();
NumericVector ans(nRow);
double v_j;
for(int j = 0; j < nCol; j++){
v_j = v[j];
for(int i = 0; i < nRow; i++){
ans[i] += m(i,j) * v_j;
}
}
return(ans);
}
"
# Compiling
my_mm = cppFunction(code = mm_code)
# Simulating data to use
nRow = 10^4
nCol = 10^4
m = matrix(rnorm(nRow * nCol), …Run Code Online (Sandbox Code Playgroud) 我正在尝试使用纯python将两个矩阵相乘.输入(X1是3x3,Xt是3x2):
X1 = [[1.0016, 0.0, -16.0514],
[0.0, 10000.0, -40000.0],
[-16.0514, -40000.0, 160513.6437]]
Xt = [(1.0, 1.0),
(0.0, 0.25),
(0.0, 0.0625)]
Run Code Online (Sandbox Code Playgroud)
其中Xt是另一个矩阵的zip转置.现在这里是代码:
def matrixmult (A, B):
C = [[0 for row in range(len(A))] for col in range(len(B[0]))]
for i in range(len(A)):
for j in range(len(B[0])):
for k in range(len(B)):
C[i][j] += A[i][k]*B[k][j]
return C
Run Code Online (Sandbox Code Playgroud)
python给我的错误是:IndexError:列表索引超出范围.现在我不确定Xt是否被识别为矩阵并且仍然是列表对象,但从技术上讲这应该可行.
假设我有一个AxBxC矩阵X 和一个BxD矩阵Y.
是否有一种非循环方法,通过它我可以将每个C AxB矩阵与Y?
做点什么
import numpy as np
a = np.random.rand(10**4, 10**4)
b = np.dot(a, a)
Run Code Online (Sandbox Code Playgroud)
使用多个核心,运行良好.
a但是,中的元素是64位浮点数(或32位平台中的32位?),我想将8位整数数组相乘.但尝试以下方法:
a = np.random.randint(2, size=(n, n)).astype(np.int8)
Run Code Online (Sandbox Code Playgroud)
导致dot产品不使用多个内核,因此在我的PC上运行速度慢约1000倍.
array: np.random.randint(2, size=shape).astype(dtype)
dtype shape %time (average)
float32 (2000, 2000) 62.5 ms
float32 (3000, 3000) 219 ms
float32 (4000, 4000) 328 ms
float32 (10000, 10000) 4.09 s
int8 (2000, 2000) 13 seconds
int8 (3000, 3000) 3min 26s
int8 (4000, 4000) 12min 20s
int8 (10000, 10000) It didn't finish in 6 hours
float16 (2000, 2000) 2min 25s
float16 (3000, …Run Code Online (Sandbox Code Playgroud) python ×5
matrix ×3
numpy ×3
arrays ×1
benchmarking ×1
blas ×1
c ×1
convolution ×1
dot-product ×1
java ×1
logarithm ×1
matlab ×1
performance ×1
r ×1
rcpp ×1
scipy ×1
tensor ×1
tensorflow ×1