Keras Embedding Layer Issue: Incorrect input length, Rank error when calling prediction

I receive the error

"Input headline is an array of rank 2, but this model only supports single vector inputs (rank 1) or a sequence of batches of vectors (rank 3)"

when I call predication.


If I change shape to [20] I get the error [core] Input feature headline was presented as a vector of length 20, but the model expects an input of length 1.


        guard let mlMultiArray = try? MLMultiArray(shape:[1,20],
                                                  dataType:MLMultiArrayDataType.double) else {
            fatalError("Unexpected runtime error. MLMultiArray")
        }

        for (index, element) in data.enumerated() {
            mlMultiArray[index] = NSNumber(floatLiteral: Double(element))
        }
        print(mlMultiArray)

        if let result = try? model.prediction(headline: mlMultiArray) {
            print("result: \(result.output[0])")
        } else {
            print("Failed!")
        }


The input type is MultiArray<Double,1> for the core ml model. The input to the modal using keras and tensor flow is in the format

array([[ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,

0, 0, 0, 0, 0, 0, 387, 2630, 1]], dtype=int32).

So Keras is taking an array of ints of size 20. But core ml expects a MLArray of size one, of type double.


How do I specify type Int for the model, and why is the array count off?


Is this possible, or must I change the model?


Any tips on how to remedy this are appreciated.


EDIT: Here is the model.

def ConvolutionalNet(vocabulary_size, embedding_dimension, input_length, embedding_weights=None):

    model = Sequential()
    if embedding_weights is None:
        model.add(Embedding(vocabulary_size, embedding_dimension, input_length=input_length, trainable=False))
    else:
        model.add(Embedding(vocabulary_size, embedding_dimension, input_length=input_length, weights=[embedding_weights], trainable=False))


    model.add(Convolution1D(32, 2, W_regularizer=l2(0.005)))
    model.add(BatchNormalization())
    model.add(Activation("relu"))


    model.add(Convolution1D(32, 2, W_regularizer=l2(0.001)))
    model.add(BatchNormalization())
    model.add(Activation("relu"))


    model.add(Convolution1D(32, 2, W_regularizer=l2(0.001)))
    model.add(BatchNormalization())
    model.add(Activation("relu"))


    model.add(MaxPooling1D(17))
    model.add(Flatten())


    model.add(Dense(1, bias=True, W_regularizer=l2(0.001)))
    model.add(BatchNormalization())
    model.add(Activation("sigmoid"))


    return model



EDIT 2:

I added model.add(Reshape((20,), input_shape=(20,))) to the top of the model so the core ml model now expects MultiArray<Double,1> but I get the following error:

/mlkitc: compiler error: Embedding layer: embedding_1 must have C,H,W = 1,1,1 input blob

Answered by kurtn2 in 244925022

I ran into the exact same problem that you had yesterday.


Please try making your MLMultiArray as follows (and see if that fixes it)?


guard let mlMultiArray = try? MLMultiArray(shape:[25,1,1],

dataType:MLMultiArrayDataType.double) else {

fatalError("Unexpected runtime error. MLMultiArray")

}


Your original Core ML Model is probably fine (i.e you don't need ot make the change you had in Edit 2)

Accepted Answer

I ran into the exact same problem that you had yesterday.


Please try making your MLMultiArray as follows (and see if that fixes it)?


guard let mlMultiArray = try? MLMultiArray(shape:[25,1,1],

dataType:MLMultiArrayDataType.double) else {

fatalError("Unexpected runtime error. MLMultiArray")

}


Your original Core ML Model is probably fine (i.e you don't need ot make the change you had in Edit 2)

That fixed my issue, thank you!

Keras Embedding Layer Issue: Incorrect input length, Rank error when calling prediction
 
 
Q