Spaces:
Runtime error
Runtime error
File size: 3,236 Bytes
a17fae6 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 | {
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# AI Trading: BitNet-Transformer Training\n",
"This notebook trains a 25M parameter ternary-quantized Transformer on 10 years of market data.\n",
"\n",
"## 1. Setup Environment"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# Clone the repository\n",
"!git clone https://github.com/luohoa97/ai-trading.git\n",
"%cd ai-trading\n",
"\n",
"# Install dependencies\n",
"!pip install torch safetensors huggingface_hub pandas numpy yfinance scikit-learn"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## 2. Configuration\n",
"Set your Hugging Face credentials to upload the model after training."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"import os\n",
"from google.colab import userdata\n",
"\n",
"# Best practice: Use Colab Secrets (the key icon on the left)\n",
"try:\n",
" os.environ[\"HF_TOKEN\"] = userdata.get('HF_TOKEN')\n",
" os.environ[\"HF_REPO_ID\"] = \"luohoa97/BitFin\"\n",
" print(\"✅ HF credentials loaded from Colab Secrets\")\n",
"except:\n",
" print(\"⚠️ HF_TOKEN not found in Secrets. Please set it manually or train without upload.\")\n",
" os.environ[\"HF_TOKEN\"] = \"\"\n",
" os.environ[\"HF_REPO_ID\"] = \"luohoa97/BitFin\""
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## 3. Data Generation & Training\n",
"This will:\n",
"1. Fetch 10 years of history for 70 symbols (if not found).\n",
"2. Train the 8-layer Transformer using CUDA (GPU).\n",
"3. Save performance metrics and the model."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# Add root to path so we can import internal scripts\n",
"import sys\n",
"sys.path.append(os.getcwd())\n",
"\n",
"from scripts.train_ai_model import train\n",
"\n",
"# Trigger the training loop\n",
"# Note: It will automatically run build_dataset() if data/trading_dataset.pt is missing\n",
"train()"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## 4. Results\n",
"Check the generated report and verify model stats."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"if os.path.exists(\"performance_report.txt\"):\n",
" with open(\"performance_report.txt\", \"r\") as f:\n",
" print(f.read())\n",
"else:\n",
" print(\"Training failed to produce a report.\")"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.10.12"
}
},
"nbformat": 4,
"nbformat_minor": 4
}
|