大数据下基于Tensorflow框架的深度学习示例教程

近几年,信息时代的快速发展产生了海量数据,诞生了无数前沿的大数据技术与应用。在当今大数据时代的产业界,商业决策日益基于数据的分析作出。当数据膨胀到一定规模时,基于机器学习对海量复杂数据的分析更能产生较好的价值,而深度学习在大数据场景下更能揭示数据内部的逻辑关系。本文就以大数据作为场景,通过自底向上的教程详述在大数据架构体系中如何应用深度学习这一技术。大数据架构中采用的是hadoop系统以及Kerberos安全认证,深度学习采用的是分布式的Tensorflow架构,hadoop解决了大数据的存储问题,而分布式Tensorflow解决了大数据训练的问题。本教程是我们团队在开发基于深度学习的实时欺诈预警服务时,部署深度学习这一模块时总结出的经验,感兴趣的欢迎深入交流。

安装Tensorflow

我们安装Tensorflow选择的是Centos7,因为Tensorflow需要使用GNU发布的1.5版本的libc库,Centos6系统并不适用该版本库而被抛弃。对于如何联网在线安装Tensorflow,官网有比较详尽的教程。本教程着重讲一下网上资料较少的离线安装方式,系统的安装更需要在意的是各软件版本的一致性,下面教程也是解决了很多版本不一致的问题后给出的一个方案。首先我们先将整个系统搭建起来吧。

1.安装编程语言Python3.5:在官网下载软件并解压后执行如下安装命令:

  1. ./configure
  2. make
  3. make test
  4. sudo make install

2.安装基于Python的科学计算包python-numpy:在官网下载软件并解压后执行如下安装命令:

python setup.py install

3.安装Python模块管理的工具wheel:在官网下载软件后执行如下安装命令:

pip install wheel-0.30.0a0-py2.py3-none-any.whl

4.安装自动下载、构建、安装和管理 python 模块的工具setuptools:在官网下载软件并解压后执行如下安装命令:

python setup.py install

5.安装Python开发包python-devel:在官网下载软件后执行如下安装命令:

sudo rpm -i --nodeps python3-devel-3.5.2-4.fc25.x86_64.rpm

6.安装Python包安装管理工具six:在官网下载软件后执行如下安装命令:

sudo pip install six-1.10.0-py2.py3-none-any.whl

7.安装Java 开发环境JDK8:在官网下载软件并解压后执行如下移动命令:

mv java1.8 /usr/local/software/jdk

设置JDK的环境变量,编辑文件 .bashrc,加入下面内容

  1. export JAVA_HOME=/usr/local/software/jdk
  2. export JRE_HOME=${JAVA_HOME}/jre
  3. export CLASSPATH=$CLASSPATH:${JAVA_HOME}/lib:${JRE_HOME}/lib
  4. export PATH=$PATH:${JAVA_HOME}/bin

进行Java版本的切换,选择对应的版本

  1. sudo update-alternatives --config java
  2. sudo update-alternatives --config javac

8.安装Bazel:Bazel是一个类似于Make的工具,是Google为其内部软件开发的特点量身定制的工具,构建Tensorflow项目。在官网下载后执行如下安装命令:

  1. chmod +x bazel-0.4.3-installer-linux-x86_64.sh
  2. ./bazel-0.4.3-installer-linux-x86_64.sh –user

9.安装Tensorflow:在官网下载软件后执行如下安装命令:

pip install --upgrade tensorflow-0.12.1-cp35-cp35m-linux_x86_64.whl

Tensorflow访问HDFS的部署

1.首先安装Hadoop客户端,在官网下载后执行下面解压移动命令:

  1. tar zxvf hadoop-2.6.0.tar.gz
  2. mv hadoop-2.6.0.tar.gz /usr/local/software/Hadoop

进行环境变量的配置/etc/profile,加入如下内容

  1. export PATH=$PATH:/usr/local/software/hadoop/bin
  2. export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:$JAVA_HOME/jre/lib/amd64/server
  3. export HADOOP_HOME=/usr/local/software/hadoop
  4. export HADOOP_HDFS_HOME=/usr/local/software/hadoop

配置完后进行配置更新source /etc/profile

2.其次,安装完客户端后,配置自己的hadoop集群环境文件。

Tensorflow与Kerberos验证的部署

在Tesorflow0.12版本中已经支持了Kerberos验证,本机只要配置好Kerberos文件即可使用。该文中不详述Kerberos的配置内容,罗列一下相关的配置流程。

  • 首先在/etc/krb5.conf文件中进行服务器跟验证策略的配置;
  • 然后在Kerberos服务端生成一个用户文件传至本机;
  • 最后进行Kerberos客户端的权限认证并设置定时任务。

大数据场景下基于分布式Tensorflow的深度学习示例

一、进行数据格式的转换

本文的示例是做的MNIST数据的识别模型,为了更好的读取数据更好的利用内存,我们将本地GZ文件转换成Tensorflow的内定标准格式TFRecord,然后再将转换后的文件上传到HDFS存储。在实际应用中,我们实际利用Spark做了大规模格式转换的处理程序。我们对本地数据处理的相应的转换代码为:

  1. from __future__ import absolute_import
  2. from __future__ import division
  3. from __future__ import print_function
  4. import argparse
  5. import os
  6. import tensorflow as tf
  7. from tensorflow.contrib.learn.python.learn.datasets import mnist
  8. SOURCE_URL = 'http://yann.lecun.com/exdb/mnist/'
  9. TRAIN_IMAGES = 'train-images-idx3-ubyte.gz' # MNIST filenames
  10. TRAIN_LABELS = 'train-labels-idx1-ubyte.gz'
  11. TEST_IMAGES = 't10k-images-idx3-ubyte.gz'
  12. TEST_LABELS = 't10k-labels-idx1-ubyte.gz'
  13. FLAGS = None
  14. def _int64_feature(value):
  15. return tf.train.Feature(int64_list=tf.train.Int64List(value=[value]))
  16.  
  17. def _bytes_feature(value):
  18. return tf.train.Feature(bytes_list=tf.train.BytesList(value=[value]))
  19.  
  20. def convert_to(data_set, name):
  21. images = data_set.images
  22. labels = data_set.labels
  23. num_examples = data_set.num_examples
  24. if images.shape[0] != num_examples:
  25. raise ValueError('Images size %d does not match label size %d.' %
  26. (images.shape[0], num_examples))
  27. rows = images.shape[1]
  28. cols = images.shape[2]
  29. depth = images.shape[3]
  30. filename = os.path.join(FLAGS.directory, name + '.tfrecords')
  31. print('Writing', filename)
  32. writer = tf.python_io.TFRecordWriter(filename)
  33. for index in range(num_examples):
  34. image_raw = images[index].tostring()
  35. example = tf.train.Example(features=tf.train.Features(feature={
  36. 'height': _int64_feature(rows),
  37. 'width': _int64_feature(cols),
  38. 'depth': _int64_feature(depth),
  39. 'label': _int64_feature(int(labels[index])),
  40. 'image_raw': _bytes_feature(image_raw)}))
  41. writer.write(example.SerializeToString())
  42. writer.close()
  43.  
  44. def main(argv):
  45. # Get the data.
  46. data_sets = mnist.read_data_sets(FLAGS.directory,
  47. dtype=tf.uint8,
  48. reshape=False,
  49. validation_size=FLAGS.validation_size)
  50. # Convert to Examples and write the result to TFRecords.
  51. convert_to(data_sets.train, 'train')
  52. convert_to(data_sets.validation, 'validation')
  53. convert_to(data_sets.test, 'test')
  54. if __name__ == '__main__':
  55. parser = argparse.ArgumentParser()
  56. parser.add_argument(
  57. '--directory',
  58. type=str,
  59. default='/tmp/data',
  60. help='Directory to download data files and write the converted result'
  61. )
  62. parser.add_argument(
  63. '--validation_size',
  64. type=int,
  65. default=5000,
  66. help="""\
  67. Number of examples to separate from the training data for the validation
  68. set.\
  69. """
  70. )
  71. FLAGS = parser.parse_args()
  72. tf.app.run()

二、Tensorflow读取HDFS数据的设置

文中前面内容介绍了HDFS的配置以及将数据转换后存储到HDFS,Tensorflow读取HDFS时只需要简单的两步,首先执行项目时需要加入环境前缀:

CLASSPATH=$($HADOOP_HDFS_HOME/bin/hadoop classpath --glob) python example.py

其次读取数据时,需要在数据的路径前面加入HDFS前缀,比如:

hdfs://default/user/data/example.txt

三、分布式模型的示例代码

该示例代码是读取HDFS上的MNIST数据,建立相应的server与work集群构建出一个三层的深度网络,包含两层卷积层以及一层SoftMax层。代码如下:

  1. from __future__ import print_function
  2. import math
  3. import os
  4. import tensorflow as tf
  5. flags = tf.app.flags
  6. # Flags for configuring the task
  7. flags.DEFINE_string("job_name", None, "job name: worker or ps")
  8. flags.DEFINE_integer("task_index", 0,
  9. "Worker task index, should be >= 0. task_index=0 is "
  10. "the chief worker task the performs the variable "
  11. "initialization")
  12. flags.DEFINE_string("ps_hosts", "",
  13. "Comma-separated list of hostname:port pairs")
  14. flags.DEFINE_string("worker_hosts", "",
  15. "Comma-separated list of hostname:port pairs")
  16. # Training related flags
  17. flags.DEFINE_string("data_dir", None,
  18. "Directory where the mnist data is stored")
  19. flags.DEFINE_string("train_dir", None,
  20. "Directory for storing the checkpoints")
  21. flags.DEFINE_integer("hidden1", 128,
  22. "Number of units in the 1st hidden layer of the NN")
  23. flags.DEFINE_integer("hidden2", 128,
  24. "Number of units in the 2nd hidden layer of the NN")
  25. flags.DEFINE_integer("batch_size", 100, "Training batch size")
  26. flags.DEFINE_float("learning_rate", 0.01, "Learning rate")
  27. FLAGS = flags.FLAGS
  28. TRAIN_FILE = "train.tfrecords"
  29. NUM_CLASSES = 10
  30. IMAGE_SIZE = 28
  31. IMAGE_PIXELS = IMAGE_SIZE * IMAGE_SIZE
  32.  
  33. def inference(images, hidden1_units, hidden2_units):
  34. with tf.name_scope('hidden1'):
  35. weights = tf.Variable(
  36. tf.truncated_normal([IMAGE_PIXELS, hidden1_units],
  37. stddev=1.0 / math.sqrt(float(IMAGE_PIXELS))),name='weights')
  38. biases = tf.Variable(tf.zeros([hidden1_units]),name='biases')
  39. hidden1 = tf.nn.relu(tf.matmul(images, weights) + biases)
  40. with tf.name_scope('hidden2'):
  41. weights = tf.Variable(
  42. tf.truncated_normal([hidden1_units, hidden2_units],
  43. stddev=1.0 / math.sqrt(float(hidden1_units))),
  44. name='weights')
  45. biases = tf.Variable(tf.zeros([hidden2_units]),
  46. name='biases')
  47. hidden2 = tf.nn.relu(tf.matmul(hidden1, weights) + biases)
  48. with tf.name_scope('softmax_linear'):
  49. weights = tf.Variable(
  50. tf.truncated_normal([hidden2_units, NUM_CLASSES],
  51. stddev=1.0 / math.sqrt(float(hidden2_units))),name='weights')
  52. biases = tf.Variable(tf.zeros([NUM_CLASSES]),name='biases')
  53. logits = tf.matmul(hidden2, weights) + biases
  54. return logits
  55.  
  56. def lossFunction(logits, labels):
  57. labels = tf.to_int64(labels)
  58. cross_entropy = tf.nn.sparse_softmax_cross_entropy_with_logits(
  59. logits, labels, name='xentropy')
  60. loss = tf.reduce_mean(cross_entropy, name='xentropy_mean')
  61. return loss
  62.  
  63. def training(loss, learning_rate):
  64. tf.summary.scalar(loss.op.name, loss)
  65. optimizer = tf.train.GradientDescentOptimizer(learning_rate)
  66. global_step = tf.Variable(0, name='global_step', trainable=False)
  67. train_op = optimizer.minimize(loss, global_step=global_step)
  68. return train_op
  69.  
  70. def read_and_decode(filename_queue):
  71. reader = tf.TFRecordReader()
  72. _, serialized_example = reader.read(filename_queue)
  73. features = tf.parse_single_example(
  74. serialized_example,
  75. # Defaults are not specified since both keys are required.
  76. features={
  77. 'image_raw': tf.FixedLenFeature([], tf.string),
  78. 'label': tf.FixedLenFeature([], tf.int64),
  79. })
  80.  
  81. # Convert from a scalar string tensor (whose single string has
  82. # length mnist.IMAGE_PIXELS) to a uint8 tensor with shape
  83. # [mnist.IMAGE_PIXELS].
  84. image = tf.decode_raw(features['image_raw'], tf.uint8)
  85. image.set_shape([IMAGE_PIXELS])
  86. image = tf.cast(image, tf.float32) * (1. / 255) - 0.5
  87. # Convert label from a scalar uint8 tensor to an int32 scalar.
  88. label = tf.cast(features['label'], tf.int32)
  89. return image, label
  90.  
  91. def inputs(batch_size):
  92. """Reads input data.
  93.  
  94. Args:
  95. batch_size: Number of examples per returned batch.
  96. Returns:
  97. A tuple (images, labels), where:
  98. * images is a float tensor with shape [batch_size, mnist.IMAGE_PIXELS]
  99. in the range [-0.5, 0.5].
  100. * labels is an int32 tensor with shape [batch_size] with the true label,
  101. a number in the range [0, mnist.NUM_CLASSES).
  102. """
  103. filename = os.path.join(FLAGS.data_dir, TRAIN_FILE)
  104.  
  105. with tf.name_scope('input'):
  106. filename_queue = tf.train.string_input_producer([filename])
  107. # Even when reading in multiple threads, share the filename
  108. # queue.
  109. image, label = read_and_decode(filename_queue)
  110. # Shuffle the examples and collect them into batch_size batches.
  111. # (Internally uses a RandomShuffleQueue.)
  112. # We run this in two threads to avoid being a bottleneck.
  113. images, sparse_labels = tf.train.shuffle_batch(
  114. [image, label], batch_size=batch_size, num_threads=2,
  115. capacity=1000 + 3 * batch_size,
  116. # Ensures a minimum amount of shuffling of examples.
  117. min_after_dequeue=1000)
  118. return images, sparse_labels
  119.  
  120. def device_and_target():
  121. # If FLAGS.job_name is not set, we're running single-machine TensorFlow.
  122. # Don't set a device.
  123. if FLAGS.job_name is None:
  124. raise ValueError("Must specify an explicit `job_name`")
  125. # Otherwise we're running distributed TensorFlow.
  126. print("Running distributed training")
  127. if FLAGS.task_index is None or FLAGS.task_index == "":
  128. raise ValueError("Must specify an explicit `task_index`")
  129. if FLAGS.ps_hosts is None or FLAGS.ps_hosts == "":
  130. raise ValueError("Must specify an explicit `ps_hosts`")
  131. if FLAGS.worker_hosts is None or FLAGS.worker_hosts == "":
  132. raise ValueError("Must specify an explicit `worker_hosts`")
  133. cluster_spec = tf.train.ClusterSpec({
  134. "ps": FLAGS.ps_hosts.split(","),
  135. "worker": FLAGS.worker_hosts.split(","),
  136. })
  137. server = tf.train.Server(
  138. cluster_spec, job_name=FLAGS.job_name, task_index=FLAGS.task_index)
  139. return (
  140. cluster_spec,
  141. server,
  142. )
  143.  
  144. def main(unused_argv):
  145. if FLAGS.data_dir is None or FLAGS.data_dir == "":
  146. raise ValueError("Must specify an explicit `data_dir`")
  147. if FLAGS.train_dir is None or FLAGS.train_dir == "":
  148. raise ValueError("Must specify an explicit `train_dir`")
  149. cluster_spec, server = device_and_target()
  150. if FLAGS.job_name == "ps":
  151. server.join()
  152. elif FLAGS.job_name == "worker":
  153. with tf.device(tf.train.replica_device_setter(worker_device = "/job:worker/task:{}".format(FLAGS.task_index), cluster=cluster_spec)):
  154. images, labels = inputs(FLAGS.batch_size)
  155. logits = inference(images, FLAGS.hidden1, FLAGS.hidden2)
  156. loss = lossFunction(logits, labels)
  157. train_op = training(loss, FLAGS.learning_rate)
  158. with tf.train.MonitoredTrainingSession(
  159. master=server.target,
  160. is_chief=(FLAGS.task_index == 0),
  161. checkpoint_dir=FLAGS.train_dir) as sess:
  162. while not sess.should_stop():
  163. sess.run(train_op)
  164.  
  165. if __name__ == "__main__":
  166. tf.app.run()

四、分布式模型的启动

首先关闭防火墙

sudo iptable –F

然后在不同的机器上面启动服务

  1. #在246.1机器上面运行参数服务器,命令:
  2. CLASSPATH=$($HADOOP_HDFS_HOME/bin/hadoop classpath --glob) python /home/bdusr01/tine/Distributed_Tensorflow_MNIST_Model_Used_NN_Read_TFRecords_On_HDFS_Support_Kerberos.py --ps_hosts=10.142.246.1:1120 --worker_hosts=10.142.78.41:1121,10.142.78.45:1122 --data_dir=hdfs://default/user/bdusr01/asy/MNIST_data --train_dir=/home/bdusr01/checkpoint/ --job_name=ps --task_index=0
  3.  
  4.  
  5. #在78.41机器上面运行worker0,命令:
  6. CLASSPATH=$($HADOOP_HDFS_HOME/bin/hadoop classpath --glob) python /home/bdusr01/tine/Distributed_Tensorflow_MNIST_Model_Used_NN_Read_TFRecords_On_HDFS_Support_Kerberos.py --ps_hosts=10.142.246.1:1120 --worker_hosts=10.142.78.41:1121,10.142.78.45:1122 --data_dir=hdfs://default/user/bdusr01/asy/MNIST_data --train_dir=/home/bdusr01/checkpoint/ --job_name=worker --task_index=0
  7.  
  8. #在78.45机器上面运行worker1,命令:
  9. CLASSPATH=$($HADOOP_HDFS_HOME/bin/hadoop classpath --glob) python /home/bdusr01/tine/Distributed_Tensorflow_MNIST_Model_Used_NN_Read_TFRecords_On_HDFS_Support_Kerberos.py--ps_hosts=10.142.246.1:1120 --worker_hosts=10.142.78.41:1121,10.142.78.45:1122 --data_dir=hdfs://default/user/bdusr01/asy/MNIST_data --train_dir=/home/bdusr01/checkpoint/ --job_name=worker --task_index=1
  10.  
  11. #在78.41机器上面运行监控,命令:
  12. tensorboard --logdir=/home/bdusr01/checkpoint/

五、模型监控

我们在刚刚的41机器上面启动了TensorBoard,可以通过地址http://10.142.78.41:6006/进行模型的监控。模型训练过程中参数可以动态的进行观测,示例如下:

大数据下基于Tensorflow框架的深度学习示例教程

模型的网络结构可以详细的参看每个细节,示例如下:

大数据下基于Tensorflow框架的深度学习示例教程

当我们利用分布式的Tensorflow对大数据进行训练完成后,可以利用Bazel构建一个灵活高可用的服务–TensorFlow Serving,能够很方便的将深度学习生产化,解决了模型无法提供服务的弊端。到此为止,本文就将自己项目中的一个基础模块的示例介绍完了,本项目更有含金量的是模型建立、工程开发、业务逻辑部分,如有机会再进行更详细的交流作者:丁廷鹤,硕士期间在复旦大学计算机学院上海市智能信息重点实验室从事数据挖掘学习,目前在上海一家央企总部工作,从事大数据领域spark全栈模块、机器学习、深度学习方面的开发和研究。

上一篇:Device tree customization


下一篇:Mybatis学习——一对一关联表查询