TensorFlow介绍与安装

1. 介绍

首先让我们来看看TensorFlow!

但是在我们开始之前,我们先来看看Python API中的TensorFlow代码,这样你就可以感受到我们怎么做的。

这段很短的 Python 程序生成了一些三维数据, 然后用一个平面拟合它。

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]

# Close the Session when we're done.
sess.close()

该代码的第一部分构建了数据流图。在创建会话并run 调用该函数之前,TensorFlow实际上不会运行任何计算。

为了进一步提高您的兴趣,我们建议您在TensorFlow中学习常见的机器学习问题。在神经网络的领域上,最经典的问题是MNIST手写数字分类问题。我们在这里为机器学习新手专业人士分别提供了两种教程。

如果您已经确定要学习并安装TensorFlow,可以跳过这些步骤,直接阅读后面的章节。
不用担心,我们会使用MNIST做为技术教程的示例来阐述TensorFlow的特性,所以您仍然可以看到MNIST。

2. 下载与安装

您可以从我们提供的二进制包或github源安装TensorFlow。

要求

TensorFlow支持Python 2.7和Python 3.3+。

GPU版本最好使用Cuda Toolkit 8.0和cuDNN v5。只有从源代码安装时,才支持其他版本(Cuda工具包> = 7.0和cuDNN> = v3)。有关详细信息,请参阅Cuda安装。对于Mac OS X,请参阅安装GPU for Mac

概述

你可以使用我们提供的 Pip, Docker, Virtualenv, Anaconda 或 源码编译的方法安装 TensorFlow。

如果您熟悉Pip,Virtualenv,Anaconda或Docker,请随时根据您的需求进行调整。
如果遇到安装错误,请参阅常见问题的解决方案。

基于Pip安装

Pip是用于安装和管理用Python编写的软件包的软件包管理系统。我们在Linux,Mac OS X和Windows上为TensorFlow提供pip软件包。有关Windows的说明,请参阅Windows上的Pip安装

首先安装 pip (或 Python3 的 pip3 ):

# Ubuntu/Linux 64-bit
$ sudo apt-get install python-pip python-dev

# Mac OS X
$ sudo easy_install pip
$ sudo easy_install --upgrade six

我们也将CPU版本的二进制文件上传到Pypi,因此您可以简单地在Linux,Mac或Windows上pip安装TensorFlow:

$ pip install tensorflow

请注意,您的pip版本需要在8.1或更高版本才能让上述命令在Linux上工作。

对于Windows用户,您还可以使用以下方式安装GPU版本的二进制文件: bash $ pip install tensorflow-gpu
由于其大小超过Pypi限制,所以此命令尚不适用于在Linux或Mac安装GPU版本的二进制文件。

如果上述命令不能在您的系统上运行,或者您想在Linux或Mac上安装GPU版本的二进制文件,则可以按照以下步骤进行操作:

# Ubuntu/Linux 64-bit, CPU only, Python 2.7
$ export TF_BINARY_URL=https://storage.googleapis.com/tensorflow/linux/cpu/tensorflow-0.12.0rc1-cp27-none-linux_x86_64.whl

# Ubuntu/Linux 64-bit, GPU enabled, Python 2.7
# Requires CUDA toolkit 8.0 and CuDNN v5. For other versions, see "Installing from sources" below.
$ export TF_BINARY_URL=https://storage.googleapis.com/tensorflow/linux/gpu/tensorflow_gpu-0.12.0rc1-cp27-none-linux_x86_64.whl

# Mac OS X, CPU only, Python 2.7:
$ export TF_BINARY_URL=https://storage.googleapis.com/tensorflow/mac/cpu/tensorflow-0.12.0rc1-py2-none-any.whl

# Mac OS X, GPU enabled, Python 2.7:
$ export TF_BINARY_URL=https://storage.googleapis.com/tensorflow/mac/gpu/tensorflow_gpu-0.12.0rc1-py2-none-any.whl

# Ubuntu/Linux 64-bit, CPU only, Python 3.4
$ export TF_BINARY_URL=https://storage.googleapis.com/tensorflow/linux/cpu/tensorflow-0.12.0rc1-cp34-cp34m-linux_x86_64.whl

# Ubuntu/Linux 64-bit, GPU enabled, Python 3.4
# Requires CUDA toolkit 8.0 and CuDNN v5. For other versions, see "Installing from sources" below.
$ export TF_BINARY_URL=https://storage.googleapis.com/tensorflow/linux/gpu/tensorflow_gpu-0.12.0rc1-cp34-cp34m-linux_x86_64.whl

# Ubuntu/Linux 64-bit, CPU only, Python 3.5
$ export TF_BINARY_URL=https://storage.googleapis.com/tensorflow/linux/cpu/tensorflow-0.12.0rc1-cp35-cp35m-linux_x86_64.whl

# Ubuntu/Linux 64-bit, GPU enabled, Python 3.5
# Requires CUDA toolkit 8.0 and CuDNN v5. For other versions, see "Installing from sources" below.
$ export TF_BINARY_URL=https://storage.googleapis.com/tensorflow/linux/gpu/tensorflow_gpu-0.12.0rc1-cp35-cp35m-linux_x86_64.whl

# Mac OS X, CPU only, Python 3.4 or 3.5:
$ export TF_BINARY_URL=https://storage.googleapis.com/tensorflow/mac/cpu/tensorflow-0.12.0rc1-py3-none-any.whl

# Mac OS X, GPU enabled, Python 3.4 or 3.5:
$ export TF_BINARY_URL=https://storage.googleapis.com/tensorflow/mac/gpu/tensorflow_gpu-0.12.0rc1-py3-none-any.whl

安装TensorFlow:

# Python 2
$ sudo pip install --upgrade $TF_BINARY_URL

# Python 3
$ sudo pip3 install --upgrade $TF_BINARY_URL

注意:如果之前安装过 TensorFlow < 0.7.1 的版本,应该先使用 pip uninstall 卸载 TensorFlow 和 protobuf ,保证获取的是一个最新 protobuf 依赖下的安装包。

安装完成之后,您就可以测试您的安装是否成功了。

在Windows上Pip安装

TensorFlow在Windows上仅支持64位Python 3.5。我们已经使用以下Python版本测试了pip软件包:

注意:TensorFlow要求MSVCP140.DLL文件,它可能不会安装在您的系统上。如果当您import tensorflow as tf遇到错误No module named "_pywrap_tensorflow"和/或DLL load failed,请检查MSVCP140.DLL是否在您的%PATH%,如果不是,您应该安装Visual C ++ 2015可再发行组件 (x64版本)。

下面两种命令进行pip安装。

只是安装CPU版本的TensorFlow,请在命令提示符下输入以下命令:C:\> pip install --upgrade https://storage.googleapis.com/tensorflow/windows/cpu/tensorflow-0.12.0rc1-cp35-cp35m-win_amd64.whl

要安装GPU版本的TensorFlow,请在命令提示符下输入以下命令:C:\> pip install --upgrade https://storage.googleapis.com/tensorflow/windows/gpu/tensorflow_gpu-0.12.0rc1-cp35-cp35m-win_amd64.whl

安装完成之后,您就可以测试您的安装是否成功了。

您还可以使用Virtualenv或Anaconda环境来管理在Windows上安装TensorFlow。

基于Virtualenv安装

Virtualenv是为不同Python项目创建一个隔离的容器的工具。利用Virtualenv安装TensorFlow不会影响原先存在的的Python环境。

使用Virtualenv安装步骤如下:

  • 安装pip和Virtualenv。
  • 创建一个Virtualenv环境。
  • 激活Virtualenv环境并在其中安装TensorFlow。
  • 安装完成后,您将在每次要使用TensorFlow时激活Virtualenv环境。

安装pip和Virtualenv:

# Ubuntu/Linux 64-bit
$ sudo apt-get install python-pip python-dev python-virtualenv

# Mac OS X
$ sudo easy_install pip
$ sudo pip install --upgrade virtualenv

在目录中创建一个Virtualenv环境~/tensorflow:

$ virtualenv --system-site-packages ~/tensorflow

激活环境:

$ source ~/tensorflow/bin/activate  # If using bash
$ source ~/tensorflow/bin/activate.csh  # If using csh
(tensorflow)$  # Your prompt should change

最后,Pip安装TensorFlow。

首先选择正确的二进制文件进行安装:

# Ubuntu/Linux 64-bit, CPU only, Python 2.7
(tensorflow)$ export TF_BINARY_URL=https://storage.googleapis.com/tensorflow/linux/cpu/tensorflow-0.12.0rc1-cp27-none-linux_x86_64.whl

# Ubuntu/Linux 64-bit, GPU enabled, Python 2.7
# Requires CUDA toolkit 8.0 and CuDNN v5. For other versions, see "Installing from sources" below.
(tensorflow)$ export TF_BINARY_URL=https://storage.googleapis.com/tensorflow/linux/gpu/tensorflow_gpu-0.12.0rc1-cp27-none-linux_x86_64.whl

# Mac OS X, CPU only, Python 2.7:
(tensorflow)$ export TF_BINARY_URL=https://storage.googleapis.com/tensorflow/mac/cpu/tensorflow-0.12.0rc1-py2-none-any.whl

# Mac OS X, GPU enabled, Python 2.7:
(tensorflow)$ export TF_BINARY_URL=https://storage.googleapis.com/tensorflow/mac/gpu/tensorflow_gpu-0.12.0rc1-py2-none-any.whl

# Ubuntu/Linux 64-bit, CPU only, Python 3.4
(tensorflow)$ export TF_BINARY_URL=https://storage.googleapis.com/tensorflow/linux/cpu/tensorflow-0.12.0rc1-cp34-cp34m-linux_x86_64.whl

# Ubuntu/Linux 64-bit, GPU enabled, Python 3.4
# Requires CUDA toolkit 8.0 and CuDNN v5. For other versions, see "Installing from sources" below.
(tensorflow)$ export TF_BINARY_URL=https://storage.googleapis.com/tensorflow/linux/gpu/tensorflow_gpu-0.12.0rc1-cp34-cp34m-linux_x86_64.whl

# Ubuntu/Linux 64-bit, CPU only, Python 3.5
(tensorflow)$ export TF_BINARY_URL=https://storage.googleapis.com/tensorflow/linux/cpu/tensorflow-0.12.0rc1-cp35-cp35m-linux_x86_64.whl

# Ubuntu/Linux 64-bit, GPU enabled, Python 3.5
# Requires CUDA toolkit 8.0 and CuDNN v5. For other versions, see "Installing from sources" below.
(tensorflow)$ export TF_BINARY_URL=https://storage.googleapis.com/tensorflow/linux/gpu/tensorflow_gpu-0.12.0rc1-cp35-cp35m-linux_x86_64.whl

# Mac OS X, CPU only, Python 3.4 or 3.5:
(tensorflow)$ export TF_BINARY_URL=https://storage.googleapis.com/tensorflow/mac/cpu/tensorflow-0.12.0rc1-py3-none-any.whl

# Mac OS X, GPU enabled, Python 3.4 or 3.5:
(tensorflow)$ export TF_BINARY_URL=https://storage.googleapis.com/tensorflow/mac/gpu/tensorflow_gpu-0.12.0rc1-py3-none-any.whl

最后安装TensorFlow:

# Python 2
(tensorflow)$ pip install --upgrade $TF_BINARY_URL

# Python 3
(tensorflow)$ pip3 install --upgrade $TF_BINARY_URL

在Virtualenv环境激活后,您现在可以测试您的安装。

当您完成使用TensorFlow后,停用环境。

(tensorflow)$ deactivate

$  # Your prompt should change back

要使用TensorFlow,您将需要再次激活Virtualenv环境:

$ source ~/tensorflow/bin/activate  # If using bash.
$ source ~/tensorflow/bin/activate.csh  # If using csh.
(tensorflow)$  # Your prompt should change.
# Run Python programs that use TensorFlow.
...
# When you are done using TensorFlow, deactivate the environment.
(tensorflow)$ deactivate

基于Anaconda安装

Anaconda是一个集成许多第三方科学计算库的 Python 科学计算环境。Anaconda使用一个名为“conda”的包管理器,它具有与Virtualenv相似的环境系统

与Virtualenv一样,不同Python 工程需要的依赖包,conda 将他们存储在不同的地方。利用Anconda安装TensorFlow不会影响原先存在的的Python环境。

  • 安装Anaconda。
  • 创建一个conda环境。
  • 激活conda环境并在其中安装TensorFlow。
  • 安装完成后,每次要使用TensorFlow时,都需要激活环境。
  • 可选地将ipython和其他软件包安装到conda环境中。

安装Anaconda:

按照Anaconda下载站点上的说明进行操作。

注意:如果已经通过Anaconda环境以外的pip安装TensorFlow,但想在Anaconda环境中使用TensorFlow,就需要先卸载之前在环境以外的利用pip安装的TensorFlow,因为Anaconda从.local更高优先级搜索系统site-packages 。

Python 2

$ pip uninstall tensorflow

Python 3

$ pip3 uninstall tensorflow 

创建一个名为tensorflow的conda环境:

# 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

激活环境,并在其中使用conda或pip安装TensorFlow。

使用conda

目前只有CPU版本的TensorFlow可用,可以安装在Python 2或Python 3的conda环境中。

$ 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

使用pip

如果使用pip进行安装,请确保使用--ignore-installed选项来防止出现easy_install的错误提示。

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

现在,像普通Pip安装一样安装TensorFlow。首先选择正确的二进制文件进行安装:

# Ubuntu/Linux 64-bit, CPU only, Python 2.7
(tensorflow)$ export TF_BINARY_URL=https://storage.googleapis.com/tensorflow/linux/cpu/tensorflow-0.12.0rc1-cp27-none-linux_x86_64.whl

# Ubuntu/Linux 64-bit, GPU enabled, Python 2.7
# Requires CUDA toolkit 8.0 and CuDNN v5. For other versions, see "Installing from sources" below.
(tensorflow)$ export TF_BINARY_URL=https://storage.googleapis.com/tensorflow/linux/gpu/tensorflow_gpu-0.12.0rc1-cp27-none-linux_x86_64.whl

# Mac OS X, CPU only, Python 2.7:
(tensorflow)$ export TF_BINARY_URL=https://storage.googleapis.com/tensorflow/mac/cpu/tensorflow-0.12.0rc1-py2-none-any.whl

# Mac OS X, GPU enabled, Python 2.7:
(tensorflow)$ export TF_BINARY_URL=https://storage.googleapis.com/tensorflow/mac/gpu/tensorflow_gpu-0.12.0rc1-py2-none-any.whl

# Ubuntu/Linux 64-bit, CPU only, Python 3.4
(tensorflow)$ export TF_BINARY_URL=https://storage.googleapis.com/tensorflow/linux/cpu/tensorflow-0.12.0rc1-cp34-cp34m-linux_x86_64.whl

# Ubuntu/Linux 64-bit, GPU enabled, Python 3.4
# Requires CUDA toolkit 8.0 and CuDNN v5. For other versions, see "Installing from sources" below.
(tensorflow)$ export TF_BINARY_URL=https://storage.googleapis.com/tensorflow/linux/gpu/tensorflow_gpu-0.12.0rc1-cp34-cp34m-linux_x86_64.whl

# Ubuntu/Linux 64-bit, CPU only, Python 3.5
(tensorflow)$ export TF_BINARY_URL=https://storage.googleapis.com/tensorflow/linux/cpu/tensorflow-0.12.0rc1-cp35-cp35m-linux_x86_64.whl

# Ubuntu/Linux 64-bit, GPU enabled, Python 3.5
# Requires CUDA toolkit 8.0 and CuDNN v5. For other versions, see "Installing from sources" below.
(tensorflow)$ export TF_BINARY_URL=https://storage.googleapis.com/tensorflow/linux/gpu/tensorflow_gpu-0.12.0rc1-cp35-cp35m-linux_x86_64.whl

# Mac OS X, CPU only, Python 3.4 or 3.5:
(tensorflow)$ export TF_BINARY_URL=https://storage.googleapis.com/tensorflow/mac/cpu/tensorflow-0.12.0rc1-py3-none-any.whl

# Mac OS X, GPU enabled, Python 3.4 or 3.5:
(tensorflow)$ export TF_BINARY_URL=https://storage.googleapis.com/tensorflow/mac/gpu/tensorflow_gpu-0.12.0rc1-py3-none-any.whl

最后安装TensorFlow:

# Python 2
(tensorflow)$ pip install --ignore-installed --upgrade $TF_BINARY_URL

# Python 3
(tensorflow)$ pip3 install --ignore-installed --upgrade $TF_BINARY_URL

使用方法

在环境环境激活后,您现在可以测试您的安装。

当您完成使用TensorFlow后,退出环境。

(tensorflow)$ source deactivate

$  # Your prompt should change back

要使用TensorFlow,您需要重新激活conda环境:

$ 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

安装IPython

要使用带有IPython的TensorFlow环境,可能需要将IPython安装到环境中:

$ source activate tensorflow
(tensorflow)$ conda install ipython

同样,像pandas一样的其他Python包可能需要安装到环境中才能与TensorFlow一起使用。

Docker安装

Docker是一种在Linux操作系统上运行的自建版本的虚拟机。当您通过Docker安装和运行TensorFlow时,将与机器上预先安装存在的包完全隔离。

我们提供4个Docker镜像:

  • gcr.io/tensorflow/tensorflow:TensorFlow CPU二进制镜像。
  • gcr.io/tensorflow/tensorflow:latest-develCPU二进制镜像加源码。
  • gcr.io/tensorflow/tensorflow:latest-gpu:TensorFlow GPU二进制镜像。
  • gcr.io/tensorflow/tensorflow:latest-devel-gpu:GPU二进制镜像加源代码。

我们也有标签latest替换为已发布的版本(例如, 0.12.0-rc1-gpu)。

Docker的安装如下:

  • 在您的机器上安装Docker。
  • 创建一个Docker group以允许启动容器。
  • 启动包含TensorFlow映像的Docker容器。首次启动时,镜像会自动下载。

有关在您的计算机上安装Docker的说明,请参阅安装Docker

Docker安装完成后,启动包含TensorFlow映像的Docker容器。

$ docker run -it -p 8888:8888 gcr.io/tensorflow/tensorflow

该选项-p 8888:8888用于将Docker容器内部端口发布到主机,在这种情况下可以确保Jupyter notebook的连接。

端口映射的格式是hostPort:containerPort。您可以为主机端口指定任何有效的端口号,但必须将8888用于容器端口部分。

如果您正在使用具有GPU支持的容器,则必须传递一些附加选项来将GPU设备公开到容器。

NVidia GPU支持安装最新的NVidia驱动程序和nvidia-docker

$ nvidia-docker run -it -p 8888:8888 gcr.io/tensorflow/tensorflow:latest-gpu

如果您运行nvidia-docker有任何问题,就使用默认配置运行,我们会在安装位置中植入一个带选项的脚本,如下列命令行所示的。

$ path/to/repo/tensorflow/tools/docker/docker_run_gpu.sh -p 8888:8888 gcr.io/tensorflow/tensorflow:latest-gpu

有关详细信息,请参阅TensorFlow docker readme

您现在可以在Docker容器内测试您的安装了。

测试TensorFlow的安装

启用GPU支持(LINUX,选项)

如果您安装了TensorFlow的GPU版本,还必须安装Cuda Toolkit 8.0和cuDNN v5。请看Cuda安装

您还需要设置LD_LIBRARY_PATH和CUDA_HOME环境变量。将以下命令添加到您的~/.bash_profile文件中。下面的命令假设您的CUDA安装在/usr/local/cuda目录下:

export LD_LIBRARY_PATH="$LD_LIBRARY_PATH:/usr/local/cuda/lib64:/usr/local/cuda/extras/CUPTI/lib64"
export CUDA_HOME=/usr/local/cuda

从命令行运行TensorFlow

如果发生错误,请参阅常见问题

打开terminal并键入以下内容:

$ python
...
>>> import tensorflow as tf
>>> hello = tf.constant('Hello, TensorFlow!')
>>> sess = tf.Session()
>>> print(sess.run(hello))
Hello, TensorFlow!
>>> a = tf.constant(10)
>>> b = tf.constant(32)
>>> print(sess.run(a + b))
42
>>>

从源代码安装

从源代码安装时,您将构建一个pip wheel,然后使用pip安装。

Windows上从源代码安装TensorFlow,您可以在Windows上使用BazelTensorFlow CMake build的支持。

克隆TensorFlow仓库

$ git clone https://github.com/tensorflow/tensorflow

请注意,上述代码将安装主分支上最新的TensorFlow。如果要安装特定的分支(如发行版分支),请传递-b <branchname>给git clone命令和--recurse-submodules给TensorFlow所依赖的r0.8及更早版本的protobuf库。

Linux环境准备

安装Bazel

首先按照教程安装bazel的依赖。然后下载适合您操作系统的最新稳定版的bazel,最后按照下面脚本执行:

$ chmod +x PATH_TO_INSTALL.SH
$ ./PATH_TO_INSTALL.SH --user

注意把 PATH_TO_INSTALL.SH 替换为你下载的安装包的文件路径。

安装其他依赖

# For Python 2.7:
$ sudo apt-get install python-numpy python-dev python-wheel python-mock
# For Python 3.x:
$ sudo apt-get install python3-numpy python3-dev python3-wheel python3-mock

可选:安装CUDA(在Linux上启用GPU支持)

为了编译并运行具有GPU支持的TensorFlow,需要安装NVIDIA的Cuda Toolkit(>=7.0)和cuDNN(>=v3)。

TensorFlow GPU支持需要具有NVIDIA计算能力(> = 3.0)的GPU卡。支持的显卡包括:

  • NVidia Titan

  • NVidia Titan X

  • NVidia K20

  • NVidia K40

...

检查您的GPU卡的NVIDIA计算能力:https://developer.nvidia.com/cuda-gpus

下载并安装Cuda Toolkit:https://developer.nvidia.com/cuda-downloads

如果使用的是二进制版本,请安装8.0版。

安装Toolkit到如下路径:/usr/local/cuda。

下载并安装cuDNN:https://developer.nvidia.com/cudnn

下载cuDNN v5。

解压缩并将cuDNN文件复制到Toolkit目录中。假设安装了Toolkit到/usr/local/cuda,请运行以下命令(cuDNN版本设置为您下载的版本):

tar xvzf cudnn-8.0-linux-x64-v5.1-ga.tgz
sudo cp -P cuda/include/cudnn.h /usr/local/cuda/include/
sudo cp -P cuda/lib64/libcudnn* /usr/local/cuda/lib64/
sudo chmod a+r /usr/local/cuda/include/cudnn.h /usr/local/cuda/lib64/libcudnn*

可选:安装OpenCL(仅限实验和Linux系统)

为了通过OpenCL支持构建或运行TensorFlow,需要安装OpenCL(> = 1.2)和ComputeCpp(> = 0.1.1)。

TensorFlow只能利用支持OpenCL 1.2的加速器。支持的加速器包括:

  • AMD Fiji

  • AMD Hawaii

...

请注意,此支持目前是实验性的,生产不应依赖于它(后去会逐渐成熟)。

下载并安装OpenCL驱动程序

安装所需功能的OpenCL取决于你的环境。在Unbuntu 14.04,正常按照以下步骤进行安装:

sudo apt-get install ocl-icd-opencl-dev opencl-headers

您还需要为加速器本身安装驱动程序。我们已经测试了在Ubuntu 14.04上安装以下的AMD Fiji和Hawaii GPU驱动程序:

sudo apt-get install fglrx-core fglrx-dev
下载并安装ComputeCpp编译器

Codeplay的网站下载编译器,解压缩并将文件复制到如下的路径/usr/local/computecpp:

tar -xvzf ComputeCpp-CE-0.1.1-Ubuntu.14.04-64bit.tar.gz
sudo mkdir /usr/local/computecpp
sudo cp -R ComputeCpp-CE-0.1.1-Linux /usr/local/computecpp
sudo chmod -R a+r /usr/local/computecpp/
sudo chmod -R a+x /usr/local/computecpp/bin

Mac环境准备

我们建议使用homebrew来安装bazel依赖项,并使用easy_install或pip安装python依赖项。

依赖

按照这里的说明安装bazel的依赖项。然后可以使用homebrew来安装bazel:

$ brew install bazel

您可以使用easy_install或pip安装python依赖项。使用easy_install。

$ sudo easy_install -U six
$ sudo easy_install -U numpy
$ sudo easy_install wheel

我们还推荐使用IPython。

$ sudo easy_install ipython

可选:为Mac设置GPU支持

如果您打算构建GPU支持,则需要确保您通过homebrew安装GNU coreutils:

$ brew install coreutils

接下来,您将需要确保您已经安装了CUDA Toolkit。需要从NVIDIA或使用Homebrew Cask扩展下载适用于您的OSX版本的软件包 :

$ brew tap caskroom/cask
$ brew cask install cuda

一旦安装了CUDA Toolkit,您将需要添加以下内容到~/.bash_profile文件中来设置所需的环境变量:

export CUDA_HOME=/usr/local/cuda
export DYLD_LIBRARY_PATH="$DYLD_LIBRARY_PATH:$CUDA_HOME/lib"
export PATH="$CUDA_HOME/bin:$PATH"

最后,您还将安装CUDA Deep Neural Network (cuDNN v5)库。一旦您下载它到本地,您可以解压缩并将根目录和库移动到本地的CUDA Toolkit文件夹:

$ sudo mv include/cudnn.h /Developer/NVIDIA/CUDA-8.0/include/
$ sudo mv lib/libcudnn* /Developer/NVIDIA/CUDA-8.0/lib
$ sudo ln -s /Developer/NVIDIA/CUDA-8.0/lib/libcudnn* /usr/local/cuda/lib/

要验证CUDA安装,您可以构建并运行deviceQuery以确保安装成功。

$ cp -r /usr/local/cuda/samples ~/cuda-samples
$ pushd ~/cuda-samples
$ make
$ popd
$ ~/cuda-samples/bin/x86_64/darwin/release/deviceQuery

如果需要编译TensorFlow需要Xcode 7.3并且安装了CUDA 7.5,但是需要注意的是,Xcode 7.3尚不兼容CUDA 7.5。所以您可以升级到CUDA 8.0,或者安装并设置Xcode 7.2为默认值:

$ sudo xcode-select -s /Application/Xcode-7.2/Xcode.app

配置安装

从源码树的根路径运行configure脚本。配置脚本时候会询问您的Python解释器的路径,并允许(可选)配置CUDA库。

此步骤用于定位python和numpy的头文件。如果您安装了支持CUDA的GPU和Toolkit,则支持构建GPU支持。当提示是否构建TensorFlow的GPU支持,选择Y选项。

例如:

$ ./configure
Please specify the location of python. [Default is /usr/bin/python]:
Do you wish to build TensorFlow with Google Cloud Platform support? [y/N] N
No Google Cloud Platform support will be enabled for TensorFlow
Do you wish to build TensorFlow with GPU support? [y/N] y
Do you wish to build TensorFlow with OpenCL support? [y/N] N
GPU support will be enabled for TensorFlow
Please specify which gcc nvcc should use as the host compiler. [Default is /usr/bin/gcc]:
Please specify the Cuda SDK version you want to use, e.g. 7.0. [Leave empty to use system default]: 8.0
Please specify the location where CUDA 8.0 toolkit is installed. Refer to README.md for more details. [Default is /usr/local/cuda]:
Please specify the cuDNN version you want to use. [Leave empty to use system default]: 5
Please specify the location where cuDNN 5 library is installed. Refer to README.md for more details. [Default is /usr/local/cuda]:
Please specify a list of comma-separated Cuda compute capabilities you want to build with.
You can find the compute capability of your device at: https://developer.nvidia.com/cuda-gpus.
Please note that each additional compute capability significantly increases your build time and binary size.

Setting up Cuda include
Setting up Cuda lib
Setting up Cuda bin
Setting up Cuda nvvm
Setting up CUPTI include
Setting up CUPTI lib64
Configuration finished

这将创建一个规范的符号链接到您的Cuda库。每次您更改Cuda路径时,您需要在调用bazel构建命令之前再次运行此步骤。对于cuDNN库,R3使用“7.0”,R4使用“4.0.7”。

如果需要构建对OpenCL的支持,请在构建TensorFlow时选择OpenCL支持,并提供ComputeCpp编译器的位置(例如/usr/local/computecpp)。

已知的问题

  • 尽管可以在同一个源码树下编译开启 Cuda 支持和禁用 Cuda 支持的版本, 我们还是推荐在 在切换这两种不同的编译配置时, 使用 "bazel clean" 清理环境.。

  • 在执行 bazel 编译前必须先运行 configure, 否则编译会失败并提示错误信息. 未来, 我们可能考虑将 configure 步骤包含在编译过程中, 以简化整个过程, 前提是 bazel 能够提供新的特性支持这样。

创建pip包并安装

从源代码构建时,您仍然会构建一个pip包并进行安装。

请注意,默认情况下,源的构建需要大量内存资源,如果要限制RAM使用情况,可以在调用bazel时添加--local_resources 2048,.5,1.0。

$ bazel build -c opt //tensorflow/tools/pip_package:build_pip_package

# To build with GPU support:
$ bazel build -c opt --config=cuda //tensorflow/tools/pip_package:build_pip_package

$ bazel-bin/tensorflow/tools/pip_package/build_pip_package /tmp/tensorflow_pkg

# The name of the .whl file will depend on your platform.
$ sudo pip install /tmp/tensorflow_pkg/tensorflow-0.12.0rc1-py2-none-any.whl

优化CPU性能

为了与尽可能广泛的机器兼容,TensorFlow默认仅在x86机器上使用SSE4.1 SIMD指令。大多数现代的PC和Mac支持更高级的指令,所以如果你正在构建一个只能在自己的机器上运行的二进制文件,那么你可以通过--copt=-march=native在你的bazel build命令中使用这些二进制文件 。例如:

$ bazel build --copt=-march=native -c opt //tensorflow/tools/pip_package:build_pip_package

如果您正在分发二进制文件但知道要运行的计算机的功能,则可以手动选择正确的说明--copt=-march=avx。例如,您可能还想使用多个参数启用多个功能 --copt=-mavx2 --copt=-mfma。

如果在不支持SIMD的计算机上运行使用SIMD指令构建的二进制文件,则执行该代码时会发现非法指令错误。

设置TensorFlow的发展

如果您正在使用TensorFlow,则可以在交互式python shell中测试更改,而无需重新安装TensorFlow。

要设置TensorFlow的所有文件从链接到系统目录,请在TensorFlow根目录下运行以下命令:

bazel build -c opt //tensorflow/tools/pip_package:build_pip_package

# To build with GPU support:
bazel build -c opt --config=cuda //tensorflow/tools/pip_package:build_pip_package

mkdir _python_build
cd _python_build
ln -s ../bazel-bin/tensorflow/tools/pip_package/build_pip_package.runfiles/org_tensorflow/* .
ln -s ../tensorflow/tools/pip_package/* .
python setup.py develop

请注意,此设置仍需要您在每次更改C ++文件时重建//tensorflow/tools/pip_package:build_pip_package目标;添加,删除或移动任何python文件; 或者更改bazel构建规则。

还要注意,bazel test并不总是通过这些符号链接正确解决依赖关系,因此测试结果可能不可靠。解决方法是_python_build在运行之前删除bazel test目录。

训练您的第一个TensorFlow神经网络模型

首先从GitHub 克隆TensorFlow models repo。运行以下命令:

$ cd models/tutorials/image/mnist
$ python convolutional.py
Successfully downloaded train-images-idx3-ubyte.gz 9912422 bytes.
Successfully downloaded train-labels-idx1-ubyte.gz 28881 bytes.
Successfully downloaded t10k-images-idx3-ubyte.gz 1648877 bytes.
Successfully downloaded t10k-labels-idx1-ubyte.gz 4542 bytes.
Extracting data/train-images-idx3-ubyte.gz
Extracting data/train-labels-idx1-ubyte.gz
Extracting data/t10k-images-idx3-ubyte.gz
Extracting data/t10k-labels-idx1-ubyte.gz
Initialized!
Epoch 0.00
Minibatch loss: 12.054, learning rate: 0.010000
Minibatch error: 90.6%
Validation error: 84.6%
Epoch 0.12
Minibatch loss: 3.285, learning rate: 0.010000
Minibatch error: 6.2%
Validation error: 7.0%
...
...

常见问题

GPU 相关问题

如果在尝试运行一个 TensorFlow 程序时出现以下错误:

ImportError: libcudart.so.7.0: cannot open shared object file: No such file or directory

请确认你正确安装了GPU支持。

Protobuf库相关问题

TensorFlow的pip包依赖于protobuf 3.1.0版本的pip软件包。可以从PyPI(运行时pip install protobuf)下载的Protobuf的pip软件包,Python能利用它实现原型序列化/反序列化的,它比C ++实现慢10倍到50倍。Protobuf还支持包含基于快速C ++的原语解析的Python包的二进制扩展。此扩展在只有Python标准的PIP包中不可用。我们为protobuf创建了一个包含二进制扩展名的自定义二进制pip包。按照以下说明安装自定义二进制protobuf pip软件包:

# Ubuntu/Linux 64-bit:
$ pip install --upgrade https://storage.googleapis.com/tensorflow/linux/cpu/protobuf-3.0.0-cp27-none-linux_x86_64.whl

# Mac OS X:
$ pip install --upgrade https://storage.googleapis.com/tensorflow/mac/cpu/protobuf-3.0.0-cp27-cp27m-macosx_10_11_x86_64.whl

对于Python 3.5

# Ubuntu/Linux 64-bit:
$ pip3 install --upgrade https://storage.googleapis.com/tensorflow/linux/cpu/protobuf-3.0.0-cp35-cp35m-linux_x86_64.whl

# Mac OS X:
$ pip3 install --upgrade https://storage.googleapis.com/tensorflow/mac/cpu/protobuf-3.0.0-cp35-cp35m-macosx_10_11_x86_64.whl

如果您的系统/配置未列在上面,您可以使用以下说明来构建您自己的protobuf wheel文件。安装之前,请参阅这里

然后:

$ git clone https://github.com/google/protobuf.git
$ cd protobuf
$ ./autogen.sh
$ CXXFLAGS="-fPIC -g -O2" ./configure
$ make -j12
$ export PROTOC=$PWD/src/protoc
$ cd python
$ python setup.py bdist_wheel --cpp_implementation --compile_static_extension
$ pip uninstall protobuf
$ pip install dist/<wheel file name>

在安装TensorFlow之后,通过pip安装上面的软件包,因为标准地进行pip安装tensorflow,只能安装python支持的pip软件包。上述pip包将覆盖现有的protobuf包。
请注意,二进制pip包最大支持已经大于64MB的protobuf,应该修复以下错误:

[libprotobuf ERROR google/protobuf/src/google/protobuf/io/coded_stream.cc:207] A
protocol message was rejected because it was too big (more than 67108864 bytes).
To increase the limit (or to disable these warnings), see
CodedInputStream::SetTotalBytesLimit() in google/protobuf/io/coded_stream.h.

Pip安装问题

Cannot import name 'descriptor'

ImportError: Traceback (most recent call last):
  File "/usr/local/lib/python3.4/dist-packages/tensorflow/core/framework/graph_pb2.py", line 6, in <module>
    from google.protobuf import descriptor as _descriptor
ImportError: cannot import name 'descriptor'

如果升级到较新版本的TensorFlow时出现上述错误,请尝试卸载TensorFlow和protobuf(如果已安装)并重新安装TensorFlow(也请安装正确的protobuf依赖项)。

找不到setup.py

如果,在pip install,你遇到错误如下:

...
IOError: [Errno 2] No such file or directory: '/tmp/pip-o6Tpui-build/setup.py'

解决方案:升级您的PIP版本:

pip install --upgrade pip

这可能需要sudo,取决于以前如何安装PIP的。

SSLError:SSL_VERIFY_FAILED

如果在从网址安装pip的过程中遇到如下错误:

...
SSLError: [SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed

解决方案:通过curl或wget手动下载wheel文件,并在本地pip安装。

Operation not permitted

...
Installing collected packages: setuptools, protobuf, wheel, numpy, tensorflow
Found existing installation: setuptools 1.1.6
Uninstalling setuptools-1.1.6:
Exception:
...
[Errno 1] Operation not permitted: '/tmp/pip-a1DXRT-uninstall/System/Library/Frameworks/Python.framework/Versions/2.7/Extras/lib/python/_markerlib'

解决方案:在pip命令中添加--ignore-installed选项。

Cannot remove entries from nonexistent file: easy-install.pth

Cannot remove entries from nonexistent file <path-to-anaconda-instalation>/anaconda[version]/lib/site-packages/easy-install.pth
  1. 升级安装工具:pip install --upgrade -I setuptools

  2. 安装TensorFlow再添加--ignore-installed选项:pip install --ignore-installed --upgrade <tensorflow_url>

步骤1可能已经解决了问题,但如果问题仍然存在,请执行步骤2。

在 Linux 上

如果出现错误:

...
 "__add__", "__radd__",
             ^
SyntaxError: invalid syntax

解决方案: 确认正在使用的 Python 版本为 Python 2.7.

在 Mac OS X 上

如果出现错误:

import six.moves.copyreg as copyreg
ImportError: No module named copyreg

解决方案: TensorFlow 使用的 protobuf 依赖 six-1.10.0. 但是, Apple 的默认 python 环境 已经安装了 six-1.4.1, 该版本可能很难升级. 这里提供几种方法来解决该问题:

  • 升级全系统的 six:
$ sudo easy_install -U six
  • 通过隔离环境安装TensorFlow:

使用VIRTUALENV或者使用Docker安装。

  • 通过Homebrew或 MacPorts安装隔离的Python环境,并在该版本的Python中重新安装TensorFlow。

3. 基本用法

使用 TensorFlow, 你必须明白 TensorFlow:

  • 使用图(graph)来表示计算任务

  • 在被称之为会话(Session)的上下文(context)中执行图

  • 使用 tensor 表示数据

  • 通过变量(Variable)维护状态

  • 使用feed和fetch可以为任意的操作(arbitrary operation)赋值或者从其中获取数据

概述

TensorFlow是一个编程系统,使用图(graph)来表示计算任务。图中的节点称为op(operation)。一个op取零个或多个Tensors来执行计算,并产生零个或多个Tensors。在TensorFlow术语中,一个Tensor是一个类型化的多维数组。例如,您可以将一小组的图像表示为一个4维的浮点数数组,这4个维度分别是[batch, height, width, channels]。

一个TensorFlow图(graph)描述了计算的过程。为了进行计算图(graph)必须在会话(Session)中启动。一个会话(Session)将图(graph)的ops分发到诸如GPUs或者CPUs的设备上,同时提供执行op的方法。这些方法后,将产生的tensor返回。在Python中,返回的tensor是numpy的ndarry对象;在C或者C++中,返回的tensor是tensorflow::Tensor 实例。

计算图(graph)

TensorFlow 程序通常被组织成一个构建阶段和一个执行阶段。 在构建阶段,op的执行步骤被描述成一个图(graph),在执行阶段, 使用会话执行执行图中的op。

例如, 通常在构建阶段创建一个图(graph)来表示和训练神经网络, 然后在执行阶段反复执行图(graph)中的训练op。

TensorFlow支持C,C++,Python编程语言。目前,TensorFlow的Python 库更加易用,它提供了大量的辅助函数来简化构建图的工作,但这些函数尚未被C和C++库支持。

三种语言的会话库(session libraries)是一样的。

构建图(graph)

构建图(graph)的第一步是创建一个不需要任何输入的源op,力图常量(constant)。源op的输出传递给其它op进行计算。

Python库中的op构造函数返回表示构造的ops的输出的对象。您可以将这些作为输入传递给其他op构造函数。

Python的TensorFlow库有一个默认图(default graph),op构造器可以为其增加节点。这个默认图对许多程序来说已经足够用了。阅读Graph类文档来了解如何管理多个图。

import tensorflow as tf

# Create a constant op that produces a 1x2 matrix.  The op is
# added as a node to the default graph.
#
# The value returned by the constructor represents the output
# of the constant op.
matrix1 = tf.constant([[3., 3.]])

# Create another constant that produces a 2x1 matrix.
matrix2 = tf.constant([[2.],[2.]])

# Create a matmul op that takes 'matrix1' and 'matrix2' as inputs.
# The returned value, 'product', represents the result of the matrix
# multiplication.
product = tf.matmul(matrix1, matrix2)

上述默认图现在有三个节点:两个常量(constant)op, 和一个矩阵乘法(matmul)op。为了真正进行矩阵相乘运算,并得到矩阵乘法的结果,你必须在会话里启动这个图。

在一个会话(Session)中启动图(graph)

构造阶段完成后,才能启动图。启动图的第一步是创建一个Session对象,如果无任何创建参数,会话构造器将启动默认图。

欲了解完整的会话 API,请阅读Session类

# Launch the default graph.
sess = tf.Session()

# To run the matmul op we call the session 'run()' method, passing 'product'
# which represents the output of the matmul op.  This indicates to the call
# that we want to get the output of the matmul op back.
#
# All inputs needed by the op are run automatically by the session.  They
# typically are run in parallel.
#
# The call 'run(product)' thus causes the execution of three ops in the
# graph: the two constants and matmul.
#
# The output of the matmul is returned in 'result' as a numpy `ndarray` object.
result = sess.run(product)
print(result)
# ==> [[ 12.]]

# Close the Session when we're done.
sess.close()

Session对象在使用完后需要关闭以释放资源。除了显式调用close外,也可以使用with语句来自动完成关闭动作。

with tf.Session() as sess:
  result = sess.run([product])
  print(result)

在实现上,TensorFlow将图(graph)定义转换成分布式执行的操作,以充分利用可用的计算资源(如CPU或GPU)。一般你不需要显式指定使用CPU还是GPU,TensorFlow能自动检测。如果检测到GPU,TensorFlow会尽可能地利用找到的第一个GPU来执行操作。

如果机器上有超过一个可用的GPU,除第一个外的其它GPU默认是不参与计算的。为了让TensorFlow使用这些GPU,你必须将op明确指派给它们执行。with...Device语句用来指派特定的CPU或GPU执行操作:

with tf.Session() as sess:
  with tf.device("/gpu:1"):
    matrix1 = tf.constant([[3., 3.]])
    matrix2 = tf.constant([[2.],[2.]])
    product = tf.matmul(matrix1, matrix2)
    ...

设备用字符串进行标识。目前支持的设备包括:

  • "/cpu:0":机器的CPU
  • "/gpu:0":机器的第一个 GPU,如果有的话
  • "/gpu:1":机器的第二个 GPU,以此类推

阅读使用GPU章节,了解TensorFlow GPU使用的更多信息。

在分布式会话(Session)中启动图(graph)

要创建TensorFlow集群,请启动集群中的每台计算机上TensorFlow服务器。当您在客户端中实例化一个会话(Session)时,可以将其中一台机器的网络位置传递给集群:

with tf.Session("grpc://example.org:2222") as sess:
  # Calls to sess.run(...) will be executed on the cluster.
  ...

则该机器成为会话(Session)的主机。主人在集群(工作人员)中的其他机器上分发图(graph),就像利用本地机器的可用计算资源来实现图(graph)。

您可以使用“with tf.device():”语句直接指定图形中指定部分的工作人员:

with tf.device("/job:ps/task:0"):
  weights = tf.Variable(...)
  biases = tf.Variable(...)

有关分布式会话和集群的更多信息,请参阅TensorFlow分布式会话和集群

交互式使用

文档中的Python示例使用一个会话Session来启动图,并调用Session.run()方法执行操作。

为了便于使用诸如IPython之类的Python交互环境,可以使用InteractiveSession代替Session类, 使用Tensor.eval()和Operation.run()方法代替Session.run()。这样就可以避免使用一个变量来保存会话。

# Enter an interactive TensorFlow Session.
import tensorflow as tf
sess = tf.InteractiveSession()

x = tf.Variable([1.0, 2.0])
a = tf.constant([3.0, 3.0])

# Initialize 'x' using the run() method of its initializer op.
x.initializer.run()

# Add an op to subtract 'a' from 'x'.  Run it and print the result
sub = tf.sub(x, a)
print(sub.eval())
# ==> [-2. -1.]

# Close the Session when we're done.
sess.close()

Tensor

TensorFlow程序使用tensor数据结构来代表所有的数据,计算图(graph)的时候,操作间传递的数据都是tensor。你可以把TensorFlow的tensor 看作是一个n维的数组或列表。一个tensor包含一个静态类型rank和一个shape。想了解TensorFlow关于这些概念的信息,参见Rank,Shape和Type

变量(Variables)

变量(Variables)保持图(graph)执行过程中的状态。下面的例子演示了如何使用变量(Variables)实现一个简单的计数器。参见变量(Variables)章节了解更多细节。

# Create a Variable, that will be initialized to the scalar value 0.
state = tf.Variable(0, name="counter")

# Create an Op to add one to `state`.

one = tf.constant(1)
new_value = tf.add(state, one)
update = tf.assign(state, new_value)

# Variables must be initialized by running an `init` Op after having
# launched the graph.  We first have to add the `init` Op to the graph.
init_op = tf.global_variables_initializer()

# Launch the graph and run the ops.
with tf.Session() as sess:
  # Run the 'init' op
  sess.run(init_op)
  # Print the initial value of 'state'
  print(sess.run(state))
  # Run the op that updates 'state' and print 'state'.
  for _ in range(3):
    sess.run(update)
    print(sess.run(state))

# output:

# 0
# 1
# 2
# 3

代码中assign()操作是图(graph)所描绘表达式的一部分, 正如add()操作一样。所以在调用run()执行表达式之前,它并不会真正执行赋值操作。

通常会将一个统计模型中的参数表示为一组变量。例如,你可以将一个神经网络的权重作为某个变量存储在一个tensor中。在训练过程中,通过重复运行训练图,更新这个tensor.

Fetch

为了获取操作的输出内容,可以在使用Session对象的run()调用执行图时,传入一些tensor,这些tensor会帮助你获取结果。在之前的例子里,我们只取回了单个节点state,但是你也可以取回多个tensor:

input1 = tf.constant([3.0])
input2 = tf.constant([2.0])
input3 = tf.constant([5.0])
intermed = tf.add(input2, input3)
mul = tf.mul(input1, intermed)

with tf.Session() as sess:
  result = sess.run([mul, intermed])
  print(result)

# output:
# [array([ 21.], dtype=float32), array([ 7.], dtype=float32)]

需要获取的多个tensor值,在op的一次运行中会一起获得(而不是逐个去获取tensor)。

Feed

上述示例在计算图中引入了tensor,以常量或变量的形式存储。TensorFlow还提供了feed机制,该机制可以临时替代图(graph)中的任意操作中的tensor,可以对图(graph)中任何操作提交补丁,直接插入一个tensor。

feed使用一个tensor值临时替换一个操作的输出结果。你可以提供feed数据作为run()调用的参数。feed只在调用它的方法内有效。方法结束,feed就会消失。最常见的用例是将某些特殊的操作指定为feed操作,标记方法是使用tf.placeholder()为这些操作创建占位符。

input1 = tf.placeholder(tf.float32)
input2 = tf.placeholder(tf.float32)
output = tf.mul(input1, input2)

with tf.Session() as sess:
  print(sess.run([output], feed_dict={input1:[7.], input2:[2.]}))

# output:
# [array([ 14.], dtype=float32)]

如果没有正确提供feed,placeholder()操作将会产生错误。MNIST完全连接feed教程(source code) 给出了一个大规模的使用feed的例子。

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 159,015评论 4 362
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 67,262评论 1 292
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 108,727评论 0 243
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 43,986评论 0 205
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 52,363评论 3 287
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 40,610评论 1 219
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 31,871评论 2 312
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 30,582评论 0 198
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 34,297评论 1 242
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 30,551评论 2 246
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 32,053评论 1 260
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 28,385评论 2 253
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 33,035评论 3 236
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 26,079评论 0 8
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 26,841评论 0 195
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 35,648评论 2 274
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 35,550评论 2 270

推荐阅读更多精彩内容