### Run Training with Configuration File (Python) Source: https://context7.com/derposoft/crypto_pump_and_dump_with_deep_learning/llms.txt This command executes the training script with a specified configuration file. It allows users to define hyperparameters and dataset paths for model training. Ensure the 'train.py' script and the configuration JSON file exist in the specified paths. ```python python train.py --config models/transformer_config.json ``` -------------------------------- ### Initialize and Use ConvLSTM Model in PyTorch Source: https://context7.com/derposoft/crypto_pump_and_dump_with_deep_learning/llms.txt Demonstrates the initialization and forward pass of a ConvLSTM model for time-series analysis. It shows how to configure the model with various parameters like number of features, kernel size, embedding size, and normalization options. The code also illustrates moving the model to a CUDA-enabled device if available and prints input/output shapes and prediction ranges. ```python import torch from models.convlstm import ConvLSTM # Initialize ConvLSTM model model = ConvLSTM( num_feats=13, # Number of input features conv_kernel_size=3, # Convolution kernel size embedding_size=350, # LSTM hidden dimension num_layers=1, # Number of LSTM layers dropout=0.0, # Dropout probability cell_norm=False, # Layer normalization in LSTM cell out_norm=False # Layer normalization on LSTM output ) device = torch.device('cuda' if torch.cuda.is_available() else 'cpu') model = model.to(device) # Forward pass # Input shape: (batch_size, sequence_length, num_features) batch_size, seq_len, num_feats = 128, 15, 13 x = torch.randn(batch_size, seq_len, num_feats).to(device) # Output shape: (batch_size, sequence_length) predictions = model(x) print(f'Input shape: {x.shape}') # torch.Size([128, 15, 13]) print(f'Output shape: {predictions.shape}') # torch.Size([128, 15]) # Values are sigmoid-activated probabilities [0, 1] print(f'Prediction range: [{predictions.min():.4f}, {predictions.max():.4f}]') # With layer normalization enabled model_with_norm = ConvLSTM( num_feats=13, conv_kernel_size=5, embedding_size=256, num_layers=2, dropout=0.1, cell_norm=True, # Normalize LSTM cell gates out_norm=True # Normalize LSTM output ).to(device) # Count trainable parameters from models.utils import count_parameters print(f'Parameters: {count_parameters(model_with_norm)}') # ~500k parameters ``` -------------------------------- ### Initialize and Use AnomalyTransformer Models in PyTorch Source: https://context7.com/derposoft/crypto_pump_and_dump_with_deep_learning/llms.txt Details the initialization of various AnomalyTransformer models, including basic, intermediate, and full versions, along with the AnomalyAttention module. These models are designed for anomaly detection using specialized attention mechanisms. The code demonstrates setting up models with parameters like sequence length (N), feature dimension (d_model), number of layers, and device placement. It also shows a forward pass and outlines basic training steps with MSELoss and Adam optimizer, including custom loss functions for intermediate and full variants. ```python import torch from models.anomaly_transformer import ( AnomalyTransformer, AnomalyTransfomerIntermediate, AnomalyTransfomerBasic, AnomalyAttention ) device = torch.device('cuda' if torch.cuda.is_available() else 'cpu') # Basic Anomaly Transformer - simplest anomaly attention usage model_basic = AnomalyTransfomerBasic( N=15, # Sequence length d_model=12, # Feature dimension layers=5, # Number of anomaly transformer blocks device=device, output_size=1 ).to(device) # Intermediate variant - uses association discrepancy in loss model_intermediate = AnomalyTransfomerIntermediate( N=15, d_model=12, layers=4, lambda_=0.0001, # Weight for KL divergence loss device=device ).to(device) # Full Anomaly Transformer - minimax optimization strategy model_full = AnomalyTransformer( N=15, d_model=12, layers=4, lambda_=0.0001, device=device ).to(device) # Forward pass (same for all variants) batch_size, seq_len, features = 32, 15, 12 x = torch.randn(batch_size, seq_len, features).to(device) predictions = model_basic(x) print(f'Output shape: {predictions.shape}') # torch.Size([32, 15]) # Training with custom loss functions criterion_basic = torch.nn.MSELoss() optimizer = torch.optim.Adam(model_intermediate.parameters(), lr=1e-3) # For Intermediate model - loss includes association discrepancy y = torch.rand(batch_size, seq_len).to(device) predictions = model_intermediate(x) loss = model_intermediate.loss_fn(predictions, y) loss.backward() optimizer.step() # For full AnomalyTransformer - minimax training optimizer_full = torch.optim.Adam(model_full.parameters(), lr=1e-3) optimizer_full.zero_grad() predictions = model_full(x) ``` -------------------------------- ### Model Training Step: Loss Calculation and Backward Pass Source: https://context7.com/derposoft/crypto_pump_and_dump_with_deep_learning/llms.txt This snippet demonstrates a single step in the model training process. It calculates the loss using a custom loss function, performs backpropagation to compute gradients, and updates the model's weights using an optimizer. It also shows how to access association matrices after a forward pass. ```python loss = model_full.loss_fn(predictions, y) loss.backward() optimizer_full.step() print(f'Prior associations (P): {len(model_full.P_layers)} layers') print(f'Series associations (S): {len(model_full.S_layers)} layers') print(f'P shape per layer: {model_full.P_layers[0].shape}') ``` -------------------------------- ### Load and Preprocess Time-Series Data for P&D Detection (Python) Source: https://context7.com/derposoft/crypto_pump_and_dump_with_deep_learning/llms.txt This module provides utilities for loading compressed CSV trading data, processing pump events into time segments, and creating PyTorch DataLoaders. It supports automatic caching of processed data, undersampling of non-anomaly samples, and configurable segment lengths. Functions like `get_data`, `create_loaders`, and `load_data` facilitate efficient data handling for model training. ```python from data.data import get_data, create_loaders, create_loader, load_data, process_data # Load and preprocess data with automatic caching data = get_data( './data/features_25S.csv.gz', batch_size=128, train_ratio=0.8, undersample_ratio=0.05, # Keep 5% of non-anomaly samples segment_length=60, # Window size in time chunks save=True # Cache processed numpy arrays ) # Returns: numpy array of shape (num_segments, segment_length, num_features+1) # Get train/test loaders directly train_loader, test_loader = get_data( './data/features_15S.csv.gz', batch_size=256, train_ratio=0.8, undersample_ratio=0.05, segment_length=15, return_loaders=True ) # Create loaders from existing data array train_loader, test_loader = create_loaders( data, train_ratio=0.8, batch_size=128, undersample_ratio=0.05 ) # Create single loader with custom settings from torch import Generator g = Generator() g.manual_seed(42) train_loader = create_loader( train_data, batch_size=64, undersample_ratio=0.05, shuffle=True, drop_last=True, generator=g, verbose=True ) # Output: # Train data shape: (50000, 15, 14) # Train data shape after undersampling: (5234, 15, 14) # Low-level data loading raw_df = load_data('./data/features_25S.csv.gz') processed = process_data( raw_df, segment_length=60, remove_post_anomaly_data=True ) # Output: # Processing data... # Segment length: 60 # Remove post anomaly data: True # Data shape: (100000, 15) # 245 pumps # Removed 1234 rows with post-anomaly data # 45678 rows of data after processing ``` -------------------------------- ### Loading Transformer Model Configurations from JSON Source: https://context7.com/derposoft/crypto_pump_and_dump_with_deep_learning/llms.txt This Python code shows how to load hyperparameter configurations for Transformer models from a JSON file named 'models/transformer_config.json'. It demonstrates accessing different variants of the Anomaly Transformer, such as '15S_optimal_AnomalyTransformer', which are used for experiments. These configurations ensure consistency across runs. ```python import json with open('models/transformer_config.json') as f: transformer_configs = json.load(f) config = transformer_configs['15S_optimal_AnomalyTransformer'] ``` -------------------------------- ### Initialize and Use TransformerTimeSeries Models in PyTorch Source: https://context7.com/derposoft/crypto_pump_and_dump_with_deep_learning/llms.txt Shows how to initialize and perform a forward pass with the TransformerTimeSeries and TransformerTimeSeriesExpandedFeatures models. These models are adapted from standard Transformer encoder architectures for time-series anomaly detection. The code highlights parameter configuration such as feature size, output size, number of attention heads, layers, and dropout. It also demonstrates a variant designed for handling expanded features. ```python import torch from models.transformer import TransformerTimeSeries, TransformerTimeSeriesExpandedFeatures # Initialize Transformer model model = TransformerTimeSeries( feature_size=13, # Input feature dimension (also d_model) output_size=1, # Output dimension per timestep nhead=3, # Number of attention heads (must divide feature_size) num_layers=2, # Number of transformer encoder layers dropout=0.1 # Dropout probability ) device = torch.device('cuda' if torch.cuda.is_available() else 'cpu') model = model.to(device) # Forward pass batch_size, seq_len, feature_size = 32, 15, 13 x = torch.randn(batch_size, seq_len, feature_size).to(device) predictions = model(x) print(f'Input shape: {x.shape}') # torch.Size([32, 15, 13]) print(f'Output shape: {predictions.shape}') # torch.Size([32, 15]) # Feature expansion variant for smaller input features model_expanded = TransformerTimeSeriesExpandedFeatures( feature_size=24, # Expanded feature dimension output_size=1, nhead=4, num_layers=2, dropout=0.1, input_size=8, # Original input features to expand non_expansion_size=5 # Features passed through unchanged ).to(device) x_small = torch.randn(32, 15, 13).to(device) predictions = model_expanded(x_small) print(f'Output shape: {predictions.shape}') # torch.Size([32, 15]) ``` -------------------------------- ### Loading ConvLSTM Model Configurations from JSON Source: https://context7.com/derposoft/crypto_pump_and_dump_with_deep_learning/llms.txt This Python snippet demonstrates how to load pre-tuned hyperparameter configurations for the ConvLSTM model from a JSON file. It opens 'models/conv_lstm_config.json', parses the JSON data, and then accesses specific configurations like '15S_optimal_5Folds'. These configurations are essential for reproducible experiments. ```python import json with open('models/conv_lstm_config.json') as f: clstm_configs = json.load(f) config = clstm_configs['15S_optimal_5Folds'] ``` -------------------------------- ### PyTorch Training and Validation Functions for ConvLSTM Source: https://context7.com/derposoft/crypto_pump_and_dump_with_deep_learning/llms.txt This Python code sets up a ConvLSTM model, data loaders, and training components using PyTorch. It includes functions for single-epoch training, picking an optimal classification threshold, validating the model, and collecting metrics over multiple epochs. The code utilizes CUDA if available for faster computation. ```python import torch from train import train, validate, pick_threshold, collect_metrics_n_epochs from models.conv_lstm import ConvLSTM from data.data import get_data, create_loaders device = torch.device('cuda' if torch.cuda.is_available() else 'cpu') model = ConvLSTM(13, 3, 350, 1).to(device) data = get_data('./data/features_25S.csv.gz', batch_size=128, train_ratio=0.8, undersample_ratio=0.05, segment_length=15) train_loader, test_loader = create_loaders( data, train_ratio=0.8, batch_size=128, undersample_ratio=0.05 ) criterion = torch.nn.BCELoss() optimizer = torch.optim.Adam(model.parameters(), lr=1e-3) epoch_loss = train( model=model, dataloader=train_loader, opt=optimizer, criterion=criterion, device=device, feature_count=13 ) print(f'Epoch loss: {epoch_loss:.5f}') threshold = pick_threshold( model=model, dataloader=train_loader, undersample_ratio=0.05, device=device, verbose=True, feature_count=13 ) acc, precision, recall, f1 = validate( model=model, dataloader=test_loader, device=device, verbose=True, pr_threshold=threshold, feature_count=13 ) print(f'Accuracy: {acc:.5f}') print(f'Precision: {precision:.5f}') print(f'Recall: {recall:.5f}') print(f'F1 Score: {f1:.5f}') from argparse import Namespace config = Namespace( n_epochs=100, train_output_every_n=5, validate_every_n=10, time_epochs=True, final_run=False, prthreshold=0.0, undersample_ratio=0.05, verbose=False, lr_decay_step=0 ) best_metrics = collect_metrics_n_epochs( model, train_loader=train_loader, test_loader=test_loader, optimizer=optimizer, criterion=criterion, device=device, config=config, feature_count=13 ) acc, precision, recall, f1 = best_metrics print(f'Best metrics - Acc: {acc:.5f}, Prec: {precision:.5f}, Rec: {recall:.5f}, F1: {f1:.5f}') ``` -------------------------------- ### Train Deep Learning Models for P&D Detection (Python) Source: https://context7.com/derposoft/crypto_pump_and_dump_with_deep_learning/llms.txt The `train.py` script serves as the primary entry point for training various deep learning models. It supports multiple architectures like CLSTM, Anomaly Transformer, and TransformerTimeSeries, with configurable hyperparameters via command-line arguments. The script handles dataset loading, model instantiation, training loops, and evaluation, outputting training progress and performance metrics. ```python python train.py --model CLSTM --n_epochs 100 --dataset ./data/features_25S.csv.gz python train.py --model AnomalyTransformer \ --n_epochs 200 \ --feature_size 12 \ --n_layers 4 \ --lambda_ 0.0001 \ --lr 1e-3 \ --batch_size 32 \ --segment_length 15 \ --undersample_ratio 0.05 \ --dataset ./data/features_15S.csv.gz python train.py --model CLSTM \ --kfolds 5 \ --n_epochs 200 \ --embedding_size 350 \ --batch_size 600 \ --prthreshold 0.7 \ --dataset ./data/features_15S.csv.gz python train.py --model TransformerTimeSeries \ --feature_size 12 \ --n_layers 2 \ --n_head 3 \ --dropout 0.1 \ --lr 1e-4 \ --batch_size 32 \ --prthreshold 0.48 \ --dataset ./data/features_5S.csv.gz python train.py --model AnomalyTransfomerIntermediate \ --final_run True \ --n_epochs 100 \ --lambda_ 0.0001 \ --seed 42 \ --run_count 3 # Expected output: # Model using 495001 parameters: # Epoch 10 (0.45s) -- Train Loss: 0.12345 # Best threshold: 0.65 (train f1: 0.78) # Mean output at 0: 0.23456 at 1: 0.76543 # Val -- Acc: 0.89123 -- Precision: 0.75432 -- Recall: 0.82345 -- F1: 0.78765 # Best F1 this run: 0.78765 ``` -------------------------------- ### LaMoregia Baseline Classifier Implementation Source: https://context7.com/derposoft/crypto_pump_and_dump_with_deep_learning/llms.txt This Python code utilizes the `lamorgia_classifier` module to run a Random Forest baseline classifier. The classifier is executed for different time frequencies ('5S', '15S', '25S') to evaluate its performance on various data granularities. The expected output includes recall, precision, and F1 score for each frequency. ```python from lamorgia_classifier import classifier classifier(time_freq='5S') classifier(time_freq='15S') classifier(time_freq='25S') ``` -------------------------------- ### Define ConvLSTM Model Architecture (Python) Source: https://context7.com/derposoft/crypto_pump_and_dump_with_deep_learning/llms.txt This code snippet shows the import statement for the ConvLSTM model from the `models.conv_lstm` module. The ConvLSTM architecture combines 1D convolutional layers for spatial feature extraction with Long Short-Term Memory (LSTM) layers for capturing temporal dependencies in the time-series data. It is suitable for sequence modeling tasks where both local patterns and sequential information are important. ```python import torch from models.conv_lstm import ConvLSTM ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.