{"id":1757,"date":"2025-01-23T07:57:00","date_gmt":"2025-01-22T23:57:00","guid":{"rendered":"https:\/\/blog.laoyulaoyu.top\/?p=1757"},"modified":"2024-12-28T13:00:10","modified_gmt":"2024-12-28T05:00:10","slug":"%e7%b2%be%e5%87%86%e6%8a%8a%e6%8f%a1%e4%bd%8e%e4%b9%b0%e9%ab%98%e5%8d%96%e6%97%b6%e6%9c%ba%ef%bc%9alstm%e6%a8%a1%e5%9e%8b%e5%8a%a9%e5%8a%9b%e8%82%a1%e7%a5%a8%e9%a2%84%e6%b5%8b%e5%88%86%e6%9e%90","status":"publish","type":"post","link":"https:\/\/www.laoyulaoyu.com\/index.php\/2025\/01\/23\/%e7%b2%be%e5%87%86%e6%8a%8a%e6%8f%a1%e4%bd%8e%e4%b9%b0%e9%ab%98%e5%8d%96%e6%97%b6%e6%9c%ba%ef%bc%9alstm%e6%a8%a1%e5%9e%8b%e5%8a%a9%e5%8a%9b%e8%82%a1%e7%a5%a8%e9%a2%84%e6%b5%8b%e5%88%86%e6%9e%90\/","title":{"rendered":"\u7cbe\u51c6\u628a\u63e1\u4f4e\u4e70\u9ad8\u5356\u65f6\u673a\uff1aLSTM\u6a21\u578b\u52a9\u529b\u80a1\u7968\u9884\u6d4b\u5206\u6790\uff08\u9644\u5b8c\u6574\u6e90\u7801\uff09"},"content":{"rendered":"\n<p>\u4f5c\u8005\uff1a<a href=\"https:\/\/www.laoyulaoyu.com\/\" target=\"_blank\" rel=\"noreferrer noopener\">\u8001\u4f59\u635e\u9c7c<\/a><\/p>\n\n\n\n<p><strong><mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-cyan-bluish-gray-color\">\u539f\u521b\u4e0d\u6613\uff0c\u8f6c\u8f7d\u8bf7\u6807\u660e\u51fa\u5904\u53ca\u539f\u4f5c\u8005\u3002<\/mark><\/strong><\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img decoding=\"async\" src=\"https:\/\/www.laoyulaoyu.com\/wp-content\/uploads\/2024\/12\/1_En8N3hoG-4ysZg5mSAsWHQ.jpg\" alt=\"\" class=\"wp-image-3290\"\/><\/figure>\n\n\n\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\">\n<pre class=\"wp-block-verse\"><strong>\u5199\u5728\u524d\u9762\u7684\u8bdd\uff1a<\/strong>\u5728\u6211\u6b64\u524d\u7684\u6587\u7ae0\u4e2d\uff0c\u6211\u5df2\u7ecf\u591a\u6b21\u63a2\u8ba8LSTM\u6a21\u578b\u5728\u80a1\u7968\u4ef7\u683c\u9884\u6d4b\u4e2d\u7684\u5e94\u7528\u3002\u4e0d\u5c11\u8bfb\u8005\u53cd\u9988\uff0c\u5e0c\u671b\u80fd\u770b\u5230\u4e00\u4e2a\u66f4\u52a0\u6613\u4e8e\u7406\u89e3\u7684\u5b9e\u6218\u6559\u7a0b\u3002\u5e94\u5927\u5bb6\u7684\u9700\u6c42\uff0c<mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-vivid-cyan-blue-color\">\u672c\u6587\u5c06\u624b\u628a\u624b\u5e26\u4f60\u6df1\u5165LSTM\u7684\u4e16\u754c\uff0c\u4ece\u6570\u636e\u6293\u53d6\u3001\u6e05\u6d17\u5904\u7406\uff0c\u5230\u6a21\u578b\u7684\u642d\u5efa\u4e0e\u8bad\u7ec3\uff0c\u6bcf\u4e00\u6b65\u90fd\u662f\u6211\u5b9e\u6218\u7ecf\u9a8c\u7684\u603b\u7ed3<\/mark>\uff0c\u65e8\u5728\u8ba9\u4f60\u8f7b\u677e\u638c\u63e1\u80a1\u7968\u4ef7\u683c\u9884\u6d4b\u7684\u7cbe\u9ad3\u3002<\/pre>\n<\/blockquote>\n\n\n\n<p><strong><a href=\"https:\/\/mp.weixin.qq.com\/s\/T1N1nT5Kze3jVPmTXUq5hQ?token=889801821&amp;lang=zh_CN\" target=\"_blank\" rel=\"noreferrer noopener\">\u300a\u63ed\u79d8\uff1a\u5982\u4f55\u7528\u601d\u60f3\u589e\u5f3a\u578bLSTM\u7f51\u7edc\u7cbe\u51c6\u9884\u6d4b\u80a1\u4ef7\uff1f\u300b<\/a><\/strong><\/p>\n\n\n\n<p>\u5728\u8fd9\u7bc7\u6587\u7ae0\u4e2d\uff0c\u6211\u8be6\u7ec6\u4ecb\u7ecd\u4e86\u4ec0\u4e48\u662f<strong> LSTM <\/strong>\uff0c\u5174\u8da3\u7684\u8bfb\u8005\u53ef\u4ee5\u53bb\u4e86\u89e3\uff0c\u5728\u672c\u6587\u4e2d\u5c31\u4e0d\u518d\u7d2f\u8ff0\u4e86\u3002<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>\u4e00\u3001\u5b9e\u6218\u6307\u5357\uff1a6\u6b65\u9884\u6d4b META \u80a1\u7968\u4ef7\u683c&nbsp;<\/strong><\/h2>\n\n\n\n<p>\u4e3a\u4e86\u5c55\u793a LSTM \u6a21\u578b\u5728\u80a1\u7968\u4ea4\u6613\u9886\u57df\u5b9e\u73b0\u4f4e\u4e70\u9ad8\u5356\u7684\u5b9e\u7528\u6027\uff0c\u6211\u4eec\u5c06\u4ee5Meta Platforms Inc.\uff08\u80a1\u7968\u4ee3\u7801\uff1aMETA\uff09\u4e3a\u4f8b\u8fdb\u884c\u5b9e\u64cd\u6f14\u793a\u3002\u901a\u8fc76\u6b65\u5c06\u6784\u5efa\u5e76\u8bad\u7ec3\u4e00\u4e2aLSTM\u6a21\u578b\u6765\u9884\u6d4bMETA\u7684\u672a\u6765\u80a1\u4ef7\u8d70\u52bf\uff0c\u5e76\u5bf9\u9884\u6d4b\u7ed3\u679c\u8fdb\u884c\u76f4\u89c2\u7684\u53ef\u89c6\u5316\u5c55\u793a\u3002<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>\u6b65\u9aa4\u4e00\uff1a\u83b7\u53d6\u548c\u51c6\u5907\u6570\u636e<\/strong><\/h3>\n\n\n\n<p>\u9996\u5148\uff0c\u6211\u4eec\u4e0b\u8f7d META \u7684\u5386\u53f2\u80a1\u4ef7\u6570\u636e\uff0c\u5e76\u5bf9\u5176\u8fdb\u884c\u5f52\u4e00\u5316\u5904\u7406\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>import yfinance as yf\nimport numpy as np\nfrom sklearn.preprocessing import MinMaxScaler\n\n# Fetch historical data for META\ndata = yf.download('META', start='2020-01-01', end='2024-07-21')\ndata = data&#91;&#91;'Close']]\n\n# Normalize the data\nscaler = MinMaxScaler(feature_range=(0, 1))\nscaled_data = scaler.fit_transform(data)<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>\u6b65\u9aa4\u4e8c\uff1a\u4e3a LSTM \u51c6\u5907\u6570\u636e&nbsp;<\/strong><\/h3>\n\n\n\n<p>\u5c06\u89c4\u8303\u5316\u6570\u636e\u8f6c\u6362\u6210\u9002\u5408 LSTM \u6a21\u578b\u7684\u683c\u5f0f\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>def create_dataset(data, time_step=1):\n    X, Y = &#91;], &#91;]\n    for i in range(len(data) - time_step - 1):\n        a = data&#91;i:(i + time_step), 0]\n        X.append(a)\n        Y.append(data&#91;i + time_step, 0])\n    return np.array(X), np.array(Y)\n\ntime_step = 100\nX, Y = create_dataset(scaled_data, time_step)<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>\u6b65\u9aa4\u4e09\uff1a\u5206\u5272\u6570\u636e\u5e76\u91cd\u5851<\/strong><\/h3>\n\n\n\n<p>\u6211\u4eec\u628a\u6570\u636e\u5206\u4e3a\u8bad\u7ec3\u96c6\u548c\u6d4b\u8bd5\u96c6\uff0c\u5e76\u4e3a LSTM \u6a21\u578b\u91cd\u5851\u6570\u636e\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>train_size = int(len(X) * 0.65)\ntest_size = len(X) - train_size\nX_train, X_test = X&#91;0:train_size], X&#91;train_size:len(X)]\nY_train, Y_test = Y&#91;0:train_size], Y&#91;train_size:len(Y)]\n\nX_train = X_train.reshape(X_train.shape&#91;0], X_train.shape&#91;1], 1)\nX_test = X_test.reshape(X_test.shape&#91;0], X_test.shape&#91;1], 1)<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>\u6b65\u9aa4\u56db\uff1a\u6784\u5efa\u548c\u8bad\u7ec3 LSTM \u6a21\u578b<\/strong><\/h3>\n\n\n\n<p>\u901a\u8fc7 L2 \u6b63\u5219\u5316\u521b\u5efa\u5e76\u8bad\u7ec3 LSTM \u6a21\u578b\uff0c\u4ee5\u907f\u514d\u8fc7\u5ea6\u62df\u5408\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>import tensorflow as tf\nfrom tensorflow.keras.models import Sequential\nfrom tensorflow.keras.layers import LSTM, Dense\nfrom tensorflow.keras.regularizers import l2\n\nmodel = Sequential()\nmodel.add(LSTM(50, return_sequences=True, input_shape=(time_step, 1), kernel_regularizer=l2(0.01)))\nmodel.add(LSTM(50, return_sequences=False, kernel_regularizer=l2(0.01)))\nmodel.add(Dense(25, kernel_regularizer=l2(0.01)))\nmodel.add(Dense(1))\n\nmodel.compile(optimizer='adam', loss='mean_squared_error')\nmodel.fit(X_train, Y_train, batch_size=1, epochs=1)<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>\u6b65\u9aa4\u4e94\uff1a\u9884\u6d4b\u548c\u8bc4\u4f30<\/strong><\/h3>\n\n\n\n<p>\u8fdb\u884c\u9884\u6d4b\u5e76\u8bc4\u4f30\u5176\u6027\u80fd\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>from sklearn.metrics import mean_squared_error\nimport math\n\ntrain_predict = model.predict(X_train)\ntest_predict = model.predict(X_test)\n\ntrain_predict = scaler.inverse_transform(train_predict)\ntest_predict = scaler.inverse_transform(test_predict)\n\ntrain_rmse = math.sqrt(mean_squared_error(Y_train, train_predict))\ntest_rmse = math.sqrt(mean_squared_error(Y_test, test_predict))\nprint(f'Train RMSE: {train_rmse}, Test RMSE: {test_rmse}')<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>\u6b65\u9aa4\u516d\uff1a\u7ed3\u679c\u53ef\u89c6\u5316<\/strong><\/h3>\n\n\n\n<p>\u6700\u540e\uff0c\u6211\u4eec\u5c06\u5b9e\u9645\u4ef7\u683c\u4e0e\u9884\u6d4b\u4ef7\u683c\u8fdb\u884c\u53ef\u89c6\u5316\u5bf9\u6bd4\u3002\u8bf7\u6ce8\u610f\uff0cX \u8f74\u4ee3\u8868\u7684\u662f\u65e5\u671f\u6570\uff0c\u800c\u4e0d\u662f\u5b9e\u9645\u65e5\u5386\u65e5\u671f\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>import matplotlib.pyplot as plt\n\nlook_back = time_step\ntrain_predict_plot = np.empty_like(scaled_data)\ntrain_predict_plot&#91;:, :] = np.nan\ntrain_predict_plot&#91;look_back:len(train_predict) + look_back, :] = train_predict\n\ntest_predict_plot = np.empty_like(scaled_data)\ntest_predict_plot&#91;:, :] = np.nan\ntest_predict_plot&#91;len(train_predict) + look_back:len(train_predict) + look_back + len(test_predict), :] = test_predict\n\nplt.figure(figsize=(14, 8))\nplt.plot(scaler.inverse_transform(scaled_data), label='Actual Stock Price')\nplt.plot(train_predict_plot, label='Train Predict')\nplt.plot(test_predict_plot, label='Test Predict')\nplt.legend()\nplt.xlabel('Number of Dates')\nplt.ylabel('Stock Price')\nplt.title('Stock Price Prediction with LSTM')\nplt.show()<\/code><\/pre>\n\n\n\n<figure class=\"wp-block-image size-full\"><img decoding=\"async\" src=\"https:\/\/www.laoyulaoyu.com\/wp-content\/uploads\/2024\/12\/image-53.png\" alt=\"\" class=\"wp-image-3291\"\/><\/figure>\n\n\n\n<p>\u53ef\u8fd0\u884c\u4ee3\u7801\u548c Google Colab \u7b14\u8bb0\u672c\u5730\u5740\u5982\u4e0b\uff1a<br><a href=\"https:\/\/colab.research.google.com\/drive\/1qorTenNGaOCrcxhAQEYkUi46FWhRGkng?usp=sharing\">https:\/\/colab.research.google.com\/drive\/1qorTenNGaOCrcxhAQEYkUi46FWhRGkng?usp=sharing<\/a><\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><br>\u4e8c\u3001<strong>\u89c2\u70b9\u603b\u7ed3<\/strong><\/h2>\n\n\n\n<p><strong>\u9884\u6d4b\u6a21\u578b\u5728\u80a1\u7968\u4ea4\u6613\u4e2d\u7684\u91cd\u8981\u6027<\/strong>\uff1a\u901a\u8fc7\u4f7f\u7528LSTM\u6a21\u578b\u8fdb\u884c\u80a1\u7968\u4ef7\u683c\u9884\u6d4b\uff0c\u4ea4\u6613\u8005\u53ef\u4ee5\u66f4\u597d\u5730\u5224\u65ad\u4e70\u5356\u65f6\u673a\uff0c\u4ece\u800c\u63d0\u9ad8\u4ea4\u6613\u6548\u7387\u3002<\/p>\n\n\n\n<p><strong>\u5b9e\u7528\u6027\u548c\u64cd\u4f5c\u6027<\/strong>\uff1a\u672c\u6587\u5185\u5bb9\u5177\u6709\u5f88\u5f3a\u7684\u5b9e\u7528\u6027\uff0c\u901a\u8fc7\u5177\u4f53\u7684\u4ee3\u7801\u793a\u4f8b\u548c\u6b65\u9aa4\u6307\u5bfc\uff0c\u4f7f\u5f97\u8bfb\u8005\u80fd\u591f\u6e05\u6670\u5730\u7406\u89e3\u5982\u4f55\u5b9e\u65bd\u80a1\u7968\u4ef7\u683c\u9884\u6d4b\u3002<\/p>\n\n\n\n<p><strong>\u6a21\u578b\u8bc4\u4f30\u4e0e\u98ce\u9669\u7ba1\u7406<\/strong>\uff1a\u5f3a\u8c03\u4e86\u6a21\u578b\u8bc4\u4f30\u7684\u91cd\u8981\u6027\uff0c\u901a\u8fc7\u8ba1\u7b97RMSE\u6765\u8861\u91cf\u6a21\u578b\u7684\u9884\u6d4b\u51c6\u786e\u6027\uff0c\u5e0c\u671b\u4ea4\u6613\u8005\u5728\u5b9e\u9645\u4ea4\u6613\u4e2d\u7ed3\u5408\u98ce\u9669\u7ba1\u7406\u7b56\u7565\uff0c\u6bd4\u5982\u7eb3\u5165\u6b62\u635f\u8ba2\u5355\u7b49\u98ce\u9669\u7ba1\u7406\u7b56\u7565\uff0c\u4ee5\u9632\u8303\u610f\u5916\u7684\u5e02\u573a\u6ce2\u52a8\u3002<\/p>\n\n\n\n<p><strong>\u6301\u7eed\u5b66\u4e60\u4e0e\u6a21\u578b\u66f4\u65b0<\/strong>\uff1a\u5229\u7528\u6700\u65b0\u6570\u636e\u5b9a\u671f\u66f4\u65b0\u6a21\u578b\uff0c\u4ee5\u4fdd\u6301\u5176\u5728\u80a1\u7968\u5e02\u573a\u4e2d\u7684\u9884\u6d4b\u80fd\u529b\u3002<\/p>\n\n\n\n<p><em>\u611f\u8c22\u60a8\u9605\u8bfb\u5230\u6700\u540e\uff0c\u5e0c\u671b\u672c\u6587\u80fd\u7ed9\u60a8\u5e26\u6765\u65b0\u7684\u6536\u83b7\u3002\u795d\u60a8\u6295\u8d44\u987a\u5229\uff01\u5982\u679c\u5bf9\u6587\u4e2d\u7684\u5185\u5bb9\u6709\u4efb\u4f55\u7591\u95ee\uff0c\u8bf7\u7ed9\u6211\u7559\u8a00\uff0c\u5fc5\u590d\u3002<\/em><\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<p class=\"has-text-align-center\" id=\"a1c6\">\u672c\u6587\u5185\u5bb9\u4ec5\u9650\u6280\u672f\u63a2\u8ba8\u548c\u5b66\u4e60\uff0c\u4e0d\u6784\u6210\u4efb\u4f55\u6295\u8d44\u5efa\u8bae\u3002<\/p>\n\n\n\n<p><\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u4f5c\u8005\uff1a\u8001\u4f59\u635e\u9c7c \u539f\u521b\u4e0d\u6613\uff0c\u8f6c\u8f7d\u8bf7\u6807\u660e\u51fa\u5904\u53ca\u539f\u4f5c\u8005\u3002&#8230;<\/p>\n<div class=\"more-link-wrapper\"><a class=\"more-link\" href=\"https:\/\/www.laoyulaoyu.com\/index.php\/2025\/01\/23\/%e7%b2%be%e5%87%86%e6%8a%8a%e6%8f%a1%e4%bd%8e%e4%b9%b0%e9%ab%98%e5%8d%96%e6%97%b6%e6%9c%ba%ef%bc%9alstm%e6%a8%a1%e5%9e%8b%e5%8a%a9%e5%8a%9b%e8%82%a1%e7%a5%a8%e9%a2%84%e6%b5%8b%e5%88%86%e6%9e%90\/\">Continue reading<span class=\"screen-reader-text\">\u7cbe\u51c6\u628a\u63e1\u4f4e\u4e70\u9ad8\u5356\u65f6\u673a\uff1aLSTM\u6a21\u578b\u52a9\u529b\u80a1\u7968\u9884\u6d4b\u5206\u6790\uff08\u9644\u5b8c\u6574\u6e90\u7801\uff09<\/span><\/a><\/div>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[2],"tags":[5,6],"class_list":["post-1757","post","type-post","status-publish","format-standard","hentry","category-aiinvest","tag-ai","tag-6","entry"],"_links":{"self":[{"href":"https:\/\/www.laoyulaoyu.com\/index.php\/wp-json\/wp\/v2\/posts\/1757","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.laoyulaoyu.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.laoyulaoyu.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.laoyulaoyu.com\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.laoyulaoyu.com\/index.php\/wp-json\/wp\/v2\/comments?post=1757"}],"version-history":[{"count":2,"href":"https:\/\/www.laoyulaoyu.com\/index.php\/wp-json\/wp\/v2\/posts\/1757\/revisions"}],"predecessor-version":[{"id":1760,"href":"https:\/\/www.laoyulaoyu.com\/index.php\/wp-json\/wp\/v2\/posts\/1757\/revisions\/1760"}],"wp:attachment":[{"href":"https:\/\/www.laoyulaoyu.com\/index.php\/wp-json\/wp\/v2\/media?parent=1757"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.laoyulaoyu.com\/index.php\/wp-json\/wp\/v2\/categories?post=1757"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.laoyulaoyu.com\/index.php\/wp-json\/wp\/v2\/tags?post=1757"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}