线程中的异常 "main" java.lang.ArrayIndexOutOfBoundsException ArrayIndexOutOfExceptions 乘以两个矩阵
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException ArrayIndexOutOfExceptions in multiplying two matrices
尝试在 java 中实现神经网络并出现上述错误,我已经尝试并在谷歌上搜索了很多,但我无法获得任何解决方案。
Driver class:
public class Driver {
static double [][]X ={
{0, 0},
{1, 0},
{0, 1},
{1, 1}
};
static double [][] Y = {
{0},{1},{1},{0}
};
public static void main(String[] args) {
NNMain nn = new NNMain(2,10,1); // 2,10,1
List<Double> output;
nn.fit(X, Y, 50000);
double [][] inputMatrix = {
{0,0},{0,1},{1,0},{1,1}
};
for(double d[]:inputMatrix)
{
output = nn.predict(d);
System.out.println(output.toString());
}
The error is occurring while trying to multiply two matrices:
public static Matrix multiply(Matrix a, Matrix b) {
Matrix temp = new Matrix(a.r, a.c);
for (int i = 0; i < temp.r; i++)
{
for (int j = 0; j < temp.c; j++) {
double sum=0;
for(int k=0; k < a.c; k++)
{
sum += a.data[i][k] * b.data[k][j]; // ERROR HERE
}
temp.data[i][j]=sum;
}
}
return temp;
}
这里的fit方法是调用上面在另一个文件中定义的multiply函数。我已尝试更改 for 循环条件 <= 0 或 1,但我无法使其正常工作。
你确定 a 的列数 (a.c) 总是等于 b 的行数 (b.r) 这是矩阵乘法的条件吗?
尝试在 java 中实现神经网络并出现上述错误,我已经尝试并在谷歌上搜索了很多,但我无法获得任何解决方案。 Driver class:
public class Driver {
static double [][]X ={
{0, 0},
{1, 0},
{0, 1},
{1, 1}
};
static double [][] Y = {
{0},{1},{1},{0}
};
public static void main(String[] args) {
NNMain nn = new NNMain(2,10,1); // 2,10,1
List<Double> output;
nn.fit(X, Y, 50000);
double [][] inputMatrix = {
{0,0},{0,1},{1,0},{1,1}
};
for(double d[]:inputMatrix)
{
output = nn.predict(d);
System.out.println(output.toString());
}
The error is occurring while trying to multiply two matrices:
public static Matrix multiply(Matrix a, Matrix b) {
Matrix temp = new Matrix(a.r, a.c);
for (int i = 0; i < temp.r; i++)
{
for (int j = 0; j < temp.c; j++) {
double sum=0;
for(int k=0; k < a.c; k++)
{
sum += a.data[i][k] * b.data[k][j]; // ERROR HERE
}
temp.data[i][j]=sum;
}
}
return temp;
}
这里的fit方法是调用上面在另一个文件中定义的multiply函数。我已尝试更改 for 循环条件 <= 0 或 1,但我无法使其正常工作。
你确定 a 的列数 (a.c) 总是等于 b 的行数 (b.r) 这是矩阵乘法的条件吗?