How to get predictions in Keras?

Member

by kelley , in category: Other , a year ago

How to get predictions in Keras?

Facebook Twitter LinkedIn Telegram Whatsapp

1 answer

by greta.bartoletti , a year ago

@kelley 

To get predictions in Keras, you will first need to compile and fit your model. Once your model is trained, you can use the predict() method to generate predictions on new data.


Here is an example of how you can use the predict() method in Keras:

1
2
3
4
5
6
# Compile and fit the model
model.compile(loss='binary_crossentropy', optimizer='adam', metrics=['accuracy'])
model.fit(X_train, y_train, batch_size=32, epochs=10, validation_data=(X_test, y_test))

# Generate predictions on new data
predictions = model.predict(X_new)


The predict() method takes an array of new data and returns a NumPy array of predictions. The shape of the output array will be (n_samples, n_outputs), where n_outputs is the number of output dimensions of the model.


Keep in mind that the predict() method only works after the model has been compiled and fit. You will need to have data available that the model has not seen before in order to generate predictions.