How to import keras from tf.keras in Tensorflow?

前端 未结 8 1476
隐瞒了意图╮
隐瞒了意图╮ 2020-12-12 22:55
import tensorflow as tf
import tensorflow 

from tensorflow import keras
from keras.layers import Dense

I am getting the below error



        
相关标签:
8条回答
  • 2020-12-12 23:07

    To make it simple I will take the two versions of the code in keras and tf.keras. The example here is a simple Neural Network Model with different layers in it.

    In Keras (v2.1.5)

    from keras.models import Sequential
    from keras.layers import Dense
    
    def get_model(n_x, n_h1, n_h2):
        model = Sequential()
        model.add(Dense(n_h1, input_dim=n_x, activation='relu'))
        model.add(Dense(n_h2, activation='relu'))
        model.add(Dropout(0.5))
        model.add(Dense(4, activation='softmax'))
        model.compile(loss='binary_crossentropy', optimizer='adam', metrics=['accuracy'])
        print(model.summary())
        return model
    

    In tf.keras (v1.9)

    import tensorflow as tf
    
    def get_model(n_x, n_h1, n_h2):
        model = tf.keras.Sequential()
        model.add(tf.keras.layers.Dense(n_h1, input_dim=n_x, activation='relu'))
        model.add(tf.keras.layers.Dense(n_h2, activation='relu'))
        model.add(tf.keras.layers.Dropout(0.5))
        model.add(tf.keras.layers.Dense(4, activation='softmax'))
        model.compile(loss='categorical_crossentropy', optimizer='adam', metrics=['accuracy'])
        print(model.summary())
    
        return model
    

    or it can be imported the following way instead of the above-mentioned way

    from tensorflow.keras.layers import Dense
    

    The official documentation of tf.keras

    Note: TensorFlow Version is 1.9

    0 讨论(0)
  • 2020-12-12 23:08

    I had the same problem with Tensorflow 2.0.0 in PyCharm. PyCharm did not recognize tensorflow.keras; I updated my PyCharm and the problem was resolved!

    0 讨论(0)
  • 2020-12-12 23:13

    Use the keras module from tensorflow like this:

    import tensorflow as tf

    Import classes

    from tensorflow.python.keras.layers import Input, Dense

    or use directly

    dense = tf.keras.layers.Dense(...)

    EDIT Tensorflow 2

    from tensorflow.keras.layers import Input, Dense

    and the rest stays the same.

    0 讨论(0)
  • 2020-12-12 23:14

    Update for everybody coming to check why tensorflow.keras is not visible in PyCharm.

    Starting from TensorFlow 2.0, only PyCharm versions > 2019.3 are able to recognise tensorflow and keras inside tensorflow (tensorflow.keras) properly.

    Also, it is recommended(by Francois Chollet) that everybody switches to tensorflow.keras in place of plain keras.

    0 讨论(0)
  • 2020-12-12 23:15

    Try from tensorflow.python import keras

    with this, you can easily change keras dependent code to tensorflow in one line change.

    You can also try from tensorflow.contrib import keras. This works on tensorflow 1.3

    Edited: for tensorflow 1.10 and above you can use import tensorflow.keras as keras to get keras in tensorflow.

    0 讨论(0)
  • 2020-12-12 23:17

    this worked for me in tensorflow==1.4.0

    from tensorflow.python import keras

    0 讨论(0)
提交回复
热议问题