- mongodb - 在 MongoDB mapreduce 中,如何展平值对象?
- javascript - 对象传播与 Object.assign
- html - 输入类型 ="submit"Vs 按钮标签它们可以互换吗?
- sql - 使用 MongoDB 而不是 MS SQL Server 的优缺点
我正在使用 Clojure 开发一个应用程序,该应用程序需要将大型矩阵相乘,与相同的 Numpy 版本相比,我遇到了一些较大的性能问题。 Numpy 似乎能够在一秒钟内将一个 1,000,000x23 矩阵乘以它的转置,而等效的 clojure 代码则需要六分钟以上。 (我可以从 Numpy 打印出结果矩阵,所以它肯定会评估所有内容)。
我在这个 Clojure 代码中做错了什么吗?是否有一些我可以尝试模仿的 Numpy 技巧?
这是 python :
import numpy as np
def test_my_mult(n):
A = np.random.rand(n*23).reshape(n,23)
At = A.T
t0 = time.time()
res = np.dot(A.T, A)
print time.time() - t0
print np.shape(res)
return res
# Example (returns a 23x23 matrix):
# >>> results = test_my_mult(1000000)
#
# 0.906938076019
# (23, 23)
还有clojure:
(defn feature-vec [n]
(map (partial cons 1)
(for [x (range n)]
(take 22 (repeatedly rand)))))
(defn dot-product [x y]
(reduce + (map * x y)))
(defn transpose
"returns the transposition of a `coll` of vectors"
[coll]
(apply map vector coll))
(defn matrix-mult
[mat1 mat2]
(let [row-mult (fn [mat row]
(map (partial dot-product row)
(transpose mat)))]
(map (partial row-mult mat2)
mat1)))
(defn test-my-mult
[n afn]
(let [xs (feature-vec n)
xst (transpose xs)]
(time (dorun (afn xst xs)))))
;; Example (yields a 23x23 matrix):
;; (test-my-mult 1000 i/mmult) => "Elapsed time: 32.626 msecs"
;; (test-my-mult 10000 i/mmult) => "Elapsed time: 628.841 msecs"
;; (test-my-mult 1000 matrix-mult) => "Elapsed time: 14.748 msecs"
;; (test-my-mult 10000 matrix-mult) => "Elapsed time: 434.128 msecs"
;; (test-my-mult 1000000 matrix-mult) => "Elapsed time: 375751.999 msecs"
;; Test from wikipedia
;; (def A [[14 9 3] [2 11 15] [0 12 17] [5 2 3]])
;; (def B [[12 25] [9 10] [8 5]])
;; user> (matrix-mult A B)
;; ((273 455) (243 235) (244 205) (102 160))
更新:我使用 JBLAS 实现了相同的基准测试。库并发现了巨大的速度改进。感谢大家的意见!是时候用 Clojure 包装这个傻瓜了。这是新代码:
(import '[org.jblas FloatMatrix])
(defn feature-vec [n]
(FloatMatrix.
(into-array (for [x (range n)]
(float-array (cons 1 (take 22 (repeatedly rand))))))))
(defn test-mult [n]
(let [xs (feature-vec n)
xst (.transpose xs)]
(time (let [result (.mmul xst xs)]
[(.rows result)
(.columns result)]))))
;; user> (test-mult 10000)
;; "Elapsed time: 6.99 msecs"
;; [23 23]
;; user> (test-mult 100000)
;; "Elapsed time: 43.88 msecs"
;; [23 23]
;; user> (test-mult 1000000)
;; "Elapsed time: 383.439 msecs"
;; [23 23]
(defn matrix-stream [rows cols]
(repeatedly #(FloatMatrix/randn rows cols)))
(defn square-benchmark
"Times the multiplication of a square matrix."
[n]
(let [[a b c] (matrix-stream n n)]
(time (.mmuli a b c))
nil))
;; forma.matrix.jblas> (square-benchmark 10)
;; "Elapsed time: 0.113 msecs"
;; nil
;; forma.matrix.jblas> (square-benchmark 100)
;; "Elapsed time: 0.548 msecs"
;; nil
;; forma.matrix.jblas> (square-benchmark 1000)
;; "Elapsed time: 107.555 msecs"
;; nil
;; forma.matrix.jblas> (square-benchmark 2000)
;; "Elapsed time: 793.022 msecs"
;; nil
最佳答案
Python 版本正在编译为 C 中的循环,而 Clojure 版本正在为此代码中的每个映射调用构建一个新的中间序列。您看到的性能差异很可能来自数据结构的差异。
为了比这更好,您可以使用像 Incanter 这样的库。或按照 this SO question 中的说明编写您自己的版本.另见this one , neanderthal或 nd4j .如果您真的想保留序列以保持惰性评估属性等,那么您可以通过查看 transients 获得真正的提升。用于内部矩阵计算
编辑:忘记添加调优clojure的第一步,打开“反射警告”
关于python - Clojure 与 Numpy 中的矩阵乘法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8899773/
如果矩阵A在X中,矩阵B在Y中。 进行乘法运算只是 Z = X*Y。正确假设两个数组的大小相同。 如何使用 for 循环计算它? 最佳答案 ja72 的anwser 是错误的,请查看我在其下的评论以了
我有一个 C 程序,它有 n 次乘法(单次乘法和 n 次迭代),我发现另一个逻辑有 n/2 次迭代(1 次乘法 + 2 次加法)。我知道两者都是 O(n) 的复杂性。但就 CPU 周期而言。哪个更快?
我有一个矩阵x: x <- matrix(1:8, nrow = 2, ncol = 4, byrow = 2) # [,1] [,2] [,3] [,4] #[1,] 1 2 3
我有一个矩阵x: x <- matrix(1:8, nrow = 2, ncol = 4, byrow = 2) # [,1] [,2] [,3] [,4] #[1,] 1 2 3
我正在创建一个基于电影 InTime 的 Minecraft 插件,并尝试创建代码,在玩家死亡时玩家将失去 25% 的时间。 当前代码是: String minus = itapi.getTimeSt
我正在尝试将 2 个矩阵与重载的 * 运算符相乘并打印结果。虽然看起来我不能为重载函数提供超过 1 个参数。如何将这两个矩阵传递给重载函数?请在下面查看我的实现。 #include #include
为什么在 Java 中使用 .*?例如 double probability = 1.*count/numdata; 给出相同的输出: double probability = count/numda
如果我尝试将两个值与单位相乘,则会出现意外错误。 $test: 10px; .testing{ width: $test * $test; } result: 100px*px isn't a v
我正在尝试计算库存中所有产品的总值(value)。表中的每种产品都有价格和数量。因此,我需要将每种产品的价格乘以数量,然后将所有这些加在一起以获得所有产品的总计。根据上一个问题,我现在可以使用 MyS
我正在尝试计算库存中所有产品的总值(value)。表中的每种产品都有价格和数量。因此,我需要将每种产品的价格乘以数量,然后将所有这些加在一起以获得所有产品的总计。根据上一个问题,我现在可以使用 MyS
大家好,我有以下代码行 solution first = mylist.remove((int)(Math.random() * mylist)); 这给了我一个错误说明 The operator *
我必须做很多乘法运算。如果我考虑效率,那么我应该使用位运算而不是常规的 * 运算吗?如果有差异如何进行位运算?提前致谢.. 最佳答案 不,您应该使用乘法运算符,让优化编译器决定如何最快地完成它。 您会
两个 n 位数字 A 和 B 的乘法可以理解为移位的总和: (A << i1) + (A << i2) + ... 其中 i1, i2, ... 是 B 中设置为 1 的位数。 现在让我们用 OR
我想使用 cuda 6 进行 bool 乘法,但我无法以正确的方式做到这一点。B 是一个 bool 对称矩阵,我必须进行 B^n bool 乘法。 我的 C++ 代码是: for (m=0; m
我正在编写一个定点类,但遇到了一些问题...乘法、除法部分,我不确定如何模拟。我对部门运算符(operator)进行了非常粗暴的尝试,但我确信这是错误的。到目前为止,它是这样的: class Fixe
我有TABLE_A我需要创建 TABLE_A_FINAL 规则: 在TABLE_A_FINAL中我们有包含 ID_C 的所有可能组合的行如果在 TABLE_A与 ID_C 的组合相同我们乘以 WEIG
这个问题在这里已经有了答案: Simple way to repeat a string (32 个答案) 关闭 6 年前。 我有一个任务是重复字符乘以它例如用户应该写重复输入 3 R 输出的字母和
我最近学习了C++的基础知识。我发现了一些我不明白的东西。这是让我有点困惑的程序。 #include using namespace std; int main()
我有两个列表: list_a = list_b = list(范围(2, 6)) final_list = [] 我想知道如何将两个列表中的所有值相乘。我希望我的 final_list 包含 [2*2
如何修改此代码以适用于任何基数? (二进制、十六进制、基数 10 等) int mult(int a, int b, int base){ if((a<=base)||(b<=base)){
我是一名优秀的程序员,十分优秀!