简体   繁体   中英

Error when doing "import tensorflow.keras.utils.np_utils"

I have two question:

(1) The question about importing some subpackages inside tensorflow.keras.

(2) How to differentiate between the packages installed by 'pip install' and 'conda install'.(under windows)

I am using anaconda with tensorflow 2.0.0. I am trying to import package like:

import tensorflow.keras.utils.np_utils

However, the error shown that:

---------------------------------------------------------------------------

> ModuleNotFoundError                       Traceback (most recent call
> last) <ipython-input-2-ee1bc59a14ab> in <module>
> ----> 1 import tensorflow.keras.utils.np_utils
> 
> ModuleNotFoundError: No module named 'tensorflow.keras.utils.np_utils'

I am confused about why this is happening, I install the tensorflow with command:

conda install tensorflow==2.0.0 

from Anaconda prompt.

Yes, I know the anaconda should have already had all the data science package inside it, the reason that I uninstall tensorflow provided by anaconda and reinstall it was before using anaconda, I had Python installed on my PC, since anaconda came with another Python(if I can think in this way :), I just got confused about the differences between packages installed by 'pip install' command and 'conda install' command, so I kept trying uninstalling and reinstalling them... Just want to see in which case I can use them in jupyter notebook and in which case I can use them in Pycharm.

Here is some examples of the things I am confused:

  1. If I install a package P with 'pip' command, in the command
    line(not anaconda prompt), when I open jupyter notebook from the
    virtual environment that I created in anaconda, can I import this
    P ?

  2. If have installed tensorflow two times, one time from command line
    and one time from anaconda prompt, when I use the virtual environment of anaconda and import tensorflow, which one is it actually calling?

  3. Can I use 'pip install' command in anaconda prompt? What is the
    difference between using 'pip' in anaconda prompt and windows command line?

If possible, hope to get sulution to 1) and maybe a simple explanation to question (2), sorry about posting too many, but I am just too confused.

Thanks a lot :)

Using:

from tensorflow.keras import utils

Then try to replace:

 np_utils.to_categorical

With:

 utils.to_categorical

Sorry if this is very delayed hope you are fine!

Tensorflow 2.0

from tensorflow.keras.utils import to_categorical

Keras is now part of tensorflow 2.0, in past you will have to install with pip like other packages

from tensorflow.keras import utils
utils.to_categorical

This is working for me

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM