最新消息:VPS服务器又从fzhost.net换回Linode了,主题仍用朋友推荐的大前端D8

【记录】安装Tensor Flow

Mac crifan 13浏览 0评论

折腾:

TensorFlow Anaconda Cuda Cudnn

后,去安装TensorFlow

【记录】折腾Anaconda

之后,去参考:

Win10下用Anaconda安装TensorFlow – CSDN博客

去切换为all:

才看到tensorflow

然后勾选上:

mac anaconda安装tensorflow

Mac下通过Anaconda安装Tensorflow – 简书

教程:在 Mac OS X 上安装 TensorFlow

mac/linux 安装tensorflow和安装Anaconda – CSDN博客

通过Anaconda安装TensorFlow环境 – CSDN博客

重启后,就可以看到已经安装了:

Installing TensorFlow on Windows  |  TensorFlow

Installing TensorFlow on macOS  |  TensorFlow

“In Anaconda, you may use conda to create a virtual environment. However, within Anaconda, we recommend installing TensorFlow with the pip install command, not with the conda install command.”

教程:在 Mac OS X 上安装 TensorFlow

“选择 Anaconda,你可以使用 conda 创建一个虚拟环境,我们建议使用 pip install 命令而不是 coda install 命令安装 TensorFlow。”

➜  youtubeSubtitle pip install –upgrade virtualenv

Collecting virtualenv

  Downloading virtualenv-15.1.0-py2.py3-none-any.whl (1.8MB)

    100% |████████████████████████████████| 1.8MB 40kB/s

Installing collected packages: virtualenv

Successfully installed virtualenv-15.1.0

接着去:

➜  youtubeSubtitle virtualenv –help

Usage: virtualenv [OPTIONS] DEST_DIR

Options:

  –version             show program’s version number and exit

  -h, –help            show this help message and exit

  -v, –verbose         Increase verbosity.

  -q, –quiet           Decrease verbosity.

  -p PYTHON_EXE, –python=PYTHON_EXE

                        The Python interpreter to use, e.g.,

                        –python=python2.5 will use the python2.5 interpreter

                        to create the new environment.  The default is the

                        interpreter that virtualenv was installed with

                        (/usr/local/opt/python/bin/python2.7)

  –clear               Clear out the non-root install and start from scratch.

  –no-site-packages    DEPRECATED. Retained only for backward compatibility.

                        Not having access to global site-packages is now the

                        default behavior.

  –system-site-packages

                        Give the virtual environment access to the global

                        site-packages.

  –always-copy         Always copy files rather than symlinking.

  –unzip-setuptools    Unzip Setuptools when installing it.

  –relocatable         Make an EXISTING virtualenv environment relocatable.

                        This fixes up scripts and makes all .pth files

                        relative.

  –no-setuptools       Do not install setuptools in the new virtualenv.

  –no-pip              Do not install pip in the new virtualenv.

  –no-wheel            Do not install wheel in the new virtualenv.

  –extra-search-dir=DIR

                        Directory to look for setuptools/pip distributions in.

                        This option can be used multiple times.

  –download            Download preinstalled packages from PyPI.

  –no-download, –never-download

                        Do not download preinstalled packages from PyPI.

  –prompt=PROMPT       Provides an alternative prompt prefix for this

                        environment.

  –setuptools          DEPRECATED. Retained only for backward compatibility.

                        This option has no effect.

  –distribute          DEPRECATED. Retained only for backward compatibility.

                        This option has no effect.

然后创建目录:

➜  youtubeSubtitle virtualenv –system-site-packages /Users/crifan/dev/dev_root/tensorflow

New python executable in /Users/crifan/dev/dev_root/tensorflow/bin/python2.7

Also creating executable in /Users/crifan/dev/dev_root/tensorflow/bin/python

Installing setuptools, pip, wheel…done.

【貌似无需解决】Mac中active后virtualenv没有生效

暂时先假定已经正常激活了虚拟环境了,接着去安装tensorflow

先确保pip版本大于8.1

➜  virtualenv easy_install -U pip

Searching for pip

Reading https://pypi.python.org/simple/pip/

Downloading https://pypi.python.org/packages/b6/ac/7015eb97dc749283ffdec1c3a88ddb8ae03b8fad0f0e611408f196358da3/pip-9.0.1-py2.py3-none-any.whl#md5=297dbd16ef53bcef0447d245815f5144

Best match: pip 9.0.1

Processing pip-9.0.1-py2.py3-none-any.whl

Installing pip-9.0.1-py2.py3-none-any.whl to /Users/crifan/dev/dev_root/virtualenv/tensorflow/lib/python2.7/site-packages

writing requirements to /Users/crifan/dev/dev_root/virtualenv/tensorflow/lib/python2.7/site-packages/pip-9.0.1-py2.7.egg/EGG-INFO/requires.txt

Adding pip 9.0.1 to easy-install.pth file

Installing pip script to /Users/crifan/dev/dev_root/virtualenv/tensorflow/bin

Installing pip3.5 script to /Users/crifan/dev/dev_root/virtualenv/tensorflow/bin

Installing pip3 script to /Users/crifan/dev/dev_root/virtualenv/tensorflow/bin

Installed /Users/crifan/dev/dev_root/virtualenv/tensorflow/lib/python2.7/site-packages/pip-9.0.1-py2.7.egg

Processing dependencies for pip

Finished processing dependencies for pip

➜  virtualenv pip –version

pip 9.0.1 from /Users/crifan/dev/dev_root/virtualenv/tensorflow/lib/python2.7/site-packages (python 2.7)

继续安装:

➜  tensorflow pip install –upgrade tensorflow

Collecting tensorflow

  Downloading tensorflow-1.5.0-cp27-cp27m-macosx_10_11_x86_64.whl (42.5MB)

    100% |████████████████████████████████| 42.5MB 14kB/s

Requirement already up-to-date: wheel in ./lib/python2.7/site-packages (from tensorflow)

Collecting backports.weakref>=1.0rc1 (from tensorflow)

  Downloading backports.weakref-1.0.post1-py2.py3-none-any.whl

Collecting absl-py>=0.1.6 (from tensorflow)

  Downloading absl-py-0.1.9.tar.gz (79kB)

    100% |████████████████████████████████| 81kB 21kB/s

Collecting protobuf>=3.4.0 (from tensorflow)

  Downloading protobuf-3.5.1-py2.py3-none-any.whl (388kB)

    100% |████████████████████████████████| 389kB 54kB/s

Collecting mock>=2.0.0 (from tensorflow)

  Downloading mock-2.0.0-py2.py3-none-any.whl (56kB)

    100% |████████████████████████████████| 61kB 41kB/s

Collecting tensorflow-tensorboard<1.6.0,>=1.5.0 (from tensorflow)

  Downloading tensorflow_tensorboard-1.5.0-py2-none-any.whl (3.0MB)

    100% |████████████████████████████████| 3.0MB 57kB/s

Collecting numpy>=1.12.1 (from tensorflow)

  Downloading numpy-1.14.0-cp27-cp27m-macosx_10_6_intel.macosx_10_9_intel.macosx_10_9_x86_64.macosx_10_10_intel.macosx_10_10_x86_64.whl (4.7MB)

    100% |████████████████████████████████| 4.7MB 40kB/s

Collecting six>=1.10.0 (from tensorflow)

  Downloading six-1.11.0-py2.py3-none-any.whl

Requirement already up-to-date: enum34>=1.1.6 in /usr/local/lib/python2.7/site-packages (from tensorflow)

Requirement already up-to-date: setuptools in ./lib/python2.7/site-packages (from protobuf>=3.4.0->tensorflow)

Collecting funcsigs>=1; python_version < “3.3” (from mock>=2.0.0->tensorflow)

  Downloading funcsigs-1.0.2-py2.py3-none-any.whl

Collecting pbr>=0.11 (from mock>=2.0.0->tensorflow)

  Downloading pbr-3.1.1-py2.py3-none-any.whl (99kB)

    100% |████████████████████████████████| 102kB 30kB/s

Collecting markdown>=2.6.8 (from tensorflow-tensorboard<1.6.0,>=1.5.0->tensorflow)

  Downloading Markdown-2.6.11-py2.py3-none-any.whl (78kB)

    100% |████████████████████████████████| 81kB 26kB/s

Collecting bleach==1.5.0 (from tensorflow-tensorboard<1.6.0,>=1.5.0->tensorflow)

  Downloading bleach-1.5.0-py2.py3-none-any.whl

Collecting werkzeug>=0.11.10 (from tensorflow-tensorboard<1.6.0,>=1.5.0->tensorflow)

  Downloading Werkzeug-0.14.1-py2.py3-none-any.whl (322kB)

    100% |████████████████████████████████| 327kB 52kB/s

Collecting html5lib==0.9999999 (from tensorflow-tensorboard<1.6.0,>=1.5.0->tensorflow)

  Downloading html5lib-0.9999999.tar.gz (889kB)

    100% |████████████████████████████████| 890kB 28kB/s

Collecting futures>=3.1.1 (from tensorflow-tensorboard<1.6.0,>=1.5.0->tensorflow)

  Downloading futures-3.2.0-py2-none-any.whl

Building wheels for collected packages: absl-py, html5lib

  Running setup.py bdist_wheel for absl-py … done

  Stored in directory: /Users/crifan/Library/Caches/pip/wheels/04/f5/7c/5d4eab10ddf87dec875016e74ba289d87270a90fb2662a76fc

  Running setup.py bdist_wheel for html5lib … done

  Stored in directory: /Users/crifan/Library/Caches/pip/wheels/6f/85/6c/56b8e1292c6214c4eb73b9dda50f53e8e977bf65989373c962

Successfully built absl-py html5lib

Installing collected packages: backports.weakref, six, absl-py, protobuf, funcsigs, pbr, mock, markdown, html5lib, bleach, werkzeug, futures, numpy, tensorflow-tensorboard, tensorflow

  Found existing installation: six 1.10.0

    Not uninstalling six at /usr/local/lib/python2.7/site-packages, outside environment /Users/crifan/dev/dev_root/virtualenv/tensorflow/bin/..

Successfully installed absl-py-0.1.9 backports.weakref-1.0.post1 bleach-1.5.0 funcsigs-1.0.2 futures-3.2.0 html5lib-0.9999999 markdown-2.6.11 mock-2.0.0 numpy-1.14.0 pbr-3.1.1 protobuf-3.5.1 six-1.11.0 tensorflow-1.5.0 tensorflow-tensorboard-1.5.0 werkzeug-0.14.1

➜  tensorflow

然后去写代码:

# Python

import tensorflow as tf

hello = tf.constant(‘Hello, TensorFlow!’)

sess = tf.Session()

print(sess.run(hello))

测试结果是:

➜  code python testTensorFlow.py

2018-01-27 14:00:51.745675: I tensorflow/core/platform/cpu_feature_guard.cc:137] Your CPU supports instructions that this TensorFlow binary was not compiled to use: SSE4.2 AVX AVX2 FMA

Hello, TensorFlow!

即:

虽然有警告,但是可以正常使用tf的。

再去

tensorfly.cn

介绍 | TensorFlow 官方文档中文版

试了试代码:

#!/usr/bin/python

# -*- coding: UTF-8 -*-

# # Python

# import tensorflow as tf

# hello = tf.constant(‘Hello, TensorFlow!’)

# sess = tf.Session()

# print(sess.run(hello))

import tensorflow as tf

import numpy as np

# 使用 NumPy 生成假数据(phony data), 总共 100 个点.

x_data = np.float32(np.random.rand(2, 100)) # 随机输入

y_data = np.dot([0.100, 0.200], x_data) + 0.300

# 构造一个线性模型

#

b = tf.Variable(tf.zeros([1]))

W = tf.Variable(tf.random_uniform([1, 2], -1.0, 1.0))

y = tf.matmul(W, x_data) + b

# 最小化方差

loss = tf.reduce_mean(tf.square(y – y_data))

optimizer = tf.train.GradientDescentOptimizer(0.5)

train = optimizer.minimize(loss)

# 初始化变量

init = tf.initialize_all_variables()

# 启动图 (graph)

sess = tf.Session()

sess.run(init)

# 拟合平面

for step in xrange(0, 201):

sess.run(train)

if step % 20 == 0:

print step, sess.run(W), sess.run(b)

# 得到最佳拟合结果 W: [[0.100 0.200]], b: [0.300]

结果:

➜  code python testTensorFlow.py

WARNING:tensorflow:From /Users/crifan/dev/dev_root/virtualenv/tensorflow/lib/python2.7/site-packages/tensorflow/python/util/tf_should_use.py:118: initialize_all_variables (from tensorflow.python.ops.variables) is deprecated and will be removed after 2017-03-02.

Instructions for updating:

Use `tf.global_variables_initializer` instead.

2018-01-27 14:04:47.933379: I tensorflow/core/platform/cpu_feature_guard.cc:137] Your CPU supports instructions that this TensorFlow binary was not compiled to use: SSE4.2 AVX AVX2 FMA

0 [[ 1.0089685  -0.40529624]] [0.32873368]

20 [[0.25958443 0.09268568]] [0.2754195]

40 [[0.13087982 0.1839285 ]] [0.2927249]

60 [[0.10624495 0.19799037]] [0.29786307]

80 [[0.10133421 0.19988437]] [0.29937512]

100 [[0.10030308 0.20004883]] [0.29981774]

120 [[0.10007316 0.20002869]] [0.29994693]

140 [[0.10001864 0.20001088]] [0.29998457]

160 [[0.10000495 0.2000036 ]] [0.29999554]

180 [[0.10000136 0.20000112]] [0.2999987]

200 [[0.10000039 0.20000035]] [0.29999962]

至此,虽然有点曲折,但是算是

安装了tensor flow

可以正常用python代码调用tf了。

【后记】

想要继续测试更多代码,可以参考:

Getting Started with TensorFlow  |  TensorFlow

Getting Started for ML Beginners  |  TensorFlow

另外也继续去Anaconda中试试

感觉好像不是我需要的

切换回到默认的base root环境:

Last login: Sat Jan 27 13:48:06 on ttys002

/Users/crifan/.anaconda/navigator/a.tool ; exit;

➜  ~ /Users/crifan/.anaconda/navigator/a.tool ; exit;

/Users/crifan/.anaconda/navigator/a.tool: line 1: syntax error near unexpected token `(‘

/Users/crifan/.anaconda/navigator/a.tool: line 1: `bash –init-file <(echo “source activate /Users/crifan/anaconda2;”)’

[进程已完成]

然后参考:

mac/linux 安装tensorflow和安装Anaconda – CSDN博客

找到了conda所在位置:

➜  tensorflow pwd

/Users/crifan/anaconda2/envs/tensorflow

➜  tensorflow conda

zsh: command not found: conda

➜  tensorflow ll ~/anaconda2/bin/conda

-rwxrwxr-x  1 crifan  staff   230B  1 27 00:10 /Users/crifan/anaconda2/bin/conda

然后加到环境变量中:

➜  tensorflow export PATH=~/anaconda2/bin:$PATH

➜  tensorflow which conda

/Users/crifan/anaconda2/bin/conda

➜  tensorflow echo $PATH

/Users/crifan/anaconda2/bin:/Users/crifan/anaconda/bin:/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin

然后就可以去添加tensorflow的环境了。

转载请注明:在路上 » 【记录】安装Tensor Flow

发表我的评论
取消评论

表情

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址
15 queries in 0.245 seconds, using 10.38MB memory