Technology-机器学习-Tensorflow 安装

Tensorflow 在多个系统安装流程。

CentOS 系统:

WHL 安装

1
2
pip install scipy sklearn six mock wheel protobuf numpy
pip install https://storage.googleapis.com/tensorflow/linux/cpu/tensorflow-1.3.0-cp27-none-linux_x86_64.whl

源码安装

Java 安装:

1
2
3
4
5
6
7
8
9
10
yum -y install java-1.8.0-openjdk*
ls -lrt /etc/alternatives/java // 查看安装路径
vi /etc/profile

JAVA_HOME=/usr/lib/jvm/java-1.8.0-openjdk-1.8.0.144-0.b01.el7_4.x86_64
JRE_HOME=$JAVA_HOME/jre
CLASSPATH=$JAVA_HOME/lib:$JRE_HOME/lib:$CLASSPATH
PATH=$JAVA_HOME/bin:$JRE_HOME/bin:$PATH

source /etc/profile

Tomcat 安装:

1
2
3
4
5
sudo yum install tomcat
sudo yum install tomcat-webapps tomcat-admin-webapps
路径为:/usr/share/tomcat
sudo systemctl start tomcat
sudo systemctl stop tomcat

Bazel 安装:

1
2
3
4
5
wget https://github.com/bazelbuild/bazel/releases/download/0.6.1/bazel-0.6.1-without-jdk-installer-linux-x86_64.sh
chmod 777 bazel-0.6.1-without-jdk-installer-linux-x86_64.sh
./bazel-0.6.1-without-jdk-installer-linux-x86_64.sh
cp /usr/local/bin/bazel /usr/bin/
cp /usr/local/bin/bazel /usr/bin/bazel-real

编译:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
yum -y install patch git
git clone https://github.com/tensorflow/tensorflow
cd tensorflow
./configure
# 仅CPU,不使用MKL
bazel build --config=opt //tensorflow/tools/pip_package:build_pip_package
# 仅CPU, 使用MKL(限intel处理器)
bazel build --config=opt --config=mkl //tensorflow/tools/pip_package:build_pip_package
# 仅CPU, 使用MKL,且CPU是Intel XEON或phi处理器
bazel build --config=opt --config=mkl --copt="-DEIGEN_USE_VML" //tensorflow/tools/pip_package:build_pip_package
# 启用CUDA
bazel build --config=opt --config=mkl --copt="-DEIGEN_USE_VML" --config=cuda //tensorflow/tools/pip_package:build_pip_package
# INTEL CPU + CUDA
bazel build --config=opt --config=mkl --config=cuda //tensorflow/tools/pip_package:build_pip_package

bazel-bin/tensorflow/tools/pip_package/build_pip_package /tmp/tensorflow_pkg
sudo pip uninstall tensorflow
sudo pip install /tmp/tensorflow_pkg/tensorflow-XXX.whl

Ubuntu 系统源码安装:

1
2
3
4
5
6
7
8
9
10
11
12
13
conda create -y -n tensorflow python=2.7
source activate tensorflow
sudo /opt/miniconda/envs/tn/bin/pip install -i https://pypi.tuna.tsinghua.edu.cn/simple numpy
git clone https://github.com/tensorflow/tensorflow
#git checkout r1.4
sudo apt-get install -y openjdk-8-jdk
sudo add-apt-repository ppa:webupd8team/java
sudo apt-get update && sudo apt-get install -y oracle-java8-installer
echo "deb [arch=amd64] http://storage.googleapis.com/bazel-apt stable jdk1.8" | sudo tee /etc/apt/sources.list.d/bazel.list
curl https://bazel.build/bazel-release.pub.gpg | sudo apt-key add -
sudo apt-get update && sudo apt-get install bazel
sudo apt-get install python-numpy python-dev python-pip python-wheel
bazel build --config=opt --config=mkl //tensorflow/tools/pip_package:build_pip_package

报错处理

Protobuf 版本报错

protobuf 如果版本不行,需要从源码进行安装:

1
2
3
4
5
6
7
8
9
yum install install autoconf automake libtool
git clone https://github.com/google/protobuf.git
cd protobuf/
./autogen.sh
./configure
make
make check
make install
export LD_LIBRARY_PATH=/usr/local/lib:/usr/lib:/usr/local/lib64:/usr/lib64

GLIBC_2.14 not found

问题:

1
/lib64/libc.so.6: version `GLIBC_2.14' not found

解决:

1
2
3
4
5
6
7
8
9
10
11
12
13
wget http://ftp.gnu.org/gnu/glibc/glibc-2.20.tar.gz  
wget http://ftp.gnu.org/gnu/glibc/glibc-ports-2.15.tar.gz
tar -xvf glibc-2.20.tar.gz
tar -xvf glibc-ports-2.15.tar.gz
mv glibc-ports-2.15 glibc-2.20/ports
mkdir glibc-build-2.20
cd glibc-build-2.20
# CentOS
../glibc-2.20/configure --prefix=/usr --disable-profile --enable-add-ons --with-headers=/usr/include --with-binutils=/usr/bin
# Ubuntu
../glibc-2.20/configure --prefix=/opt/glibc-2.20
sudo make -j 32
sudo make install

GLIBCXX_3.4.19 not found

问题:

1
/usr/lib64/libstdc++.so.6: version 'GLIBCXX_3.4.19' not found

解决:

CentOS升级gcc参考1

CentOS升级gcc参考2

Undefined symbol: PyUnicodeUCS2_FromStringAndSize

问题:

1
2
3
4
5
6
from scipy import misc
File "/usr/local/lib/python2.7/site-packages/scipy/__init__.py", line 114, in <module>
from scipy._lib._ccallback import LowLevelCallable
File "/usr/local/lib/python2.7/site-packages/scipy/_lib/_ccallback.py", line 1, in <module>
from . import _ccallback_c
ImportError: /usr/local/lib/python2.7/site-packages/scipy/_lib/_ccallback_c.so: undefined symbol: PyUnicodeUCS2_FromStringAndSize

解决:

1
2
pip uninstall scipy
pip install scipy

No such instruction: `rorx $48,%r12,%r12’

问题:

1
2
/tmp/ccH9f9L1.s: Assembler messages:
/tmp/ccH9f9L1.s:54: Error: no such instruction: `rorx $48,%r12,%r12'

解决:

1
bazel build --linkopt='-lrt' -c opt --copt=-mavx --copt=-msse4.2 --copt=-msse4.1 --copt=-mavx2 --copt=-mfma -k //tensorflow/tools/pip_package:build_pip_package