{"id":1381,"date":"2024-08-31T07:39:00","date_gmt":"2024-08-30T23:39:00","guid":{"rendered":"https:\/\/blog.laoyulaoyu.top\/?p=1381"},"modified":"2024-08-28T17:10:08","modified_gmt":"2024-08-28T09:10:08","slug":"%e9%80%9a%e8%bf%87-ts-mixer-%e5%ae%9e%e7%8e%b0%e8%82%a1%e7%a5%a8%e4%bb%b7%e6%a0%bc%e9%a2%84%e6%b5%8b","status":"publish","type":"post","link":"https:\/\/laoyulaoyu.com\/index.php\/2024\/08\/31\/%e9%80%9a%e8%bf%87-ts-mixer-%e5%ae%9e%e7%8e%b0%e8%82%a1%e7%a5%a8%e4%bb%b7%e6%a0%bc%e9%a2%84%e6%b5%8b\/","title":{"rendered":"\u901a\u8fc7\u00a0 TS-Mixer \u5b9e\u73b0\u80a1\u7968\u4ef7\u683c\u9884\u6d4b"},"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\/08\/image-67.png\" alt=\"\" class=\"wp-image-1615\"\/><\/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\u9047\u5230\u4e86\u5f3a\u5927\u7684 Time Mixer \u6a21\u578b\uff0c\u8be5\u6a21\u578b\u4ee5\u5728\u590d\u6742\u6570\u636e\u96c6\u4e0a\u63d0\u4f9b\u4ee4\u4eba\u5370\u8c61\u6df1\u523b\u7684\u7ed3\u679c\u800c\u95fb\u540d\u3002\u51fa\u4e8e\u597d\u5947\uff0c\u6211\u51b3\u5b9a\u5c06\u5176\u5e94\u7528\u4e8e\u6211\u5728 Kaggle \u4e0a\u627e\u5230\u7684\u6570\u636e\u96c6\uff0c\u5176\u4e2d\u5305\u542b Microsoft \u7684\u5386\u53f2\u80a1\u7968\u4ef7\u683c\u3002\u5728\u672c\u6587\u4e2d\uff0c\u6211\u4eec\u5c06\u63a2\u8ba8\u5982\u4f55<mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-vivid-cyan-blue-color\">\u5229\u7528 Time Mixer \u6765\u9884\u6d4b Microsoft \u80a1\u7968\u672a\u6765\u67d0\u4e2a\u65f6\u6bb5\u7684\u5b9e\u9645\u6536\u76d8\u4ef7<\/mark>\uff0c\u4ece\u800c\u5c55\u793a\u5176\u5728\u8d22\u52a1\u65f6\u95f4\u5e8f\u5217\u9884\u6d4b\u65b9\u9762\u7684\u6f5c\u529b\u3002<\/pre>\n<\/blockquote>\n\n\n\n<h2 class=\"wp-block-heading\">\u4e00\u3001<strong>\u4ec0\u4e48\u662fTime Mixer\uff1f<\/strong><\/h2>\n\n\n\n<p>\u65f6\u95f4\u6df7\u5408\u5668\u4ee3\u8868\u4e86\u65f6\u95f4\u5e8f\u5217\u5206\u6790\u5411\u524d\u8fc8\u51fa\u7684\u91cd\u8981\u4e00\u6b65\uff0c\u5c24\u5176\u662f\u5728\u5904\u7406\u76ee\u6807\u53d8\u91cf\u968f\u65f6\u95f4\u53d8\u5316\u7684\u590d\u6742\u53d8\u5316\u65f6\u3002\u50cf ARIMA \u8fd9\u6837\u7684\u4f20\u7edf\u6a21\u578b\u53ef\u80fd\u96be\u4ee5\u6355\u6349\u8fd9\u4e9b\u590d\u6742\u7684\u6a21\u5f0f\uff0c\u4ece\u800c\u5bfc\u81f4\u9884\u6d4b\u7ed3\u679c\u4e0d\u4f73\u3002\u8fd9\u5c31\u662f Time Mixer \u7684\u4eae\u70b9\u3002<\/p>\n\n\n\n<p>Time Mixer \u5c5e\u4e8e\u4e00\u7c7b\u79f0\u4e3a Mixer \u6a21\u578b\u7684\u65b0\u6a21\u578b\u3002\u8fd9\u4e9b\u6a21\u578b\u65e8\u5728\u8bc6\u522b\u5e8f\u5217\u4e2d\u7684\u4f9d\u8d56\u5173\u7cfb\u548c\u9690\u85cf\u6a21\u5f0f\uff0c\u5c31\u50cf\u8f6c\u6362\u5668\u5904\u7406\u6587\u672c\u6216\u56fe\u50cf\u7684\u65b9\u5f0f\u4e00\u6837\u3002\u4e0e\u9010\u6b65\u5904\u7406\u5e8f\u5217\u7684\u9012\u5f52\u795e\u7ecf\u7f51\u7edc \uff08RNN\uff09 \u548c\u957f\u77ed\u671f\u8bb0\u5fc6 \uff08LSTM\uff09 \u7f51\u7edc\u4e0d\u540c\uff0c\u65f6\u95f4\u6df7\u5408\u5668\u5e76\u884c\u8fd0\u884c\uff0c\u4f7f\u5176\u80fd\u591f\u66f4\u6709\u6548\u5730\u5904\u7406\u5e8f\u5217\u3002\u6b64\u5916\uff0c\u4e0e transformer \u76f8\u6bd4\uff0c\u5b83\u66f4\u7b80\u5355\u3001\u66f4\u7cbe\u7b80\uff0c\u4f7f\u5176\u6210\u4e3a\u4f20\u7edf\u65b9\u6cd5\u4e0d\u8db3\u7684\u65f6\u95f4\u5e8f\u5217\u5206\u6790\u7684\u4ee4\u4eba\u5174\u594b\u7684\u9009\u62e9<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>\u65f6\u95f4\u6df7\u5408\u5668\u8bbe\u8ba1\uff1a<\/strong><\/h3>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Time-mixer \u4e13\u95e8\u9488\u5bf9<strong>\u65f6\u95f4\u5e8f\u5217\u6570\u636e<\/strong>\uff0c\u5176\u4e2d\u6d89\u53ca\u6309\u65f6\u95f4\u987a\u5e8f\u7d22\u5f15\u7684\u6570\u636e\u70b9\u5e8f\u5217\u3002\u5b83\u65e8\u5728\u6709\u6548\u5730\u5bf9\u65f6\u95f4\u4f9d\u8d56\u5173\u7cfb\u8fdb\u884c\u5efa\u6a21\uff0c\u540c\u65f6\u6355\u83b7\u77ed\u671f\u548c\u957f\u671f\u6a21\u5f0f\u3002<\/li>\n\n\n\n<li>\u8be5\u6a21\u578b\u901a\u5e38\u5305\u62ec<strong>\u6df7\u5408\u5c42<\/strong>\uff0c\u8fd9\u4e9b\u5c42\u5904\u7406 Importing \u5e8f\u5217\u7684\u65b9\u5f0f\u5141\u8bb8\u6a21\u578b\u5b66\u4e60\u590d\u6742\u7684\u65f6\u95f4\u6a21\u5f0f\uff0c\u800c\u65e0\u9700\u660e\u786e\u7684\u6ce8\u610f\u673a\u5236\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\/2024\/08\/image-66.png\" alt=\"\" class=\"wp-image-1614\"\/><figcaption class=\"wp-element-caption\">Time Mixer \u7684\u67b6\u6784<\/figcaption><\/figure>\n\n\n\n<p><\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>\u4e8c\u3001\u63a2\u7d22\u6027\u6570\u636e\u5206\u6790 \uff08EDA\uff09<\/strong><\/h2>\n\n\n\n<pre class=\"wp-block-code\"><code>data = pd.read_csv\uff08\u201c\/content\/Microsoft_Stock.csv\u201d\uff09<\/code><\/pre>\n\n\n\n<p>\u8ba9\u6211\u4eec\u4ece\u5b89\u88c5\u5e93\u5f00\u59cb\uff0c\u4f7f\u7528\u6700\u5e38\u7528\u7684PIP\u5b89\u88c5\u6a21\u5f0f\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>pip install pandas<br>pip install numpy<br>pip install tensorflow<br>pip install sklearn<br>pip install matplotlib<\/code><\/pre>\n\n\n\n<p>\u8ba9\u6211\u4eec\u6df1\u5165\u7814\u7a76\u6570\u636e,\u770b\u770b\u6570\u636e\u957f\u4ec0\u4e48\u6837\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>microsoft_data = pd.read_csv(\"\/content\/Microsoft_Stock.csv\")<br>microsoft_data.head()<\/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\/08\/image-68.png\" alt=\"\" class=\"wp-image-1616\"\/><\/figure>\n\n\n\n<p><\/p>\n\n\n\n<p>\u6240\u4ee5\uff0c\u8fd9\u4e2a\u6570\u636e\u6846\u770b\u8d77\u6765\u67096\u5217\uff0c\u5206\u522b\u662f<strong>\u65e5\u671f\u3001\u5f00\u76d8\u4ef7\u3001\u6700\u9ad8\u4ef7\u3001\u6700\u4f4e\u4ef7\u3001\u6536\u76d8\u4ef7\u548c\u6210\u4ea4\u91cf<\/strong>\u3002\u8ba9\u6211\u4eec\u68c0\u67e5\u4e00\u4e0b\u6240\u6709\u5217\u7684\u6570\u636e\u7c7b\u578b\u4ee5\u53ca\u6570\u636e\u5e27\u7684\u5f62\u72b6\u3002\u8fd9\u4e2a\u6570\u636e\u4e5f\u662f\u6211\u4eec\u5e73\u65f6\u80fd\u63a5\u89e6\u5230\u7684\u6700\u5e38\u89c1\u7684\u80a1\u7968\u6570\u636e\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>microsoft_data.info()<\/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\/08\/image-69.png\" alt=\"\" class=\"wp-image-1617\"\/><\/figure>\n\n\n\n<p>\u6211\u4eec\u53ef\u4ee5\u770b\u5230 Date \u5217\u7684\u6570\u636e\u7c7b\u578b\u4e3a object\uff0c\u6570\u636e\u96c6\u4e2d\u7684\u884c\u6570\u4e3a 1511\uff0c\u8ba9\u6211\u4eec\u5148\u5c06 Date \u5217\u7684\u6570\u636e\u7c7b\u578b\u8f6c\u6362\u4e3a datetime\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>microsoft_data&#91;'Date']=microsoft_data.Date.astype('datetime64&#91;ns]')<\/code><\/pre>\n\n\n\n<p>\u8ba9\u6211\u4eec\u7ed8\u5236\u4e00\u4e2a\u6761\u5f62\u56fe\u6765\u67e5\u770b\u4e00\u5e74\u4e2d\u7684\u53d8\u5316\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>stock_volume = microsoft_data.groupby(microsoft_data.Date.dt.year).Volume.max().reset_index()<br>stock_volume<br>plt.figure(figsize=&#91;16,5])<br>plt.bar(stock_volume.Date,stock_volume.Volume)<br>plt.xlabel('Date')<br>plt.ylabel('Volume')<br>plt.title(\"Volume with Year\")<\/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\/08\/image-70.png\" alt=\"\" class=\"wp-image-1618\"\/><\/figure>\n\n\n\n<p>\u4ece\u56fe\u8868\u4e0a\u770b\uff0c\u6211\u4eec\u53ef\u4ee5\u770b\u5230 2019 \u5e74\u7684\u4ea4\u6613\u91cf\u4e0b\u964d\uff0c\u8fd9\u662f\u4ed6\u4eec\u7684\u4ea4\u6613\u91cf\u6700\u4f4e\u7684\u5730\u65b9\uff0c<strong>\u6211\u4eec\u5728\u6b64\u56fe\u8868\u4e2d\u53d6\u4e86\u6bcf\u4e2a\u6700\u5927\u4ea4\u6613\u91cf\u3002<\/strong>\u8ba9\u6211\u4eec\u521b\u5efa\u66f4\u591a\u5217\uff0c\u4ee5\u4fbf\u66f4\u597d\u5730\u7406\u89e3\u6570\u636e\u96c6\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>microsoft_data = microsoft_data.set_index('Date')<br>microsoft_data&#91;'year'] = microsoft_data.index.year<br>microsoft_data&#91;'month'] = microsoft_data.index.month<br>microsoft_data&#91;'day'] = microsoft_data.index.day<br>microsoft_data&#91;'hour'] = microsoft_data.index.hour<br>microsoft_data&#91;'dayofweek'] = microsoft_data.index.dayofweek<br>microsoft_data&#91;'dayofyear'] = microsoft_data.index.dayofyear<br>microsoft_data&#91;'weekofyear'] = microsoft_data.index.isocalendar().week<br>microsot_data&#91;'quarter'] = microsot_data.index.quarter<\/code><\/pre>\n\n\n\n<p>\u8fd9\u5c31\u662f\u6570\u636e\u96c6\u4e2d\u65b0\u6dfb\u52a0\u7684\u5217\u7684\u503c\u3002<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img decoding=\"async\" src=\"https:\/\/www.laoyulaoyu.com\/wp-content\/uploads\/2024\/08\/image-71.png\" alt=\"\" class=\"wp-image-1619\"\/><\/figure>\n\n\n\n<p>\u73b0\u5728\uff0c\u8ba9\u6211\u4eec\u770b\u770b\u591a\u5e74\u6765\u6210\u4ea4\u91cf\u7684\u5b50\u56fe\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>import seaborn as sns<br>color_pal = sns.color_palette('tab10')<br>plt.subplots(figsize=(15, 5))<br>sns.lineplot(microsoft_data=microsoft_data, x='month', y='Volume', hue='year',palette=color_pal, ci=False)<br>plt.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\/08\/image-72.png\" alt=\"\" class=\"wp-image-1620\"\/><\/figure>\n\n\n\n<p>\u8ba9\u6211\u4eec\u4f7f\u7528\u56fe\u8868\u53ef\u89c6\u5316\u6536\u76d8\u4ef7\u591a\u5e74\u6765\u7684\u8d8b\u52bf\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>data&#91;'Close'].plot(figsize=&#91;15,7])<br>plt.xlabel(\"Date\")<br>plt.ylabel(\"Close\")<br>plt.plot()<\/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\/08\/image-73.png\" alt=\"\" class=\"wp-image-1621\"\/><\/figure>\n\n\n\n<p>\u597d\u5427\uff0c\u591a\u5e74\u6765\u4ef7\u683c\u663e\u8457\u589e\u957f\uff0c\u4f46\u5728 2020 \u5e74\u6709\u6240\u4e0b\u964d\u3002<\/p>\n\n\n\n<p><\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>\u4e09\u3001TS-Mixer \u6a21\u578b<\/strong><\/h2>\n\n\n\n<p>\u73b0\u5728\uff0c\u8ba9\u6211\u4eec\u6784\u5efa\u6211\u4eec\u7684\u6a21\u578b\u3002\u6211\u4eec\u5c06\u4ece\u5bfc\u5165\u6240\u9700\u7684\u5e93\u5f00\u59cb\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>#importing necessary libraries<br>import torch<br>import torch.nn as nn<br>import torch.optim as optim<br>from sklearn.preprocessing import MinMaxScaler<br>from torch.utils.data import DataLoader, TensorDataset<br>import pandas as pd<br>import numpy as np<\/code><\/pre>\n\n\n\n<p>\u6211\u5df2\u7ecf\u4e3aEDA\uff08\u63a2\u7d22\u6027\u6570\u636e\u5206\u6790\uff09\u548cTS-mixer\u5206\u522b\u521b\u5efa\u4e86\u4e00\u4e2a\u5355\u72ec\u7684\u6587\u4ef6\u3002\u6211\u5efa\u8bae\u4e3a\u5b83\u4eec\u5404\u81ea\u521b\u5efa\u4e00\u4e2a\u5355\u72ec\u7684\u6587\u4ef6\uff0c\u8fd9\u6837\u53ef\u4ee5\u5e2e\u52a9\u4f60\u66f4\u5bb9\u6613\u5730\u627e\u5230\u5b83\u4eec\uff0c\u5e76\u4e14\u4ee5\u8fd9\u79cd\u65b9\u5f0f\u7ec4\u7ec7\u4e8b\u7269\u4f1a\u66f4\u52a0\u6709\u5e8f\u3002\u8ba9\u6211\u4eec\u4ece\u4e0a\u4f20CSV\u6587\u4ef6\u5230\u6570\u636e\u6846\u5f00\u59cb\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>data = pd.read_csv(\"\/content\/Microsoft_Stock.csv\")<br># Convert the 'Date' column to datetime<br>data&#91;'Date'] = pd.to_datetime(data&#91;'Date'])<\/code><\/pre>\n\n\n\n<p>\u73b0\u5728\u6211\u4eec\u5c06\u4f7f\u7528\u6700\u5c0f-\u6700\u5927\u7f29\u653e\u5668\uff08Min Max Scaler\uff09\u6765\u5f52\u4e00\u5316\u6211\u4eec\u7684\u5217\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>#Normalizing the data<br>scaler = MinMaxScaler()<br>data&#91;&#91;'Open', 'High', 'Low', 'Close', 'Volume']] = scaler.fit_transform(data&#91;&#91;'Open', 'High', 'Low', 'Close', 'Volume']])<\/code><\/pre>\n\n\n\n<p>DataFrame \u8f6c\u5316\u5230numpy array\u683c\u5f0f\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>values = data&#91;&#91;'Open', 'High', 'Low', 'Close', 'Volume']].values<\/code><\/pre>\n\n\n\n<p>\u6211\u4eec\u5c06\u63d0\u4f9b\u4e00\u4e2a\u5e8f\u5217\u957f\u5ea6\u5e76\u521b\u5efa\u6211\u4eec\u7684\u6570\u636e\u96c6\u3002\u5e8f\u5217\u957f\u5ea6\u6307\u7684\u662f\u5728\u65f6\u95f4\u5e8f\u5217\u6a21\u578b\u4e2d\u7528\u4e8e\u8fdb\u884c\u9884\u6d4b\u7684\u65f6\u95f4\u6bb5\u6570\u91cf\u3002\u5728\u6211\u4eec\u7684\u4f8b\u5b50\u4e2d\uff0c\u5b83\u6307\u7684\u662f\u5728\u9884\u6d4b\u4e0b\u4e00\u5929\u7684\u4ef7\u683c\u65f6\u8003\u8651\u7684\u524d\u591a\u5c11\u5929\uff08\u6216\u89c2\u5bdf\u503c\uff09\u3002\u503c\u5f97\u6ce8\u610f\u7684\u662f\uff0c<strong>\u6587\u7ae0\u4e2d\u9009\u53d6\u7684target\u662f\u6536\u76d8\u4ef7\uff0c\u975e\u5e38\u4e0d\u5efa\u8bae\u4f7f\u7528\u6536\u76d8\u4ef7\u4e3a\u9884\u6d4b\u76ee\u6807<\/strong>\uff0c\u5b9e\u9645\u4f7f\u7528\u65f6\uff0c\u53ef\u4ee5\u5c1d\u8bd5\u4f7f\u7528\u7b2c\u4e8c\u5929\u7684\u6da8\u8dcc\uff0c\u6216\u8005\u662f\u6da8\u8dcc\u5e45\u4e3a\u9884\u6d4b\u76ee\u6807\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>def create_sequences(data, seq_length):<br>    xs, ys = &#91;], &#91;]<br>    for i in range(len(data) - seq_length):<br>        x = data&#91;i:i+seq_length]<br>        y = data&#91;i+seq_length, 3]  # Close price as target<br>        xs.append(x)<br>        ys.append(y)<br>    return torch.tensor(xs, dtype=torch.float32), torch.tensor(ys, dtype=torch.float32)<br><br># Set the sequence length to 180<br>seq_length = 180<br>X, y = create_sequences(values, seq_length)<\/code><\/pre>\n\n\n\n<p>\u5c06\u6570\u636e\u62c6\u5206\u4e3a\u8bad\u7ec3\u6570\u636e\u96c6\u548c\u6d4b\u8bd5\u6570\u636e\u96c6\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>train_size = int(X.shape&#91;0] * 0.8)\nX_train, y_train = X&#91;:train_size], y&#91;:train_size]\nX_test, y_test = X&#91;train_size:], y&#91;train_size:]<\/code><\/pre>\n\n\n\n<p>\u521b\u5efa DataLoader\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>train_loader = DataLoader(TensorDataset(X_train, y_train), batch_size=32, shuffle=True)<br>test_loader = DataLoader(TensorDataset(X_test, y_test), batch_size=32)<\/code><\/pre>\n\n\n\n<p>\u8bbe\u7f6e\u6211\u4eec\u7684\u6a21\u578b\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>class TSMixer(nn.Module):<br>    def __init__(self, input_size, hidden_size, num_layers, output_size):<br>        super(TSMixer, self).__init__()<br>        self.mixer = nn.Sequential(<br>            nn.Conv1d(in_channels=input_size, out_channels=hidden_size, kernel_size=1),<br>            nn.ReLU(),<br>            *&#91;nn.Sequential(<br>                nn.Conv1d(in_channels=hidden_size, out_channels=hidden_size, kernel_size=1),<br>                nn.ReLU()) for _ in range(num_layers)],<br>            nn.Conv1d(in_channels=hidden_size, out_channels=output_size, kernel_size=1)<br>        )<br>    <br>    def forward(self, x):<br>        x = x.transpose(1, 2)  <br>        x = self.mixer(x)<br>        x = x.transpose(1, 2)  <br>        return x&#91;:, -1, :]<\/code><\/pre>\n\n\n\n<p>\u521d\u59cb\u5316\u6a21\u578b\u3001\u635f\u5931\u51fd\u6570\u548c\u4f18\u5316\u5668\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>input_size = X_train.shape&#91;2]  # 5 features: Open, High, Low, Close, Volume<br>hidden_size = 64<br>num_layers = 2<br>output_size = 1<br><br>model = TSMixer(input_size, hidden_size, num_layers, output_size)<br>criterion = nn.MSELoss()<br>optimizer = optim.Adam(model.parameters(), lr=0.001)<\/code><\/pre>\n\n\n\n<p>\u8bad\u7ec3\u6211\u4eec\u7684\u6a21\u578b\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>epochs = 50<br>for epoch in range(epochs):<br>    model.train()<br>    for batch_X, batch_y in train_loader:<br>        optimizer.zero_grad()<br>        outputs = model(batch_X)<br>        loss = criterion(outputs, batch_y.unsqueeze(1))<br>        loss.backward()<br>        optimizer.step()<br><br>    print(f'Epoch {epoch+1}\/{epochs}, Loss: {loss.item()}')<br><br># 4. Prediction<br>model.eval()<br>predictions = &#91;]<br>with torch.no_grad():<br>    for batch_X, _ in test_loader:<br>        pred = model(batch_X)<br>        predictions.append(pred)<br>        <br># Convert predictions to a numpy array and invert normalization<br># Create a placeholder for inverse scaling<br>predictions = torch.cat(predictions).numpy()<br># Initialize an array of zeros with the same number of features as the original data<br>predicted_prices_full = np.zeros((predictions.shape&#91;0], values.shape&#91;1]))<br><br># Place the predictions in the 'Close' column (3rd index)<br>predicted_prices_full&#91;:, 3] = predictions&#91;:, 0]<br><br># Inverse transform<br>predicted_prices_full = scaler.inverse_transform(predicted_prices_full)<br><br># Extract the 'Close' prices from the inverse-transformed data<br>predicted_prices = predicted_prices_full&#91;:, 3]<br><br># Convert predictions to a DataFrame and save to a CSV file<br>predicted_prices_df = pd.DataFrame(predicted_prices, columns=&#91;'Predicted_Close'])<br>predicted_prices_df.to_csv('predicted_prices.csv', index=False)<br><br>print(\"Predictions saved to predicted_prices.csv\")<\/code><\/pre>\n\n\n\n<p>\u8fd9\u91cc\u6211\u53d6\u4e86 epoch \u503c \u4e3a50\u3002<\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-full\"><img decoding=\"async\" src=\"https:\/\/www.laoyulaoyu.com\/wp-content\/uploads\/2024\/08\/image-74.png\" alt=\"\" class=\"wp-image-1622\"\/><figcaption class=\"wp-element-caption\">\u8fd9\u4e9b\u662f epoch \u503c\u4ee5\u53ca MSE<\/figcaption><\/figure>\n<\/div>\n\n\n<p>\u8f93\u51fa\u4f1a\u662f\u8fd9\u6837\u7684\uff0c\u60a8\u8fd8\u53ef\u4ee5\u6839\u636e\u6570\u636e\u9700\u8981\u66f4\u6539\u5b66\u4e60\u7387\u7684\u503c\u3002\u662f\u65f6\u5019\u68c0\u67e5\u6a21\u578b\u7684\u51c6\u786e\u6027\u4e86\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>y_test_np = y_test.numpy().reshape(-1, 1)<br><br># Create an array with the same shape as the original features, filled with zeros<br>y_test_full = np.zeros((y_test_np.shape&#91;0], values.shape&#91;1]))<br><br># Place y_test_np in the Close column (assuming it's the 4th column as before)<br>y_test_full&#91;:, 3] = y_test_np&#91;:, 0]<br><br># Apply inverse transform only on the relevant column<br>actual_prices_full = scaler.inverse_transform(y_test_full)<br><br># Extract the actual Close prices<br>actual_prices = actual_prices_full&#91;:, 3]<br><br># Calculate the MSE between actual and predicted Close prices<br>mse = mean_squared_error(actual_prices, predicted_prices)<br>print(f\"Mean Squared Error: {mse}\")<br><br># Matching predictions with dates<br>predicted_dates = data&#91;'Date'].iloc&#91;train_size + seq_length:].reset_index(drop=True)<br><br># Combine dates, actual prices, and predicted prices into a DataFrame<br>predicted_prices_df = pd.DataFrame({<br>    'Date': predicted_dates,<br>    'Actual_Close': actual_prices,<br>    'Predicted_Close': predicted_prices<br>})<br><br># Save the predictions with dates<br>predicted_prices_df.to_csv('predicted_prices_with_dates.csv', index=False)<br><br>print(\"Predictions with dates saved to predicted_prices_with_dates.csv\")<br><br># Visualization<br>plt.figure(figsize=(12, 6))<br>plt.plot(predicted_prices_df&#91;'Date'], predicted_prices_df&#91;'Actual_Close'], label='Actual Close Price')<br>plt.plot(predicted_prices_df&#91;'Date'], predicted_prices_df&#91;'Predicted_Close'], label='Predicted Close Price')<br>plt.xlabel('Date')<br>plt.ylabel('Close Price')<br>plt.title('Actual vs Predicted Close Prices')<br>plt.legend()<br>plt.xticks(rotation=45)<br>plt.tight_layout()<br>plt.show()<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>\u56db\u3001\u7ed3\u679c\u5c55\u793a<\/strong><\/h2>\n\n\n\n<p>\u5b9e\u9645\u6536\u76d8\u4ef7\u548c\u9884\u6d4b\u6536\u76d8\u4ef7\u7684\u56fe\u8868\u5982\u4e0b\u6240\u793a\uff1a<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img decoding=\"async\" src=\"https:\/\/www.laoyulaoyu.com\/wp-content\/uploads\/2024\/08\/image-75.png\" alt=\"\" class=\"wp-image-1623\"\/><\/figure>\n\n\n\n<p>\u5f53 MSE\uff08\u5747\u65b9\u8bef\u5dee\uff09\u5206\u6570\u4e3a 21.56 \u65f6\uff0c\u7ed3\u679c\u975e\u5e38\u60ca\u4eba\uff0c\u53ef\u4ee5\u901a\u8fc7\u66f4\u6539\u8f93\u5165\u5c42\u7684\u503c\u6765\u8fdb\u4e00\u6b65\u51cf\u5c0f\u8be5\u503c\uff0c\u4f46\u5728\u589e\u52a0\u5c42\u65f6\u5e94\u5c0f\u5fc3\uff0c\u56e0\u4e3a\u5b83\u53ef\u80fd\u4f1a\u5bfc\u81f4\u8fc7\u5ea6\u62df\u5408\u3002\u6240\u4ee5\uff0c\u8fd9\u5c31\u662f\u6211\u4eec\u7684\u6a21\u578b\u7684\u5de5\u4f5c\u539f\u7406\uff0c\u60a8\u53ef\u4ee5\u901a\u8fc7\u5c06\u5176\u7ed3\u679c\u4e0e ARIMA\u3001Exponential Smoothing \u7b49\u5176\u4ed6\u6a21\u578b\u8fdb\u884c\u6bd4\u8f83\u6765\u8fdb\u4e00\u6b65\u5206\u6790\u5176\u6548\u7387\u3002<\/p>\n\n\n\n<p><\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<p><\/p>\n\n\n\n<p><\/p>\n\n\n\n<p><\/p>\n\n\n\n<p class=\"has-text-align-center\"><strong><mark style=\"background-color:#ffffff\" class=\"has-inline-color has-cyan-bluish-gray-color\">\u672c\u6587\u5185\u5bb9\u4ec5\u4ec5\u662f\u6280\u672f\u63a2\u8ba8\u548c\u5b66\u4e60\uff0c\u5e76\u4e0d\u6784\u6210\u4efb\u4f55\u6295\u8d44\u5efa\u8bae\u3002<br>\u8f6c\u53d1\u8bf7\u6ce8\u660e\u539f\u4f5c\u8005\u548c\u51fa\u5904\u3002<\/mark><\/strong><\/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\/2024\/08\/31\/%e9%80%9a%e8%bf%87-ts-mixer-%e5%ae%9e%e7%8e%b0%e8%82%a1%e7%a5%a8%e4%bb%b7%e6%a0%bc%e9%a2%84%e6%b5%8b\/\">Continue reading<span class=\"screen-reader-text\">\u901a\u8fc7\u00a0 TS-Mixer \u5b9e\u73b0\u80a1\u7968\u4ef7\u683c\u9884\u6d4b<\/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-1381","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\/1381","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=1381"}],"version-history":[{"count":1,"href":"https:\/\/laoyulaoyu.com\/index.php\/wp-json\/wp\/v2\/posts\/1381\/revisions"}],"predecessor-version":[{"id":1382,"href":"https:\/\/laoyulaoyu.com\/index.php\/wp-json\/wp\/v2\/posts\/1381\/revisions\/1382"}],"wp:attachment":[{"href":"https:\/\/laoyulaoyu.com\/index.php\/wp-json\/wp\/v2\/media?parent=1381"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/laoyulaoyu.com\/index.php\/wp-json\/wp\/v2\/categories?post=1381"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/laoyulaoyu.com\/index.php\/wp-json\/wp\/v2\/tags?post=1381"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}