Model1.py
unknown
python
11 days ago
2.5 kB
3
Indexable
# model_train_eval.py """ Trains a Transformer model on saved data, evaluates predictability, and saves models to mar2025/models/. Run after data_download.py in Google Colab (GPU recommended). Includes time features and observed mask for TimeSeriesTransformerForPrediction with None checks. """ from google.colab import drive import numpy as np import pandas as pd import matplotlib.pyplot as plt from sklearn.preprocessing import MinMaxScaler import torch from transformers import TimeSeriesTransformerForPrediction, TimeSeriesTransformerConfig import os # Mount Google Drive drive.mount('/content/drive') # Configuration BASE_DIR = '/content/drive/MyDrive/mar2025' DATA_PATH = os.path.join(BASE_DIR, 'data') MODEL_PATH = os.path.join(BASE_DIR, 'models') device = torch.device("cuda" if torch.cuda.is_available() else "cpu") print(f"Using device: {device}") # Load data def load_data(symbol): file_path = os.path.join(DATA_PATH, f"{symbol}.csv") if os.path.exists(file_path): return pd.read_csv(file_path, index_col=0, parse_dates=True) raise FileNotFoundError(f"Data for {symbol} not found at {file_path}") # Prepare sequences with time features def create_sequences(data, context_length, prediction_length): scaler = MinMaxScaler(feature_range=(0, 1)) value_scaled = scaler.fit_transform(data[['value']]) time_features = data[['day', 'month', 'year', 'day_of_year']].values # Ensure time features exist if len(data) < context_length + prediction_length: raise ValueError(f"Data length {len(data)} too short for context_length {context_length} and prediction_length {prediction_length}") X_values, y_values, X_time = [], [], [] for i in range(len(data) - context_length - prediction_length + 1): X_values.append(value_scaled[i:i + context_length]) y_values.append(value_scaled[i + context_length:i + context_length + prediction_length]) X_time.append(time_features[i:i + context_length]) X_values = np.array(X_values) y_values = np.array(y_values) X_time = np.array(X_time) if X_values.size == 0 or y_values.size == 0 or X_time.size == 0: raise ValueError(f"Empty sequence arrays for {symbol}") return (X_values, y_values, X_time, scaler) # Train and predict def train_and_predict(data, symbol, context_length=20, prediction_length=5): try: X_values, y_values, X_time, scaler... Something went wrong, please try again.
Editor is loading...
Leave a Comment