Ich versuche, das Punktprodukt von einer Matrix 331x23152 und 23152x23152 zu nehmen.Langsames Punktprodukt in R
In Python und Octave ist dies eine triviale Operation, aber in R scheint dies unglaublich langsam zu sein.
N <- 331
M <- 23152
mat_1 = matrix(rnorm(N*M,mean=0,sd=1), N, M)
mat_2 = matrix(rnorm(N*M,mean=0,sd=1), M, M)
tm3 <- system.time({
mat_3 = mat_1%*%mat_2
})
print(tm3)
Der Ausgang ist
user system elapsed
101.95 0.04 101.99
Mit anderen Worten, dieses Punktprodukt über 100 Sekunden dauert auszuführen.
Ich benutze R-3.4.0 64-bit, mit RStudio v1.0.143 auf einem i7-4790 mit 16 GB RAM. Daher habe ich nicht erwartet, dass diese Operation so lange dauert.
Habe ich etwas übersehen? Ich habe angefangen, die Pakete bigmemory und bigalgebra zu betrachten, aber ich kann nicht anders, als zu denken, dass es eine Lösung gibt, ohne auf Pakete zurückgreifen zu müssen.
EDIT
Um Ihnen eine Vorstellung von Zeitdifferenz, hier ist ein Skript für Octave:
n = 331;
m = 23152;
mat_1 = rand(n,m);
mat_2 = rand(m,m);
tic
mat_3 = mat_1*mat_2;
toc
Der Ausgang ist
Elapsed time is 3.81038 seconds.
Und in Python:
import numpy as np
import time
n = 331
m = 23152
mat_1 = np.random.random((n,m))
mat_2 = np.random.random((m,m))
tm_1 = time.time()
mat_3 = np.dot(mat_1,mat_2)
tm_2 = time.time()
tm_3 = tm_2 - tm_1
print(tm_3)
Der Ausgang ist
2.781277894973755
Wie Sie sehen können, diese Zahlen sind nicht einmal in derselben Liga.
EDIT 2
Bei Zheyuan Li Anfrage sind hier Spielzeug Beispiele für Punktprodukte.
In R:
mat_1 = matrix(c(1,2,1,2,1,2), nrow = 2, ncol = 3)
mat_2 = matrix(c(1,1,1,2,2,2,3,3,3), nrow = 3, ncol = 3)
mat_3 = mat_1 %*% mat_2
print(mat_3)
Der Ausgang ist:
[,1] [,2] [,3]
[1,] 3 6 9
[2,] 6 12 18
In Octave:
mat_1 = [1,1,1;2,2,2];
mat_2 = [1,2,3;1,2,3;1,2,3];
mat_3 = mat_1*mat_2
Der Ausgang ist:
mat_3 =
3 6 9
6 12 18
In Python:
import numpy as np
mat_1 = np.array([[1,1,1],[2,2,2]])
mat_2 = np.array([[1,2,3],[1,2,3],[1,2,3]])
mat_3 = np.dot(mat_1, mat_2)
print(mat_3)
Der Ausgang ist:
[[ 3 6 9]
[ 6 12 18]]
Für weitere Informationen über Matrix Skalarprodukte: https://en.wikipedia.org/wiki/Matrix_multiplication
EDIT 3
Die Ausgabe für sessionInfo()
ist:
> sessionInfo()
R version 3.4.0 (2017-04-21)
Platform: x86_64-w64-mingw32/x64 (64-bit)
Running under: Windows 7 x64 (build 7601) Service Pack 1
Matrix products: default
locale:
[1] LC_COLLATE=Dutch_Netherlands.1252 LC_CTYPE=Dutch_Netherlands.1252 LC_MONETARY=Dutch_Netherlands.1252
[4] LC_NUMERIC=C LC_TIME=Dutch_Netherlands.1252
attached base packages:
[1] stats graphics grDevices utils datasets methods base
loaded via a namespace (and not attached):
[1] compiler_3.4.0 tools_3.4.0
EDIT 4
Ich versuchte, das bigalgebra
Paket aber das schien nicht die Dinge zu beschleunigen:
library('bigalgebra')
N <- 331
M <- 23152
mat_1 = matrix(rnorm(N*M,mean=0,sd=1), N, M)
mat_1 <- as.big.matrix(mat_1)
mat_2 = matrix(rnorm(N*M,mean=0,sd=1), M, M)
tm3 <- system.time({
mat_3 = mat_1%*%mat_2
})
print(tm3)
Die Ausgabe lautet:
user system elapsed
101.79 0.00 101.81
EDIT 5
James schlug meine zufällig generierten Matrix zu verändern:
N <- 331
M <- 23152
mat_1 = matrix(runif(N*M), N, M)
mat_2 = matrix(runif(M*M), M, M)
tm3 <- system.time({
mat_3 = mat_1%*%mat_2
})
print(tm3)
Die Ausgabe lautet:
user system elapsed
102.46 0.05 103.00
Die Geschwindigkeit von Rs Matrix-Operationen hängt von Ihrer R-Version ab und davon, ob die BLAS-Bibliotheken eingebunden sind. Eine einfache Möglichkeit ist die Installation von Microsoft R Open, oder Sie können es an [Intel MKL] (https : //software.intel.com/en-us/articles/using-intel-mkl-with-r). [Siehe hier für mehr] (https://simplystatistics.org/2016/01/21/parallel-blas-in-r/). –
@ 李哲源 ZheyuanLi: Wenn du meinst, möchte ich das Skalarprodukt nehmen, dann ja? Soweit ich weiß, nehmen alle drei Implementierungen das Skalarprodukt aus zwei Matrizen, oder fehlt mir etwas? – BdB
Mit 8 Kernen: R: 4 bis 5 Kerne, Python: 7 bis 8 Kerne, Oktave: 8 Kerne. In der Tat scheint es, dass R etwa die Hälfte der verfügbaren Verarbeitungsleistung verwendet. – BdB