121 lines
4.6 KiB
PowerShell
121 lines
4.6 KiB
PowerShell
[CmdletBinding()]
|
|
param(
|
|
[string]$Symbols = "",
|
|
[int]$Limit = 0,
|
|
[string]$Lookbacks = "",
|
|
[string]$Architectures = "",
|
|
[string]$HiddenSizes = "",
|
|
[string]$Layers = "",
|
|
[string]$Dropouts = "",
|
|
[int]$Horizon = 0,
|
|
[string]$Features = "",
|
|
[int]$Epochs = 0,
|
|
[int]$Patience = 0,
|
|
[string]$Interval = "",
|
|
[string]$EnvFile = ""
|
|
)
|
|
|
|
$ErrorActionPreference = "Stop"
|
|
|
|
$RepoRoot = (Resolve-Path (Join-Path $PSScriptRoot "..")).Path
|
|
$RuntimeDir = Join-Path $RepoRoot "runtime"
|
|
$LogFile = Join-Path $RuntimeDir "torch_retrain.log"
|
|
New-Item -ItemType Directory -Force -Path $RuntimeDir | Out-Null
|
|
|
|
function Write-RetrainLog {
|
|
param([string]$Message)
|
|
$timestamp = Get-Date -Format "yyyy-MM-dd HH:mm:ssK"
|
|
"[$timestamp] $Message" | Tee-Object -FilePath $LogFile -Append
|
|
}
|
|
|
|
function Resolve-Python {
|
|
$venvPython = Join-Path $RepoRoot ".venv\Scripts\python.exe"
|
|
if (Test-Path $venvPython) {
|
|
return $venvPython
|
|
}
|
|
|
|
$userPython = Join-Path $env:LOCALAPPDATA "Programs\TradeBotPython312\python.exe"
|
|
if (Test-Path $userPython) {
|
|
return $userPython
|
|
}
|
|
|
|
foreach ($candidate in @("python.exe", "python")) {
|
|
$command = Get-Command $candidate -ErrorAction SilentlyContinue
|
|
if (-not $command) {
|
|
continue
|
|
}
|
|
return $command.Source
|
|
}
|
|
throw "Python was not found. Create .venv or install Python 3.12."
|
|
}
|
|
|
|
if (-not $Symbols -and $env:TORCH_RETRAIN_SYMBOLS) { $Symbols = $env:TORCH_RETRAIN_SYMBOLS }
|
|
if ($Limit -le 0) {
|
|
$Limit = if ($env:TORCH_RETRAIN_LIMIT) { [int]$env:TORCH_RETRAIN_LIMIT } else { 1000 }
|
|
}
|
|
if (-not $Lookbacks) { $Lookbacks = if ($env:TORCH_RETRAIN_LOOKBACKS) { $env:TORCH_RETRAIN_LOOKBACKS } else { "32,64" } }
|
|
if (-not $Architectures) { $Architectures = if ($env:TORCH_RETRAIN_ARCHITECTURES) { $env:TORCH_RETRAIN_ARCHITECTURES } else { "lstm,gru" } }
|
|
if (-not $HiddenSizes) { $HiddenSizes = if ($env:TORCH_RETRAIN_HIDDEN_SIZES) { $env:TORCH_RETRAIN_HIDDEN_SIZES } else { "32,64" } }
|
|
if (-not $Layers) { $Layers = if ($env:TORCH_RETRAIN_LAYERS) { $env:TORCH_RETRAIN_LAYERS } else { "2" } }
|
|
if (-not $Dropouts) { $Dropouts = if ($env:TORCH_RETRAIN_DROPOUTS) { $env:TORCH_RETRAIN_DROPOUTS } else { "0.15" } }
|
|
if ($Horizon -le 0 -and $env:TORCH_RETRAIN_HORIZON) { $Horizon = [int]$env:TORCH_RETRAIN_HORIZON }
|
|
if (-not $Features -and $env:TORCH_RETRAIN_FEATURES) { $Features = $env:TORCH_RETRAIN_FEATURES }
|
|
if ($Epochs -le 0) { $Epochs = if ($env:TORCH_RETRAIN_EPOCHS) { [int]$env:TORCH_RETRAIN_EPOCHS } else { 60 } }
|
|
if ($Patience -le 0) { $Patience = if ($env:TORCH_RETRAIN_PATIENCE) { [int]$env:TORCH_RETRAIN_PATIENCE } else { 10 } }
|
|
if (-not $Interval -and $env:TORCH_RETRAIN_INTERVAL) { $Interval = $env:TORCH_RETRAIN_INTERVAL }
|
|
if (-not $EnvFile -and $env:TORCH_RETRAIN_ENV) { $EnvFile = $env:TORCH_RETRAIN_ENV }
|
|
if (-not $EnvFile -and (Test-Path (Join-Path $RepoRoot ".env"))) { $EnvFile = Join-Path $RepoRoot ".env" }
|
|
|
|
$mutex = New-Object System.Threading.Mutex($false, "TradeBotTorchRecurrentRetrainer")
|
|
$hasLock = $false
|
|
$pushedLocation = $false
|
|
|
|
try {
|
|
$hasLock = $mutex.WaitOne(0)
|
|
if (-not $hasLock) {
|
|
Write-RetrainLog "Another PyTorch recurrent retrain is already running; skipping."
|
|
exit 0
|
|
}
|
|
|
|
$python = Resolve-Python
|
|
$trainerArgs = @(
|
|
"-u",
|
|
"tools\train_torch_recurrent_forecaster.py",
|
|
"--limit", $Limit.ToString(),
|
|
"--lookbacks", $Lookbacks,
|
|
"--architectures", $Architectures,
|
|
"--hidden-sizes", $HiddenSizes,
|
|
"--layers", $Layers,
|
|
"--dropouts", $Dropouts,
|
|
"--epochs", $Epochs.ToString(),
|
|
"--patience", $Patience.ToString()
|
|
)
|
|
if ($Symbols) { $trainerArgs += @("--symbols", $Symbols) }
|
|
if ($Interval) { $trainerArgs += @("--interval", $Interval) }
|
|
if ($EnvFile) { $trainerArgs += @("--env", $EnvFile) }
|
|
if ($Horizon -gt 0) { $trainerArgs += @("--horizon", $Horizon.ToString()) }
|
|
if ($Features) { $trainerArgs += @("--features", $Features) }
|
|
|
|
Push-Location $RepoRoot
|
|
$pushedLocation = $true
|
|
Write-RetrainLog "Starting PyTorch recurrent retrain: $python $($trainerArgs -join ' ')"
|
|
& $python @trainerArgs 2>&1 | Tee-Object -FilePath $LogFile -Append
|
|
if ($LASTEXITCODE -ne 0) {
|
|
throw "Trainer failed with exit code $LASTEXITCODE."
|
|
}
|
|
Write-RetrainLog "Finished PyTorch recurrent retrain."
|
|
}
|
|
catch {
|
|
Write-RetrainLog "ERROR: $($_.Exception.Message)"
|
|
exit 1
|
|
}
|
|
finally {
|
|
if ($pushedLocation) {
|
|
Pop-Location -ErrorAction SilentlyContinue
|
|
}
|
|
if ($hasLock) {
|
|
$mutex.ReleaseMutex()
|
|
}
|
|
$mutex.Dispose()
|
|
}
|