No module named 'tensorflow.models' 的问题解决方法

最近在学习tensorflow的教程,跑cifar的例子的时候,有一句

from tensorflow.models.image.cifar10 import cifar10

跑程序的时候报错No module named 'tensorflow.models' 。之前安装的时候我是用Anaconda安装的,安装的时候没有下载models文件。

对于这个问题,可以在github上下载对应的文件。步骤如下:

1. 下载models

git clone https://github.com/tensorflow/models
Cloning into 'models'...
remote: Counting objects: 21199, done.
remote: Compressing objects: 100% (15/15), done.
remote: Total 21199 (delta 1), reused 4 (delta 0), pack-reused 21184
Receiving objects: 100% (21199/21199), 558.32 MiB | 1.15 MiB/s, done.
Resolving deltas: 100% (12433/12433), done.
Checking connectivity... done.
Checking out files: 100% (2669/2669), done.

2. 复制到tensorflow的文件夹下

如果不知道tensorflow的文件夹在哪里,可以通过以下方式查询

rin@ubuntu:~$ python
Python 3.5.2 (default, Nov 23 2017, 16:37:01) 
[GCC 5.4.0 20160609] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import tensorflow as tf
>>> tf.__path__
['/usr/local/lib/python3.5/dist-packages/tensorflow']

注意path前后各有两条下划线.

接着利用cp指令赋值。因为涉及到usr文件夹,所以要用管理员权限。需要注意的是models文件夹下面还有文件夹,需要加上-r参数递归赋值

rin@ubuntu:~$ sudo cp -r /home/rin/models /usr/local/lib/python3.5/dist-packages/tensorflow/

3. 修改路径

将原来的from tensorflow.models.image.cifar10 import cifar10,根据实际文件夹的分布改成

from tensorflow.models.tutorials.image.cifar10 import cifar10

 

  • 3
    点赞
  • 14
    收藏
    觉得还不错? 一键收藏
  • 5
    评论
The error message you're encountering indicates that the TensorFlow Keras API cannot be found when trying to import the `Model` class from the `tensorflow.keras.models` module. This issue might occur due to several reasons: 1. **TensorFlow installation**: Ensure that TensorFlow is installed and properly imported. You can check if TensorFlow is installed by running `import tensorflow as tf` in your Python environment, and then `tf.__version__` to verify the version. 2. **Keras package**: TensorFlow 2.x includes the Keras API as an integral part, so if you have a newer version of TensorFlow installed, you might not need to separately install Keras. Make sure you are using TensorFlow >= 2.x. 3. **Environment isolation**: If you're working with multiple Python environments, ensure that the correct one has both TensorFlow and Keras installed. You can use `conda` or `virtualenv` to manage separate environments. 4. **Imports and paths**: Check if the `keras` package is being imported correctly. It's common to use `from tensorflow import keras` instead of the full path. 5. **Incorrect or outdated imports**: If you've recently updated your code, ensure that all import statements are up-to-date. To resolve this issue, try these steps: - Uninstall and reinstall TensorFlow. - Update your TensorFlow installation to the latest version. - Adjust your imports accordingly (e.g., `from tensorflow import keras`). - Restart your Python interpreter or kernel. If the problem persists, please share the exact version numbers of TensorFlow and Keras you're using.
评论 5
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值