MacOS 搭建 Tensorflow PyCharm 环境

TensorFlow 最初由Google大脑小组(隶属于Google机器智能研究机构)的研究员和工程师们开发出来,用于机器学习和深度神经网络方面的研究,但这个系统的通用性使其也可普遍用于其余计算领域。目前来讲,Github上star最多的项目就是它了。python

在这以前,笔者写过一篇简单的入门文章《初探 TensorFlow》。当时没能成功搭建环境,加上后期的工做缘由,至此搁置了一段时间。今天,终于各类折腾,在本身的Mac上通过多种尝试以后,完美搭建成功。这里就把它分享出来,但愿对你们有所帮助。linux

基于 Anaconda 的安装

Anaconda 是一个集成许多第三方科学计算库的 Python 科学计算环境,Anaconda 使用 conda 做为本身的包管理工具,同时具备本身的计算环境,相似 Virtualenv.git

Virtualenv 同样,不一样 Python 工程须要的依赖包,conda 将他们存储在不一样的地方。 TensorFlow 上安装的 Anaconda 不会对以前安装的 Python 包进行覆盖.github

  • 安装 Anaconda
  • 创建一个 conda 计算环境
  • 激活环境,使用 conda 安装 TensorFlow
  • 安装成功后,每次使用 TensorFlow 的时候须要激活 conda 环境

安装 Anaconda :

参考 Anaconda 的下载页面的指导c#

创建环境

创建一个 conda 计算环境名字叫tensorflow:api

# Python 2.7
$ conda create -n tensorflow python=2.7

# Python 3.4
$ conda create -n tensorflow python=3.4
复制代码

激活

激活tensorflow环境,而后使用其中的 pip 安装 TensorFlow. 当使用easy_install使用--ignore-installed标记防止错误的产生。bash

URL of the TensorFlow Python package网络

$ source activate tensorflow
(tensorflow)$  # Your prompt should change

# Ubuntu/Linux 64-bit, CPU only, Python 2.7:
(tensorflow)$ pip install --ignore-installed --upgrade https://storage.googleapis.com/tensorflow/linux/cpu/tensorflow-0.8.0rc0-cp27-none-linux_x86_64.whl

# Ubuntu/Linux 64-bit, GPU enabled, Python 2.7. Requires CUDA toolkit 7.5 and CuDNN v4.
# For other versions, see "Install from sources" below.
(tensorflow)$ pip install --ignore-installed --upgrade https://storage.googleapis.com/tensorflow/linux/gpu/tensorflow-0.8.0rc0-cp27-none-linux_x86_64.whl

# Mac OS X, CPU only:
(tensorflow)$ pip install --ignore-installed --upgrade https://storage.googleapis.com/tensorflow/mac/cpu/tensorflow-0.11.0rc0-py2-none-any.whl
复制代码

对于 Python 3.x :机器学习

$ source activate tensorflow
(tensorflow)$  # Your prompt should change

# Ubuntu/Linux 64-bit, CPU only, Python 3.4:
(tensorflow)$ pip install --ignore-installed --upgrade https://storage.googleapis.com/tensorflow/linux/cpu/tensorflow-0.8.0rc0-cp34-cp34m-linux_x86_64.whl

# Ubuntu/Linux 64-bit, GPU enabled, Python 3.4. Requires CUDA toolkit 7.5 and CuDNN v4.
# For other versions, see "Install from sources" below.
(tensorflow)$ pip install --ignore-installed --upgrade https://storage.googleapis.com/tensorflow/linux/gpu/tensorflow-0.8.0rc0-cp34-cp34m-linux_x86_64.whl

# Mac OS X, CPU only:
(tensorflow)$ pip install --ignore-installed --upgrade https://storage.googleapis.com/tensorflow/mac/tensorflow-0.8.0rc0-py3-none-any.whl
复制代码

conda 环境激活后,你能够测试:ide

$ python
>>> import tensorflow as tf
>>> print(tf.__version__)
# 0.11.0rc0
复制代码

开启或关闭环境

当你不用 TensorFlow 的时候,关闭环境:

(tensorflow)$ source deactivate

$  # Your prompt should change back
复制代码

再次使用的时候再激活 :

$ source activate tensorflow
(tensorflow)$  # Your prompt should change.
# Run Python programs that use TensorFlow.
...
# When you are done using TensorFlow, deactivate the environment.
(tensorflow)$ source deactivate
复制代码

PyCharm 配置

**重点:**正确配置ProjectInterpreter便可

方法

  • Preferences
  • Project Interpreter
  • Click More

附图

  • 打开Preferences

打开Preferences

选择more

  • 打开Project Interpreters

选择正确的tensorflow路径

  • Demo运行结果

demo运行结果
相关文章
相关标签/搜索