1

everyone. I am a Geophysicist who has recently started learning Data Science. As part of this process I am going over some example source code included in the book "Machine Learning for the Oil and Gas Industry". The problem I have is that the source code has the following lines of code:

from tensorflow.keras import layers
from tensorflow.keras.models import Sequential, load_model
from keras.wrappers.scikit_learn import KerasRegressor, KerasClassifier

The first two lines seem to run correctly, however a ModuleNotFoundError is raised for the third line. I use Anaconda individual distribution and installed Tensorflow using Anaconda Navigator in my base environment.

Is there any way to get this to run properly?

Angel
  • 13
  • 4
  • I have the same problem. I hope someone can help us on that. – PeterBe Dec 14 '20 at 17:59
  • From another question I received the following advice which fixed my problem: "Can you try to create a fresh env with conda create -n tf tensorflow=2.1, then do conda activate tf and python -c "import tensorflow;import tensorflow.keras?" – PeterBe Dec 15 '20 at 15:02
  • Hi, Peter, thanks for your answer. It was useful. I think you might find the answer provided by ncasas helpful. I think the advice you were given solved your problem because keras was still a module for that version of tensorflow. – Angel Dec 20 '20 at 21:09

2 Answers2

0

You can replace the third line with the following import

from tensorflow.keras.wrappers.scikit_learn import KerasRegressor, KerasClassifier

Hope this helps.

Himaprasoon
  • 101
  • 2
0

You should import from tensorflow.keras.wrappers.scikit_learn.

The confusion comes because there are 2 different Keras:

  1. At the beginning, Keras was independent from tensorflow. At that time, you had to install Keras separately from tensorflow and its packages were keras.*. This version of Keras still exists but it is no longer maintained, but there are a lot of old blog posts and StackOverflow questions that still contain references to keras.*.

  2. Later, Tensorflow absorbed Keras into its tensorflow.keras.* packages. This is the only currently maintained version of Keras.

If you want to know more about the relation between Keras and Tensorflow I suggest you take a look at this answer.

noe
  • 26,410
  • 1
  • 46
  • 76