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