Questions tagged [keras]

Keras is a neural network library providing a high-level API in Python and R. Use this tag for questions relating to how to use this API. Please also include the tag for the language/backend ([python], [r], [tensorflow], [theano], [cntk]) that you are using. If you are using tensorflow's built-in keras, use the [tf.keras] tag.

Keras is a high-level deep learning API, written in , similar in spirit to and . It is developed with a focus on enabling fast experimentation and now solely uses as backend. Additionally, it also has a interface.

Having a simple API with less capabilities, Keras is often seen as a good place to start experimenting with deep learning. For beginners, the Sequential API is easy to learn. For intermediate users, the Functional API has more capabilities and flexibility, but it comes at the cost of simplicity. For expert users, the Subclassing API enable ultimate capabilities, that should only be used in experimental settings.

Starting from TensorFlow 1.8 versions, Keras is also integrated in the TensorFlow framework. The creator of Keras, Francois Chollet, recommends that Keras should to be used from inside TensorFlow, as of TensorFlow version 2.0, since the latter package is much better maintained and will be updated in the future/less prone to errors as compared to the plain Keras library.

References:

42247 questions
87
votes
2 answers

How does Keras handle multilabel classification?

I am unsure how to interpret the default behavior of Keras in the following situation: My Y (ground truth) was set up using scikit-learn's MultilabelBinarizer(). Therefore, to give a random example, one row of my y column is one-hot encoded as…
user798719
  • 9,619
  • 25
  • 84
  • 123
86
votes
4 answers

In Keras, what exactly am I configuring when I create a stateful `LSTM` layer with N `units`?

The first arguments in a normal Dense layer is also units, and is the number of neurons/nodes in that layer. A standard LSTM unit however looks like the following: (This is a reworked version of "Understanding LSTM Networks") In Keras, when I…
André C. Andersen
  • 8,955
  • 3
  • 53
  • 79
86
votes
8 answers

How to save final model using keras?

I use KerasClassifier to train the classifier. The code is below: import numpy from pandas import read_csv from keras.models import Sequential from keras.layers import Dense from keras.wrappers.scikit_learn import KerasClassifier from keras.utils…
yensheng
  • 1,315
  • 2
  • 14
  • 22
86
votes
9 answers

Get class labels from Keras functional model

I have a functional model in Keras (Resnet50 from repo examples). I trained it with ImageDataGenerator and flow_from_directory data and saved model to .h5 file. When I call model.predict I get an array of class probabilities. But I want to associate…
Ledzz
  • 1,266
  • 1
  • 12
  • 14
84
votes
12 answers

How can I use a pre-trained neural network with grayscale images?

I have a dataset containing grayscale images and I want to train a state-of-the-art CNN on them. I'd very much like to fine-tune a pre-trained model (like the ones here). The problem is that almost all models I can find the weights for have been…
Jcart
  • 950
  • 1
  • 7
  • 6
84
votes
6 answers

Keras - Plot training, validation and test set accuracy

I want to plot the output of this simple neural network: model.compile(loss='binary_crossentropy', optimizer='adam', metrics=['accuracy']) history = model.fit(x_test, y_test, nb_epoch=10, validation_split=0.2,…
Simone
  • 4,800
  • 12
  • 30
  • 46
82
votes
4 answers

How to add and remove new layers in keras after loading weights?

I am trying to do a transfer learning; for that purpose I want to remove the last two layers of the neural network and add another two layers. This is an example code which also output the same error. from keras.models import Sequential from…
Eka
  • 14,170
  • 38
  • 128
  • 212
81
votes
5 answers

Save and load weights in keras

Im trying to save and load weights from the model i have trained. the code im using to save the model is. TensorBoard(log_dir='/output') model.fit_generator(image_a_b_gen(batch_size), steps_per_epoch=1,…
Ryan
  • 8,459
  • 14
  • 40
  • 66
79
votes
12 answers

NotImplementedError: Cannot convert a symbolic Tensor (2nd_target:0) to a numpy array

I try to pass 2 loss functions to a model as Keras allows that. loss: String (name of objective function) or objective function or Loss instance. See losses. If the model has multiple outputs, you can use a different loss on each output by passing…
T D Nguyen
  • 7,054
  • 4
  • 51
  • 71
79
votes
30 answers

Failed to get convolution algorithm. This is probably because cuDNN failed to initialize,

In Tensorflow/ Keras when running the code from https://github.com/pierluigiferrari/ssd_keras, use the estimator: ssd300_evaluation. I received this error. Failed to get convolution algorithm. This is probably because cuDNN failed to initialize,…
Steve-0 Dev.
  • 1,008
  • 1
  • 7
  • 11
79
votes
9 answers

How to import keras from tf.keras in Tensorflow?

import tensorflow as tf import tensorflow from tensorflow import keras from keras.layers import Dense I am getting the below error from keras.layers import Input, Dense Traceback (most recent call last): File "",…
GeorgeOfTheRF
  • 8,244
  • 23
  • 57
  • 80
77
votes
2 answers

How does keras handle multiple losses?

If I have something like: model = Model(inputs = input, outputs = [y1,y2]) l1 = 0.5 l2 = 0.3 model.compile(loss = [loss1,loss2], loss_weights = [l1,l2], ...) what does Keras do with the losses to obtain the final loss? Is it something…
jfga
  • 803
  • 1
  • 8
  • 8
77
votes
15 answers

ImportError('Could not import PIL.Image. ' working with keras-ternsorflow

I'm following some lectures from lynda.com about deep learning using Keras-TensorFlow in a PyCharmCE enviroment and they didn't have this problem. I get this error: raise ImportError('Could not import PIL.Image. ' ImportError: Could not import…
Rogelio Em
  • 779
  • 1
  • 5
  • 4
77
votes
23 answers

ImportError: Failed to import pydot. You must install pydot and graphviz for `pydotprint` to work

I have seen similar issue but it is not solved either, so I decided to ask. I am trying to visualize my model in keras using from keras.utils import plot_model plot_model(model, to_file='model.png') First, it showed error ImportError: Failed to…
bit_scientist
  • 1,496
  • 2
  • 15
  • 34
77
votes
6 answers

Keras model.summary() result - Understanding the # of Parameters

I have a simple NN model for detecting hand-written digits from a 28x28px image written in python using Keras (Theano backend): model0 = Sequential() #number of epochs to train for nb_epoch = 12 #amount of data each iteration in an epoch…
user3501476
  • 1,095
  • 2
  • 14
  • 26