1
$\begingroup$

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?

$\endgroup$
3
  • $\begingroup$ I have the same problem. I hope someone can help us on that. $\endgroup$
    – PeterBe
    Dec 14 '20 at 17:59
  • $\begingroup$ 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?" $\endgroup$
    – PeterBe
    Dec 15 '20 at 15:02
  • $\begingroup$ 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. $\endgroup$
    – Angel
    Dec 20 '20 at 21:09
0
$\begingroup$

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.

$\endgroup$
0
$\begingroup$

You can replace the third line with the following import

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

Hope this helps.

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.