敲开TensorFlow的大门

RobertRAG 7年前
   <p>目前的AI还处在比较低级的阶段,但是像TensorFlow的出现,极大的推进了AI的进程,使得各类应用可以以最低的成本最快的的时间接入AI,今天我们就通过一个简单的模型训练来敲开TensorFlow的大门。</p>    <p>AI的模型训练其实就是: 对模型变量的值进行不断调整,直到训练模型对所有可能的变量求出的结果都无限接近目标值,而我们要做的工作就是给足够的数据进行训练,以及不断优化训练算法</p>    <h2>I. macOS Sierra中配置运行环境</h2>    <p>官方教程地址: <a href="/misc/goto?guid=4959742004833919215" rel="nofollow,noindex">https://www.tensorflow.org/install/install_mac#installing_with_virtualenv</a></p>    <p>一共有四种方式,根据官方推荐,我采用的是virtualenv</p>    <p>由于我的MBP没有NVIDIA CUDA GPU,因此这里我没有配GPU</p>    <p style="text-align: center;"><img src="https://simg.open-open.com/show/e917da612698a76e2f665411758e7f75.png" alt="敲开TensorFlow的大门" width="1329" height="669"></p>    <p style="text-align: center;"><img src="https://simg.open-open.com/show/a9e0c43415c72affebc131bc8a2d60a3.png"></p>    <p style="text-align: center;"><img src="https://simg.open-open.com/show/d40526eda72bd6af8c5a95fe1c971d7e.png"></p>    <p>由于我的Python是2.7.10版本,并且没有支持GPU,因此执行:</p>    <p>如果是其他情况的请参照 <a href="/misc/goto?guid=4959742004833919215" rel="nofollow,noindex">官方教程</a></p>    <p style="text-align: center;"><img src="https://simg.open-open.com/show/dd89536bd0848acbbcbe6978c183c58c.png"></p>    <p>验证安装</p>    <p>官方地址: <a href="/misc/goto?guid=4959742004947166288" rel="nofollow,noindex">https://www.tensorflow.org/install/install_mac#ValidateYourInstallation</a></p>    <p><img src="https://simg.open-open.com/show/f2c562e5da252669b2ed0f35bc1b1379.png"></p>    <h2>II. 训练模型案例</h2>    <p>该案例来自 <a href="/misc/goto?guid=4959742005031897955" rel="nofollow,noindex">官方的入门案例</a></p>    <p><img src="https://simg.open-open.com/show/b5b9a235281fee365e2ee8646e749144.png"></p>    <p>该次模型训练的性能决定因素是: 优化器选择、精度选择、训练数据</p>    <h2>III. 常见的API</h2>    <p>具体API可以参看 <a href="/misc/goto?guid=4959742005118642678" rel="nofollow,noindex">官网文档</a></p>    <p style="text-align: center;"><img src="https://simg.open-open.com/show/0caea5b4ec99eac5a1a72232f7c498cc.jpg" alt="敲开TensorFlow的大门" width="874" height="620"></p>    <ul>     <li>定义常量: tf.constant(value, type) ,如 tf.constant(3.0, tf.float32) ,当type没有给定的时候,会根据所给value定义</li>     <li>定义变量: tf.placeholder(type) , 如 tf.placeholder(tf.float32)</li>     <li>定义训练模型: tf.Variable([value], type] , 如 tf.Variable([.3], tf.float32)</li>     <li>计算结果: 通过 tf.Session() 的 run 去计算</li>     <li>初始化训练模型: tf.global_variables_initializer() ,对其进行复位运行起对象即可,如Session对象是 sees ,初始化模型对象是 init 时: sess.run(init)</li>     <li>对模型重新赋值: 如对 W 模型重新赋值: fixW = tf.assign(W, [-1.])</li>     <li>求平方: tf.square(value)</li>     <li>求和: tf.reduce_sum(value)</li>    </ul>    <p> </p>    <p>来自:https://blog.dreamtobe.cn/tensorflow-sample/</p>    <p> </p>