HomeDeep LearningDive Into TensorFlow, Part I: Getting Started with TensorFlow
Deep Learning Specialization on Coursera

TensorFlow is one of most popular open source deep learning libraries launched by Google. This is the first article in a series where I will give a detail tutorial about TensorFlow, here is an index of all the articles in the series that have been published to date:

Part I: Getting Started with TensorFlow (this article)

About TensorFlow

Here is a description from the TensorFlow official site:

TensorFlow™ is an open source software library for numerical computation using data flow graphs. Nodes in the graph represent mathematical operations, while the graph edges represent the multidimensional data arrays (tensors) communicated between them. The flexible architecture allows you to deploy computation to one or more CPUs or GPUs in a desktop, server, or mobile device with a single API. TensorFlow was originally developed by researchers and engineers working on the Google Brain Team within Google’s Machine Intelligence research organization for the purposes of conducting machine learning and deep neural networks research, but the system is general enough to be applicable in a wide variety of other domains as well.

Installing TensorFlow
Install TensorFlow CPU only version on Ubuntu/Linux 64-bit is very easy, here is the simplest pip install steps on my ubuntu 14.04 64bit notebook.


sudo apt-get install python-pip python-dev

sudo pip install --upgrade https://storage.googleapis.com/tensorflow/linux/cpu/tensorflow-0.8.0-cp27-none-linux_x86_64.whl
Downloading tensorflow-0.8.0-cp27-none-linux_x86_64.whl (22.2MB): 22.2MB downloaded
.....
x86_64-linux-gnu-gcc: numpy/random/mtrand/distributions.c
x86_64-linux-gnu-gcc: numpy/random/mtrand/mtrand.c
x86_64-linux-gnu-gcc: numpy/random/mtrand/initarray.c
x86_64-linux-gnu-gcc: numpy/random/mtrand/randomkit.c
x86_64-linux-gnu-gcc -pthread -shared -Wl,-O1 -Wl,-Bsymbolic-functions -Wl,-Bsymbolic-functions -Wl,-z,relro -fno-strict-aliasing -DNDEBUG -g -fwrapv -O2 -Wall -Wstrict-prototypes -D_FORTIFY_SOURCE=2 -g -fstack-protector --param=ssp-buffer-size=4 -Wformat -Werror=format-security build/temp.linux-x86_64-2.7/numpy/random/mtrand/mtrand.o build/temp.linux-x86_64-2.7/numpy/random/mtrand/randomkit.o build/temp.linux-x86_64-2.7/numpy/random/mtrand/initarray.o build/temp.linux-x86_64-2.7/numpy/random/mtrand/distributions.o -Lbuild/temp.linux-x86_64-2.7 -o build/lib.linux-x86_64-2.7/numpy/random/mtrand.so
Creating build/scripts.linux-x86_64-2.7/f2py
adding 'build/scripts.linux-x86_64-2.7/f2py' to scripts
changing mode of build/scripts.linux-x86_64-2.7/f2py from 644 to 755

warning: no previously-included files matching '*.pyo' found anywhere in distribution
warning: no previously-included files matching '*.pyd' found anywhere in distribution
changing mode of /usr/local/bin/f2py to 755
Found existing installation: wheel 0.24.0
Not uninstalling wheel at /usr/lib/python2.7/dist-packages, owned by OS
Found existing installation: six 1.5.2
Not uninstalling six at /usr/lib/python2.7/dist-packages, owned by OS
Found existing installation: setuptools 3.3
Not uninstalling setuptools at /usr/lib/python2.7/dist-packages, owned by OS
Successfully installed tensorflow numpy protobuf wheel six setuptools
Cleaning up...

After successfully installed tensorflow, we can launch a python interpreter to test it, this is just “tip of the iceberg” for TensorFlow:

textminer@textminer:~$ ipython

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
Python 2.7.6 (default, Jun 22 2015, 17:58:13) 
Type "copyright", "credits" or "license" for more information.
 
IPython 1.2.1 -- An enhanced Interactive Python.
?         -> Introduction and overview of IPython's features.
%quickref -> Quick reference.
help      -> Python's own help system.
object?   -> Details about 'object', use 'object??' for extra details.
 
In [1]: import tensorflow as tf
 
In [2]: hello = tf.constant('Hello, TensorFlow!')
 
In [3]: sess = tf.Session()
 
In [4]: print sess.run(hello)
Hello, TensorFlow!
 
In [5]: a = tf.constant(10)
 
In [6]: b = tf.constant(100)
 
In [7]: print sess.run(a + b)
110
 
In [8]: c = a * b
 
In [11]: with tf.Session() as sess:
   ....:     print sess.run(c)
   ....:     print c.eval()
   ....:     
1000
1000
In [16]: tf.InteractiveSession()
Out[16]: <tensorflow.python.client.session.InteractiveSession at 0x7fe6547d3750>
 
In [17]: a = tf.zeros((2,2)); b = tf.ones((2,2))
 
In [18]: tf.reduce_sum(b, reduction_indices=1).eval()
Out[18]: array([ 2.,  2.], dtype=float32)
 
In [19]: a.get_shape()
Out[19]: TensorShape([Dimension(2), Dimension(2)])
 
In [20]: tf.reshape(a, (1,4)).eval()
Out[20]: array([[ 0.,  0.,  0.,  0.]], dtype=float32)

Actually, I have tried to install TensorFlow on my macbook,but met a lot of problems and just gave it up now. Here is the record, just for reference.

First, update pip with:

sudo pip install --upgrade pip

Installing collected packages: pip
Found existing installation: pip 8.1.1
Uninstalling pip-8.1.1:
Successfully uninstalled pip-8.1.1
Successfully installed pip-8.1.2

Then install TensorFlow as the official install guide:

sudo pip install --upgrade https://storage.googleapis.com/tensorflow/mac/tensorflow-0.8.0-py2-none-any.whl

Downloading https://storage.googleapis.com/tensorflow/mac/tensorflow-0.8.0-py2-none-any.whl (19.3MB)
......
Requirement already up-to-date: six>=1.10.0 in /data/homebrew/lib/python2.7/site-packages/six-1.10.0-py2.7.egg (from tensorflow==0.8.0)
Requirement already up-to-date: protobuf==3.0.0b2 in /data/homebrew/lib/python2.7/site-packages (from tensorflow==0.8.0)
Requirement already up-to-date: numpy>=1.10.1 in /data/homebrew/lib/python2.7/site-packages (from tensorflow==0.8.0)
Requirement already up-to-date: wheel in /data/homebrew/lib/python2.7/site-packages (from tensorflow==0.8.0)
Collecting setuptools (from protobuf==3.0.0b2->tensorflow==0.8.0)
Downloading setuptools-21.2.0-py2.py3-none-any.whl (509kB)
Installing collected packages: tensorflow, setuptools
Found existing installation: tensorflow 0.8.0
Uninstalling tensorflow-0.8.0:
Successfully uninstalled tensorflow-0.8.0
Found existing installation: setuptools 19.4
Uninstalling setuptools-19.4:
Successfully uninstalled setuptools-19.4
Successfully installed setuptools-21.2.0 tensorflow-0.8.0

It seems that install tensorflow on mac os successfully, but when I opened the ipython interpreter, a problem appeared:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
Python 2.7.6 (default, Jun  3 2014, 07:43:23) 
Type "copyright", "credits" or "license" for more information.
 
IPython 3.1.0 -- An enhanced Interactive Python.
?         -> Introduction and overview of IPython's features.
%quickref -> Quick reference.
help      -> Python's own help system.
object?   -> Details about 'object', use 'object??' for extra details.
 
In [1]: import tensorflow as tf
---------------------------------------------------------------------------
ImportError                               Traceback (most recent call last)
<ipython-input-1-41389fad42b5> in <module>()
----> 1 import tensorflow as tf
 
/data/homebrew/lib/python2.7/site-packages/tensorflow/__init__.py in <module>()
     21 from __future__ import print_function
     22 
---> 23 from tensorflow.python import *
 
/data/homebrew/lib/python2.7/site-packages/tensorflow/python/__init__.py in <module>()
     43 _default_dlopen_flags = sys.getdlopenflags()
     44 sys.setdlopenflags(_default_dlopen_flags | ctypes.RTLD_GLOBAL)
---> 45 from tensorflow.python import pywrap_tensorflow
     46 sys.setdlopenflags(_default_dlopen_flags)
     47 
 
/data/homebrew/lib/python2.7/site-packages/tensorflow/python/pywrap_tensorflow.py in <module>()
     26                 fp.close()
     27             return _mod
---> 28     _pywrap_tensorflow = swig_import_helper()
     29     del swig_import_helper
     30 else:
 
/data/homebrew/lib/python2.7/site-packages/tensorflow/python/pywrap_tensorflow.py in swig_import_helper()
     22         if fp is not None:
     23             try:
---> 24                 _mod = imp.load_module('_pywrap_tensorflow', fp, pathname, description)
     25             finally:
     26                 fp.close()
 
ImportError: dlopen(tensorflow/python/_pywrap_tensorflow.so, 10): Symbol not found: ___sincos_stret
  Referenced from: tensorflow/python/_pywrap_tensorflow.so
  Expected in: /usr/lib/libSystem.B.dylib
 in tensorflow/python/_pywrap_tensorflow.so

After google this problem, I found a same question in stackoverflow: Python unable to import tensorflow to Mac OS X 10.8. Yes, my os version is OS X 10.8.5, and the answer is:

The issue appears to be that the binary distribution of TensorFlow is not compatible with Mac OS X 10.8. The relevant part of the error message is this one:

Symbol not found: ___sincos_stret
According to various sources, this is a known issue when using binaries compiled for a newer version of Mac OS X on 10.8 (Mavericks), because the symbol ___sincos_stret is not defined in the standard libraries for that version of the OS.

To address this, you will need to (i) upgrade to Mac OS X 10.9 or later, (ii) follow the instructions to install from source, or (iii) use Docker.

I didn’t want upgrade my mac os to 10.9+ now, so I followed the TensorFlow “Install from source” steps, but met more problems.

First get the TensorFlow source code:

git clone –recurse-submodules https://github.com/tensorflow/tensorflow

Cloning into 'tensorflow'...
remote: Counting objects: 45383, done.
remote: Total 45383 (delta 0), reused 0 (delta 0), pack-reused 45383
Receiving objects: 100% (45383/45383), 36.94 MiB | 517 KiB/s, done.
Resolving deltas: 100% (32922/32922), done.
Checking out files: 100% (3262/3262), done.
Submodule 'google/protobuf' (https://github.com/google/protobuf.git) registered for path 'google/protobuf'
Cloning into 'google/protobuf'...
remote: Counting objects: 33312, done.
remote: Compressing objects: 100% (20/20), done.
remote: Total 33312 (delta 6), reused 0 (delta 0), pack-reused 33292
Receiving objects: 100% (33312/33312), 31.76 MiB | 1.33 MiB/s, done.
Resolving deltas: 100% (22285/22285), done.
Submodule path 'google/protobuf': checked out 'fb714b3606bd663b823f6960a73d052f97283b74'

Then “brew install bazel swig” but failed:
Warning: Your Xcode (4.5.2) is outdated
Please update to Xcode 5.1.
Xcode can be updated from the App Store.
Warning: It appears you have MacPorts or Fink installed.
Software installed with other package managers causes known problems for
Homebrew. If a formula fails to build, uninstall MacPorts/Fink and try again.
Error: No available formula for bazel
Searching taps...

So I need install bazel from the source code:

git clone https://github.com/bazelbuild/bazel.git
Cloning into 'bazel'...
remote: Counting objects: 95606, done.
remote: Compressing objects: 100% (889/889), done.
remote: Total 95606 (delta 436), reused 0 (delta 0), pack-reused 94454
Receiving objects: 100% (95606/95606), 184.46 MiB | 734 KiB/s, done.
Resolving deltas: 100% (58662/58662), done.
Checking out files: 100% (5135/5135), done.

But before compile bazel, I need upgrade the Java JDK to JDK 8, because it is the bazel system requiremnts:

System Requirements

Supported platforms:

Ubuntu Linux (Wily 15.10 and Trusty 14.04 LTS)
Mac OS X
Java:

Java JDK 8 or later (JDK 7 is still supported but deprecated).
Windows Support

Currently, Windows support is highly experimental. For more information, see Building Bazel on Windows.

Checking the Java JDK version by “ls /System/Library/Frameworks/JavaVM.framework/Versions” and I found the “CurrentJDK -> /System/Library/Java/JavaVirtualMachines/1.6.0.jdk/Contents”. So download the jdk-8….img file from the official oracal website and installed JDK8 by click it.

After this step, compile the bazel in its directory:

./compile.sh
INFO: You can skip this first step by providing a path to the bazel binary as second argument:
INFO: ./compile.sh compile /path/to/bazel
🍃 Building Bazel from scratch..
....
🍃 Building Bazel with Bazel.
......
external/local_config_cc/cc_wrapper.sh -U_FORTIFY_SOURCE '-D_FORTIFY_SOURCE=1' -fstack-protector -Wall -Wthread-safety -Wself-assign -fno-omit-frame-pointer '-std=c++0x' -fPIC -iquote . -iquote bazel-out/local-fastbuild/genfiles -iquote external/bazel_tools -iquote bazel-out/local-fastbuild/genfiles/external/bazel_tools -isystem external/bazel_tools/tools/cpp/gcc3 -no-canonical-prefixes -Wno-builtin-macro-redefined '-D__DATE__="redacted"' '-D__TIMESTAMP__="redacted"' '-D__TIME__="redacted"' '-frandom-seed=bazel-out/local-fastbuild/bin/src/main/cpp/_objs/blaze_abrupt_exit/src/main/cpp/blaze_abrupt_exit.pic.o' -MD -MF bazel-out/local-fastbuild/bin/src/main/cpp/_objs/blaze_abrupt_exit/src/main/cpp/blaze_abrupt_exit.pic.d -c src/main/cpp/blaze_abrupt_exit.cc -o bazel-out/local-fastbuild/bin/src/main/cpp/_objs/blaze_abrupt_exit/src/main/cpp/blaze_abrupt_exit.pic.o): com.google.devtools.build.lib.shell.BadExitStatusException: Process exited with status 1.
cc1plus: error: unrecognized command line option "-Wthread-safety"
cc1plus: error: unrecognized command line option "-Wself-assign"
cc1plus: error: unrecognized command line option "-Wno-builtin-macro-redefined"
cc1plus: error: unrecognized command line option "-std=c++0x"
Target //src:bazel failed to build

After google this problem, I found that my xcode vesion is lower and need upgraded >= 6.10. But when I upgrade it in the APP Store, they reminded me I need upgrade my os version to OS v10.11. It seems like a chicken-and-egg problem for me now.

Update:
After upgrade my mac os up to “OS X EI Capitan Version 10.11.5”, everything is ok, now I can use TensorFlow on macbook pro.

TensorFlow Reference
TensorFlow Official Doc: https://www.tensorflow.org/
CS224d: TensorFlow Tutorial: http://cs224d.stanford.edu/lectures/CS224d-Lecture7.pdf
Udacity Deep Learning Course by Google: https://www.udacity.com/course/deep-learning–ud730
Simple tutorials using Google’s TensorFlow Framework: https://github.com/nlintz/TensorFlow-Tutorials
UFLDL Tutorial: http://ufldl.stanford.edu/wiki/index.php/UFLDL_Tutorial
UFLDL Deep Learning Tutorial: http://ufldl.stanford.edu/tutorial/
Google TensorFlow Tutorial:http://www.slideshare.net/tw_dsconf/tensorflow-tutorial
TensorFlow Tutorial:https://medium.com/@ilblackdragon/tensorflow-tutorial-part-1-c559c63c0cb1#.5th1bqh3y
TensorFlow tutorials and code examples for beginners:https://github.com/aymericdamien/TensorFlow-Examples
IMPLEMENTING A CNN FOR TEXT CLASSIFICATION IN TENSORFLOW:http://www.wildml.com/2015/12/implementing-a-cnn-for-text-classification-in-tensorflow/
A TensorFlow Tutorial: Email Classification: http://jrmeyer.github.io/tutorial/2016/02/01/TensorFlow-Tutorial.html
TFLearn: Deep learning library featuring a higher-level API for TensorFlow: http://tflearn.org/
TensorFlow Tutorial: http://terryum.io/ml_practice/2016/05/28/TFIntroSlides/
TensorFlow_Exercises: https://github.com/terryum/TensorFlow_Exercises
Diving into Machine Learning through TensorFlow: https://www.youtube.com/watch?list=PLBkISg6QfSX9HL6us70IBs9slFciFFa4W&v=GZBIPwdGtkk
A Practical Guide for Debugging Tensorflow Codes: https://wookayin.github.io/TensorflowKR-2016-talk-debugging
Tensorflow Tutorials using Jupyter Notebook: https://github.com/sjchoi86/Tensorflow-101

Posted by TextMiner

Deep Learning Specialization on Coursera

Comments

Dive Into TensorFlow, Part I: Getting Started with TensorFlow — 1 Comment

Leave a Reply

Your email address will not be published.