简体   繁体   中英

Cannot predict single instance in Keras using a loop

I'm playing a little with deep learning and Keras has been my choice due to its simplicity.

I've built a simple multilayer perceptron model for binary classification and fitted it on input data (the same that I'm using for other ML models and which are working ok).

The Following picture displays the Model summary:

在此处输入图片说明

The first dense layer was defined as such:

model.add(Dense(18, input_dim=len(X_encoded.columns), activation = "relu", kernel_initializer="uniform"))

When I attempt to predict over a loop like so:

for vals in X_encoded.values:
    print("Survives?", model.predict([vals], batch_size=1))

I get the following error:

ValueError: Error when checking input: expected dense_90_input to have shape (35,) but got array with shape (1,)

These are my variable sizes:

print("Shape of vals:", vals.shape, "Number of Columns and First Layer Dimension:", len(X_encoded.columns))

Result:

Shape of vals: (35,) Number of Columns and First Layer Dimension: 35

As you can see, these match in size which is the expected input.

What is going on? When I pass the entire dataframe "predict" it works correctly, but not when I pass a single value...

You need an array, not a list. You only use a list for multiple input tensors.

model.predict(np.array([vals]), batch_size=1)    

But why not:

model.predict(X_encoded.values, batch_size=1)    

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM