How to import keras from tf.keras in Tensorflow?

前端 未结 8 1477
隐瞒了意图╮
隐瞒了意图╮ 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:22

    I have a similar problem importing those libs. I am using Anaconda Navigator 1.8.2 with Spyder 3.2.8.

    My code is the following:

    import matplotlib.pyplot as plt
    import tensorflow as tf
    import numpy as np
    import math
    
    #from tf.keras.models import Sequential  # This does not work!
    from tensorflow.python.keras.models import Sequential
    from tensorflow.python.keras.layers import InputLayer, Input
    from tensorflow.python.keras.layers import Reshape, MaxPooling2D
    from tensorflow.python.keras.layers import Conv2D, Dense, Flatten
    

    I get the following error:

    from tensorflow.python.keras.models import Sequential
    
    ModuleNotFoundError: No module named 'tensorflow.python.keras'
    

    I solve this erasing tensorflow.python

    With this code I solve the error:

    import matplotlib.pyplot as plt
    import tensorflow as tf
    import numpy as np
    import math
    
    #from tf.keras.models import Sequential  # This does not work!
    from keras.models import Sequential
    from keras.layers import InputLayer, Input
    from keras.layers import Reshape, MaxPooling2D
    from keras.layers import Conv2D, Dense, Flatten
    
    0 讨论(0)
  • 2020-12-12 23:30

    Its not quite fine to downgrade everytime, you may need to make following changes as shown below:

    Tensorflow

    import tensorflow as tf

    #Keras
    from tensorflow.keras.models import Sequential, Model, load_model, save_model
    from tensorflow.keras.callbacks import ModelCheckpoint
    from tensorflow.keras.layers import Dense, Activation, Dropout, Input, Masking, TimeDistributed, LSTM, Conv1D, Embedding
    from tensorflow.keras.layers import GRU, Bidirectional, BatchNormalization, Reshape
    from tensorflow.keras.optimizers import Adam
    
    from tensorflow.keras.layers import Reshape, Dropout, Dense,Multiply, Dot, Concatenate,Embedding
    from tensorflow.keras import optimizers
    from tensorflow.keras.callbacks import ModelCheckpoint
    

    The point is that instead of using

    from keras.layers import Reshape, Dropout, Dense,Multiply, Dot, Concatenate,Embedding
    

    you need to add

    from tensorflow.keras.layers import Reshape, Dropout, Dense,Multiply, Dot, Concatenate,Embedding
    
    0 讨论(0)
提交回复
热议问题