How to import keras from tf.keras in Tensorflow?

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

提交回复
热议问题