No module named 'tensorflow.python.keras.engine.base_layer_v1' in python code with tensor flow keras

11,559

Solution 1

I had similar error while working with gaborNet-CNN. I tired following and it worked in my case.

import numpy as np
from matplotlib import pyplot as plt
from tqdm import tqdm
import keras
from keras import backend as K
from keras import activations, initializers, regularizers, constraints, metrics
from keras.datasets import cifar10
from keras.preprocessing.image import ImageDataGenerator
from keras.models import Sequential, Model
from keras.layers import (Dense, Dropout, Activation, Flatten, Reshape, Layer,
                          BatchNormalization, LocallyConnected2D,
                          ZeroPadding2D, Conv2D, MaxPooling2D, Conv2DTranspose,
                          GaussianNoise, UpSampling2D, Input)
from keras.utils import conv_utils, multi_gpu_model
from keras.layers import Lambda
from keras.engine import Layer, InputSpec
from keras.legacy import interfaces

Solution 2

in my case, I just reinstall keras and it works

Share:
11,559
FaisalAlsalm
Author by

FaisalAlsalm

Updated on June 05, 2022

Comments

  • FaisalAlsalm
    FaisalAlsalm almost 2 years

    hi i'm doing this code in google colab and i have this error No module named 'tensorflow.python.keras.engine.base_layer_v1' in python code with tensor flow keras

    i did use tensorflow.keras instead of keras since i use tensorflow v=2.1.0 and keras v=2.3.0-tf

    i tried both tensorflow  v=2.1.0 and v=2.2.0-rc2
    import tensorflow as tf
    from tensorflow import keras
    from tensorflow.keras import layers
    from tensorflow.keras.models import Sequential, load_model
    from tensorflow.keras.callbacks import EarlyStopping
    from tensorflow.keras.layers import Dense, Embedding, LSTM, SpatialDropout1D
    from sklearn.model_selection import train_test_split
    
    MAX_NB_WORDS=50000
    EMBEDDING_DIM=100
    
    model = tf.keras.Sequential()
    model.add(Embedding(MAX_NB_WORDS, EMBEDDING_DIM, input_length=train.shape[1]))
    model.add(SpatialDropout1D(0.2))
    model.add(LSTM(100, dropout=0.2, recurrent_dropout=0.2))
    model.add(Dense(13, activation='softmax'))
    model.compile(loss='categorical_crossentropy', optimizer='adam', metrics=['accuracy'])
    print(model.summary())
    
    epochs = 5
    batch_size = 64
    history = model.fit(x_train, y_train, epochs=epochs, batch_size=batch_size, validation_split=0.1, callbacks=[EarlyStopping(monitor='val_loss', patience=3, min_delta=0.0001)])
    
    accr = model.evaluate(x_test,y_test)
    print('Test set\n  Loss: {:0.3f}\n  Accuracy: {:0.3f}'.format(accr[0],accr[1]))
    
    • Bob Smith
      Bob Smith about 4 years
      Please share a self-contained notebook that reproduces your problem. Since you're attempting to replace the system TensorFlow and keras versions, your initialization process will be important to diagnosing the problem.
    • Swapnil Masurekar
      Swapnil Masurekar almost 4 years
      Always share the entire Traceback while asking the question