Java/Processing 如何用点积做矩阵乘法
Java/Processing how to do Matrix Multipication with the dot product
你好,我正在使用处理( 本质上只是 Java),我想要一个非常简单的矩阵 class 来帮助我的神经网络。
它工作正常,但 "matrix-multipication" 部分实际上不起作用。
我知道我的代码有误,但我似乎找不到解决方法。
class 的开头如下所示:
class Matrix {
int rows;
int cols;
double[][] matrix;
Matrix(int rows_ , int cols_ ) {
rows = rows_;
cols = cols_;
// set size of matrix
matrix = new double[rows][cols];
// fill with 0s
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
matrix[i][j] = 0;
}
}
}
错误的部分在这里:
Matrix Matrix_Multipication(Matrix b) {
// Create new Matrix for the result
Matrix c = new Matrix(b.cols,rows);
// check if not number of cols is number of rows of b
if (cols != b.rows) {
return c;
}
// Compute
for(int i=0; i< c.cols; i++){
for(int j=0; j< c.rows; j++){
for(int k=0; k< rows; k++){
c.matrix[i][j] = c.matrix[i][j] + matrix[i][k] * b.matrix[k][j]; // here is the error
}
}
}
// return new matrix
return c;
}
错误是:
ArrayIndexOutOfBoundsExcpetion : 1
如果列大小为一,我只会收到此错误:
Matrix m1 = new Matrix(2,3);
Matrix m2 = new Matrix(3,1); // here the 1
Matrix m3 = m1.Matrix_Multipication(m2); // apply Matrix_Multipication
我想也许构造函数也有问题,但我不知道怎么会错。
您还可以向我展示一个矩阵库,以及如果您找不到任何错误我如何安装它们。
ps: 我做了研究,但我没有中继发现任何东西。
我尝试编写自己版本的神经网络库形式 "The-Coding-Train" https://www.youtube.com/watch?v=NgZAIkDcPkI&list=PLRqwX-V7Uu6Y7MdSCaIfsxc561QI0U0Tb&index=8 .
请告诉我在这个问题和这段代码上我需要改进什么。
第一个索引表示行。但是你迭代到 c.cols。
第二个索引是列。但是你迭代到 c.rows。
只是交换它们。
您交换了行和列。
构造函数中的第一个参数是行,第二个参数是列:
Matrix c = new Matrix(rows,b.cols);
并且由于矩阵是行主序矩阵,控制变量i
必须运行从= 0
到< c.rows
和控制变量j
从 = 0
到 < c.cols
:
for(int i=0; i< c.rows; i++){
for(int j=0; j< c.cols; j++){
for(int k=0; k< rows; k++){
c.matrix[i][j] = c.matrix[i][j] + matrix[i][k] * b.matrix[k][j];
}
}
}
你好,我正在使用处理( 本质上只是 Java),我想要一个非常简单的矩阵 class 来帮助我的神经网络。
它工作正常,但 "matrix-multipication" 部分实际上不起作用。
我知道我的代码有误,但我似乎找不到解决方法。
class 的开头如下所示:
class Matrix {
int rows;
int cols;
double[][] matrix;
Matrix(int rows_ , int cols_ ) {
rows = rows_;
cols = cols_;
// set size of matrix
matrix = new double[rows][cols];
// fill with 0s
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
matrix[i][j] = 0;
}
}
}
错误的部分在这里:
Matrix Matrix_Multipication(Matrix b) {
// Create new Matrix for the result
Matrix c = new Matrix(b.cols,rows);
// check if not number of cols is number of rows of b
if (cols != b.rows) {
return c;
}
// Compute
for(int i=0; i< c.cols; i++){
for(int j=0; j< c.rows; j++){
for(int k=0; k< rows; k++){
c.matrix[i][j] = c.matrix[i][j] + matrix[i][k] * b.matrix[k][j]; // here is the error
}
}
}
// return new matrix
return c;
}
错误是:
ArrayIndexOutOfBoundsExcpetion : 1
如果列大小为一,我只会收到此错误:
Matrix m1 = new Matrix(2,3);
Matrix m2 = new Matrix(3,1); // here the 1
Matrix m3 = m1.Matrix_Multipication(m2); // apply Matrix_Multipication
我想也许构造函数也有问题,但我不知道怎么会错。
您还可以向我展示一个矩阵库,以及如果您找不到任何错误我如何安装它们。
ps: 我做了研究,但我没有中继发现任何东西。 我尝试编写自己版本的神经网络库形式 "The-Coding-Train" https://www.youtube.com/watch?v=NgZAIkDcPkI&list=PLRqwX-V7Uu6Y7MdSCaIfsxc561QI0U0Tb&index=8 .
请告诉我在这个问题和这段代码上我需要改进什么。
第一个索引表示行。但是你迭代到 c.cols。 第二个索引是列。但是你迭代到 c.rows。 只是交换它们。
您交换了行和列。
构造函数中的第一个参数是行,第二个参数是列:
Matrix c = new Matrix(rows,b.cols);
并且由于矩阵是行主序矩阵,控制变量i
必须运行从= 0
到< c.rows
和控制变量j
从 = 0
到 < c.cols
:
for(int i=0; i< c.rows; i++){
for(int j=0; j< c.cols; j++){
for(int k=0; k< rows; k++){
c.matrix[i][j] = c.matrix[i][j] + matrix[i][k] * b.matrix[k][j];
}
}
}