Simple sequential model example

public class SimpleSequentialMlpImport {

public static String dataLocalPath;

public static void main(String[] args) throws Exception {

	String SIMPLE_MLP = new ClassPathResource("simple_mlp.h5").getFile().getPath();

	System.out.println(SIMPLE_MLP);
	MultiLayerNetwork model = KerasModelImport.importKerasSequentialModelAndWeights(SIMPLE_MLP, false);
	
	INDArray input = Nd4j.create(DataType.FLOAT, 256, 100);
	INDArray output = model.output(input);

	model.fit(input, output);
	
    }

}

Trying to load a tf.keras sequential model using dl4j throws the error below.?!

Exception in thread “main” org.deeplearning4j.exception.DL4JException: Cannot calculate gradient and score with respect to labels: final layer is not an IOutputLayer. Final layer class: class org.deeplearning4j.nn.layers.feedforward.dense.DenseLayer. To calculate gradients and fit a network using backpropagation, the final layer must be an output layer

@hodophile you seem to have an invalid keras model. Could you post your keras code? I almost guarantee you forgot to specify a loss function or something.

You are ending on a dense layer.

from keras.models import Sequential
from keras.layers import Dense
import tensorflow as tf

model = Sequential()
model.add(Dense(units=64, activation=‘relu’, input_dim=100))
model.add(Dense(units=10, activation=‘softmax’))
model.compile(loss=‘categorical_crossentropy’,optimizer=tf.keras.optimizers.experimental.SGD(), metrics=tf.keras.metrics.Accuracy())

model.save(‘simple_mlp.h5’)

@hodophile what version of dl4j and keras are you using? Just to double check.

keras : 2.12.0
dl4j : 1.0.0-M2.1

@hodophile

do this:

package org.example;

import org.deeplearning4j.nn.conf.layers.OutputLayer;
import org.deeplearning4j.nn.modelimport.keras.KerasModelImport;
import org.deeplearning4j.nn.multilayer.MultiLayerNetwork;
import org.deeplearning4j.nn.transferlearning.FineTuneConfiguration;
import org.deeplearning4j.nn.transferlearning.TransferLearning;
import org.nd4j.linalg.activations.impl.ActivationSoftmax;
import org.nd4j.linalg.api.buffer.DataType;
import org.nd4j.linalg.api.ndarray.INDArray;
import org.nd4j.linalg.factory.Nd4j;
import org.nd4j.linalg.learning.config.Adam;

public class TestForumMlp {

    public static void main(String...args) throws Exception {

        String SIMPLE_MLP = "simple_mlp.h5";

        System.out.println(SIMPLE_MLP);
        MultiLayerNetwork model = KerasModelImport.importKerasSequentialModelAndWeights(SIMPLE_MLP, false);
        MultiLayerNetwork transferLearning = new TransferLearning.Builder(model)
                .fineTuneConfiguration(new FineTuneConfiguration.Builder()
                        .updater(new Adam()).build())
                .addLayer(new OutputLayer.Builder()
                        .nIn(10)
                        .nOut(10)

                        .activation(new ActivationSoftmax())
                        .build())
                .build();


        INDArray input = Nd4j.create(DataType.FLOAT, 256, 100);
        INDArray output = model.output(input);

        transferLearning.fit(input, output);


    }



}

Remove the extra output layer and you should be good to go. 99% of the time model import is used for inference then you add your updaters and other training information on top of that.

1 Like

@hodophile sorry this is not very intuitive I’ll work on fixing that. Thanks for the test case.

1 Like