我正在将一个多层感知器算法从R程序转录到python,但是我遇到了一个问题,矩阵乘法给了我不同的结果:
R语言:
> hidden <- matrix(c(-0.4442772, -0.1844143, 0.2783537, 0.2599441, -0.3190043, 0.2062185), nrow=2, ncol=3)
> Xp <- c(0, 1, 1)
> hidden %*% Xp
[,1]
[1,] -0.0406506
[2,] 0.4661626
的Python:
>>> import numpy as np
>>> Xp = np.array([0, 1, 1])
>>> hidden = np.asmatrix([[-0.4442772, -0.1844143, 0.2783537], [0.2599441, -0.3190043, 0.2062185]])
>>> np.dot(hidden, nq)
matrix([[ 0.0939394, -0.1127858]])
使用Wolfram,Python是正确的,但即使它没有收敛到正确的算法结果。我在这方面搜索了两种语言之间的差异,但没有找到任何结果。除此之外,我还应用了另一个矩阵乘法进行测试,两个结果相同:
R语言:
> m1 <- matrix(c(2,3,4,7,0,1), nrow = 3, ncol=2)
> m2 <- matrix(c(1,6,4,3,6,9), nrow = 2, ncol=3)
> m1 %*% m2
[,1] [,2] [,3]
[1,] 44 29 75
[2,] 3 12 18
[3,] 10 19 33
的Python:
>>> import numpy as np
>>> m1 = np.matrix([[2,7], [3,0], [4,1]])
>>> m2 = np.matrix([[1,4,6], [6,3,9]])
>>> np.dot(m1,m2)
matrix([[44, 29, 75],
[ 3, 12, 18],
[10, 19, 33]])
这种情况有什么区别?我怎样才能在Python中达到这个结果?
答案 0 :(得分:2)
我建议你打印你创建的矩阵:
# [,1] [,2] [,3]
#[1,] -0.4442772 0.2783537 -0.3190043
#[2,] -0.1844143 0.2599441 0.2062185
这与你的Wolfram Alpha链接显示的不一样。
如果你创建相同的矩阵,你会得到与python和Mathematica相同的结果:
hidden <- matrix(c(-0.4442772, -0.1844143, 0.2783537, 0.2599441, -0.3190043, 0.2062185), nrow=2, ncol=3,
byrow = TRUE) #fill matrix by row instead of default by column
# [,1] [,2] [,3]
#[1,] -0.4442772 -0.1844143 0.2783537
#[2,] 0.2599441 -0.3190043 0.2062185
Xp <- c(0, 1, 1)
hidden %*% Xp
# [,1]
#[1,] 0.0939394
#[2,] -0.1127858