सामग्री पर जाएं
लाइब्रेरी के सभी दस्तावेज़

बहुचर टाइम-सीरीज़ रिग्रेशन के लिए PatchTST डिज़ाइन

कोड Machine Learning for Trading

सारांश

यह मॉडल एडैप्टर लंबी अवधि की टाइम-सीरीज़ पूर्वानुमान वास्तुकला PatchTST को अदिश रिग्रेशन पर लागू करता है। यह समय और फ़ीचर के अनुसार व्यवस्थित क्रम स्वीकार करता है, उन्हें बैकबोन के लिए पुनर्व्यवस्थित करता है और प्रति-फ़ीचर आउटपुट को एक पूर्वानुमान में घटाता है। इसका डिज़ाइन चैनल-स्वतंत्र एन्कोडिंग पर आधारित है: एन्कोडर के भीतर चैनलों को आपस में मिलाए बिना हर फ़ीचर चैनल साझा Transformer भारों से गुज़रता है।

कार्यान्वयन में रिवर्सिबल इंस्टेंस नॉर्मलाइज़ेशन भी है, जो एन्कोडिंग से पहले हर नमूने और चैनल को सामान्यीकृत करता है और बाद में उसका पैमाना बहाल करता है। ओवरलैपिंग पैच स्थानीय क्रम-संरचना बनाए रखते हैं और फ़्लैटनिंग प्रेडिक्शन हेड एन्कोड किए गए क्रम को आउटपुट में बदलता है। अंतिम रैखिक परत चैनल आउटपुट को एक अदिश मान में जोड़ती है। ये वास्तु विकल्प बताते हैं कि एडैप्टर सामान्य पूर्वानुमान मॉडल को बहुचर रिग्रेशन इंटरफ़ेस में कैसे ढालता है; दस्तावेज़ में कोई ट्रेडिंग प्रयोग, डेटासेट, बेंचमार्क या प्रदर्शन परिणाम नहीं है। इसके दावे मॉडल संरचना तक सीमित हैं, इसलिए वित्तीय डेटा के लिए पूर्वानुमान क्षमता यहाँ स्थापित नहीं होती।

मुख्य विचार

  • PatchTST हर इनपुट चैनल को पैच में बाँटता है और साझा भारों से चैनलों को एन्कोड करता है।
  • रिवर्सिबल इंस्टेंस नॉर्मलाइज़ेशन प्रति नमूना और प्रति चैनल के आँकड़ों को हटाता और फिर बहाल करता है।
  • डिज़ाइन में ग्लोबल मीन पूलिंग के बजाय ओवरलैपिंग पैच और फ़्लैटनिंग हेड का उपयोग है।
  • अंतिम रैखिक प्रोजेक्शन चैनल-स्तरीय आउटपुट को अदिश रिग्रेशन पूर्वानुमान में जोड़ता है।
  • दस्तावेज़ वास्तुकला बताता है, लेकिन ट्रेडिंग प्रदर्शन का कोई प्रमाण नहीं देता।

टैग

पूरा पाठ
# patchtst.py


```py
"""PatchTST: patching + channel-independent Transformer for time series.

From Nie, Nguyen, Sinthong, Kalagnanam (2023), *A Time Series is Worth 64
Words: Long-term Forecasting with Transformers*, ICLR 2023.

Two structural properties distinguish the paper's PatchTST from naive
"tokenize-the-input-with-a-Transformer" baselines:

1. **Channel-independent patching.** Each feature channel is treated as its
   own univariate sequence and passed through the same shared Transformer
   weights. There is no cross-channel mixing inside the encoder. This file
   delegates to ``PatchTST_backbone`` from the authors' repo to preserve
   this exactly.
2. **RevIN (Reversible Instance Normalization).** Per-sample per-channel
   statistics are removed before the backbone and re-added after, making
   the model robust to distribution shift. The vendored backbone wires
   this up when ``revin=True``.

Additionally, the paper uses **overlapping patches** (stride < patch_len)
and a **flatten + linear** prediction head rather than global mean pooling.

Adapter layer on top of the backbone:
- The backbone returns ``(batch, n_vars, target_window)``. For cross-sectional
  scalar regression we set ``target_window=1`` and then project the per-channel
  outputs to a single scalar via ``Linear(n_vars -> 1)``.

Reference implementation vendored from https://github.com/yuqinie98/PatchTST
(MIT License) into ``_reference/`` with import paths adjusted. RevIN is
vendored from https://github.com/ts-kim/RevIN (MIT License).

Interface preserved for the factory:
  PatchTST(n_features: int, lookback: int, patch_size: int = 16, ...)
  forward(x: (batch, seq_len, n_features)) -> (batch,)
"""

from __future__ import annotations

import torch
import torch.nn as nn

from case_studies.config.patchtst._reference import PatchTST_backbone


class PatchTST(nn.Module):
    """PatchTST channel-independent regressor.

    Wraps the paper authors' ``PatchTST_backbone`` with a scalar regression
    head. RevIN on by default; overlapping patches with stride=patch_size/2.
    """

    def __init__(
        self,
        n_features: int,
        lookback: int,
        patch_size: int = 16,
        stride: int | None = None,
        d_model: int = 64,
        n_heads: int = 4,
        n_layers: int = 2,
        d_ff: int | None = None,
        dropout: float = 0.1,
        attn_dropout: float = 0.0,
        revin: bool = True,
        affine: bool = True,
        subtract_last: bool = False,
        padding_patch: str = "end",
    ):
        super().__init__()

        if stride is None:
            stride = max(1, patch_size // 2)
        if d_ff is None:
            d_ff = d_model * 4

        self.backbone = PatchTST_backbone(
            c_in=n_features,
            context_window=lookback,
            target_window=1,
            patch_len=patch_size,
            stride=stride,
            n_layers=n_layers,
            d_model=d_model,
            n_heads=n_heads,
            d_ff=d_ff,
            attn_dropout=attn_dropout,
            dropout=dropout,
            revin=revin,
            affine=affine,
            subtract_last=subtract_last,
            padding_patch=padding_patch,
            head_type="flatten",
            individual=False,
        )
        self.head = nn.Linear(n_features, 1)

    def forward(self, x: torch.Tensor) -> torch.Tensor:
        # x: (batch, seq_len, n_features) → backbone wants (batch, n_vars, seq_len)
        z = x.permute(0, 2, 1)
        # backbone out: (batch, n_vars, target_window=1)
        z = self.backbone(z)
        # collapse target_window and project across channels to scalar
        z = z.squeeze(-1)  # (batch, n_vars)
        return self.head(z).squeeze(-1)

```

स्रोत के लाइसेंस के तहत श्रेय सहित पूरा पाठ दिखाया गया है। लाइसेंस: MIT

यह सारांश मूल स्रोत के आधार पर Stratmill के शोध एजेंट ने लिखा है; यह स्रोत की प्रति नहीं है।