{"id":864,"date":"2025-01-11T11:58:46","date_gmt":"2025-01-11T03:58:46","guid":{"rendered":"https:\/\/www.laixuexila.com\/?p=864"},"modified":"2025-01-11T11:58:46","modified_gmt":"2025-01-11T03:58:46","slug":"%e5%a6%82%e4%bd%95%e4%bd%bf%e7%94%a8tensorflow%e8%bf%9b%e8%a1%8c%e6%9c%ba%e5%99%a8%e5%ad%a6%e4%b9%a0%ef%bc%9a%e4%bb%8e%e5%85%a5%e9%97%a8%e5%88%b0%e5%ae%9e%e8%b7%b5","status":"publish","type":"post","link":"https:\/\/www.laixuexila.com\/index.php\/2025\/01\/11\/%e5%a6%82%e4%bd%95%e4%bd%bf%e7%94%a8tensorflow%e8%bf%9b%e8%a1%8c%e6%9c%ba%e5%99%a8%e5%ad%a6%e4%b9%a0%ef%bc%9a%e4%bb%8e%e5%85%a5%e9%97%a8%e5%88%b0%e5%ae%9e%e8%b7%b5\/","title":{"rendered":"\u5982\u4f55\u4f7f\u7528TensorFlow\u8fdb\u884c\u673a\u5668\u5b66\u4e60\uff1a\u4ece\u5165\u95e8\u5230\u5b9e\u8df5"},"content":{"rendered":"\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\">\n<p><strong>TensorFlow<\/strong> \u662f\u4e00\u4e2a\u7531 Google \u5f00\u53d1\u7684\u5f00\u6e90\u6df1\u5ea6\u5b66\u4e60\u6846\u67b6\uff0c\u5e7f\u6cdb\u5e94\u7528\u4e8e\u673a\u5668\u5b66\u4e60\uff08ML\uff09\u548c\u4eba\u5de5\u667a\u80fd\uff08AI\uff09\u9886\u57df\u3002\u5b83\u63d0\u4f9b\u4e86\u5f3a\u5927\u7684\u5de5\u5177\u548c\u529f\u80fd\uff0c\u652f\u6301\u4ece\u7b80\u5355\u7684\u6a21\u578b\u6784\u5efa\u5230\u590d\u6742\u7684\u795e\u7ecf\u7f51\u7edc\u5e94\u7528\u3002\u65e0\u8bba\u4f60\u662f\u521a\u5f00\u59cb\u63a5\u89e6\u673a\u5668\u5b66\u4e60\uff0c\u8fd8\u662f\u6709\u4e00\u5b9a\u7ecf\u9a8c\u7684\u5f00\u53d1\u8005\uff0cTensorFlow \u90fd\u80fd\u5e2e\u52a9\u4f60\u9ad8\u6548\u5730\u5b9e\u73b0\u5404\u79cd\u673a\u5668\u5b66\u4e60\u4efb\u52a1\u3002<\/p>\n<\/blockquote>\n\n\n\n<p>\u672c\u6587\u5c06\u5e26\u4f60\u4ece\u5165\u95e8\u5230\u5b9e\u8df5\uff0c\u9010\u6b65\u5b66\u4e60\u5982\u4f55\u4f7f\u7528 TensorFlow \u8fdb\u884c\u673a\u5668\u5b66\u4e60\u3002<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">1. \u5b89\u88c5 TensorFlow<\/h3>\n\n\n\n<p>\u5728\u5f00\u59cb\u4f7f\u7528 TensorFlow \u4e4b\u524d\uff0c\u4f60\u9700\u8981\u5148\u8fdb\u884c\u5b89\u88c5\u3002\u53ef\u4ee5\u901a\u8fc7 <code>pip<\/code> \u6765\u5b89\u88c5 TensorFlow\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>pip install tensorflow<\/code><\/pre>\n\n\n\n<p>\u6b64\u547d\u4ee4\u4f1a\u5b89\u88c5 TensorFlow \u7684\u6700\u65b0\u7a33\u5b9a\u7248\u672c\u3002\u5bf9\u4e8e\u4e0d\u540c\u7684\u786c\u4ef6\u5e73\u53f0\uff08\u5982 GPU \u6216 CPU\uff09\uff0cTensorFlow \u63d0\u4f9b\u4e86\u4e0d\u540c\u7684\u5b89\u88c5\u65b9\u5f0f\uff0c\u4f60\u53ef\u4ee5\u53c2\u8003 <a href=\"https:\/\/www.tensorflow.org\/install\">TensorFlow\u5b98\u7f51<\/a> \u8fdb\u884c\u66f4\u8be6\u7ec6\u7684\u5b89\u88c5\u3002<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">2. TensorFlow\u57fa\u7840<\/h3>\n\n\n\n<p>TensorFlow \u7684\u6838\u5fc3\u6784\u5efa\u5757\u662f\u5f20\u91cf\uff08tensor\uff09\u3002\u5f20\u91cf\u662f\u4e00\u79cd\u591a\u7ef4\u6570\u7ec4\uff0c\u7c7b\u4f3c\u4e8e NumPy \u6570\u7ec4\uff0c\u4f46\u5b83\u53ef\u4ee5\u5728 GPU \u4e0a\u8fdb\u884c\u8ba1\u7b97\uff0c\u4ece\u800c\u5927\u5927\u52a0\u901f\u8ba1\u7b97\u901f\u5ea6\u3002<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">2.1 \u521b\u5efa\u5f20\u91cf<\/h4>\n\n\n\n<p>\u5728 TensorFlow \u4e2d\uff0c\u4f60\u53ef\u4ee5\u521b\u5efa\u4e0d\u540c\u7ef4\u5ea6\u7684\u5f20\u91cf\uff0c\u4f8b\u5982\u6807\u91cf\u3001\u5411\u91cf\u3001\u77e9\u9635\u7b49\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>import tensorflow as tf\n\n# \u521b\u5efa\u4e00\u4e2a\u5e38\u6570\u5f20\u91cf\ntensor = tf.constant(&#91;1, 2, 3, 4], dtype=tf.float32)\nprint(tensor)\n\n# \u521b\u5efa\u4e00\u4e2a2D\u5f20\u91cf\uff08\u77e9\u9635\uff09\nmatrix = tf.constant(&#91;&#91;1, 2], &#91;3, 4]])\nprint(matrix)<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\">2.2 \u5f20\u91cf\u8fd0\u7b97<\/h4>\n\n\n\n<p>TensorFlow \u652f\u6301\u8bb8\u591a\u6570\u5b66\u8fd0\u7b97\uff0c\u5305\u62ec\u52a0\u6cd5\u3001\u4e58\u6cd5\u3001\u77e9\u9635\u8fd0\u7b97\u7b49\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>a = tf.constant(&#91;1, 2, 3, 4])\nb = tf.constant(&#91;5, 6, 7, 8])\n\n# \u52a0\u6cd5\u8fd0\u7b97\nsum_tensor = tf.add(a, b)\nprint(sum_tensor)\n\n# \u4e58\u6cd5\u8fd0\u7b97\nprod_tensor = tf.multiply(a, b)\nprint(prod_tensor)<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">3. \u6784\u5efa\u7b80\u5355\u7684\u673a\u5668\u5b66\u4e60\u6a21\u578b<\/h3>\n\n\n\n<p>TensorFlow \u63d0\u4f9b\u4e86\u975e\u5e38\u76f4\u89c2\u7684 API\uff0c\u5e2e\u52a9\u4f60\u6784\u5efa\u548c\u8bad\u7ec3\u673a\u5668\u5b66\u4e60\u6a21\u578b\u3002\u6211\u4eec\u5c06\u901a\u8fc7\u4e00\u4e2a\u7b80\u5355\u7684\u4f8b\u5b50\u6765\u6f14\u793a\u5982\u4f55\u4f7f\u7528 TensorFlow \u6784\u5efa\u4e00\u4e2a\u7ebf\u6027\u56de\u5f52\u6a21\u578b\u3002<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">3.1 \u51c6\u5907\u6570\u636e<\/h4>\n\n\n\n<p>\u9996\u5148\uff0c\u6211\u4eec\u751f\u6210\u4e00\u4e9b\u7b80\u5355\u7684\u7ebf\u6027\u56de\u5f52\u6570\u636e\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>import numpy as np\nimport tensorflow as tf\nimport matplotlib.pyplot as plt\n\n# \u751f\u6210\u6570\u636e\uff1ay = 2x + 1\nx_data = np.linspace(1, 10, 100)\ny_data = 2 * x_data + 1 + np.random.randn(100) * 0.5  # \u52a0\u4e0a\u4e00\u4e9b\u566a\u58f0\n\n# \u53ef\u89c6\u5316\u6570\u636e\nplt.scatter(x_data, y_data)\nplt.xlabel(\"x\")\nplt.ylabel(\"y\")\nplt.title(\"Linear Data with Noise\")\nplt.show()<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\">3.2 \u6784\u5efa\u6a21\u578b<\/h4>\n\n\n\n<p>\u6211\u4eec\u4f7f\u7528 TensorFlow \u7684 <code>keras<\/code> API \u6765\u6784\u5efa\u4e00\u4e2a\u7b80\u5355\u7684\u7ebf\u6027\u56de\u5f52\u6a21\u578b\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code># \u5b9a\u4e49\u6a21\u578b\nmodel = tf.keras.Sequential(&#91;\n    tf.keras.layers.Dense(1, input_shape=(1,))\n])\n\n# \u7f16\u8bd1\u6a21\u578b\nmodel.compile(optimizer='adam', loss='mean_squared_error')\n\n# \u67e5\u770b\u6a21\u578b\u6982\u51b5\nmodel.summary()<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\">3.3 \u8bad\u7ec3\u6a21\u578b<\/h4>\n\n\n\n<p>\u63a5\u4e0b\u6765\uff0c\u6211\u4eec\u7528\u6211\u4eec\u751f\u6210\u7684\u6570\u636e\u6765\u8bad\u7ec3\u6a21\u578b\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code># \u8bad\u7ec3\u6a21\u578b\nmodel.fit(x_data, y_data, epochs=100)\n\n# \u67e5\u770b\u8bad\u7ec3\u540e\u7684\u6743\u91cd\nweights = model.get_weights()\nprint(\"\u6743\u91cd:\", weights)<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\">3.4 \u8fdb\u884c\u9884\u6d4b<\/h4>\n\n\n\n<p>\u6a21\u578b\u8bad\u7ec3\u5b8c\u6bd5\u540e\uff0c\u6211\u4eec\u53ef\u4ee5\u7528\u5b83\u6765\u8fdb\u884c\u9884\u6d4b\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code># \u4f7f\u7528\u8bad\u7ec3\u597d\u7684\u6a21\u578b\u8fdb\u884c\u9884\u6d4b\nx_test = np.array(&#91;12, 13, 14])  # \u65b0\u7684\u8f93\u5165\u6570\u636e\ny_pred = model.predict(x_test)\nprint(\"\u9884\u6d4b\u7ed3\u679c:\", y_pred)<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">4. \u66f4\u590d\u6742\u7684\u6a21\u578b\uff1a\u795e\u7ecf\u7f51\u7edc<\/h3>\n\n\n\n<p>\u5bf9\u4e8e\u66f4\u590d\u6742\u7684\u4efb\u52a1\uff0c\u901a\u5e38\u9700\u8981\u4f7f\u7528\u795e\u7ecf\u7f51\u7edc\u6a21\u578b\u3002\u4e0b\u9762\u6211\u4eec\u901a\u8fc7\u4e00\u4e2a\u7b80\u5355\u7684\u795e\u7ecf\u7f51\u7edc\u6a21\u578b\u6765\u89e3\u51b3\u5206\u7c7b\u95ee\u9898\u3002<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">4.1 \u52a0\u8f7d\u6570\u636e\u96c6<\/h4>\n\n\n\n<p>\u6211\u4eec\u4f7f\u7528 TensorFlow \u5185\u7f6e\u7684 MNIST \u6570\u636e\u96c6\uff0c\u5b83\u5305\u542b 28&#215;28 \u50cf\u7d20\u7684\u624b\u5199\u6570\u5b57\u56fe\u50cf\uff0c\u9002\u5408\u7528\u4e8e\u5206\u7c7b\u4efb\u52a1\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code># \u52a0\u8f7d MNIST \u6570\u636e\u96c6\nfrom tensorflow.keras.datasets import mnist\n\n(x_train, y_train), (x_test, y_test) = mnist.load_data()\n\n# \u6570\u636e\u9884\u5904\u7406\uff1a\u5f52\u4e00\u5316\u5e76\u8c03\u6574\u5f62\u72b6\nx_train = x_train \/ 255.0\nx_test = x_test \/ 255.0\nx_train = x_train.reshape(-1, 28, 28, 1)\nx_test = x_test.reshape(-1, 28, 28, 1)<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\">4.2 \u6784\u5efa\u795e\u7ecf\u7f51\u7edc<\/h4>\n\n\n\n<p>\u6211\u4eec\u4f7f\u7528\u5377\u79ef\u795e\u7ecf\u7f51\u7edc\uff08CNN\uff09\u6765\u5904\u7406\u56fe\u50cf\u6570\u636e\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code># \u5b9a\u4e49\u795e\u7ecf\u7f51\u7edc\u6a21\u578b\nmodel = tf.keras.Sequential(&#91;\n    tf.keras.layers.Conv2D(32, (3, 3), activation='relu', input_shape=(28, 28, 1)),\n    tf.keras.layers.MaxPooling2D((2, 2)),\n    tf.keras.layers.Conv2D(64, (3, 3), activation='relu'),\n    tf.keras.layers.MaxPooling2D((2, 2)),\n    tf.keras.layers.Flatten(),\n    tf.keras.layers.Dense(64, activation='relu'),\n    tf.keras.layers.Dense(10, activation='softmax')  # 10\u7c7b\u8f93\u51fa\n])\n\n# \u7f16\u8bd1\u6a21\u578b\nmodel.compile(optimizer='adam', loss='sparse_categorical_crossentropy', metrics=&#91;'accuracy'])<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\">4.3 \u8bad\u7ec3\u795e\u7ecf\u7f51\u7edc<\/h4>\n\n\n\n<p>\u63a5\u4e0b\u6765\uff0c\u4f7f\u7528\u8bad\u7ec3\u6570\u636e\u8fdb\u884c\u8bad\u7ec3\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code># \u8bad\u7ec3\u6a21\u578b\nmodel.fit(x_train, y_train, epochs=5)<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\">4.4 \u6d4b\u8bd5\u6a21\u578b<\/h4>\n\n\n\n<p>\u6a21\u578b\u8bad\u7ec3\u5b8c\u6bd5\u540e\uff0c\u4f7f\u7528\u6d4b\u8bd5\u6570\u636e\u6765\u8bc4\u4f30\u5176\u6027\u80fd\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code># \u6d4b\u8bd5\u6a21\u578b\ntest_loss, test_acc = model.evaluate(x_test, y_test)\nprint(\"Test accuracy:\", test_acc)<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">5. \u6df1\u5ea6\u5b66\u4e60\u6a21\u578b\u4f18\u5316<\/h3>\n\n\n\n<p>TensorFlow \u63d0\u4f9b\u4e86\u591a\u79cd\u4f18\u5316\u624b\u6bb5\uff0c\u53ef\u4ee5\u5e2e\u52a9\u6211\u4eec\u63d0\u9ad8\u6a21\u578b\u7684\u51c6\u786e\u6027\u548c\u6027\u80fd\u3002\u5305\u62ec\uff1a<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>\u8c03\u6574\u8d85\u53c2\u6570<\/strong>\uff1a\u5982\u5b66\u4e60\u7387\u3001\u6279\u6b21\u5927\u5c0f\u3001\u8bad\u7ec3\u8f6e\u6570\u7b49\u3002<\/li>\n\n\n\n<li><strong>\u4f7f\u7528\u65e9\u505c\u6cd5\uff08Early Stopping\uff09<\/strong>\uff1a\u9632\u6b62\u8fc7\u62df\u5408\u3002<\/li>\n\n\n\n<li><strong>\u4f7f\u7528\u56de\u8c03\uff08Callbacks\uff09<\/strong>\uff1a\u5982\u6a21\u578b\u4fdd\u5b58\u3001\u5b66\u4e60\u7387\u8c03\u6574\u7b49\u3002<\/li>\n<\/ul>\n\n\n\n<pre class=\"wp-block-code\"><code>from tensorflow.keras.callbacks import EarlyStopping\n\n# \u5b9a\u4e49\u65e9\u505c\u56de\u8c03\nearly_stop = EarlyStopping(monitor='val_loss', patience=3)\n\n# \u8bad\u7ec3\u65f6\u4f7f\u7528\u56de\u8c03\nmodel.fit(x_train, y_train, epochs=50, validation_split=0.2, callbacks=&#91;early_stop])<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">6. \u603b\u7ed3<\/h3>\n\n\n\n<p>\u901a\u8fc7\u672c\u7bc7\u6587\u7ae0\uff0c\u6211\u4eec\u5b66\u4e60\u4e86\u5982\u4f55\u4f7f\u7528 TensorFlow \u8fdb\u884c\u673a\u5668\u5b66\u4e60\uff0c\u4ece\u57fa\u672c\u7684\u5f20\u91cf\u64cd\u4f5c\u5230\u6784\u5efa\u7b80\u5355\u7684\u7ebf\u6027\u56de\u5f52\u6a21\u578b\uff0c\u518d\u5230\u4f7f\u7528\u795e\u7ecf\u7f51\u7edc\u89e3\u51b3\u5206\u7c7b\u95ee\u9898\u3002TensorFlow \u63d0\u4f9b\u4e86\u975e\u5e38\u5f3a\u5927\u7684\u5de5\u5177\uff0c\u9002\u7528\u4e8e\u5404\u79cd\u673a\u5668\u5b66\u4e60\u548c\u6df1\u5ea6\u5b66\u4e60\u4efb\u52a1\u3002<\/p>\n\n\n\n<p>\u65e0\u8bba\u4f60\u662f\u521a\u5165\u95e8\u7684\u673a\u5668\u5b66\u4e60\u7231\u597d\u8005\uff0c\u8fd8\u662f\u5e0c\u671b\u6784\u5efa\u9ad8\u6548\u6df1\u5ea6\u5b66\u4e60\u6a21\u578b\u7684\u4e13\u5bb6\uff0cTensorFlow \u90fd\u662f\u4e00\u4e2a\u6781\u597d\u7684\u9009\u62e9\u3002\u5728\u5b66\u4e60\u8fc7\u7a0b\u4e2d\uff0c\u4f60\u53ef\u4ee5\u9010\u6b65\u638c\u63e1 TensorFlow \u7684\u66f4\u591a\u9ad8\u7ea7\u529f\u80fd\uff0c\u5982\u6a21\u578b\u90e8\u7f72\u3001\u5206\u5e03\u5f0f\u8bad\u7ec3\u7b49\uff0c\u4e3a\u5b9e\u9645\u5e94\u7528\u5960\u5b9a\u57fa\u7840\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>TensorFlow \u662f\u4e00\u4e2a\u7531 Google \u5f00\u53d1\u7684\u5f00\u6e90\u6df1\u5ea6\u5b66\u4e60\u6846\u67b6\uff0c\u5e7f\u6cdb\u5e94\u7528\u4e8e\u673a\u5668\u5b66\u4e60\uff08ML\uff09\u548c\u4eba\u5de5\u667a\u80fd\uff08A [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[54],"tags":[],"class_list":["post-864","post","type-post","status-publish","format-standard","hentry","category-python"],"_links":{"self":[{"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/posts\/864","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/comments?post=864"}],"version-history":[{"count":1,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/posts\/864\/revisions"}],"predecessor-version":[{"id":865,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/posts\/864\/revisions\/865"}],"wp:attachment":[{"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/media?parent=864"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/categories?post=864"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/tags?post=864"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}