ModuleNotFoundError: No module named 'keras.layers.core'

keras_model.py", line 6, in
from keras.layers.core import Dense, Activation, Dropout, Reshape, Permute
ModuleNotFoundError: No module named ‘keras.layers.core’

// Import “keras.layers.core” could not be resolved
from keras.layers.core import Dense, Activation, Dropout, Reshape, Permute

// Import “keras.layers.recurrent” could not be resolved
from keras.layers.recurrent import GRU

// Import “keras.layers.normalization” could not be resolved
from keras.layers.normalization import BatchNormalization

// Import “keras.layers.wrappers” could not be resolved
from keras.layers.wrappers import TimeDistributed

Edit #1:
python -c “import keras; print(keras.version)”
2024-01-25 12:35:45.373322: I tensorflow/core/util/port.cc:113] oneDNN custom operations are on. You may see slightly different numerical results due to floating-point round-off errors from different computation orders. To turn them off, set the environment variable TF_ENABLE_ONEDNN_OPTS=0.
C:\Program Files\Python310\lib\site-packages\scipy_init_.py:146: UserWarning: A NumPy version >=1.16.5 and <1.23.0 is required for this version of SciPy (detected version 1.26.3
warnings.warn(f"A NumPy version >={np_minversion} and <{np_maxversion}"
WARNING:tensorflow:From C:\Program Files\Python310\lib\site-packages\keras\src\losses.py:2976: The name tf.losses.sparse_softmax_cross_entropy is deprecated. Please use tf.compat.v1.losses.sparse_softmax_cross_entropy instead.

2.15.0

Hi @SriKavipriyan12, You can import those layers directly from keras.layers

import keras
from keras.layers import Dense, Activation, Dropout, Reshape, Permute

Thank You.

The error you’re encountering, ModuleNotFoundError: No module named ‘keras.layers.core’, is likely due to changes in the Keras API in recent versions. Since you’re using Keras version 2.15.0, some of the modules you’re trying to import have been restructured or renamed. Here’s how you can resolve the import issues:

  1. For Core Layers:
  • Instead of from keras.layers.core import ..., you can directly import layers from keras.layers. For example, use from keras.layers import Dense, Activation, Dropout etc.
  1. For Recurrent Layers:
  • Similarly, for recurrent layers like GRU, use from keras.layers import GRU.
  1. For Batch Normalization:
  • Batch normalization can be imported directly from keras.layers. Use from keras.layers import BatchNormalization.
  1. For TimeDistributed Wrapper:
  • The TimeDistributed wrapper is also directly available under keras.layers. Use from keras.layers import TimeDistributed.

Here’s how your imports should look:

pythonCopy code

from keras.layers import Dense, Activation, Dropout, Reshape, Permute, GRU, BatchNormalization, TimeDistributed

Regarding the warning about TensorFlow and NumPy versions, make sure that the versions of TensorFlow, Keras, and NumPy are compatible. TensorFlow 2.15.0 might have specific requirements for the NumPy version. If you’re encountering issues or warnings, consider installing a version of NumPy that is compatible with both TensorFlow 2.15.0 and SciPy versions you’re using. You can refer to the TensorFlow and SciPy documentation for the recommended versions.