keras - taoualiw/My-Knowledge-Base GitHub Wiki

Keras

  • Keras provides a simple, module neural network library that can flexibly use various other machine learning frameworks as its backend.
  • One note about Keras is it doesn’t support multi-GPU environments by default for training a network.
  • The state-of-the-art pre-trained networks included in the Keras core library represent some of the highest performing Convolutional Neural Networks on the ImageNet challenge over the past few years.

The Keras Sequential model API.

  • allows to create models layer-by-layer for most problems.
  • It is limited in that it does not allow you to create models that share layers or have multiple inputs or outputs.
  • The layers can be defined and passed to the Sequential as an array:
    from keras.models import Sequential
    from keras.layers import Dense
    model = Sequential([Dense(2, input_dim=1), Dense(1)])
  • Layers can also be added piecewise:
    from keras.models import Sequential
    from keras.layers import Dense
    model = Sequential()
    model.add(Dense(2, input_dim=1))
    model.add(Dense(1))

The Keras Functional model API

  • is the way to go for defining complex models, such as multi-output models, directed acyclic graphs, or models with shared layers.
  • Unlike the Sequential model, you must create and define a standalone Input layer that specifies the shape of input data. The input layer takes a shape argument that is a tuple that indicates the dimensionality of the input data.
  • When input data is one-dimensional, the shape tuple is always defined with a hanging last dimension when the input is one-dimensional (2,)
    from keras.layers import Input
    visible = Input(shape=(2,))
  • The layers in the model are connected pairwise.
    from keras.layers import Input
    from keras.layers import Dense
    visible = Input(shape=(2,))
    hidden = Dense(2)(visible)
  • It is this way of connecting layers piece by piece that gives the functional API its flexibility.
  • After creating all of your model layers and connecting them together, you must define the model.
    from keras.models import Model
    from keras.layers import Input
    from keras.layers import Dense
    visible = Input(shape=(2,))
    hidden = Dense(2)(visible)
    model = Model(inputs=visible, outputs=hidden)

Warning:

tf.keras (formerly tf.contrib.keras) is an implementation of keras 2 implemented exclusively with/for tensorflow. It is hosted on the tensorflow repo and has a distinct code base than the official repo (the last commit there in the tf-keras branch dates back from May 2017).

As a rule of thumb, if your code use any tensorflow-specific code, say anything in tf.data.* for providing inputs or tf.summary.* for visualization in tensorboard, it is simpler to just use tf.keras. (Some may even recommend not using the reference Keras implementation with TF because of occasional problems it has with this toolkit).

On the other hand, if you plan to actively maintain a framework-agnostic code, using keras' own package is your only choice.

If you don't care much about being framework-agnostic but don't use tensorflow-specific code, I would probably advise to go with tf.keras and start using tensorflow-specific code, esp. tf.data which is a game-changer in my opinion.

Errors

Macos specific problem:

  • jupyter kernel dies/restarts while training without error message
  • tried running code in py file gives error :Initializing libiomp5.dylib, but found libiomp5.dylib already initialized.
  • ugly solution : add to the code
    import os
    os.environ['KMP_DUPLICATE_LIB_OK']='True'
  • The best thing to do is to ensure that only a single OpenMP runtime is linked into the process, e.g. by avoiding static linking of the OpenMP runtime in any library :
    • by
      • pip uninstall mxnet-mkl
      • pip install mxnet
    • or by:
      • conda uninstall mkl
      • conda uninstall numpy
      • pip install numpy

References:

⚠️ **GitHub.com Fallback** ⚠️