旧版吴恩达深度学习环境搭建(anaconda+tensorflow+jupyter notebook)(呕心沥血诚意之作)

        本人在参考多位博主的文章后,多次尝试才成功配置了tensorflow1.2.1的环境(课程建议Python3.6+tensorflow1.2.1+Keras2.0.7)。在此之前,曾经尝试用tensorflow2.x降级的方法(import tensorflow.compat.v1 as tf),运行课后作业,但是存在诸多问题,不得不弃用2.x版本,进而构建tensorflow1.2.1的环境。如果要使用tensorflow2.x的环境,可以配套使用新版的吴恩达教授深度学习课程。(本人用的旧版课程)。

        本文内容为个人见解,亲自实践得来。如果读者有不同见解,欢迎交流指正。

目录

1.anaconda下载

2.tensorflow环境配置

2.1 创建虚拟环境

2.2 安装tensorflow

2.3 安装keras

3.安装和配置jupyter

3.1 使用命令行安装

3.2 使用anaconda navigator安装

4.安装需要使用的库

5. 附录:记录一些可能用到的指令


主要参考文章:

1.吴恩达课程编程环境配置windows

2. 吴恩达2022版深度学习第二部分Annaconda+Tensorflow环境搭建

3. 在anaconda环境下安装旧版本TensorFlow1.8

1.anaconda下载

        本人使用anaconda的版本Anaconda3-2023.03-1-Windows-x86_64(当时官网下载的最新版本),在anaconda.org可以免费下载(free download)。安装过程和一些环境变量的配置大同小异,可以参考其他博客,这里就不进行赘述。

2.tensorflow环境配置

参考文章:吴恩达课程编程环境配置windows

作业中使用到了TensorFlow与Keras,相应的版本为
   python:3.6.0
   tensorflow:1.2.1
   keras:2.0.7

2.1 创建虚拟环境

在搜索栏中搜索“Anaconda Prompt”并运行

 打开 Anaconda Prompt 之后,输入命令:

创建虚拟环境,命名为dltf,编译器为python3.6

conda create --name dltf python=3.6.0

激活环境(从base环境切换到dltf环境)

activate dltf

 

2.2 安装tensorflow

参考文章:吴恩达2022版深度学习第二部分Annaconda+Tensorflow环境搭建

安装tensorflow1.2.1,输入pip install tensorflow==1.2.1(-i后面是镜像源的名字,这里用到是清华的)

pip install tensorflow==1.2.1 -i https://pypi.tuna.tsinghua.edu.cn/simple

安装好之后,查看python和tensorflow的版本

python
>>>import tensorflow as tf
>>>tf.__version__

(dltf) C:\Windows\System32>python
Python 3.6.0 |Continuum Analytics, Inc.| (default, Dec 23 2016, 11:57:41) [MSC v.1900 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import tensorflow as tf
E:\develop+\anaconda3\envs\dltf\lib\site-packages\tensorflow\python\framework\dtypes.py:458: FutureWarning: Passing (type, 1) or '1type' as a synonym of type is deprecated; in a future version of numpy, it will be understood as (type, (1,)) / '(1,)type'.
  _np_qint8 = np.dtype([("qint8", np.int8, 1)])
E:\develop+\anaconda3\envs\dltf\lib\site-packages\tensorflow\python\framework\dtypes.py:459: FutureWarning: Passing (type, 1) or '1type' as a synonym of type is deprecated; in a future version of numpy, it will be understood as (type, (1,)) / '(1,)type'.
  _np_quint8 = np.dtype([("quint8", np.uint8, 1)])
E:\develop+\anaconda3\envs\dltf\lib\site-packages\tensorflow\python\framework\dtypes.py:460: FutureWarning: Passing (type, 1) or '1type' as a synonym of type is deprecated; in a future version of numpy, it will be understood as (type, (1,)) / '(1,)type'.
  _np_qint16 = np.dtype([("qint16", np.int16, 1)])
E:\develop+\anaconda3\envs\dltf\lib\site-packages\tensorflow\python\framework\dtypes.py:461: FutureWarning: Passing (type, 1) or '1type' as a synonym of type is deprecated; in a future version of numpy, it will be understood as (type, (1,)) / '(1,)type'.
  _np_quint16 = np.dtype([("quint16", np.uint16, 1)])
E:\develop+\anaconda3\envs\dltf\lib\site-packages\tensorflow\python\framework\dtypes.py:462: FutureWarning: Passing (type, 1) or '1type' as a synonym of type is deprecated; in a future version of numpy, it will be understood as (type, (1,)) / '(1,)type'.
  _np_qint32 = np.dtype([("qint32", np.int32, 1)])
E:\develop+\anaconda3\envs\dltf\lib\site-packages\tensorflow\python\framework\dtypes.py:465: FutureWarning: Passing (type, 1) or '1type' as a synonym of type is deprecated; in a future version of numpy, it will be understood as (type, (1,)) / '(1,)type'.
  np_resource = np.dtype([("resource", np.ubyte, 1)])
>>> tf.__version__
'1.2.1'
>>>

 这里查出了tensorflow的版本,但是有警告。我们需要复制dtypes.py的路径,打开dtypes.py,然后进行修改(是你自己的路径,跟我的不同,图中看出路径为E:\develop+\anaconda3\envs\dltf\lib\site-packages\tensorflow\python\framework\dtypes.py)

文件打开是这样的

这里的前缀是你的anaconda安装地址,运行结果里也会显示。出错的行数也可能不同,具体看你的运行过程了。我测试后发现需要将报错的几个都修改一下,就不会再出现了。对报警的行进行修改。修改时要把命令行先关了。
将我的458行的_np_quint8 = np.dtype([(“quint8”, np.uint8, 1)])改为

_np_quint8 = np.dtype([("quint8", np.uint8, (1,))])

这里参考了:在anaconda环境下安装旧版本TensorFlow1.8 

原版:

_np_qint8 = np.dtype([("qint8", np.int8, 1)])
_np_quint8 = np.dtype([("quint8", np.uint8, 1)])
_np_qint16 = np.dtype([("qint16", np.int16, 1)])
_np_quint16 = np.dtype([("quint16", np.uint16, 1)])
_np_qint32 = np.dtype([("qint32", np.int32, 1)])

修改后:

_np_qint8 = np.dtype([("qint8", np.int8, (1,))])
_np_quint8 = np.dtype([("quint8", np.uint8, (1,))])
_np_qint16 = np.dtype([("qint16", np.int16, (1,))])
_np_quint16 = np.dtype([("quint16", np.uint16, (1,))])
_np_qint32 = np.dtype([("qint32", np.int32, (1,))])

后面还有一句(第465行的警告)

#np_resource = np.dtype([("resource", np.ubyte, 1)])
np_resource = np.dtype([("resource", np.ubyte, (1,))])

2.3 安装keras

安装keras

pip install keras==2.0.7 -i https://pypi.tuna.tsinghua.edu.cn/simple

3.安装和配置jupyter

3.1 (方法一)使用命令行安装

pip install --user jupyter -i http://pypi.douban.com/simple/ --trusted-host pypi.douban.com  

这里的--trusted-host pypi.douban.com,是将豆瓣源设为可信任 。

出现错误 ERROR: Command errored out with exit status 1

参考了:pip install 时报错:ERROR: Command errored out with exit status 1: python setup.py egg_inf

需要更新一下pip

#更新pip
pip install --upgrade setuptools && python -m pip install --upgrade pip

#然后重新安装jupyter
pip install --user jupyter -i http://pypi.douban.com/simple/ --trusted-host pypi.douban.com

 此时仍然出现很多warning,我们等会解决

运行jupyter notebook

jupyter notebook

 #出现已存在的包在jupyter notebook中无法import的现象(或者kernel error最佳解决方法):

解决方法:创建新内核

jupyter无法导入已安装的包?原因竟是它!

或 【工具配置】【Jupyter】Windows下,为Jupyter创建新的kernel (这篇博客中创建新环境的步骤可以省略,可直接从jupyter那一部分看起)

出现这个问题的原因是:jupyter中的kernel和新的环境不匹配(现在的kernel可能是上个环境的kernel,还没切换过来),需要新的配置文件使得kernel与新的环境匹配,然后进行kernel的切换。

#有可能出现的问题:打开的notebook网页在运行时出现kernel error(这个方法不能根治):

参考了:kernel error 解决办法

#此方法不能根治,不推荐使用
python -m ipykernel install --user
#然后再重新启动jupyter notebook

以上问题都解决后,就继续安装需要使用的库了。

3.2 (方法二)使用anaconda navigator安装

直接在导航中安装,如果安装卡了很久的话,有可能是pip没更新 

#更新pip
pip install --upgrade setuptools && python -m pip install --upgrade pip

我是在命令行安装后,才从navigator中再安装的。如果直接安装的话,可能会遇到一些问题,可以尝试先用命令行装。其他问题可以参考:吴恩达2022版深度学习第二部分Annaconda+Tensorflow环境搭建 

4.安装需要使用的库

安装matplotlib

pip install matplotlib -i https://pypi.tuna.tsinghua.edu.cn/simple

安装pillow

pip install pillow==8.4.0 -i https://pypi.tuna.tsinghua.edu.cn/simple

安装spicy

spicy版本过高的话(如:1.5.4),会报错" AttributeError: module 'scipy.misc' has no attribute 'imread' ",这里安装版本为1.2.1(因为视频比较老了)

pip install scipy==1.2.1 -i https://pypi.tuna.tsinghua.edu.cn/simple

安装h5py

注:使用python3.6时,h5py版本>3.0.0时(如3.1.0),可能会出现" attributeerror: 'str' object has no attribute 'decode' "的错误,我这里安装的版本是2.1.0

参考文章:

【错误】加载h5权重出错AttributeError: ‘str‘ object has no attribute ‘decode‘

python h5py库安装(记录使用wheel安装的方法)

pip install h5py==2.1.0 -i https://pypi.tuna.tsinghua.edu.cn/simple

#如果已经安装了,可以使用命令卸载,再重新安装
pip uninstall h5py

然后作业中缺什么包,用同样的方法安装就行了。

到此为止,就可顺利运行作业了。

注:

#遇到emojize() got an unexpected keyword argument 'use_aliases',则安装emoji-1.2.0版本

pip install emoji==1.2.0

#遇到TypeError: Calling ‘.seed()’ on instances is deprecated. Use the class method ‘Faker.seed()’ instead.

降低Faker的版本

pip install faker==2.0.0

#遇到类似报错original_keras_version = f.attrs[‘keras_version‘].decode(‘utf8‘)AttributeError: ‘str‘ object has ...('str' object has no attribute 'decode')

h5py的版本过高,降低版本(可先卸载原版本)

pip uninstall h5py
pip install h5py==2.10.0

5. 附录:记录一些可能用到的指令

(参考用,使用时可适当修改,随手记录一下)

conda clean --packages --tarballs 清除conda缓存

conda remove -n tensorflow1.2.1(环境的名字) --all  #删除环境 

conda create -n tensorflow1.2.1 python=3.6 #安装python3.6

pip install tensorflow==1.2.1 -i https://mirrors.aliyun.com/pypi/simple/

python -m pip install --upgrade pip #先更新pip

conda install ipython -i https://mirrors.aliyun.com/pypi/simple/

pip install --user tensorflow==1.2.1 --upgrade

https://blog.csdn.net/Erick2020/article/details/119706076

pip install --user jupyter -i http://pypi.douban.com/simple/ --trusted-host pypi.douban.com #jupyter的安装(在管理员模式下--user可以去掉)
pip install jupyter -i http://pypi.douban.com/simple/

使用anaconda的版本Anaconda3-2023.03-1-Windows-x86_64

conda env list #查看环境列表

pip install matplotlib -i https://pypi.tuna.tsinghua.edu.cn/simple

  • 10
    点赞
  • 24
    收藏
    觉得还不错? 一键收藏
  • 22
    评论
评论 22
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值