在 deeplearning4j 库的回归模型中使用什么数据结构进行预测?

What data structure is used for predictions in the regression model of the deeplearning4j library?

在 Python 中,我配置、训练并写入了一个具有以下架构的神经网络文件:

model_fully_connected = Sequential()
model_fully_connected.add(keras.layers.Dense(17, activation='tanh', input_shape=(x_train.shape[1],), W_regularizer=l2(l2_lambda)))
model_fully_connected.add(keras.layers.Dense(17, activation='tanh', W_regularizer=l2(l2_lambda)))
model_fully_connected.add(keras.layers.LeakyReLU (alpha=0.1))
model_fully_connected.add(keras.layers.Dense(17, activation='tanh', W_regularizer=l2(l2_lambda)))
model_fully_connected.add(keras.layers.LeakyReLU (alpha=0.1))
model_fully_connected.add(keras.layers.Dense(17, activation='tanh', W_regularizer=l2(l2_lambda)))
model_fully_connected.add(keras.layers.Dense(1))
model_fully_connected.compile(optimizer='adam', loss='mse', metrics=["mae", "mse"])

model_fully_connected.save("trained _neural_network.H5",True,True)
save_model=load_model("trained _neural_network.H5")

输入的数量是 17。对于预测,我使用了维度为 17 的 DataSet

x=save_model.predict(test)

我在 Java 中导入了这个模型:

modelMultiLayer=kerasModelImport.importKerasSequentialModelAndWeights(simpleMlp);

然后,为了进行预测,我创建了一个包含 17 个因子的 INDArray 数组,并尝试将其发送到导入的模型:

int inputs = 17;
INDArray features = Nd4j.zeros(inputs);
for (int i=0; i<inputs; i++){
features.putScalar(new int [] {i},parametrs[i]);}
forecast=modelMultiLayer.output(features).getDouble(0);

但是当我 运行 它时,它抛出一个异常,表明输入网络需要一个 2 阶矩阵,但接收到一个维度为 17 的 1 阶数组:

org.deeplearning4j.exception.DL4JInvalidInputException: Input that is
not a matrix; expected matrix (rank 2), got rank 1 array with shape
[17]. Missing preprocessor or wrong input type? (layer name: dense_6,
layer index: 0, layer type: DenseLayer)
org.deeplearning4j.nn.layers.BaseLayer.preOutputWithPreNorm(BaseLayer.java:308)
    org.deeplearning4j.nn.layers.BaseLayer.preOutput(BaseLayer.java:291)
    org.deeplearning4j.nn.layers.BaseLayer.activate(BaseLayer.java:339)
    org.deeplearning4j.nn.layers.AbstractLayer.activate(AbstractLayer.java:258)
    org.deeplearning4j.nn.multilayer.MultiLayerNetwork.outputOfLayerDetached(MultiLayerNetwork.java:1303)
    org.deeplearning4j.nn.multilayer.MultiLayerNetwork.output(MultiLayerNetwork.java:2415)
    org.deeplearning4j.nn.multilayer.MultiLayerNetwork.output(MultiLayerNetwork.java:2378)
    org.deeplearning4j.nn.multilayer.MultiLayerNetwork.output(MultiLayerNetwork.java:2369)
    org.deeplearning4j.nn.multilayer.MultiLayerNetwork.output(MultiLayerNetwork.java:2356)
    org.deeplearning4j.nn.multilayer.MultiLayerNetwork.output(MultiLayerNetwork.java:2452)
    service.NeuralNetwork.getForecast(NeuralNetwork.java:68)

为什么它需要一个 2 阶矩阵?毕竟我用的是17个参数的vector

那么输入数据集应该是什么样的?

非常简单:DL4J 总是希望获得一批数据,即使您只想在推理过程中传入一个示例。所以你的输入应该有形状 [1, 17].

此外,您使用的可能是我见过的将某些内容放入 INDArray 中的最复杂的方法。您可以这样创建一个合适的数组:

INDArray features = Nd4j.create(parameters, 1, 17);