[{"data":1,"prerenderedAt":263},["ShallowReactive",2],{"\u002F2018-02-25-tensorflow-startup-01":3},{"id":4,"title":5,"body":6,"date":240,"description":12,"extension":241,"meta":242,"navigation":245,"path":257,"seo":258,"stem":259,"tags":260,"__hash__":262},"blogs\u002F_legacy\u002F2018\u002F2018-02-25-tensorflow-startup-01.md","从零开始的TensorFlow学习（一）",{"type":7,"value":8,"toc":224},"minimark",[9,13,16,19,22,33,38,41,44,53,57,60,63,66,69,72,75,78,85,93,96,99,110,113,117,120,123,129,132,136,139,142,146,149,155,158,161,164,167,170,173,176,182,185,188,191,197,200,206,209,215,218,221],[10,11,12],"p",{},"人工智能现在如火如荼，感觉上再过不久就会变成不可或缺的工具，因此便开始打算重新接触一下。",[10,14,15],{},"当前使用的环境为Python 3.6.4；TensorFlow版本1.5.0。",[10,17,18],{},"其实一开始对于机器学习这个东西，我是拒绝的。因为感觉上，它放弃了对原理的了解。就像是我们只知道大脑内神经元的连接却不知道它是如何构成思维一样，我们其实并不确切的知道训练完成后的神经网络是如何完成计算处理的。",[10,20,21],{},"但是最近出的DeepFake改变了我的看法，既然通过深度学习可以代替我们完成很多以前需要直觉才能完成的事情。那么本着实用主义的原则，应当也能做出很多有趣的东西。",[10,23,24,25,32],{},"虽然之前读研时看过很多相关的Paper，但是已经忘得差不多了，因此这里从官方教程[",[26,27,31],"a",{"href":28,"rel":29},"https:\u002F\u002Fwww.tensorflow.org\u002Fget_started\u002Fget_started_for_beginners",[30],"nofollow","Getting Started for ML Beginners","]开始，毕竟世上没有比一知半解更危险的事情了。",[34,35,37],"h2",{"id":36},"iris-flowers-classification","Iris flowers classification",[10,39,40],{},"这个是一个比较经典的问题，大概由于是面向初学者的教程，所以使用了比较简单的问题。",[10,42,43],{},"要解决的目标问题是鸢尾花分类，通过花萼的长宽以及花瓣的长宽等四个属性来判断。",[10,45,46,47,52],{},"数据的来源是[",[26,48,51],{"href":49,"rel":50},"https:\u002F\u002Fzh.wikipedia.org\u002Fwiki\u002F%E5%AE%89%E5%BE%B7%E6%A3%AE%E9%B8%A2%E5%B0%BE%E8%8A%B1%E5%8D%89%E6%95%B0%E6%8D%AE%E9%9B%86",[30],"安德森鸢尾花卉数据集","]，原数据共150个，在这里被分为120个训练数据和30个测试数据。",[54,55,56],"h3",{"id":56},"数据载入",[10,58,59],{},"为了对机器学习的效果进行评估，通常已知的数据会被分为训练数据和测试数据两部分。",[10,61,62],{},"因为如果一味的使用数据进行训练的话，往往会导致过度拟合的问题。",[10,64,65],{},"数据载入部分的代码都在iris_data.py中。",[10,67,68],{},"load_data是数据载入的入口，首先会调用maybe_download尝试对远端数据进行下载。",[10,70,71],{},"如果数据已存在的情况下就不会下载，由于某些情况一直下不来的话可以尝试架设梯子或者从其他地方下载这些数据。",[10,73,74],{},"下载后有两个文件，iris_training.csv和iris_test.csv，默认情况下应当是保存在C:\\Users\\Administrator.keras\\datasets目录中。",[10,76,77],{},"两个文件格式相同，如下",[10,79,80],{},[81,82],"img",{"alt":83,"src":84},"image","\u002Fwp-content\u002Fuploads\u002F2018\u002F02\u002Fimage_thumb-2.png",[10,86,87,88,92],{},"第一行是指示性的数据，分别代表数据的行数、列数，而后面的分别为鸢尾花的类型名称，对应最后一列的",[89,90,91],"span",{},"0,2","的数据。",[10,94,95],{},"不过这行数据并不会使用到，只是方便阅读。",[10,97,98],{},"load_data中会使用panda库对数据进行载入，分割成数据和属性两个部分之后",[100,101,106],"pre",{"className":102,"code":104,"language":105},[103],"language-text","--- test_x ---\nSepalLength  SepalWidth  PetalLength  PetalWidth\n0           5.9         3.0          4.2         1.5\n1           6.9         3.1          5.4         2.1\n2           5.1         3.3          1.7         0.5\n3           6.0         3.4          4.5         1.6\n4           5.5         2.5          4.0         1.3\n5           6.2         2.9          4.3         1.3\n6           5.5         4.2          1.4         0.2\n7           6.3         2.8          5.1         1.5\n8           5.6         3.0          4.1         1.3\n9           6.7         2.5          5.8         1.8\n10          7.1         3.0          5.9         2.1\n11          4.3         3.0          1.1         0.1\n12          5.6         2.8          4.9         2.0\n13          5.5         2.3          4.0         1.3\n14          6.0         2.2          4.0         1.0\n15          5.1         3.5          1.4         0.2\n16          5.7         2.6          3.5         1.0\n17          4.8         3.4          1.9         0.2\n18          5.1         3.4          1.5         0.2\n19          5.7         2.5          5.0         2.0\n20          5.4         3.4          1.7         0.2\n21          5.6         3.0          4.5         1.5\n22          6.3         2.9          5.6         1.8\n23          6.3         2.5          4.9         1.5\n24          5.8         2.7          3.9         1.2\n25          6.1         3.0          4.6         1.4\n26          5.2         4.1          1.5         0.1\n27          6.7         3.1          4.7         1.5\n28          6.7         3.3          5.7         2.5\n29          6.4         2.9          4.3         1.3\n--- test_y ---\n0     1\n1     2\n2     0\n3     1\n4     1\n5     1\n6     0\n7     2\n8     1\n9     2\n10    2\n11    0\n12    2\n13    1\n14    1\n15    0\n16    1\n17    0\n18    0\n19    2\n20    0\n21    1\n22    2\n23    1\n24    1\n25    1\n26    0\n27    1\n28    2\n29    1\nName: Species, dtype: int64\n","text",[107,108,104],"code",{"__ignoreMap":109},"",[10,111,112],{},"会以这样的形式返回给main函数。",[54,114,116],{"id":115},"feature-column","Feature Column",[10,118,119],{},"Feature column是原始数据与TensorFlow之间的中介，通过指定Feature column才能将数据转化为tf可以理解的形式。",[10,121,122],{},"示例中通过对train_x的key进行读取，生成了Feature Column，具体的内容如下",[100,124,127],{"className":125,"code":126,"language":105},[103],"[\n_NumericColumn(key='SepalLength', shape=(1,), default_value=None, dtype=tf.float32, normalizer_fn=None),\n_NumericColumn(key='SepalWidth', shape=(1,), default_value=None, dtype=tf.float32, normalizer_fn=None),\n_NumericColumn(key='PetalLength', shape=(1,), default_value=None, dtype=tf.float32, normalizer_fn=None),\n_NumericColumn(key='PetalWidth', shape=(1,), default_value=None, dtype=tf.float32, normalizer_fn=None)\n]\n",[107,128,126],{"__ignoreMap":109},[10,130,131],{},"根据文档，针对更加复杂的原始数据会有更加复杂的Feature Column生成和类型，这里由于原本就是float的所以无需转换。",[54,133,135],{"id":134},"estimator","Estimator",[10,137,138],{},"Tensorflow将一些预定义的模型封装成了Estimator，方便用户使用，也可以对Estimator进行进一步的定制来符合自己的使用需求。",[10,140,141],{},"示例中使用的是tf.estimator.DNNClassifier，应当是Deep Neural Network Classifier的简称，总之就是通常印象中的神经网络。",[143,144,145],"h4",{"id":145},"定义",[10,147,148],{},"在有了上面的步骤，定义和使用Estimator相当简洁：",[100,150,153],{"className":151,"code":152,"language":105},[103],"classifier = tf.estimator.DNNClassifier(\nfeature_columns=my_feature_columns,\nhidden_units=[10, 10],\nn_classes=3)\n",[107,154,152],{"__ignoreMap":109},[10,156,157],{},"使用的DNN共有两个隐藏层，每层10个节点，目标的类型数目为3。",[143,159,160],{"id":160},"训练和评估",[10,162,163],{},"这两个过程也很简洁，直接使用iris_data中的两个函数来生成Dataset并传回给调用。",[10,165,166],{},"最重要的函数是tf.data.Dataset.from_tensor_slices，将test_x和test_y分别作为feature和label组成DataSet。",[10,168,169],{},"在train的时候还有一个batch的概念，是针对训练时的iteration的，一次iteration会使用一个batch的数据，一次iteration之后权重会进行一次更新。",[143,171,172],{"id":172},"预测",[10,174,175],{},"预测使用classifier.predict，能够输出概率性的结果，使用的数据是事先给定的。",[100,177,180],{"className":178,"code":179,"language":105},[103],"Prediction is \"Setosa\" (99.6%), expected \"Setosa\"\nPrediction is \"Versicolor\" (99.4%), expected \"Versicolor\"\nPrediction is \"Virginica\" (99.0%), expected \"Virginica\"\n",[107,181,179],{"__ignoreMap":109},[10,183,184],{},"运行的最后会看到输出结果。",[54,186,187],{"id":187},"杂项",[10,189,190],{},"运行时遇到了这样的输出：",[100,192,195],{"className":193,"code":194,"language":105},[103],"Your CPU supports instructions that this TensorFlow binary was not compiled to use: AVX AVX2\n",[107,196,194],{"__ignoreMap":109},[10,198,199],{},"虽然没什么影响，但是如果想去掉的话，可以在代码中加上",[100,201,204],{"className":202,"code":203,"language":105},[103],"import os\nos.environ['TF_CPP_MIN_LOG_LEVEL'] = '2'\n",[107,205,203],{"__ignoreMap":109},[10,207,208],{},"另外，Tensorflow的很多log是由",[100,210,213],{"className":211,"code":212,"language":105},[103],"tf.logging.set_verbosity(tf.logging.INFO)\n",[107,214,212],{"__ignoreMap":109},[10,216,217],{},"造成的，如果不想看到的话删掉这句话就好了。",[34,219,220],{"id":220},"总结",[10,222,223],{},"总体来看，TensorFlow的封装做的很好，教程也相当的简明，感觉基本上顺着官方的教程学就可以了。",{"title":109,"searchDepth":225,"depth":226,"links":227},2,3,[228,239],{"id":36,"depth":225,"text":37,"children":229},[230,231,232,238],{"id":56,"depth":226,"text":56},{"id":115,"depth":226,"text":116},{"id":134,"depth":226,"text":135,"children":233},[234,236,237],{"id":145,"depth":235,"text":145},4,{"id":160,"depth":235,"text":160},{"id":172,"depth":235,"text":172},{"id":187,"depth":226,"text":187},{"id":220,"depth":225,"text":220},"2018-02-25","md",{"layout":243,"status":244,"published":245,"author":246,"author_login":248,"author_email":249,"wordpress_id":250,"wordpress_url":251,"date_gmt":252,"excerpt":253},"post","publish",true,{"display_name":247,"login":248,"email":249,"url":109},"风铃","flinkor","flinkor@foxmail.com",2274,"\u002F?p=2274","2018-02-25 15:25:25 +0000",{"type":7,"value":254},[255],[10,256,12],{},"\u002F2018-02-25-tensorflow-startup-01",{"title":5,"description":12},"_legacy\u002F2018\u002F2018-02-25-tensorflow-startup-01",[261],"TensorFlow","L8l90HRPkMfVvmg1hTIOxvsa-Th321t4Epkf3rB_62Y",1788763183001]