我编写了一个简单的类,并在main()中对其进行了手动测试,并按预期工作:
import org.nd4j.linalg.api.ndarray.INDArray;
import org.nd4j.linalg.factory.Nd4j;
public class ND4J {
public static void main(String[] args) {
ND4J actualObject = new ND4J(Nd4j.zeros(2,2).add(4.0));
INDArray testObject = Nd4j.create(new double[][]{{4.0,4.0}, {4.0,4.0}});
if(testObject.equals(actualObject.getMatrix())){
System.out.println("OK"); // prints “OK”
}
}
private INDArray matrix;
public ND4J (INDArray matrix){
this.matrix = matrix;
}
public INDArray getMatrix(){
return this.matrix;
}
public String toString(){
return this.getMatrix().toString();
}
}
但是尝试使用JUnit 4对此类进行单元测试会抛出java.lang.AbstractMethodError:
import org.junit.Test;
import org.junit.runner.RunWith;
import org.nd4j.linalg.api.ndarray.INDArray;
import org.nd4j.linalg.factory.Nd4j;
import static org.junit.Assert.*;
public class ND4JTest {
@Test
public void print() {
ND4J actualObject = new ND4J(Nd4j.zeros(2,2).add(4.0));
//above statement throws this error
//java.lang.AbstractMethodError: org.nd4j.linalg.factory.Nd4jBackend.getConfigurationResource()Lorg/springframework/core/io/Resource;
INDArray testObject = Nd4j.create(new double[][]{{4.0,4.0}, {4.0,4.0}});
assertEquals(testObject, actualObject.getMatrix());
}
}
实际上,使用ND4J且从main()运行良好的更复杂的类在测试中也存在类似的问题。我的pom文件具有以下ND4J依赖项:javacpp,nd4j-jblas,nd4j-native-platform,nd4j-native。
谢谢
最佳答案
我没有按照ND4J get started页上提到的先决条件进行操作,而是通过遵循此处的指示进行操作:https://github.com/deeplearning4j/dl4j-examples/blob/master/standalone-sample-project/pom.xml