{ "nbformat": 4, "nbformat_minor": 0, "metadata": { "colab": { "name": "tensorflow_programming_concepts.ipynb", "version": "0.3.2", "views": {}, "default_view": {}, "collapsed_sections": [ "Zep4W80H9awM", "copyright-notice" ] } }, "cells": [ { "metadata": { "id": "copyright-notice", "colab_type": "text" }, "cell_type": "markdown", "source": [ "#### Copyright 2017 Google LLC." ] }, { "metadata": { "colab_type": "code", "id": "copyright-notice2", "cellView": "both", "colab": { "autoexec": { "startup": false, "wait_interval": 0 } } }, "outputs": [], "cell_type": "code", "execution_count": 0, "source": [ "# Licensed under the Apache License, Version 2.0 (the \"License\");\n", "# you may not use this file except in compliance with the License.\n", "# You may obtain a copy of the License at\n", "#\n", "# https://www.apache.org/licenses/LICENSE-2.0\n", "#\n", "# Unless required by applicable law or agreed to in writing, software\n", "# distributed under the License is distributed on an \"AS IS\" BASIS,\n", "# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n", "# See the License for the specific language governing permissions and\n", "# limitations under the License." ] }, { "cell_type": "markdown", "metadata": { "id": "rFpcvnCJ4Xkf", "colab_type": "text" }, "source": [ " # TensorFlow \u7f16\u7a0b\u6982\u5ff5" ] }, { "cell_type": "markdown", "metadata": { "id": "9IkBOsrNzahz", "colab_type": "text" }, "source": [ " **\u5b66\u4e60\u76ee\u6807\uff1a**\n", " * \u5b66\u4e60 TensorFlow \u7f16\u7a0b\u6a21\u578b\u7684\u57fa\u7840\u77e5\u8bc6\uff0c\u91cd\u70b9\u4e86\u89e3\u4ee5\u4e0b\u6982\u5ff5\uff1a\n", " * \u5f20\u91cf\n", " * \u6307\u4ee4\n", " * \u56fe\n", " * \u4f1a\u8bdd\n", " * \u6784\u5efa\u4e00\u4e2a\u7b80\u5355\u7684 TensorFlow \u7a0b\u5e8f\uff0c\u4f7f\u7528\u8be5\u7a0b\u5e8f\u7ed8\u5236\u4e00\u4e2a\u9ed8\u8ba4\u56fe\u5e76\u521b\u5efa\u4e00\u4e2a\u8fd0\u884c\u8be5\u56fe\u7684\u4f1a\u8bdd" ] }, { "cell_type": "markdown", "metadata": { "id": "YMG9mHPdzahz", "colab_type": "text" }, "source": [ " **\u6ce8\u610f\uff1a**\u8bf7\u4ed4\u7ec6\u9605\u8bfb\u672c\u6559\u7a0b\u3002TensorFlow \u7f16\u7a0b\u6a21\u578b\u5f88\u53ef\u80fd\u4e0e\u60a8\u9047\u5230\u7684\u5176\u4ed6\u6a21\u578b\u4e0d\u540c\uff0c\u56e0\u6b64\u53ef\u80fd\u4e0d\u5982\u60a8\u671f\u671b\u7684\u90a3\u6837\u76f4\u89c2\u3002" ] }, { "cell_type": "markdown", "metadata": { "id": "NzKsjX-ufyVY", "colab_type": "text" }, "source": [ " ## \u6982\u5ff5\u6982\u89c8\n\n", "TensorFlow \u7684\u540d\u79f0\u6e90\u81ea**\u5f20\u91cf**\uff0c\u5f20\u91cf\u662f\u4efb\u610f\u7ef4\u5ea6\u7684\u6570\u7ec4\u3002\u501f\u52a9 TensorFlow\uff0c\u60a8\u53ef\u4ee5\u64cd\u63a7\u5177\u6709\u5927\u91cf\u7ef4\u5ea6\u7684\u5f20\u91cf\u3002\u5373\u4fbf\u5982\u6b64\uff0c\u5728\u5927\u591a\u6570\u60c5\u51b5\u4e0b\uff0c\u60a8\u4f1a\u4f7f\u7528\u4ee5\u4e0b\u4e00\u4e2a\u6216\u591a\u4e2a\u4f4e\u7ef4\u5f20\u91cf\uff1a\n\n", " * **\u6807\u91cf**\u662f\u96f6\u7ef4\u6570\u7ec4\uff08\u96f6\u9636\u5f20\u91cf\uff09\u3002\u4f8b\u5982\uff0c`\\'Howdy\\'` \u6216 `5`\n", " * **\u77e2\u91cf**\u662f\u4e00\u7ef4\u6570\u7ec4\uff08\u4e00\u9636\u5f20\u91cf\uff09\u3002\u4f8b\u5982\uff0c`[2, 3, 5, 7, 11]` \u6216 `[5]`\n", " * **\u77e9\u9635**\u662f\u4e8c\u7ef4\u6570\u7ec4\uff08\u4e8c\u9636\u5f20\u91cf\uff09\u3002\u4f8b\u5982\uff0c`[[3.1, 8.2, 5.9][4.3, -2.7, 6.5]]`\n\n", "TensorFlow **\u6307\u4ee4**\u4f1a\u521b\u5efa\u3001\u9500\u6bc1\u548c\u64cd\u63a7\u5f20\u91cf\u3002\u5178\u578b TensorFlow \u7a0b\u5e8f\u4e2d\u7684\u5927\u591a\u6570\u4ee3\u7801\u884c\u90fd\u662f\u6307\u4ee4\u3002\n\n", "TensorFlow **\u56fe**\uff08\u4e5f\u79f0\u4e3a**\u8ba1\u7b97\u56fe**\u6216**\u6570\u636e\u6d41\u56fe**\uff09\u662f\u4e00\u79cd\u56fe\u6570\u636e\u7ed3\u6784\u3002\u5f88\u591a TensorFlow \u7a0b\u5e8f\u7531\u5355\u4e2a\u56fe\u6784\u6210\uff0c\u4f46\u662f TensorFlow \u7a0b\u5e8f\u53ef\u4ee5\u9009\u62e9\u521b\u5efa\u591a\u4e2a\u56fe\u3002\u56fe\u7684\u8282\u70b9\u662f\u6307\u4ee4\uff1b\u56fe\u7684\u8fb9\u662f\u5f20\u91cf\u3002\u5f20\u91cf\u6d41\u7ecf\u56fe\uff0c\u5728\u6bcf\u4e2a\u8282\u70b9\u7531\u4e00\u4e2a\u6307\u4ee4\u64cd\u63a7\u3002\u4e00\u4e2a\u6307\u4ee4\u7684\u8f93\u51fa\u5f20\u91cf\u901a\u5e38\u4f1a\u53d8\u6210\u540e\u7eed\u6307\u4ee4\u7684\u8f93\u5165\u5f20\u91cf\u3002TensorFlow \u4f1a\u5b9e\u73b0**\u5ef6\u8fdf\u6267\u884c\u6a21\u578b**\uff0c\u610f\u5473\u7740\u7cfb\u7edf\u4ec5\u4f1a\u6839\u636e\u76f8\u5173\u8282\u70b9\u7684\u9700\u6c42\u5728\u9700\u8981\u65f6\u8ba1\u7b97\u8282\u70b9\u3002\n\n", "\u5f20\u91cf\u53ef\u4ee5\u4f5c\u4e3a**\u5e38\u91cf**\u6216**\u53d8\u91cf**\u5b58\u50a8\u5728\u56fe\u4e2d\u3002\u60a8\u53ef\u80fd\u5df2\u7ecf\u731c\u5230\uff0c\u5e38\u91cf\u5b58\u50a8\u7684\u662f\u503c\u4e0d\u4f1a\u53d1\u751f\u66f4\u6539\u7684\u5f20\u91cf\uff0c\u800c\u53d8\u91cf\u5b58\u50a8\u7684\u662f\u503c\u4f1a\u53d1\u751f\u66f4\u6539\u7684\u5f20\u91cf\u3002\u4e0d\u8fc7\uff0c\u60a8\u53ef\u80fd\u6ca1\u6709\u731c\u5230\u7684\u662f\uff0c\u5e38\u91cf\u548c\u53d8\u91cf\u90fd\u53ea\u662f\u56fe\u4e2d\u7684\u4e00\u79cd\u6307\u4ee4\u3002\u5e38\u91cf\u662f\u59cb\u7ec8\u4f1a\u8fd4\u56de\u540c\u4e00\u5f20\u91cf\u503c\u7684\u6307\u4ee4\u3002\u53d8\u91cf\u662f\u4f1a\u8fd4\u56de\u5206\u914d\u7ed9\u5b83\u7684\u4efb\u4f55\u5f20\u91cf\u7684\u6307\u4ee4\u3002\n\n", "\u8981\u5b9a\u4e49\u5e38\u91cf\uff0c\u8bf7\u4f7f\u7528 `tf.constant` \u6307\u4ee4\uff0c\u5e76\u4f20\u5165\u5b83\u7684\u503c\u3002\u4f8b\u5982\uff1a\n\n", "```\n", " x = tf.constant([5.2])\n", "```\n\n", "\u540c\u6837\uff0c\u60a8\u53ef\u4ee5\u521b\u5efa\u5982\u4e0b\u53d8\u91cf\uff1a\n\n", "```\n", " y = tf.Variable([5])\n", "```\n\n", "\u6216\u8005\uff0c\u60a8\u4e5f\u53ef\u4ee5\u5148\u521b\u5efa\u53d8\u91cf\uff0c\u7136\u540e\u518d\u5982\u4e0b\u6240\u793a\u5730\u5206\u914d\u4e00\u4e2a\u503c\uff08\u6ce8\u610f\uff1a\u60a8\u59cb\u7ec8\u9700\u8981\u6307\u5b9a\u4e00\u4e2a\u9ed8\u8ba4\u503c\uff09\uff1a\n\n", "```\n", " y = tf.Variable([0])\n", " y = y.assign([5])\n", "```\n\n", "\u5b9a\u4e49\u4e00\u4e9b\u5e38\u91cf\u6216\u53d8\u91cf\u540e\uff0c\u60a8\u53ef\u4ee5\u5c06\u5b83\u4eec\u4e0e\u5176\u4ed6\u6307\u4ee4\uff08\u5982 `tf.add`\uff09\u7ed3\u5408\u4f7f\u7528\u3002\u5728\u8bc4\u4f30 `tf.add` \u6307\u4ee4\u65f6\uff0c\u5b83\u4f1a\u8c03\u7528\u60a8\u7684 `tf.constant` \u6216 `tf.Variable` \u6307\u4ee4\uff0c\u4ee5\u83b7\u53d6\u5b83\u4eec\u7684\u503c\uff0c\u7136\u540e\u8fd4\u56de\u4e00\u4e2a\u5305\u542b\u8fd9\u4e9b\u503c\u4e4b\u548c\u7684\u65b0\u5f20\u91cf\u3002\n\n", "\u56fe\u5fc5\u987b\u5728 TensorFlow **\u4f1a\u8bdd**\u4e2d\u8fd0\u884c\uff0c\u4f1a\u8bdd\u5b58\u50a8\u4e86\u5b83\u6240\u8fd0\u884c\u7684\u56fe\u7684\u72b6\u6001\uff1a\n\n", "```\n", "\u5c06 tf.Session() \u4f5c\u4e3a\u4f1a\u8bdd\uff1a\n", " initialization = tf.global_variables_initializer()\n", " print y.eval()\n", "```\n\n", "\u5728\u4f7f\u7528 `tf.Variable` \u65f6\uff0c\u60a8\u5fc5\u987b\u5728\u4f1a\u8bdd\u5f00\u59cb\u65f6\u8c03\u7528 `tf.global_variables_initializer`\uff0c\u4ee5\u660e\u786e\u521d\u59cb\u5316\u8fd9\u4e9b\u53d8\u91cf\uff0c\u5982\u4e0a\u6240\u793a\u3002\n\n", "**\u6ce8\u610f\uff1a**\u4f1a\u8bdd\u53ef\u4ee5\u5c06\u56fe\u5206\u53d1\u5230\u591a\u4e2a\u673a\u5668\u4e0a\u6267\u884c\uff08\u5047\u8bbe\u7a0b\u5e8f\u5728\u67d0\u4e2a\u5206\u5e03\u5f0f\u8ba1\u7b97\u6846\u67b6\u4e0a\u8fd0\u884c\uff09\u3002\u6709\u5173\u8be6\u60c5\uff0c\u8bf7\u53c2\u9605[\u5206\u5e03\u5f0f TensorFlow](https://www.tensorflow.org/deploy/distributed)\u3002\n\n", "### \u603b\u7ed3\n\n", "TensorFlow \u7f16\u7a0b\u672c\u8d28\u4e0a\u662f\u4e00\u4e2a\u4e24\u6b65\u6d41\u7a0b\uff1a\n\n", "1. \u5c06\u5e38\u91cf\u3001\u53d8\u91cf\u548c\u6307\u4ee4\u6574\u5408\u5230\u4e00\u4e2a\u56fe\u4e2d\u3002\n", "2. \u5728\u4e00\u4e2a\u4f1a\u8bdd\u4e2d\u8bc4\u4f30\u8fd9\u4e9b\u5e38\u91cf\u3001\u53d8\u91cf\u548c\u6307\u4ee4\u3002\n", "" ] }, { "cell_type": "markdown", "metadata": { "id": "VL0yWNNdgBpG", "colab_type": "text" }, "source": [ " ## \u521b\u5efa\u4e00\u4e2a\u7b80\u5355\u7684 TensorFlow \u7a0b\u5e8f\n", "\n", "\u6211\u4eec\u6765\u770b\u770b\u5982\u4f55\u7f16\u5199\u4e00\u4e2a\u5c06\u4e24\u4e2a\u5e38\u91cf\u76f8\u52a0\u7684\u7b80\u5355 TensorFlow \u7a0b\u5e8f\u3002" ] }, { "cell_type": "markdown", "metadata": { "id": "mN4R0gmMzah3", "colab_type": "text" }, "source": [ " ### \u6dfb\u52a0 import \u8bed\u53e5\n", "\n", "\u4e0e\u51e0\u4e4e\u6240\u6709 Python \u7a0b\u5e8f\u4e00\u6837\uff0c\u60a8\u9996\u5148\u8981\u6dfb\u52a0\u4e00\u4e9b `import` \u8bed\u53e5\u3002\n", "\u5f53\u7136\uff0c\u8fd0\u884c TensorFlow \u7a0b\u5e8f\u6240\u9700\u7684 `import` \u8bed\u53e5\u7ec4\u5408\u53d6\u51b3\u4e8e\u60a8\u7684\u7a0b\u5e8f\u5c06\u8981\u8bbf\u95ee\u7684\u529f\u80fd\u3002\u81f3\u5c11\uff0c\u60a8\u5fc5\u987b\u5728\u6240\u6709 TensorFlow \u7a0b\u5e8f\u4e2d\u6dfb\u52a0 `import tensorflow` \u8bed\u53e5\uff1a" ] }, { "metadata": { "id": "SDbi6heigEGA", "colab_type": "code", "colab": { "autoexec": { "startup": false, "wait_interval": 0 } } }, "source": [ "import tensorflow as tf" ], "cell_type": "code", "execution_count": 0, "outputs": [] }, { "cell_type": "markdown", "metadata": { "id": "6RRT4YIA4fQd", "colab_type": "text" }, "source": [ " **\u8bf7\u52ff\u5fd8\u8bb0\u6267\u884c\u524d\u9762\u7684\u4ee3\u7801\u5757\uff08`import` \u8bed\u53e5\uff09\u3002**\n", "\n", "\u5176\u4ed6\u5e38\u89c1\u7684 import \u8bed\u53e5\u5305\u62ec\uff1a\n\n", "```\n", "import matplotlib.pyplot as plt # \u6570\u636e\u96c6\u53ef\u89c6\u5316\u3002\n", "import numpy as np # \u4f4e\u7ea7\u6570\u5b57 Python \u5e93\u3002\n", "import pandas as pd # \u8f83\u9ad8\u7ea7\u522b\u7684\u6570\u5b57 Python \u5e93\u3002\n", "```" ] }, { "cell_type": "markdown", "metadata": { "id": "1aNpvufRgbTD", "colab_type": "text" }, "source": [ " TensorFlow \u63d0\u4f9b\u4e86\u4e00\u4e2a**\u9ed8\u8ba4\u56fe**\u3002\u4e0d\u8fc7\uff0c\u6211\u4eec\u5efa\u8bae\u60a8\u660e\u786e\u521b\u5efa\u81ea\u5df1\u7684 `Graph`\uff0c\u4ee5\u4fbf\u8ddf\u8e2a\u72b6\u6001\uff08\u4f8b\u5982\uff0c\u60a8\u53ef\u80fd\u5e0c\u671b\u5728\u6bcf\u4e2a\u5355\u5143\u683c\u4e2d\u4f7f\u7528\u4e00\u4e2a\u4e0d\u540c\u7684 `Graph`\uff09\u3002" ] }, { "metadata": { "id": "Md8ze8e9geMi", "colab_type": "code", "colab": { "autoexec": { "startup": false, "wait_interval": 0 } } }, "source": [ "import tensorflow as tf\n", "\n", "# Create a graph.\n", "g = tf.Graph()\n", "\n", "# Establish the graph as the \"default\" graph.\n", "with g.as_default():\n", " # Assemble a graph consisting of the following three operations:\n", " # * Two tf.constant operations to create the operands.\n", " # * One tf.add operation to add the two operands.\n", " x = tf.constant(8, name=\"x_const\")\n", " y = tf.constant(5, name=\"y_const\")\n", " sum = tf.add(x, y, name=\"x_y_sum\")\n", "\n", "\n", " # Now create a session.\n", " # The session will run the default graph.\n", " with tf.Session() as sess:\n", " print sum.eval()" ], "cell_type": "code", "execution_count": 0, "outputs": [] }, { "cell_type": "markdown", "metadata": { "id": "7mSz5GVqggZy", "colab_type": "text" }, "source": [ " ## \u7ec3\u4e60\uff1a\u5f15\u5165\u7b2c\u4e09\u4e2a\u8fd0\u7b97\u6570\n", "\n", "\u4fee\u6539\u4e0a\u9762\u7684\u4ee3\u7801\u5217\u8868\uff0c\u4ee5\u5c06\u4e09\u4e2a\u6574\u6570\uff08\u800c\u4e0d\u662f\u4e24\u4e2a\uff09\u76f8\u52a0\uff1a\n", "\n", " 1. \u5b9a\u4e49\u7b2c\u4e09\u4e2a\u6807\u91cf\u6574\u6570\u5e38\u91cf `z`\uff0c\u5e76\u4e3a\u5176\u5206\u914d\u4e00\u4e2a\u503c `4`\u3002\n", " 2. \u5c06 `sum` \u4e0e `z` \u76f8\u52a0\uff0c\u4ee5\u5f97\u51fa\u4e00\u4e2a\u65b0\u7684\u548c\u3002\n", " \n", " **\u63d0\u793a\uff1a**\u8bf7\u53c2\u9605\u6709\u5173 [tf.add()](https://www.tensorflow.org/api_docs/python/tf/add) \u7684 API \u6587\u6863\uff0c\u4e86\u89e3\u6709\u5173\u5176\u51fd\u6570\u7b7e\u540d\u7684\u66f4\u591a\u8be6\u7ec6\u4fe1\u606f\u3002\n", " \n", " 3. \u91cd\u65b0\u8fd0\u884c\u4fee\u6539\u540e\u7684\u4ee3\u7801\u5757\u3002\u8be5\u7a0b\u5e8f\u662f\u5426\u751f\u6210\u4e86\u6b63\u786e\u7684\u603b\u548c\uff1f" ] }, { "cell_type": "markdown", "metadata": { "id": "Zep4W80H9awM", "colab_type": "text" }, "source": [ " ### \u89e3\u51b3\u65b9\u6848\n", "\n", "\u70b9\u51fb\u4e0b\u65b9\uff0c\u67e5\u770b\u89e3\u51b3\u65b9\u6848\u3002" ] }, { "metadata": { "id": "4OTZPqhS9kzu", "colab_type": "code", "colab": { "autoexec": { "startup": false, "wait_interval": 0 } } }, "source": [ "# Create a graph.\n", "g = tf.Graph()\n", "\n", "# Establish our graph as the \"default\" graph.\n", "with g.as_default():\n", " # Assemble a graph consisting of three operations. \n", " # (Creating a tensor is an operation.)\n", " x = tf.constant(8, name=\"x_const\")\n", " y = tf.constant(5, name=\"y_const\")\n", " sum = tf.add(x, y, name=\"x_y_sum\")\n", " \n", " # Task 1: Define a third scalar integer constant z.\n", " z = tf.constant(4, name=\"z_const\")\n", " # Task 2: Add z to `sum` to yield a new sum.\n", " new_sum = tf.add(sum, z, name=\"x_y_z_sum\")\n", "\n", " # Now create a session.\n", " # The session will run the default graph.\n", " with tf.Session() as sess:\n", " # Task 3: Ensure the program yields the correct grand total.\n", " print new_sum.eval()" ], "cell_type": "code", "execution_count": 0, "outputs": [] }, { "cell_type": "markdown", "metadata": { "id": "BrlnLTesgywL", "colab_type": "text" }, "source": [ " ## \u66f4\u591a\u4fe1\u606f\n", "\n", "\u8981\u8fdb\u4e00\u6b65\u63a2\u7d22\u57fa\u672c TensorFlow \u56fe\uff0c\u8bf7\u4f7f\u7528\u4ee5\u4e0b\u6559\u7a0b\u8fdb\u884c\u5b9e\u9a8c\uff1a\n", "\n", " * [Mandelbrot \u96c6](https://www.tensorflow.org/tutorials/mandelbrot#mandelbrot-set)" ] } ] }