我正在研究 Neuroph ANN 框架。在训练神经网络之后,使用以下方法测试新输入(该库是开源的,因此我能够引用我导入的库内的实际实现 1 ):
/**
* Sets network input. Input is an array of double values.
*
* @param inputVector network input as double array
*/
public void setInput(double... inputVector) throws VectorSizeMismatchException {
if (inputVector.length != inputNeurons.size()) {
throw new VectorSizeMismatchException("Input vector size does not match network input dimension!");
}
int i = 0;
for (Neuron neuron : this.inputNeurons) {
neuron.setInput(inputVector[i]); // set input to the coresponding neuron
i++;
}
}
可悲的是,我在通过参数时遇到了很大的困难。首先,我将成功传递以逗号分隔的每个 Double 值。但后来我的输入大小增加到 400。我尝试将输入作为双 vector 和双数组(盲射)传递,但我收到错误,表明该方法无法使用此类参数解析。
这是我这样做的代码片段:
public String predict(String features)
{
// load the saved network
NeuralNetwork neuralNetwork = NeuralNetwork.createFromFile(nnet_path);
// set network input
String[] feature_pieces = features.split(" ");
System.out.println("\nINPUT FEATURES: " + Arrays.toString(feature_pieces));
Double[] feature_pieces_double = new Double[feature_pieces.length];
for (int i = 0; i < feature_pieces.length; i++) {
feature_pieces_double[i] = Double.parseDouble(feature_pieces[i]);
}
Vector<Double> features_vector = new Vector<>(Arrays.asList(feature_pieces_double));
//neuralNetwork.setInput(feature_pieces_double); //TODO
neuralNetwork.setInput(features_vector); //TODO
.
.
.
请帮我看看我做错了什么。
仅供引用,here is the javadoc提到此方法的部分。 (奇怪的是,javadoc 没有提及任何有关以 Array 作为输入的重载方法的信息)
P.s.有足够声誉的人,请添加更多有用的标签
最佳答案
你不需要 vector :
double[] feature_pieces_double = new double[feature_pieces.length];
for (int i = 0; i < feature_pieces.length; i++) {
feature_pieces_double[i] = Double.parseDouble(feature_pieces[i]);
}
neuralNetwork.setInput(feature_pieces_double);
但请确保使用 double
而不是 Double
。
以下方法中的args
是一个数组:
public void method(double... args)
所以这相当于
public void method(double[] args)
(除了 ...
之外,您还可以在方法调用中直接指定用逗号分隔的参数:method(1.0, 2.0)
).
关于java - 传递 Double Vector 或 Array 作为 Neuroph.set Input() 参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33453966/