Mac OS X 10.12.3安装TensorFlow

时间:2022-06-17
本文章向大家介绍Mac OS X 10.12.3安装TensorFlow,主要内容包括其使用实例、应用技巧、基本知识点总结和需要注意事项,具有一定的参考价值,需要的朋友可以参考一下。

安装TesorFlow真的是个大坑,整整搞了两天,还没安好GPU版,可能是Mac mini没有独立显卡,但是奇怪的是安装过程中它并没有报错,还安装了NVDIA的驱动,cuda8.0,为了配合cuda8.0我硬生生安了个Xcode7.3,满怀期待地跑smaples,结果一个大大的Error……

官网安装介绍网址:https://www.tensorflow.org/versions/r0.12/get_started/os_setup


安装步骤

安装Anaconda

Anaconda我所理解的作用就是一个能创造环境,快速安装包的工具,使用它的好处在于它可以给每个项目创建一个环境,安装它所需要的python版本,python依赖,当然也可以在这个环境中安装tensorflow和jupyter notebook,Anaconda安装请点击这里

创建Anaconda环境

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

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

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

我的Mac是10.12.3,python使用的2.7版本,所以用的第一个

使用前激活tensorflow环境,并在其中安装tesorflow和jupyter notebook(如果需要)

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

# Linux/Mac OS X, Python 2.7/3.4/3.5, CPU only:
(tensorflow)$ conda install -c conda-forge tensorflow

# install jupyter notebook(if needed)
(tensorflow)$ conda install jupyter notebook

测试使用

打开Anaconda-Navigator,点击刚创建的tensorflow环境(绿色箭头),选择Open with jupyter noteebook

输入下面代码,点击运行

import tensorflow as tf
import numpy as np

# Create 100 phony x, y data points in NumPy, y = x * 0.1 + 0.3
x_data = np.random.rand(100).astype(np.float32)
y_data = x_data * 0.1 + 0.3

# Try to find values for W and b that compute y_data = W * x_data + b
# (We know that W should be 0.1 and b 0.3, but TensorFlow will
# figure that out for us.)
W = tf.Variable(tf.random_uniform([1], -1.0, 1.0))
b = tf.Variable(tf.zeros([1]))
y = W * x_data + b

# Minimize the mean squared errors.
loss = tf.reduce_mean(tf.square(y - y_data))
optimizer = tf.train.GradientDescentOptimizer(0.5)
train = optimizer.minimize(loss)

# Before starting, initialize the variables.  We will 'run' this first.
init = tf.global_variables_initializer()

# Launch the graph.
sess = tf.Session()
sess.run(init)

# Fit the line.
for step in range(201):
    sess.run(train)
    if step % 20 == 0:
        print(step, sess.run(W), sess.run(b))

# Learns best fit is W: [0.1], b: [0.3]

可以看到运行结果如下

(0, array([ 0.24201418], dtype=float32), array([ 0.31082982], dtype=float32))
(20, array([ 0.13270748], dtype=float32), array([ 0.28200552], dtype=float32))
(40, array([ 0.11024268], dtype=float32), array([ 0.29436487], dtype=float32))
(60, array([ 0.10320758], dtype=float32), array([ 0.29823533], dtype=float32))
(80, array([ 0.1010045], dtype=float32), array([ 0.29944739], dtype=float32))
(100, array([ 0.10031457], dtype=float32), array([ 0.29982695], dtype=float32))
(120, array([ 0.10009852], dtype=float32), array([ 0.2999458], dtype=float32))
(140, array([ 0.10003085], dtype=float32), array([ 0.29998305], dtype=float32))
(160, array([ 0.10000967], dtype=float32), array([ 0.29999468], dtype=float32))
(180, array([ 0.10000306], dtype=float32), array([ 0.29999834], dtype=float32))
(200, array([ 0.10000096], dtype=float32), array([ 0.29999948], dtype=float32))

使用后关闭环境

(tensorflow)$ source deactivate

遇到的问题

尝试setup GPU for Mac

总是说apple clang版本存在问题,后来换成Xcode7.3,llvm7.0.3,依然存在问题,无解

CondaHTTPError

这是一个很坑爹的问题,StackOverFlow也有人碰到,但是都没有答案,有的博客说是运营商把网墙掉了,但是我用的校园网,怎么换运营商……

后来发现别的同学都能用,对比之下唯一的差距是我没有用网线,于是换成了网线,果然,问题解决了!


收获

  • 学会了切换Xcode版本来执行源代码编译,代码如下
$ sudo xcode-select -s /Applications/Xcode7.3.app/Contents/Developer

其中/Applications/Xcode7.3.app替换成自己的路径

  • 学会了查看apple llvm的版本
$ /usr/bin/cc --version