{"id":1888,"date":"2025-02-09T07:44:00","date_gmt":"2025-02-08T23:44:00","guid":{"rendered":"https:\/\/blog.laoyulaoyu.top\/?p=1888"},"modified":"2025-01-11T11:56:25","modified_gmt":"2025-01-11T03:56:25","slug":"%e8%b6%85%e8%b6%8alstm%ef%bc%81tcn%e6%a8%a1%e5%9e%8b%e5%a6%82%e4%bd%95%e7%b2%be%e5%87%86%e9%a2%84%e6%b5%8b%e8%82%a1%e5%b8%82%e6%b3%a2%e5%8a%a8%e9%99%84%e4%bb%a3%e7%a0%81","status":"publish","type":"post","link":"https:\/\/laoyulaoyu.com\/index.php\/2025\/02\/09\/%e8%b6%85%e8%b6%8alstm%ef%bc%81tcn%e6%a8%a1%e5%9e%8b%e5%a6%82%e4%bd%95%e7%b2%be%e5%87%86%e9%a2%84%e6%b5%8b%e8%82%a1%e5%b8%82%e6%b3%a2%e5%8a%a8%e9%99%84%e4%bb%a3%e7%a0%81\/","title":{"rendered":"\u8d85\u8d8aLSTM\uff01TCN\u6a21\u578b\u5982\u4f55\u7cbe\u51c6\u9884\u6d4b\u80a1\u5e02\u6ce2\u52a8(\u9644\u4ee3\u7801)"},"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\/2025\/01\/image-65.png\" alt=\"\" class=\"wp-image-3846\"\/><\/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>\u6700\u8fd1\u6211\u7528<mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-vivid-cyan-blue-color\">TCN\u65f6\u95f4\u5377\u79ef\u7f51\u7edc\u9884\u6d4b\u4e86\u6807\u666e500\u6307\u6570\uff08SPX\uff09\u7684\u6bcf\u65e5\u56de\u62a5\u7387\uff0c\u53d1\u73b0\u6548\u679c\u8fdc\u8d85\u4f20\u7edf\u65b9\u6cd5<\/mark>\u3002TCN\u901a\u8fc7\u56e0\u679c\u5377\u79ef\u548c\u81a8\u80c0\u5377\u79ef\u6355\u6349\u65f6\u95f4\u5e8f\u5217\u7684\u957f\u671f\u4f9d\u8d56\u5173\u7cfb\uff0c\u7ed3\u5408\u6b8b\u5dee\u8fde\u63a5\u63d0\u5347\u6a21\u578b\u6027\u80fd\u3002\u8fd9\u7bc7\u6587\u7ae0\u5c06\u5e26\u4f60\u4ece\u6570\u636e\u51c6\u5907\u5230\u6a21\u578b\u8bad\u7ec3\uff0c\u4e00\u6b65\u6b65\u5b9e\u73b0\u91d1\u878d\u9884\u6d4b\u7684AI\u5b9e\u6218\u3002<\/pre>\n<\/blockquote>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>\u4e00\u3001\u4ec0\u4e48\u662f\u65f6\u5e8f\u5377\u79ef\u7f51\u7edc\uff08TCN\uff09\uff1f<\/strong><\/h2>\n\n\n\n<p>\u65f6\u5e8f\u5377\u79ef\u7f51\u7edc\uff08Temporal Convolutional Network, TCN\uff09\u662f\u4e00\u79cd\u7528\u4e8e\u5904\u7406\u5e8f\u5217\u6570\u636e\u7684\u6df1\u5ea6\u5b66\u4e60\u6a21\u578b\u3002\u4e0e\u4f20\u7edf\u7684\u5faa\u73af\u795e\u7ecf\u7f51\u7edc\uff08RNN\uff09\u4e0d\u540c\uff0cTCN\u5229\u7528\u5377\u79ef\u64cd\u4f5c\u6765\u6355\u6349\u65f6\u95f4\u5e8f\u5217\u4e2d\u7684\u4f9d\u8d56\u5173\u7cfb\u3002TCN\u901a\u8fc7\u56e0\u679c\u5377\u79ef\u548c\u6269\u5f20\u5377\u79ef\u7684\u7ec4\u5408\uff0c\u80fd\u591f\u6709\u6548\u5730\u5904\u7406\u957f\u5e8f\u5217\u6570\u636e\uff0c\u5e76\u4e14\u5728\u8bb8\u591a\u4efb\u52a1\u4e2d\u8868\u73b0\u51fa\u8272\uff0c\u5982\u65f6\u95f4\u5e8f\u5217\u9884\u6d4b\u3001\u8bed\u97f3\u5904\u7406\u548c\u81ea\u7136\u8bed\u8a00\u5904\u7406\u7b49\u3002<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>1.1 TCN\u7684\u57fa\u672c\u539f\u7406<\/strong><\/h3>\n\n\n\n<p>TCN\u7684\u6838\u5fc3\u601d\u60f3\u662f\u4f7f\u7528\u5377\u79ef\u5c42\u6765\u66ff\u4ee3RNN\u4e2d\u7684\u9012\u5f52\u7ed3\u6784\u3002\u5176\u4e3b\u8981\u7279\u70b9\u5305\u62ec\uff1a<\/p>\n\n\n\n<figure class=\"wp-block-image size-full is-resized\"><img decoding=\"async\" src=\"https:\/\/www.laoyulaoyu.com\/wp-content\/uploads\/2025\/01\/image-66.png\" alt=\"\" class=\"wp-image-3847\" style=\"width:594px;height:auto\"\/><\/figure>\n\n\n\n<ul id=\"bh-p3SGRAF6urV-4FdLxk8ur\" class=\"wp-block-list\">\n<li><strong>\u56e0\u679c\u5377\u79ef<\/strong>\uff1a\u786e\u4fdd\u5f53\u524d\u65f6\u523b\u7684\u8f93\u51fa\u4ec5\u4f9d\u8d56\u4e8e\u5f53\u524d\u53ca\u4e4b\u524d\u7684\u8f93\u5165\uff0c\u907f\u514d\u672a\u6765\u4fe1\u606f\u7684\u6cc4\u9732\u3002<\/li>\n\n\n\n<li><strong>\u6269\u5f20\u5377\u79ef<\/strong>\uff1a\u901a\u8fc7\u5728\u5377\u79ef\u6838\u4e4b\u95f4\u5f15\u5165\u95f4\u9694\uff0c\u4f7f\u5f97\u7f51\u7edc\u80fd\u591f\u5728\u4e0d\u589e\u52a0\u8ba1\u7b97\u590d\u6742\u5ea6\u7684\u60c5\u51b5\u4e0b\uff0c\u6355\u6349\u66f4\u957f\u8303\u56f4\u7684\u4f9d\u8d56\u5173\u7cfb\u3002<\/li>\n\n\n\n<li><strong>\u6b8b\u5dee\u8fde\u63a5<\/strong>\uff1a\u901a\u8fc7\u5f15\u5165\u6b8b\u5dee\u8fde\u63a5\uff0cTCN\u80fd\u591f\u66f4\u597d\u5730\u8bad\u7ec3\u6df1\u5c42\u7f51\u7edc\uff0c\u51cf\u8f7b\u68af\u5ea6\u6d88\u5931\u7684\u95ee\u9898\u3002<\/li>\n<\/ul>\n\n\n\n<figure class=\"wp-block-image size-full\"><img decoding=\"async\" src=\"https:\/\/www.laoyulaoyu.com\/wp-content\/uploads\/2025\/01\/image-69.png\" alt=\"\" class=\"wp-image-3850\"\/><\/figure>\n\n\n\n<p>\u4e0a\u4e3aTCN\u7684\u7b80\u5355\u67b6\u6784\u793a\u610f\u56fe\u3002<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>1.2 TCN\u7684\u4f18\u70b9<\/strong><\/h3>\n\n\n\n<figure class=\"wp-block-image size-full is-resized\"><img decoding=\"async\" src=\"https:\/\/www.laoyulaoyu.com\/wp-content\/uploads\/2025\/01\/image-67.png\" alt=\"\" class=\"wp-image-3848\" style=\"width:554px;height:auto\"\/><\/figure>\n\n\n\n<ul id=\"bh-eGo_JWlLBpnqhf-wN-ue1\" class=\"wp-block-list\">\n<li><strong>\u5e76\u884c\u8ba1\u7b97<\/strong>\uff1a\u4e0eRNN\u4e0d\u540c\uff0cTCN\u7684\u5377\u79ef\u64cd\u4f5c\u53ef\u4ee5\u5e76\u884c\u8ba1\u7b97\uff0c\u663e\u8457\u63d0\u9ad8\u8bad\u7ec3\u901f\u5ea6\u3002<\/li>\n\n\n\n<li><strong>\u957f\u8ddd\u79bb\u4f9d\u8d56<\/strong>\uff1a\u6269\u5f20\u5377\u79ef\u4f7f\u5f97TCN\u80fd\u591f\u6709\u6548\u6355\u6349\u957f\u8ddd\u79bb\u7684\u65f6\u95f4\u4f9d\u8d56\u5173\u7cfb\u3002<\/li>\n\n\n\n<li><strong>\u7075\u6d3b\u6027<\/strong>\uff1aTCN\u53ef\u4ee5\u8f7b\u677e\u8c03\u6574\u5377\u79ef\u6838\u7684\u5927\u5c0f\u548c\u6269\u5f20\u56e0\u5b50\uff0c\u4ee5\u9002\u5e94\u4e0d\u540c\u7684\u5e8f\u5217\u957f\u5ea6\u548c\u7279\u5f81\u3002<\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>1.3  TCN\u7684\u5e94\u7528\u573a\u666f<\/strong><\/h3>\n\n\n\n<p>TCN\u5728\u591a\u4e2a\u9886\u57df\u5f97\u5230\u4e86\u5e7f\u6cdb\u5e94\u7528\uff0c\u5305\u62ec\u4f46\u4e0d\u9650\u4e8e\uff1a<\/p>\n\n\n\n<ul id=\"bh-bFSvDQVKdxcWGZItm7O7Q\" class=\"wp-block-list\">\n<li><strong>\u65f6\u95f4\u5e8f\u5217\u9884\u6d4b<\/strong>\uff1a\u5982\u80a1\u5e02\u9884\u6d4b\u3001\u6c14\u8c61\u9884\u6d4b\u7b49\u3002<\/li>\n\n\n\n<li><strong>\u8bed\u97f3\u8bc6\u522b<\/strong>\uff1a\u5904\u7406\u97f3\u9891\u4fe1\u53f7\u4e2d\u7684\u65f6\u95f4\u7279\u5f81\u3002<\/li>\n\n\n\n<li><strong>\u81ea\u7136\u8bed\u8a00\u5904\u7406<\/strong>\uff1a\u7528\u4e8e\u6587\u672c\u751f\u6210\u548c\u60c5\u611f\u5206\u6790\u7b49\u4efb\u52a1\u3002<\/li>\n<\/ul>\n\n\n\n<figure class=\"wp-block-image size-full is-resized\"><img decoding=\"async\" src=\"https:\/\/www.laoyulaoyu.com\/wp-content\/uploads\/2025\/01\/image-68.png\" alt=\"\" class=\"wp-image-3849\" style=\"width:517px;height:auto\"\/><\/figure>\n\n\n\n<p>\u65f6\u5e8f\u5377\u79ef\u7f51\u7edc\uff08TCN\uff09\u662f\u4e00\u79cd\u5f3a\u5927\u7684\u5e8f\u5217\u5efa\u6a21\u5de5\u5177\uff0c\u51ed\u501f\u5176\u72ec\u7279\u7684\u5377\u79ef\u7ed3\u6784\u548c\u9ad8\u6548\u7684\u8bad\u7ec3\u65b9\u5f0f\uff0c\u5728\u5904\u7406\u65f6\u95f4\u5e8f\u5217\u6570\u636e\u65b9\u9762\u5c55\u73b0\u4e86\u4f18\u8d8a\u7684\u6027\u80fd\u3002\u968f\u7740\u6df1\u5ea6\u5b66\u4e60\u6280\u672f\u7684\u4e0d\u65ad\u53d1\u5c55\uff0cTCN\u6709\u671b\u5728\u66f4\u591a\u5e94\u7528\u573a\u666f\u4e2d\u53d1\u6325\u91cd\u8981\u4f5c\u7528\u3002<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>\u4e8c\u3001TCN\u7684\u5e94\u7528\u5b9e\u4f8b<\/strong><\/h2>\n\n\n\n<p>\u4e0b\u9762\u8fd9\u4e2a\u5b9e\u4f8b\u5c06\u57fa\u4e8e\u6807\u666e500\u6307\u6570\uff08SPX\uff09\u8fc7\u53bb15\u5e74\u7684\u5386\u53f2\u6570\u636e\u8fdb\u884c\u6a21\u578b\u8bad\u7ec3\u3002<\/p>\n\n\n\n<p>\u4e3a\u4e86\u63d0\u9ad8\u6a21\u578b\u7684\u7a33\u5b9a\u6027\u548c\u9884\u6d4b\u6548\u679c\uff0c\u6211\u4eec\u9009\u62e9\u4f7f\u7528\u6536\u76ca\u5e8f\u5217\u800c\u975e\u4ef7\u683c\u5e8f\u5217\uff0c\u56e0\u4e3a\u6536\u76ca\u5e8f\u5217\u5177\u6709\u66f4\u597d\u7684\u9759\u6001\u7279\u6027\u3002<\/p>\n\n\n\n<p>\u6b64\u5916\uff0c\u5728\u7279\u5f81\u5de5\u7a0b\u73af\u8282\uff0c\u6211\u4eec\u8fd8\u5f15\u5165\u4e86\u6700\u8fd110\u5929\u7684\u6ce2\u52a8\u7387\u548c\u6210\u4ea4\u91cf\u6570\u636e\u4f5c\u4e3a\u8865\u5145\u7279\u5f81\uff0c\u8fd9\u4e9b\u6570\u636e\u80fd\u591f\u6709\u6548\u6355\u6349\u5e02\u573a\u52a8\u6001\uff0c\u4ece\u800c\u8fdb\u4e00\u6b65\u63d0\u5347\u9884\u6d4b\u7684\u51c6\u786e\u6027\u3002<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>2.1 \u5bfc\u5165\u5e93<\/strong><\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>import numpy as np\nimport pandas as pd\nimport matplotlib.pyplot as plt\nfrom sklearn.preprocessing import StandardScaler\nfrom sklearn.model_selection import train_test_split\nfrom tensorflow.keras import layers, models\nimport yfinance as yf<\/code><\/pre>\n\n\n\n<ul class=\"wp-block-list\">\n<li><code>numpy<\/code>&nbsp;\u548c&nbsp;<code>pandas<\/code>&nbsp;\u7528\u4e8e\u6570\u636e\u5904\u7406\u3002<\/li>\n\n\n\n<li><code>matplotlib.pyplot<\/code>&nbsp;\u7528\u4e8e\u7ed8\u56fe\u3002<\/li>\n\n\n\n<li><code>StandardScaler<\/code>&nbsp;\u7528\u4e8e\u7279\u5f81\u6807\u51c6\u5316\u3002<\/li>\n\n\n\n<li><code>train_test_split<\/code>&nbsp;\u7528\u4e8e\u5212\u5206\u8bad\u7ec3\u96c6\u548c\u6d4b\u8bd5\u96c6\u3002<\/li>\n\n\n\n<li><code>tensorflow.keras<\/code>&nbsp;\u7528\u4e8e\u6784\u5efa\u548c\u8bad\u7ec3\u6df1\u5ea6\u5b66\u4e60\u6a21\u578b\u3002<\/li>\n\n\n\n<li><code>yfinance<\/code>&nbsp;\u7528\u4e8e\u4ece Yahoo Finance \u83b7\u53d6\u91d1\u878d\u6570\u636e\u3002<\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>2.2 \u6570\u636e\u51c6\u5907<\/strong><\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>data_spx = yf.download(\"^GSPC\", start=\"2010-01-01\", end=\"2024-12-01\")\nprice = data_spx&#91;'Adj Close']\nvolume = data_spx&#91;'Volume']<\/code><\/pre>\n\n\n\n<p>\u4f7f\u7528&nbsp;<code>yfinance<\/code>&nbsp;\u4e0b\u8f7d\u6807\u666e500\u6307\u6570\uff08SPX\uff09\u4ece2010\u5e741\u67081\u65e5\u52302024\u5e7412\u67081\u65e5\u7684\u8c03\u6574\u540e\u6536\u76d8\u4ef7\u548c\u6210\u4ea4\u91cf\u6570\u636e\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>data = pd.DataFrame({\n    'Price': price,\n    'Volume': volume\n})<\/code><\/pre>\n\n\n\n<p>\u5c06\u4ef7\u683c\u548c\u6210\u4ea4\u91cf\u6570\u636e\u5b58\u50a8\u5728\u4e00\u4e2a&nbsp;<code>DataFrame<\/code>&nbsp;\u4e2d\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>data&#91;'Return'] = np.log(data&#91;'Price'] \/ data&#91;'Price'].shift(1))<\/code><\/pre>\n\n\n\n<p>\u8ba1\u7b97\u5bf9\u6570\u6536\u76ca\u7387\uff08log returns\uff09\uff0c\u5373\u6bcf\u65e5\u4ef7\u683c\u53d8\u5316\u7684\u5bf9\u6570\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>rolling_window = 10\ndata&#91;'Volatility'] = data&#91;'Return'].rolling(window=rolling_window).std()<\/code><\/pre>\n\n\n\n<p>\u8ba1\u7b9710\u5929\u6eda\u52a8\u7a97\u53e3\u7684\u6ce2\u52a8\u7387\uff08volatility\uff09\uff0c\u5373\u6536\u76ca\u7387\u7684\u6eda\u52a8\u6807\u51c6\u5dee\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>data&#91;'LogVolume'] = np.log(data&#91;'Volume'] + 1)<\/code><\/pre>\n\n\n\n<p>\u5bf9\u6210\u4ea4\u91cf\u8fdb\u884c\u5bf9\u6570\u53d8\u6362\uff0c\u4ee5\u51cf\u5c0f\u6570\u636e\u7684\u5c3a\u5ea6\u5dee\u5f02\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>data = data.dropna()<\/code><\/pre>\n\n\n\n<p>\u5220\u9664\u7531\u4e8e\u6eda\u52a8\u64cd\u4f5c\u4ea7\u751f\u7684&nbsp;<code>NaN<\/code>&nbsp;\u503c\u3002<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>2.3 \u7279\u5f81\u548c\u6807\u7b7e\u51c6\u5907<\/strong><\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>features = data&#91;&#91;'Return', 'Volatility', 'LogVolume']].values\nlabels = data&#91;'Return'].shift(-10).dropna().values<\/code><\/pre>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\u7279\u5f81\u5305\u62ec\u6536\u76ca\u7387\u3001\u6ce2\u52a8\u7387\u548c\u5bf9\u6570\u6210\u4ea4\u91cf\u3002<\/li>\n\n\n\n<li>\u6807\u7b7e\u662f\u672a\u676510\u5929\u7684\u6536\u76ca\u7387\u3002<\/li>\n<\/ul>\n\n\n\n<pre class=\"wp-block-code\"><code>features = features&#91;:-10]<\/code><\/pre>\n\n\n\n<p>\u5bf9\u9f50\u7279\u5f81\u548c\u6807\u7b7e\uff0c\u786e\u4fdd\u7279\u5f81\u548c\u6807\u7b7e\u7684\u957f\u5ea6\u4e00\u81f4\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>scaler = StandardScaler()\nfeatures = scaler.fit_transform(features)<\/code><\/pre>\n\n\n\n<p>\u5bf9\u7279\u5f81\u8fdb\u884c\u6807\u51c6\u5316\u5904\u7406\uff0c\u4f7f\u5176\u5747\u503c\u4e3a0\uff0c\u6807\u51c6\u5dee\u4e3a1\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>sequence_length = 30\nX, y = &#91;], &#91;]\nfor i in range(len(features) - sequence_length):\n    X.append(features&#91;i:i + sequence_length])\n    y.append(labels&#91;i + sequence_length - 1])\nX, y = np.array(X), np.array(y)<\/code><\/pre>\n\n\n\n<p>\u5c06\u7279\u5f81\u6570\u636e\u8f6c\u6362\u4e3a\u65f6\u95f4\u5e8f\u5217\u683c\u5f0f\uff0c\u6bcf\u4e2a\u6837\u672c\u5305\u542b30\u4e2a\u65f6\u95f4\u6b65\u7684\u7279\u5f81\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)<\/code><\/pre>\n\n\n\n<p>\u5c06\u6570\u636e\u96c6\u5212\u5206\u4e3a\u8bad\u7ec3\u96c6\u548c\u6d4b\u8bd5\u96c6\uff0c\u6d4b\u8bd5\u96c6\u536020%\u3002<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>2.4 TCN\u6a21\u578b\u5b9a\u4e49<\/strong><\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>model = models.Sequential(&#91;\n    layers.Input(shape=(sequence_length, X.shape&#91;2])),\n    layers.Conv1D(filters=64, kernel_size=3, dilation_rate=1, activation='relu'),\n    layers.Conv1D(filters=64, kernel_size=3, dilation_rate=2, activation='relu'),\n    layers.GlobalAveragePooling1D(),\n    layers.Dense(1)\n])<\/code><\/pre>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\u5b9a\u4e49\u4e00\u4e2a\u7b80\u5355\u7684TCN\u6a21\u578b\uff0c\u5305\u542b\u4e24\u4e2a1D\u5377\u79ef\u5c42\uff0c\u5206\u522b\u4f7f\u7528\u4e0d\u540c\u7684\u81a8\u80c0\u7387\uff08dilation rate\uff09\u3002<\/li>\n\n\n\n<li>\u4f7f\u7528\u5168\u5c40\u5e73\u5747\u6c60\u5316\u5c42\uff08GlobalAveragePooling1D\uff09\u5c06\u65f6\u95f4\u7ef4\u5ea6\u538b\u7f29\u4e3a\u5355\u4e2a\u503c\u3002<\/li>\n\n\n\n<li>\u6700\u540e\u662f\u4e00\u4e2a\u5168\u8fde\u63a5\u5c42\uff08Dense\uff09\uff0c\u8f93\u51fa\u672a\u676510\u5929\u7684\u6536\u76ca\u7387\u9884\u6d4b\u3002<\/li>\n<\/ul>\n\n\n\n<pre class=\"wp-block-code\"><code>model.compile(optimizer='adam', loss='mse')<\/code><\/pre>\n\n\n\n<p>\u4f7f\u7528Adam\u4f18\u5316\u5668\u548c\u5747\u65b9\u8bef\u5dee\uff08MSE\uff09\u4f5c\u4e3a\u635f\u5931\u51fd\u6570\u6765\u7f16\u8bd1\u6a21\u578b\u3002<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>2.5 \u6a21\u578b\u8bad\u7ec3<\/strong><\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>epochs = 1000\nbatch_size = 32\nhistory = model.fit(X_train, y_train, validation_data=(X_test, y_test), epochs=epochs, batch_size=batch_size)<\/code><\/pre>\n\n\n\n<p>\u8bad\u7ec3\u6a21\u578b\uff0c\u8bbe\u7f6e1000\u4e2aepoch\uff0c\u6279\u91cf\u5927\u5c0f\u4e3a32\u3002<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>2.6 \u6a21\u578b\u8bc4\u4f30<\/strong><\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>y_pred = model.predict(X_test)<\/code><\/pre>\n\n\n\n<p>\u4f7f\u7528\u6d4b\u8bd5\u96c6\u8fdb\u884c\u9884\u6d4b\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>plt.figure(figsize=(10, 6))\nplt.plot(y_test&#91;-50:], label='Actual Returns', alpha=0.7)\nplt.plot(y_pred&#91;-50:], label='Predicted Returns', alpha=0.7)\nplt.title('Comparison of Actual vs Predicted Returns')\nplt.legend()\nplt.show()<\/code><\/pre>\n\n\n\n<p>\u7ed8\u5236\u5b9e\u9645\u6536\u76ca\u7387\u548c\u9884\u6d4b\u6536\u76ca\u7387\u7684\u5bf9\u6bd4\u56fe\u3002<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>2.7 \u4fdd\u5b58\u6a21\u578b\u548c\u7ed3\u679c<\/strong><\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>model.save(\"tcn_model.h5\")<\/code><\/pre>\n\n\n\n<p>\u5c06\u8bad\u7ec3\u597d\u7684\u6a21\u578b\u4fdd\u5b58\u4e3a&nbsp;<code>tcn_model.h5<\/code>&nbsp;\u6587\u4ef6\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>model.summary()<\/code><\/pre>\n\n\n\n<p>\u6253\u5370\u6a21\u578b\u7684\u6458\u8981\u4fe1\u606f\u3002<\/p>\n\n\n\n<p>\u4e0b\u56fe\u4e3aTCN \u57fa\u7840\u9884\u6d4b\u4e0e SPX \u6700\u8fd1 50 \u5929\u7684\u5b9e\u9645\u56de\u62a5\u7387\u5bf9\u6bd4\u3002<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img decoding=\"async\" src=\"https:\/\/www.laoyulaoyu.com\/wp-content\/uploads\/2025\/01\/image-70.png\" alt=\"\" class=\"wp-image-3851\"\/><\/figure>\n\n\n\n<p>\u8fd9\u6bb5\u4ee3\u7801\u5b9e\u73b0\u4e86\u4e00\u4e2a\u57fa\u4e8eTCN\u7684\u6a21\u578b\uff0c\u7528\u4e8e\u9884\u6d4b\u6807\u666e500\u6307\u6570\u672a\u676510\u5929\u7684\u6536\u76ca\u7387\u3002\u4ee3\u7801\u6db5\u76d6\u4e86\u6570\u636e\u83b7\u53d6\u3001\u9884\u5904\u7406\u3001\u6a21\u578b\u6784\u5efa\u3001\u8bad\u7ec3\u3001\u8bc4\u4f30\u548c\u4fdd\u5b58\u7684\u5b8c\u6574\u6d41\u7a0b\u3002<\/p>\n\n\n\n<p><strong>\u5168\u6e90\u4ee3\u7801\u5982\u4e0b\uff1a<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>import numpy as np\nimport pandas as pd\nimport matplotlib.pyplot as plt\nfrom sklearn.preprocessing import StandardScaler\nfrom sklearn.model_selection import train_test_split\nfrom tensorflow.keras import layers, models\nimport yfinance as yf\n\n# === Data Preparation ===\n# Simulate example data (replace this with actual SPX data)\n#np.random.seed(42)\n#n_days = 1000\n#price = np.cumprod(1 + np.random.normal(0, 0.01, n_days)) * 1000\n#volume = np.random.randint(1e6, 1e7, n_days)\n\n# Step 1: Fetch SPX data\ndata_spx = yf.download(\"^GSPC\", start=\"2010-01-01\", end=\"2024-12-01\")\nprice = data_spx&#91;'Adj Close']\nvolume = data_spx&#91;'Volume']\n\n# Create a DataFrame\ndata = pd.DataFrame({\n    'Price': price,\n    'Volume': volume\n})\n\n# Compute returns (log returns)\ndata&#91;'Return'] = np.log(data&#91;'Price'] \/ data&#91;'Price'].shift(1))\n\n# Compute rolling volatility (10-day window)\nrolling_window = 10\ndata&#91;'Volatility'] = data&#91;'Return'].rolling(window=rolling_window).std()\n\n# Log-transform volume\ndata&#91;'LogVolume'] = np.log(data&#91;'Volume'] + 1)\n\n# Drop NaN values caused by rolling operations\ndata = data.dropna()\n\n# Prepare features and labels\nfeatures = data&#91;&#91;'Return', 'Volatility', 'LogVolume']].values\nlabels = data&#91;'Return'].shift(-10).dropna().values  # Predict 10-day-ahead return\n\n# Align features with labels\nfeatures = features&#91;:-10]\n\n# Standardize features\nscaler = StandardScaler()\nfeatures = scaler.fit_transform(features)\n\n# Reshape features for TCN (samples, timesteps, features)\nsequence_length = 30  # Lookback window\nX, y = &#91;], &#91;]\nfor i in range(len(features) - sequence_length):\n    X.append(features&#91;i:i + sequence_length])\n    y.append(labels&#91;i + sequence_length - 1])\nX, y = np.array(X), np.array(y)\n\n# Split into train and test sets\nX_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)\n\n# === TCN Model Definition ===\n# Define the TCN architecture\nmodel = models.Sequential(&#91;\n    layers.Input(shape=(sequence_length, X.shape&#91;2])),\n    layers.Conv1D(filters=64, kernel_size=3, dilation_rate=1, activation='relu'),\n    layers.Conv1D(filters=64, kernel_size=3, dilation_rate=2, activation='relu'),\n    layers.GlobalAveragePooling1D(),\n    layers.Dense(1)  # Single output for next return prediction\n])\n\n# Compile the model\nmodel.compile(optimizer='adam', loss='mse')\n\n# === Model Training ===\n# Train the model\nepochs = 1000\nbatch_size = 32\nhistory = model.fit(X_train, y_train, validation_data=(X_test, y_test), epochs=epochs, batch_size=batch_size)\n\n# === Model Evaluation ===\n# Predict on the test set\ny_pred = model.predict(X_test)\n\n# Plot actual vs predicted returns\nplt.figure(figsize=(10, 6))\nplt.plot(y_test&#91;-50:], label='Actual Returns', alpha=0.7)\nplt.plot(y_pred&#91;-50:], label='Predicted Returns', alpha=0.7)\nplt.title('Comparison of Actual vs Predicted Returns')\nplt.legend()\nplt.show()\n\n# === Save Model and Results ===\n# Save the model\nmodel.save(\"tcn_model.h5\")\n\n# Print summary\nmodel.summary()<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>\u4e09\u3001\u89c2\u70b9\u603b\u7ed3<\/strong><\/h2>\n\n\n\n<p>\u65f6\u5e8f\u5377\u79ef\u7f51\u7edc\uff08TCN\uff09\u5728\u65f6\u95f4\u5e8f\u5217\u9884\u6d4b\u4efb\u52a1\u4e2d\u8868\u73b0\u5353\u8d8a\uff0c\u4e3b\u8981\u5f97\u76ca\u4e8e\u5176\u51fa\u8272\u7684\u957f\u7a0b\u4f9d\u8d56\u5173\u7cfb\u5efa\u6a21\u80fd\u529b\u3002\u4e0e\u4f20\u7edf\u7684\u9012\u5f52\u67b6\u6784\uff08\u5982LSTM\u6216GRU\uff09\u4e0d\u540c\uff0cTCN\u91c7\u7528\u6269\u5f20\u5377\u79ef\u8fd0\u7b97\uff0c\u80fd\u591f\u9ad8\u6548\u6355\u6349\u957f\u65f6\u95f4\u8de8\u5ea6\u5185\u7684\u65f6\u95f4\u6a21\u5f0f\uff0c\u540c\u65f6\u907f\u514d\u4e86\u9012\u5f52\u6a21\u578b\u4e2d\u5e38\u89c1\u7684\u68af\u5ea6\u6d88\u5931\u95ee\u9898\u3002\u8fd9\u79cd\u72ec\u7279\u7684\u8bbe\u8ba1\u4f7f\u5176\u5728\u5904\u7406\u590d\u6742\u65f6\u95f4\u5e8f\u5217\u6570\u636e\u65f6\u66f4\u5177\u4f18\u52bf\u3002<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>TCN\u7684\u5173\u952e\u7ec4\u6210\u90e8\u5206<\/strong>\u5305\u62ec\u968f\u610f\u5377\u79ef\uff08Casual Convolutions\uff09\u3001\u7a00\u91ca\u5377\u79ef\uff08Dilated Convolutions\uff09\u548c\u6b8b\u5dee\u8fde\u63a5\uff08Residual Connection\uff09\u3002<\/li>\n\n\n\n<li>TCN\u80fd\u591f<strong>\u5e76\u884c\u5904\u7406\u6574\u4e2a\u5e8f\u5217<\/strong>\uff0c\u8fd9\u4f7f\u5f97\u5b83\u6bd4RNN\u66f4\u5feb\u5730\u8bad\u7ec3\u3002<\/li>\n\n\n\n<li>TCN\u901a\u8fc7\u7a00\u91ca\u5377\u79ef\u80fd\u591f<strong>\u6355\u6349\u8df3\u8dc3\u65f6\u95f4\u5e8f\u5217<\/strong>\uff0c\u5e76\u4e14\u80fd\u591f<strong>\u5904\u7406\u957f\u5185\u5b58<\/strong>\u3002<\/li>\n\n\n\n<li>\u901a\u8fc7\u6b8b\u5dee\u8fde\u63a5\u548c\u65e0\u9012\u5f52\uff0cTCN<strong>\u51cf\u5c11\u4e86\u68af\u5ea6\u6d88\u5931\u7b49\u4e0d\u7a33\u5b9a\u6027\u95ee\u9898<\/strong>\u3002<\/li>\n\n\n\n<li>\u5728\u5b9e\u9645\u5e94\u7528\u4e2d\uff0cTCN\u6a21\u578b\u4f7f\u7528\u4e86SPX\u6307\u6570\u7684\u5386\u53f2\u6570\u636e\uff0c\u5305\u62ec<strong>\u6536\u76ca\u7387\u3001\u6ce2\u52a8\u6027\u548c\u6210\u4ea4\u91cf<\/strong>\uff0c\u4ee5\u53ca\u5982\u4f55<strong>\u9884\u6d4b\u672a\u676510\u5929\u7684\u56de\u62a5\u7387<\/strong>\u3002<\/li>\n\n\n\n<li>TCN\u5728\u65f6\u95f4\u5e8f\u5217\u9884\u6d4b\u4efb\u52a1\u4e2d\u7684<strong>\u4f18\u8d8a\u6027\u80fd<\/strong>\uff0c\u5c24\u5176\u662f\u5728\u5904\u7406\u957f\u671f\u4f9d\u8d56\u5173\u7cfb\u548c\u907f\u514d\u68af\u5ea6\u6d88\u5931\u95ee\u9898\u65b9\u9762\u6709\u826f\u597d\u8868\u73b0\u3002<\/li>\n<\/ul>\n\n\n\n<p><\/p>\n\n\n\n<p><em>\u611f\u8c22\u60a8\u9605\u8bfb\u5230\u6700\u540e\uff0c\u5e0c\u671b\u8fd9\u7bc7\u6587\u7ae0\u4e3a\u60a8\u5e26\u6765\u4e86\u65b0\u7684\u542f\u53d1\u548c\u5b9e\u7528\u7684\u77e5\u8bc6\uff01\u5982\u679c\u89c9\u5f97\u6709\u5e2e\u52a9\uff0c\u8bf7\u4e0d\u541d\u70b9\u8d5e\u548c\u5206\u4eab\uff0c\u60a8\u7684\u652f\u6301\u662f\u6211\u6301\u7eed\u521b\u4f5c\u7684\u52a8\u529b\u3002\u795d\u60a8\u6295\u8d44\u987a\u5229\uff0c\u6536\u76ca\u957f\u8679\uff01\u5982\u679c\u5bf9\u6587\u4e2d\u5185\u5bb9\u6709\u4efb\u4f55\u7591\u95ee\uff0c\u6b22\u8fce\u7559\u8a00\uff0c\u6211\u4f1a\u5c3d\u5feb\u56de\u590d\uff01<\/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","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:\/\/laoyulaoyu.com\/index.php\/2025\/02\/09\/%e8%b6%85%e8%b6%8alstm%ef%bc%81tcn%e6%a8%a1%e5%9e%8b%e5%a6%82%e4%bd%95%e7%b2%be%e5%87%86%e9%a2%84%e6%b5%8b%e8%82%a1%e5%b8%82%e6%b3%a2%e5%8a%a8%e9%99%84%e4%bb%a3%e7%a0%81\/\">Continue reading<span class=\"screen-reader-text\">\u8d85\u8d8aLSTM\uff01TCN\u6a21\u578b\u5982\u4f55\u7cbe\u51c6\u9884\u6d4b\u80a1\u5e02\u6ce2\u52a8(\u9644\u4ee3\u7801)<\/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-1888","post","type-post","status-publish","format-standard","hentry","category-aiinvest","tag-ai","tag-6","entry"],"_links":{"self":[{"href":"https:\/\/laoyulaoyu.com\/index.php\/wp-json\/wp\/v2\/posts\/1888","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/laoyulaoyu.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/laoyulaoyu.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/laoyulaoyu.com\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/laoyulaoyu.com\/index.php\/wp-json\/wp\/v2\/comments?post=1888"}],"version-history":[{"count":1,"href":"https:\/\/laoyulaoyu.com\/index.php\/wp-json\/wp\/v2\/posts\/1888\/revisions"}],"predecessor-version":[{"id":1889,"href":"https:\/\/laoyulaoyu.com\/index.php\/wp-json\/wp\/v2\/posts\/1888\/revisions\/1889"}],"wp:attachment":[{"href":"https:\/\/laoyulaoyu.com\/index.php\/wp-json\/wp\/v2\/media?parent=1888"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/laoyulaoyu.com\/index.php\/wp-json\/wp\/v2\/categories?post=1888"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/laoyulaoyu.com\/index.php\/wp-json\/wp\/v2\/tags?post=1888"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}