Mac下Anaconda通过清华镜像安装TensorFlow2.

2020-03-14  本文已影响0人  羽落青衫薄

Anaconda是一个程序包管理器,一个环境管理器,支持 Linux, Mac, Windows系统,提供了包管理与环境管理的功能,可以很方便地解决多版本python并存、切换以及各种第三方包安装问题。关于Anaconda更详细的入门介绍,可细看Anaconda入门使用指南

1.安装Anaconda

官网下载(Mac版)最新版本的安装包

下载的文件格式为 .pkg

2.建立一个TensorFlow的运行环境

设置国内镜像

清华开源软件镜像站——Anaconda镜像使用帮助

如果需要安装许多第三方库,你会发现conda下载速度很慢,因为Anaconda.org服务器在国外。所幸的是,清华TUNA镜像源有Anaconda仓库的镜像,我们将其加入conda的配置即可,这里有两种添加方式,均可。


conda config --add channels https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/free/

conda config --add channels https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/main/

conda config --set show_channel_urls yes

如图 image 此处已添加完成

点击Add并输入 url 即可。

安装TensorFlow

打开终端,创建一个包含TensorFlow的新conda环境并激活它


$ conda create -n tensorflow

$ source activate tensorflow

$ conda install tensorflow

如果你想用 GPU 驱动的版本,就把 tensorflow 替换为 tensorflow-gpu

3.简单测试是否安装成功

还是在终端输入python


#这是假的,or TF1.0

import tensorflow as tf

hello = tf.constant('Hello, TensorFlow!')

sess = tf.Session()

print(sess.run(hello))

如果系统输出以下内容,你可以开始写 TensorFlow 程序了 !

官方文档


Hello, TensorFlow!

但是我居然失败了???


Traceback (most recent call last):

  File "<stdin>", line 1, in <module>

AttributeError: module 'tensorflow' has no attribute 'Session'

我脸上都是问号?!

我安装的是假的TF

这个测试还是在官方文档上找到的?!

然后查询了万能的百度

发现

image

大意为:

TF2.0没有session,应用tf.compat.v1.Session()来代替tf.Session()

而github上没有更新

那好吧,那就按照stack overflow上说的吧

Tensorflow 2.0 - AttributeError: module 'tensorflow' has no attribute 'Session'


import tensorflow as tf

tf.compat.v1.disable_eager_execution()

hello = tf.constant('Hello, TensorFlow!')

sess = tf.compat.v1.Session()

print(sess.run(hello))

#b'Hello, TensorFlow!'

输出如注释那就对了!

呼,忙活了大半天了,终于结束了!

开启我的TensorFlow之旅。

不对,好像还少了一步?!

4.Spyder下配置TensorFlow

打开我最爱的Spyder,Spyder 是Anaconda自带的集成开发环境(IDE),可以在Spyder中进行tensorflow的开发。

然后在IPython Consloe中输入


import tensorflow as tf

又双发现报错


ModuleNotFoundError: No module named 'tensorflow'

原因是根目录下的Spyder找不到安装在tensorflow环境的Tensorflow模块,解决办法如下: 激活tensorflow环境,确定当前的工作环境为tensorflow安装所在的环境,安装Spyder。

image

如图确保环境为tensorflow,并点击Spyder下的“Install”按钮,等待安装完成即可。此图已安装好。

然后点击“Launch”按钮,打开Spyder,重新测试,在IPython Consloe中输入


import tensorflow as tf

image

ok,没问题了!


import tensorflow as tf

tf.compat.v1.disable_eager_execution()

hello = tf.constant('Hello, TensorFlow!')

sess = tf.compat.v1.Session()

print(sess.run(hello))

用这个再来一遍

image

如图

配置成功!

VICTORY!

我的TensorFlow之旅正式开启!

参考Getting Started with TensorFlow。

tensorflow in anaconda

上一篇下一篇

猜你喜欢

热点阅读