4

I am trying to learn tensorflow and use a book to gain some first insights. The code I am currently trying to get to run can also be found here:

https://github.com/chrismattmann/MLwithTensorFlow2ed/blob/tensorflow2/TFv2/ch03/Listing%203.1-3.2.ipynb

I am using Windows 10, python 3.9.7 and tensorflow 2.6.0. I installed all the packages used here with conda and already tried reinstalling tensorflow which did not solve the problem.

Here is the code up to the line that gives me my error.

import tensorflow as tf
import numpy as np
import matplotlib.pyplot as plt

# Define constants(hyperparameters) used by the algorithm

learning_rate = 0.01
training_epochs = 100

# Set up data to run the regression

x_train = np.linspace(-1, 1, 101)
y_train = 2 * x_train + np.random.randn(*x_train.shape) * 0.33

# Set up input and output variables

X = tf.constant(x_train, dtype=tf.float32)
Y = tf.constant(y_train, dtype=tf.float32)
w = tf.Variable(0.0, name="weights", dtype=tf.float32)

# Define the cost function

cost = lambda : tf.square(Y - tf.multiply(X, w))

# Define operaton for each iteration of the learning algorithm

train_op = tf.keras.optimizers.SGD(learning_rate)

In the last line I get the following error

ModuleNotFoundError: No module named 'keras'

I tried installing keras v2.6.0 in addition to the other packages. However, that did not help and as far as I understand tensorflow comes with keras in it's newer versions. So I removed it again.

I assume something has to be wrong with my tensorflow installation but I am confused since I am able to use other parts of the package without problem. I installed tensorflow following the instructions found here:

https://anaconda.org/conda-forge/tensorflow.

I have already looked at a similar thread. However, the links provided in one of the comments were of no help to me:

ModuleNotFoundError: No module named 'keras' when using tensorflow 2.6

Update:

Here is the list of all packages installed in the environment (I used ´conda list´). Currently keras does not show up as a separate package, since I removed it again:

(AI_Learning) C:\Users\Username>conda list
# packages in environment at F:\Programs\Anaconda\envs\AI_Learning:
#
# Name                    Version                   Build  Channel
_r-mutex                  1.0.0               anacondar_1
_tflow_select             2.3.0                       mkl
abseil-cpp                20210324.2           h0e60522_0    conda-forge
absl-py                   1.0.0              pyhd8ed1ab_0    conda-forge
aiohttp                   3.8.1            py39hb82d6ee_0    conda-forge
aiosignal                 1.2.0              pyhd8ed1ab_0    conda-forge
argon2-cffi               20.1.0           py39h2bbff1b_1
astor                     0.8.1              pyh9f0ad1d_0    conda-forge
astunparse                1.6.3              pyhd8ed1ab_0    conda-forge
async-timeout             4.0.1              pyhd8ed1ab_0    conda-forge
async_generator           1.10               pyhd3eb1b0_0
attrs                     21.2.0             pyhd3eb1b0_0
backcall                  0.2.0              pyhd3eb1b0_0
blas                      1.0                         mkl
bleach                    4.0.0              pyhd3eb1b0_0
blinker                   1.4                        py_1    conda-forge
bottleneck                1.3.2            py39h7cc1a96_1
brotli                    1.0.9                ha925a31_2
brotlipy                  0.7.0           py39h2bbff1b_1003
ca-certificates           2021.10.8            h5b45459_0    conda-forge
cached-property           1.5.2                hd8ed1ab_1    conda-forge
cached_property           1.5.2              pyha770c72_1    conda-forge
cachetools                4.2.4              pyhd8ed1ab_0    conda-forge
certifi                   2021.10.8        py39hcbf5309_1    conda-forge
cffi                      1.15.0           py39h2bbff1b_0
charset-normalizer        2.0.4              pyhd3eb1b0_0
click                     8.0.3              pyhd3eb1b0_0
colorama                  0.4.4              pyhd3eb1b0_0
cryptography              35.0.0           py39h71e12ea_0
cvxopt                    1.2.7            py39h73f7814_1    conda-forge
cycler                    0.11.0             pyhd3eb1b0_0
dataclasses               0.8                pyhc8e2a94_3    conda-forge
debugpy                   1.5.1            py39hd77b12b_0
decorator                 5.1.0              pyhd3eb1b0_0
defusedxml                0.7.1              pyhd3eb1b0_0
dsdp                      5.8               h6e01ec9_1203    conda-forge
entrypoints               0.3              py39haa95532_0
fftw                      3.3.10          nompi_h77347bd_102    conda-forge
flatbuffers               2.0.0                h0e60522_0    conda-forge
fonttools                 4.25.0             pyhd3eb1b0_0
freeglut                  3.0.0                h6538335_5
freetype                  2.10.4               hd328e21_0
frozenlist                1.2.0            py39hb82d6ee_1    conda-forge
gast                      0.4.0              pyh9f0ad1d_0    conda-forge
giflib                    5.2.1                h8d14728_2    conda-forge
glpk                      4.65                 hdc00fd2_2
google-auth               1.35.0             pyh6c4a22f_0    conda-forge
google-auth-oauthlib      0.4.6              pyhd8ed1ab_0    conda-forge
google-pasta              0.2.0              pyh8c360ce_0    conda-forge
grpcio                    1.38.1           py39hb76b349_0    conda-forge
gsl                       2.7                  hdfb1a43_0    conda-forge
h5py                      3.6.0           nompi_py39hd4deaf1_100    conda-forge
hdf5                      1.12.1          nompi_h2a0e4a3_100    conda-forge
hmmlearn                  0.2.6            py39h5d4886f_1    conda-forge
icc_rt                    2019.0.0             h0cc432a_1
icu                       68.1                 h6c2663c_0
idna                      3.3                pyhd3eb1b0_0
importlib-metadata        4.8.2            py39haa95532_0
importlib_metadata        4.8.2                hd3eb1b0_0
intel-openmp              2021.4.0          haa95532_3556
ipykernel                 6.4.1            py39haa95532_1
ipython                   7.29.0           py39hd4e2768_0
ipython_genutils          0.2.0              pyhd3eb1b0_1
jasper                    2.0.14               hea7d32e_2
jedi                      0.18.0           py39haa95532_1
jinja2                    3.0.2              pyhd3eb1b0_0
joblib                    1.1.0              pyhd3eb1b0_0
jpeg                      9d                   h2bbff1b_0
jsonschema                3.2.0              pyhd3eb1b0_2
jupyter_client            7.0.6              pyhd3eb1b0_0
jupyter_core              4.9.1            py39haa95532_0
jupyterlab_pygments       0.1.2                      py_0
keras-preprocessing       1.1.2              pyhd8ed1ab_0    conda-forge
kiwisolver                1.3.1            py39hd77b12b_0
krb5                      1.19.2               h20d022d_3    conda-forge
libblas                   3.9.0              12_win64_mkl    conda-forge
libcblas                  3.9.0              12_win64_mkl    conda-forge
libclang                  11.1.0          default_h5c34c98_1    conda-forge
libcurl                   7.79.1               h789b8ee_1    conda-forge
liblapack                 3.9.0              12_win64_mkl    conda-forge
liblapacke                3.9.0              12_win64_mkl    conda-forge
libopencv                 4.5.1            py39h27d8466_0    conda-forge
libpng                    1.6.37               h2a8f88b_0
libprotobuf               3.14.0               h7755175_0    conda-forge
libssh2                   1.10.0               h680486a_2    conda-forge
libtiff                   4.2.0                hd0e1b90_0
libwebp                   1.2.0                h2bbff1b_0
libwebp-base              1.2.0                h2bbff1b_0
lz4-c                     1.9.3                h2bbff1b_1
m2w64-bwidget             1.9.10                        2
m2w64-bzip2               1.0.6                         6
m2w64-expat               2.1.1                         2
m2w64-fftw                3.3.4                         6
m2w64-flac                1.3.1                         3
m2w64-gcc-libgfortran     5.3.0                         6
m2w64-gcc-libs            5.3.0                         7
m2w64-gcc-libs-core       5.3.0                         7
m2w64-gettext             0.19.7                        2
m2w64-gmp                 6.1.0                         2
m2w64-gsl                 2.1                           2
m2w64-libiconv            1.14                          6
m2w64-libjpeg-turbo       1.4.2                         3
m2w64-libogg              1.3.2                         3
m2w64-libpng              1.6.21                        2
m2w64-libsndfile          1.0.26                        2
m2w64-libtiff             4.0.6                         2
m2w64-libvorbis           1.3.5                         2
m2w64-libwinpthread-git   5.0.0.4634.697f757               2
m2w64-libxml2             2.9.3                         4
m2w64-mpfr                3.1.4                         4
m2w64-openblas            0.2.19                        1
m2w64-pcre                8.38                          2
m2w64-speex               1.2rc2                        3
m2w64-speexdsp            1.2rc3                        3
m2w64-tcl                 8.6.5                         3
m2w64-tk                  8.6.5                         3
m2w64-tktable             2.10                          5
m2w64-wineditline         2.101                         5
m2w64-xz                  5.2.2                         2
m2w64-zlib                1.2.8                        10
markdown                  3.3.6              pyhd8ed1ab_0    conda-forge
markupsafe                2.0.1            py39h2bbff1b_0
matplotlib                3.5.0            py39haa95532_0
matplotlib-base           3.5.0            py39h6214cd6_0
matplotlib-inline         0.1.2              pyhd3eb1b0_2
mistune                   0.8.4           py39h2bbff1b_1000
mkl                       2021.4.0           h0e2418a_729    conda-forge
mkl-service               2.4.0            py39h2bbff1b_0
mkl_fft                   1.3.1            py39h277e83a_0
mkl_random                1.2.2            py39hf11a4ad_0
msys2-conda-epoch         20160418                      1
multidict                 5.2.0            py39hb82d6ee_1    conda-forge
munkres                   1.1.4                      py_0
nbclient                  0.5.3              pyhd3eb1b0_0
nbconvert                 6.1.0            py39haa95532_0
nbformat                  5.1.3              pyhd3eb1b0_0
nest-asyncio              1.5.1              pyhd3eb1b0_0
nltk                      3.6.5              pyhd3eb1b0_0
notebook                  6.4.6            py39haa95532_0
numexpr                   2.7.3            py39hb80d3ca_1
numpy                     1.21.2           py39hfca59bb_0
numpy-base                1.21.2           py39h0829f74_0
oauthlib                  3.1.1              pyhd8ed1ab_0    conda-forge
olefile                   0.46               pyhd3eb1b0_0
opencv                    4.5.1            py39hcbf5309_0    conda-forge
openssl                   1.1.1l               h8ffe710_0    conda-forge
opt_einsum                3.3.0              pyhd8ed1ab_1    conda-forge
packaging                 21.3               pyhd3eb1b0_0
pandas                    1.3.4            py39h6214cd6_0
pandocfilters             1.4.3            py39haa95532_1
parso                     0.8.2              pyhd3eb1b0_0
pickleshare               0.7.5           pyhd3eb1b0_1003
pillow                    8.4.0            py39hd45dc43_0
pip                       21.2.4           py39haa95532_0
prometheus_client         0.12.0             pyhd3eb1b0_0
prompt-toolkit            3.0.20             pyhd3eb1b0_0
protobuf                  3.14.0           py39h415ef7b_1    conda-forge
py-opencv                 4.5.1            py39h832f523_0    conda-forge
pyasn1                    0.4.8                      py_0    conda-forge
pyasn1-modules            0.2.7                      py_0    conda-forge
pycparser                 2.21               pyhd3eb1b0_0
pygments                  2.10.0             pyhd3eb1b0_0
pyjwt                     2.3.0              pyhd8ed1ab_1    conda-forge
pyopenssl                 21.0.0             pyhd3eb1b0_1
pyparsing                 3.0.4              pyhd3eb1b0_0
pyqt                      5.12.3           py39hb0d2dfa_4    conda-forge
pyqt5-sip                 4.19.18                  pypi_0    pypi
pyqtchart                 5.12                     pypi_0    pypi
pyqtwebengine             5.12.1                   pypi_0    pypi
pyrsistent                0.18.0           py39h196d8e1_0
pysocks                   1.7.1            py39haa95532_0
python                    3.9.7                h6244533_1
python-dateutil           2.8.2              pyhd3eb1b0_0
python-flatbuffers        1.12               pyhd8ed1ab_1    conda-forge
python_abi                3.9                      2_cp39    conda-forge
pytz                      2021.3             pyhd3eb1b0_0
pyu2f                     0.1.5              pyhd8ed1ab_0    conda-forge
pywin32                   228              py39hbaba5e8_1
pywinpty                  0.5.7            py39haa95532_0
pyzmq                     22.3.0           py39hd77b12b_2
qt                        5.12.9               h5909a2a_4    conda-forge
r-base                    3.5.1                hac0af64_1
r-timedate                3043.102         r351h6f4ce42_0
r-timeseries              3062.100                  r35_0    conda-forge
regex                     2021.8.3         py39h2bbff1b_0
requests                  2.26.0             pyhd3eb1b0_0
requests-oauthlib         1.3.0              pyh9f0ad1d_0    conda-forge
rsa                       4.8                pyhd8ed1ab_0    conda-forge
scikit-learn              1.0.1            py39hf11a4ad_0
scipy                     1.7.1            py39hbe87c03_2
send2trash                1.8.0              pyhd3eb1b0_1
setuptools                58.0.4           py39haa95532_0
six                       1.16.0             pyhd3eb1b0_0
snappy                    1.1.8                ha925a31_3    conda-forge
sqlite                    3.36.0               h2bbff1b_0
tbb                       2021.4.0             h59b6b97_0
tensorboard               2.6.0              pyhd8ed1ab_1    conda-forge
tensorboard-data-server   0.6.0            py39hcbf5309_1    conda-forge
tensorboard-plugin-wit    1.8.0              pyh44b312d_0    conda-forge
tensorflow                2.6.0           mkl_py39h31650da_0
tensorflow-base           2.6.0           mkl_py39h9201259_0
tensorflow-estimator      2.6.0              pyh7b7c402_0
termcolor                 1.1.0                      py_2    conda-forge
terminado                 0.9.4            py39haa95532_0
testpath                  0.5.0              pyhd3eb1b0_0
threadpoolctl             2.2.0              pyh0d69192_0
tika                      1.24               pyh9f0ad1d_0    conda-forge
tk                        8.6.11               h2bbff1b_0
tornado                   6.1              py39h2bbff1b_0
tqdm                      4.62.3             pyhd8ed1ab_0    conda-forge
traitlets                 5.1.1              pyhd3eb1b0_0
typing-extensions         4.0.1                hd8ed1ab_0    conda-forge
typing_extensions         4.0.1              pyha770c72_0    conda-forge
tzdata                    2021e                hda174b7_0
urllib3                   1.26.7             pyhd3eb1b0_0
vc                        14.2                 h21ff451_1
vs2015_runtime            14.27.29016          h5e58377_2
wcwidth                   0.2.5              pyhd3eb1b0_0
webencodings              0.5.1            py39haa95532_1
werkzeug                  2.0.1              pyhd8ed1ab_0    conda-forge
wheel                     0.35.1             pyh9f0ad1d_0    conda-forge
win_inet_pton             1.1.0            py39haa95532_0
wincertstore              0.2              py39haa95532_2
winpty                    0.4.3                         4
wrapt                     1.13.3           py39hb82d6ee_1    conda-forge
xz                        5.2.5                h62dcd97_0
yarl                      1.7.2            py39hb82d6ee_1    conda-forge
zipp                      3.6.0              pyhd3eb1b0_0
zlib                      1.2.11               h62dcd97_4
zstd                      1.4.9                h19a0ad4_0

Thanks in advance for any help!

  • 1
    keras module might be installed in a different env, try `pip install keras` – newt Dec 12 '21 at 14:26
  • Thanks for the advice! However, i already tried installing keras as a separate package using conda and it did not solve the problem. Furthermore, as far as I understand keras is integrated in tensorflow's newer versions (therefore it is called with `tf.keras`). Do I still have to install `keras` as a separate package in this case? As stated in my post, I already installed `keras=2.6.0` with conda which did not help. – Aushilfsgott Dec 12 '21 at 15:09
  • Try again using `!pip install --upgrade keras` in jupyter notebook as I can see there is no `keras` module exists in `conda list`. After that use `from tensorflow import keras` in your above mentioned code and execute the code. Please let me know if still issue persists. –  Dec 20 '21 at 08:33
  • 6
    Thanks for your advice. Before I tried your suggested solution I added `import tensorflow.keras` in my code without changing installing any more packages and now I can use `tf.keras` without any problems. I am actually confused why this works now, but I am not complaining. So apparently installing `keras` as an additional package is not necessary. – Aushilfsgott Dec 21 '21 at 12:13
  • cant believe that this was the solution thanks @Aushilfsgott – t0b4cc0 Jan 24 '22 at 04:01
  • Because now the `keras` high level API is integrated with `TensorFlow` and you can retrieve its functionality by importing it through 'from tensorflow import keras'. You no need to install keras package explicitly in the latest Tensorflow versions. –  Apr 30 '22 at 07:40
  • cant believe that this was the solution, too. @Aushilfsgott – roachsinai Jul 08 '22 at 08:33

1 Answers1

0

I had the same issue using Windows 10, Python 3.8 and Python 3.9. I installed python and Tensorflow-GPU during the creation of a conda env.

conda create --name tf_gpu_py_39 tensorflow-gpu python=3.9

Python 3.8 and Python 3.9 ended in the same issue:

ModuleNotFoundError: No module named 'keras'

Just like @Aushilfgod had expierienced. I looked up the packages using conda list:

...
tensorflow                2.6.0          
tensorflow-base           2.6.0          
tensorflow-estimator      2.6.0          
tensorflow-gpu            2.6.0  
...

everything is there, keras is supposed to be part of tensorflow.

one of the comments of @Aushilfgod said, you should use from tensorflow import keras in addition to import tensorflow as tf followed by the call of tf.keras.... I am quite surprised that this worked, since it should not change anything. But I am happy if it worked for somebody, it surely didn't work for me.

I couldn't make Tensorflow-GPU 2.6.0 running. Only a downgrade worked for me, even if this is not the most satisfying approch. After executing conda install -c anaconda tensorflow-gpu=2.5 everything was "updated" accordingly, see conda list

tensorflow                2.5.0  
tensorflow-base           2.5.0  
tensorflow-estimator      2.5.0  
tensorflow-gpu            2.5.0 

And the issue was gone. I hope this helps, even if it is not a perfect solution, more like a workaround

user2267367
  • 704
  • 7
  • 19