Tin Theethawat Savastham commited on
Commit ·
a84194a
1
Parent(s): 6cac969
✨ Update Experimental Notebook
Browse files- example/.gitignore +3 -1
- example/1-Basic-Model-Constructor.ipynb +340 -70
- functions/mini_plot.py +51 -0
example/.gitignore
CHANGED
|
@@ -1,2 +1,4 @@
|
|
| 1 |
datasets/
|
| 2 |
-
tdce-basic/
|
|
|
|
|
|
|
|
|
| 1 |
datasets/
|
| 2 |
+
tdce-basic/
|
| 3 |
+
results/
|
| 4 |
+
result/
|
example/1-Basic-Model-Constructor.ipynb
CHANGED
|
@@ -19,7 +19,7 @@
|
|
| 19 |
},
|
| 20 |
{
|
| 21 |
"cell_type": "code",
|
| 22 |
-
"execution_count":
|
| 23 |
"id": "ae7385ba",
|
| 24 |
"metadata": {},
|
| 25 |
"outputs": [],
|
|
@@ -46,12 +46,13 @@
|
|
| 46 |
},
|
| 47 |
{
|
| 48 |
"cell_type": "code",
|
| 49 |
-
"execution_count":
|
| 50 |
"id": "4bb874d3",
|
| 51 |
"metadata": {},
|
| 52 |
"outputs": [],
|
| 53 |
"source": [
|
| 54 |
"run_from_online = False\n",
|
|
|
|
| 55 |
"\n",
|
| 56 |
"\n",
|
| 57 |
"if run_from_online:\n",
|
|
@@ -75,18 +76,7 @@
|
|
| 75 |
" import viyacrab_augmentation as viya\n",
|
| 76 |
" import adjust_data as ajd\n",
|
| 77 |
" import result_display as rd\n",
|
| 78 |
-
"\n",
|
| 79 |
-
" importlib.reload(tdce)\n",
|
| 80 |
-
" importlib.reload(mfl)\n",
|
| 81 |
-
" importlib.reload(efl)\n",
|
| 82 |
-
" importlib.reload(cfl)\n",
|
| 83 |
-
" importlib.reload(loss)\n",
|
| 84 |
-
" importlib.reload(tdce)\n",
|
| 85 |
-
" importlib.reload(cmc)\n",
|
| 86 |
-
" importlib.reload(diva)\n",
|
| 87 |
-
" importlib.reload(viya)\n",
|
| 88 |
-
" importlib.reload(ajd)\n",
|
| 89 |
-
" importlib.reload(rd)\n",
|
| 90 |
" # fmt:on\n",
|
| 91 |
"else :\n",
|
| 92 |
" # fmt:off\n",
|
|
@@ -105,19 +95,21 @@
|
|
| 105 |
" import viyacrab_augmentation as viya\n",
|
| 106 |
" import adjust_data as ajd\n",
|
| 107 |
" import result_display as rd\n",
|
| 108 |
-
" \n",
|
| 109 |
-
"
|
| 110 |
-
"
|
| 111 |
-
"
|
| 112 |
-
"
|
| 113 |
-
"
|
| 114 |
-
"
|
| 115 |
-
"
|
| 116 |
-
"
|
| 117 |
-
"
|
| 118 |
-
"
|
| 119 |
-
"
|
| 120 |
-
"
|
|
|
|
|
|
|
| 121 |
]
|
| 122 |
},
|
| 123 |
{
|
|
@@ -137,6 +129,7 @@
|
|
| 137 |
"outputs": [],
|
| 138 |
"source": [
|
| 139 |
"try:\n",
|
|
|
|
| 140 |
" os.mkdir(f\"datasets\")\n",
|
| 141 |
" os.mkdir(f\"datasets/extended-random\")\n",
|
| 142 |
"except FileExistsError:\n",
|
|
@@ -159,32 +152,36 @@
|
|
| 159 |
"metadata": {},
|
| 160 |
"outputs": [],
|
| 161 |
"source": [
|
| 162 |
-
"
|
| 163 |
-
"
|
| 164 |
-
"
|
| 165 |
-
"
|
| 166 |
-
"
|
| 167 |
-
"
|
| 168 |
-
"
|
| 169 |
-
"
|
| 170 |
-
"\n",
|
| 171 |
-
"\n",
|
| 172 |
-
"
|
| 173 |
-
"
|
| 174 |
-
"
|
| 175 |
-
"
|
| 176 |
-
"
|
| 177 |
-
"
|
| 178 |
-
"
|
| 179 |
-
"
|
| 180 |
-
"
|
| 181 |
-
"
|
| 182 |
-
"
|
| 183 |
-
"
|
| 184 |
-
"
|
| 185 |
-
"
|
| 186 |
-
"
|
| 187 |
-
"
|
|
|
|
|
|
|
|
|
|
|
|
|
| 188 |
]
|
| 189 |
},
|
| 190 |
{
|
|
@@ -229,6 +226,8 @@
|
|
| 229 |
" indent=False\n",
|
| 230 |
")\n",
|
| 231 |
"\n",
|
|
|
|
|
|
|
| 232 |
"outlier_index_widget = widgets.Dropdown(\n",
|
| 233 |
" options=['1', '1.5', '2'],\n",
|
| 234 |
" value='1.5',\n",
|
|
@@ -243,6 +242,22 @@
|
|
| 243 |
" indent=False\n",
|
| 244 |
")\n",
|
| 245 |
"\n",
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 246 |
"element_level_lr_widget = widgets.Dropdown(\n",
|
| 247 |
" options=['0.001','0.05', '0.01','0.1','0.5'],\n",
|
| 248 |
" value='0.01',\n",
|
|
@@ -256,32 +271,37 @@
|
|
| 256 |
" description='Model Level Learning Rate:',\n",
|
| 257 |
" disabled=False,\n",
|
| 258 |
")\n",
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 259 |
"\n",
|
| 260 |
"\n",
|
| 261 |
"display(hour_day_employee_widget)\n",
|
| 262 |
"display(hour_day_capital_cost_widget)\n",
|
| 263 |
"display(use_outlier_removal_widget)\n",
|
| 264 |
"display(outlier_index_widget)\n",
|
| 265 |
-
"display(use_augmentation_widget)"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 266 |
]
|
| 267 |
},
|
| 268 |
{
|
| 269 |
"cell_type": "markdown",
|
| 270 |
-
"id": "
|
| 271 |
-
"metadata": {},
|
| 272 |
-
"source": [
|
| 273 |
-
"Select Learning Rate"
|
| 274 |
-
]
|
| 275 |
-
},
|
| 276 |
-
{
|
| 277 |
-
"cell_type": "code",
|
| 278 |
-
"execution_count": null,
|
| 279 |
-
"id": "fce6161f",
|
| 280 |
"metadata": {},
|
| 281 |
-
"outputs": [],
|
| 282 |
"source": [
|
| 283 |
-
"
|
| 284 |
-
"
|
|
|
|
|
|
|
| 285 |
]
|
| 286 |
},
|
| 287 |
{
|
|
@@ -295,7 +315,12 @@
|
|
| 295 |
"hour_day_capital_cost = hour_day_capital_cost_widget.value\n",
|
| 296 |
"use_outlier_removal= use_outlier_removal_widget.value\n",
|
| 297 |
"outlier_index = float(outlier_index_widget.value)\n",
|
| 298 |
-
"use_augmentation = use_augmentation_widget.value"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 299 |
]
|
| 300 |
},
|
| 301 |
{
|
|
@@ -314,12 +339,23 @@
|
|
| 314 |
"outputs": [],
|
| 315 |
"source": [
|
| 316 |
"folder_path = 'datasets/extended-random'\n",
|
|
|
|
| 317 |
"\n",
|
| 318 |
"cost_generator = cmc.CostMatrixGenerator()\n",
|
| 319 |
"cost_generator.change_data_directory(folder_path)\n",
|
| 320 |
"cost_generator.load_data()\n",
|
| 321 |
"input_variation = diva.display_input_variation_by_directory(folder_path)\n",
|
| 322 |
-
"input_variation.to_csv(f\"{folder_path}/data_variation.csv\")"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 323 |
]
|
| 324 |
},
|
| 325 |
{
|
|
@@ -472,7 +508,241 @@
|
|
| 472 |
"id": "d1bdcbbc",
|
| 473 |
"metadata": {},
|
| 474 |
"source": [
|
| 475 |
-
"## Initial Model"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 476 |
]
|
| 477 |
}
|
| 478 |
],
|
|
|
|
| 19 |
},
|
| 20 |
{
|
| 21 |
"cell_type": "code",
|
| 22 |
+
"execution_count": null,
|
| 23 |
"id": "ae7385ba",
|
| 24 |
"metadata": {},
|
| 25 |
"outputs": [],
|
|
|
|
| 46 |
},
|
| 47 |
{
|
| 48 |
"cell_type": "code",
|
| 49 |
+
"execution_count": null,
|
| 50 |
"id": "4bb874d3",
|
| 51 |
"metadata": {},
|
| 52 |
"outputs": [],
|
| 53 |
"source": [
|
| 54 |
"run_from_online = False\n",
|
| 55 |
+
"ignore_download_dataset = True\n",
|
| 56 |
"\n",
|
| 57 |
"\n",
|
| 58 |
"if run_from_online:\n",
|
|
|
|
| 76 |
" import viyacrab_augmentation as viya\n",
|
| 77 |
" import adjust_data as ajd\n",
|
| 78 |
" import result_display as rd\n",
|
| 79 |
+
" import mini_plot as mp\n",
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 80 |
" # fmt:on\n",
|
| 81 |
"else :\n",
|
| 82 |
" # fmt:off\n",
|
|
|
|
| 95 |
" import viyacrab_augmentation as viya\n",
|
| 96 |
" import adjust_data as ajd\n",
|
| 97 |
" import result_display as rd\n",
|
| 98 |
+
" import mini_plot as mp\n",
|
| 99 |
+
" # fmt:on\n",
|
| 100 |
+
"\n",
|
| 101 |
+
"importlib.reload(tdce)\n",
|
| 102 |
+
"importlib.reload(mfl)\n",
|
| 103 |
+
"importlib.reload(efl)\n",
|
| 104 |
+
"importlib.reload(cfl)\n",
|
| 105 |
+
"importlib.reload(loss)\n",
|
| 106 |
+
"importlib.reload(tdce)\n",
|
| 107 |
+
"importlib.reload(cmc)\n",
|
| 108 |
+
"importlib.reload(diva)\n",
|
| 109 |
+
"importlib.reload(viya)\n",
|
| 110 |
+
"importlib.reload(ajd)\n",
|
| 111 |
+
"importlib.reload(rd)\n",
|
| 112 |
+
"importlib.reload(mp)"
|
| 113 |
]
|
| 114 |
},
|
| 115 |
{
|
|
|
|
| 129 |
"outputs": [],
|
| 130 |
"source": [
|
| 131 |
"try:\n",
|
| 132 |
+
" os.mkdir('result')\n",
|
| 133 |
" os.mkdir(f\"datasets\")\n",
|
| 134 |
" os.mkdir(f\"datasets/extended-random\")\n",
|
| 135 |
"except FileExistsError:\n",
|
|
|
|
| 152 |
"metadata": {},
|
| 153 |
"outputs": [],
|
| 154 |
"source": [
|
| 155 |
+
"def download_file():\n",
|
| 156 |
+
" capital_cost_link = \"https://huggingface.co/datasets/theethawats98/tdce-example-extended-random/resolve/main/generated_capital_cost.csv\"\n",
|
| 157 |
+
" capital_path = 'datasets/extended-random/generated_capital_cost.csv'\n",
|
| 158 |
+
" employee_usage_link = \"https://huggingface.co/datasets/theethawats98/tdce-example-extended-random/resolve/main/generated_employee_usage.csv\"\n",
|
| 159 |
+
" employee_path = 'datasets/extended-random/generated_employee_usage.csv'\n",
|
| 160 |
+
" material_usage_link = \"https://huggingface.co/datasets/theethawats98/tdce-example-extended-random/resolve/main/generated_material_usage.csv\"\n",
|
| 161 |
+
" material_path = 'datasets/extended-random/generated_material_usage.csv'\n",
|
| 162 |
+
" process_data_link = \"https://huggingface.co/datasets/theethawats98/tdce-example-extended-random/resolve/main/generated_process_data.csv\"\n",
|
| 163 |
+
" process_path = 'datasets/extended-random/generated_process_data.csv'\n",
|
| 164 |
+
"\n",
|
| 165 |
+
"\n",
|
| 166 |
+
" for link, path in [\n",
|
| 167 |
+
" (capital_cost_link, capital_path),\n",
|
| 168 |
+
" (employee_usage_link, employee_path),\n",
|
| 169 |
+
" (material_usage_link, material_path),\n",
|
| 170 |
+
" (process_data_link, process_path)\n",
|
| 171 |
+
" ]:\n",
|
| 172 |
+
" if not os.path.exists(path):\n",
|
| 173 |
+
" response = requests.get(link)\n",
|
| 174 |
+
" if response.status_code == 200:\n",
|
| 175 |
+
" with open(path, 'wb') as file:\n",
|
| 176 |
+
" file.write(response.content)\n",
|
| 177 |
+
" print(f'File {path} downloaded successfully')\n",
|
| 178 |
+
" else:\n",
|
| 179 |
+
" print(f'Failed to download file {path}')\n",
|
| 180 |
+
" # Downloading the datasets\n",
|
| 181 |
+
" print(\"Downloading datasets...\")\n",
|
| 182 |
+
"\n",
|
| 183 |
+
"if (not ignore_download_dataset):\n",
|
| 184 |
+
" download_file()"
|
| 185 |
]
|
| 186 |
},
|
| 187 |
{
|
|
|
|
| 226 |
" indent=False\n",
|
| 227 |
")\n",
|
| 228 |
"\n",
|
| 229 |
+
"\n",
|
| 230 |
+
"\n",
|
| 231 |
"outlier_index_widget = widgets.Dropdown(\n",
|
| 232 |
" options=['1', '1.5', '2'],\n",
|
| 233 |
" value='1.5',\n",
|
|
|
|
| 242 |
" indent=False\n",
|
| 243 |
")\n",
|
| 244 |
"\n",
|
| 245 |
+
"use_early_stopping_widget = widgets.Checkbox(\n",
|
| 246 |
+
" value=False,\n",
|
| 247 |
+
" description='Enable Early Stopping',\n",
|
| 248 |
+
" disabled=False,\n",
|
| 249 |
+
" indent=False\n",
|
| 250 |
+
")\n",
|
| 251 |
+
"\n",
|
| 252 |
+
"early_stopping_patience_widget = widgets.BoundedIntText(\n",
|
| 253 |
+
" value=10,\n",
|
| 254 |
+
" min=1,\n",
|
| 255 |
+
" max=100,\n",
|
| 256 |
+
" step=1,\n",
|
| 257 |
+
" description='Early Stopping Patience Round:',\n",
|
| 258 |
+
" disabled=False\n",
|
| 259 |
+
")\n",
|
| 260 |
+
"\n",
|
| 261 |
"element_level_lr_widget = widgets.Dropdown(\n",
|
| 262 |
" options=['0.001','0.05', '0.01','0.1','0.5'],\n",
|
| 263 |
" value='0.01',\n",
|
|
|
|
| 271 |
" description='Model Level Learning Rate:',\n",
|
| 272 |
" disabled=False,\n",
|
| 273 |
")\n",
|
| 274 |
+
"epoch_widget = widgets.BoundedIntText(\n",
|
| 275 |
+
" value=100,\n",
|
| 276 |
+
" min=10,\n",
|
| 277 |
+
" max=1000,\n",
|
| 278 |
+
" step=10,\n",
|
| 279 |
+
" description='Number of Epochs:',\n",
|
| 280 |
+
" disabled=False\n",
|
| 281 |
+
")\n",
|
| 282 |
"\n",
|
| 283 |
"\n",
|
| 284 |
"display(hour_day_employee_widget)\n",
|
| 285 |
"display(hour_day_capital_cost_widget)\n",
|
| 286 |
"display(use_outlier_removal_widget)\n",
|
| 287 |
"display(outlier_index_widget)\n",
|
| 288 |
+
"display(use_augmentation_widget)\n",
|
| 289 |
+
"display(use_early_stopping_widget)\n",
|
| 290 |
+
"display(early_stopping_patience_widget)\n",
|
| 291 |
+
"display(element_level_lr_widget)\n",
|
| 292 |
+
"display(model_level_lr_widget)\n",
|
| 293 |
+
"display(epoch_widget)"
|
| 294 |
]
|
| 295 |
},
|
| 296 |
{
|
| 297 |
"cell_type": "markdown",
|
| 298 |
+
"id": "204af1e1",
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 299 |
"metadata": {},
|
|
|
|
| 300 |
"source": [
|
| 301 |
+
"If you config the value on the widget, please reexecute all of these bottom cells for create a model according to your data.\n",
|
| 302 |
+
"\n",
|
| 303 |
+
"\n",
|
| 304 |
+
"Getting Value from Widget"
|
| 305 |
]
|
| 306 |
},
|
| 307 |
{
|
|
|
|
| 315 |
"hour_day_capital_cost = hour_day_capital_cost_widget.value\n",
|
| 316 |
"use_outlier_removal= use_outlier_removal_widget.value\n",
|
| 317 |
"outlier_index = float(outlier_index_widget.value)\n",
|
| 318 |
+
"use_augmentation = use_augmentation_widget.value\n",
|
| 319 |
+
"element_level_lr = float(element_level_lr_widget.value)\n",
|
| 320 |
+
"model_level_lr = float(model_level_lr_widget.value)\n",
|
| 321 |
+
"use_early_stopping =use_early_stopping_widget.value\n",
|
| 322 |
+
"early_stopping_patience = early_stopping_patience_widget.value\n",
|
| 323 |
+
"epoch_number = epoch_widget.value"
|
| 324 |
]
|
| 325 |
},
|
| 326 |
{
|
|
|
|
| 339 |
"outputs": [],
|
| 340 |
"source": [
|
| 341 |
"folder_path = 'datasets/extended-random'\n",
|
| 342 |
+
"output_folder_path = f'result/{model_level_lr}'\n",
|
| 343 |
"\n",
|
| 344 |
"cost_generator = cmc.CostMatrixGenerator()\n",
|
| 345 |
"cost_generator.change_data_directory(folder_path)\n",
|
| 346 |
"cost_generator.load_data()\n",
|
| 347 |
"input_variation = diva.display_input_variation_by_directory(folder_path)\n",
|
| 348 |
+
"input_variation.to_csv(f\"{folder_path}/data_variation.csv\")\n",
|
| 349 |
+
"\n",
|
| 350 |
+
"(process_df,employee_usage,material_usage,capital_cost_usage) = cost_generator.get_data()"
|
| 351 |
+
]
|
| 352 |
+
},
|
| 353 |
+
{
|
| 354 |
+
"cell_type": "markdown",
|
| 355 |
+
"id": "4ea56434",
|
| 356 |
+
"metadata": {},
|
| 357 |
+
"source": [
|
| 358 |
+
"If you want to see the raw data, you can display using `process_df`, `process_df.head()`, `employee_df` and so on."
|
| 359 |
]
|
| 360 |
},
|
| 361 |
{
|
|
|
|
| 508 |
"id": "d1bdcbbc",
|
| 509 |
"metadata": {},
|
| 510 |
"source": [
|
| 511 |
+
"## Initial Model\n",
|
| 512 |
+
"\n",
|
| 513 |
+
"### Initial Layer\n",
|
| 514 |
+
"Create function to initial layer from cost matrix, it will automatically find what the input size is need for due to the data"
|
| 515 |
+
]
|
| 516 |
+
},
|
| 517 |
+
{
|
| 518 |
+
"cell_type": "code",
|
| 519 |
+
"execution_count": null,
|
| 520 |
+
"id": "2fce43cf",
|
| 521 |
+
"metadata": {},
|
| 522 |
+
"outputs": [],
|
| 523 |
+
"source": [
|
| 524 |
+
"def inital_layer(\n",
|
| 525 |
+
" material_cost_matrix,\n",
|
| 526 |
+
" employee_cost_matrix,\n",
|
| 527 |
+
" capital_cost_matrix,\n",
|
| 528 |
+
"):\n",
|
| 529 |
+
" total_col = 0\n",
|
| 530 |
+
" # Material FC Layer\n",
|
| 531 |
+
" row, high, col = material_cost_matrix.shape\n",
|
| 532 |
+
" material_layer_1 = mfl.MaterialFCLayer(col, 1)\n",
|
| 533 |
+
" # material_layer_1.annotate(material_cost_matrix, material_amount_matrix)\n",
|
| 534 |
+
" total_col += col\n",
|
| 535 |
+
"\n",
|
| 536 |
+
" # Monthy Employee FC Layer\n",
|
| 537 |
+
" row, high, col = employee_cost_matrix.shape\n",
|
| 538 |
+
" employee_layer_1 = efl.EmployeeFCLayer(col, 1, 8)\n",
|
| 539 |
+
" total_col += col\n",
|
| 540 |
+
" # monthy_employee_layer_1.annotate(monthy_employee_cost_matrix, duration_matrix)\n",
|
| 541 |
+
"\n",
|
| 542 |
+
" # Capital Cost FC Layer\n",
|
| 543 |
+
" row, high, col = capital_cost_matrix.shape\n",
|
| 544 |
+
" capital_cost_layer1 = cfl.CapitalCostFCLayer(col, 1, 21)\n",
|
| 545 |
+
" total_col += col\n",
|
| 546 |
+
" # capital_cost_layer1.annotate(\n",
|
| 547 |
+
" # capital_cost_matrix, life_time_matrix, machine_hour_matrix, duration_matrix)\n",
|
| 548 |
+
"\n",
|
| 549 |
+
" return (\n",
|
| 550 |
+
" material_layer_1,\n",
|
| 551 |
+
" employee_layer_1,\n",
|
| 552 |
+
" capital_cost_layer1,\n",
|
| 553 |
+
" )\n"
|
| 554 |
+
]
|
| 555 |
+
},
|
| 556 |
+
{
|
| 557 |
+
"cell_type": "markdown",
|
| 558 |
+
"id": "662b0bd2",
|
| 559 |
+
"metadata": {},
|
| 560 |
+
"source": [
|
| 561 |
+
"### Model Initialization\n",
|
| 562 |
+
"Create the model object from its class"
|
| 563 |
+
]
|
| 564 |
+
},
|
| 565 |
+
{
|
| 566 |
+
"cell_type": "code",
|
| 567 |
+
"execution_count": null,
|
| 568 |
+
"id": "53684466",
|
| 569 |
+
"metadata": {},
|
| 570 |
+
"outputs": [],
|
| 571 |
+
"source": [
|
| 572 |
+
"# Initial Model\n",
|
| 573 |
+
"tdce_model = tdce.TDCEModel()\n",
|
| 574 |
+
" \n",
|
| 575 |
+
"# Create the Layer\n",
|
| 576 |
+
"(\n",
|
| 577 |
+
" material_layer_1,\n",
|
| 578 |
+
" employee_layer_1,\n",
|
| 579 |
+
" capital_cost_layer1,\n",
|
| 580 |
+
") = inital_layer(\n",
|
| 581 |
+
" capital_cost_matrix=capital_cost_matrix,\n",
|
| 582 |
+
" employee_cost_matrix=employee_cost_matrix,\n",
|
| 583 |
+
" material_cost_matrix=material_cost_matrix,\n",
|
| 584 |
+
")\n",
|
| 585 |
+
"\n",
|
| 586 |
+
"# Add the Layer to the Model\n",
|
| 587 |
+
"tdce_model.inital_inside_element(\n",
|
| 588 |
+
" material_layer=material_layer_1,\n",
|
| 589 |
+
" capital_cost_layer=capital_cost_layer1,\n",
|
| 590 |
+
" employee_layer=employee_layer_1,\n",
|
| 591 |
+
")\n",
|
| 592 |
+
"\n",
|
| 593 |
+
"# Install the error calculator\n",
|
| 594 |
+
"tdce_model.use(loss=loss.mse, loss_prime=loss.mse_prime,\n",
|
| 595 |
+
" loss_percent=loss.rmspe)\n",
|
| 596 |
+
"\n",
|
| 597 |
+
"# Set Early Stopping\n",
|
| 598 |
+
"if use_early_stopping:\n",
|
| 599 |
+
" tdce_model.activate_early_stopping()\n",
|
| 600 |
+
" tdce_model.edit_patience_round(early_stopping_patience)"
|
| 601 |
+
]
|
| 602 |
+
},
|
| 603 |
+
{
|
| 604 |
+
"cell_type": "markdown",
|
| 605 |
+
"id": "df6d4796",
|
| 606 |
+
"metadata": {},
|
| 607 |
+
"source": [
|
| 608 |
+
"Setting the learning rate"
|
| 609 |
+
]
|
| 610 |
+
},
|
| 611 |
+
{
|
| 612 |
+
"cell_type": "code",
|
| 613 |
+
"execution_count": null,
|
| 614 |
+
"id": "b75da97b",
|
| 615 |
+
"metadata": {},
|
| 616 |
+
"outputs": [],
|
| 617 |
+
"source": [
|
| 618 |
+
"# Use the same learning rate for all element level\n",
|
| 619 |
+
"tdce_model.set_learning_rate(\n",
|
| 620 |
+
" element_level_lr,element_level_lr,element_level_lr\n",
|
| 621 |
+
")\n",
|
| 622 |
+
"\n",
|
| 623 |
+
"# activate model weight\n",
|
| 624 |
+
"tdce_model.activete_model_weight()"
|
| 625 |
+
]
|
| 626 |
+
},
|
| 627 |
+
{
|
| 628 |
+
"cell_type": "markdown",
|
| 629 |
+
"id": "38df7ced",
|
| 630 |
+
"metadata": {},
|
| 631 |
+
"source": [
|
| 632 |
+
"## Training\n",
|
| 633 |
+
"\n",
|
| 634 |
+
"Fit a model with input and output data"
|
| 635 |
+
]
|
| 636 |
+
},
|
| 637 |
+
{
|
| 638 |
+
"cell_type": "code",
|
| 639 |
+
"execution_count": null,
|
| 640 |
+
"id": "2fe52d20",
|
| 641 |
+
"metadata": {},
|
| 642 |
+
"outputs": [],
|
| 643 |
+
"source": [
|
| 644 |
+
"start_time = time.time()\n",
|
| 645 |
+
"\n",
|
| 646 |
+
"tdce_model.fit_with_validation(\n",
|
| 647 |
+
" epoch=epoch_number,\n",
|
| 648 |
+
" learning_rate=model_level_lr,\n",
|
| 649 |
+
" material_amount_matrix=material_amount_matrix,\n",
|
| 650 |
+
" material_cost_matrix=material_cost_matrix,\n",
|
| 651 |
+
" employee_cost_matrix=employee_cost_matrix,\n",
|
| 652 |
+
" employee_duration_matrix=employee_duration_matrix,\n",
|
| 653 |
+
" employee_day_amount_matrix=employee_day_amount_matrix,\n",
|
| 654 |
+
" result_matrix=result_matrix,\n",
|
| 655 |
+
" capital_cost_matrix=capital_cost_matrix,\n",
|
| 656 |
+
" day_amount_matrix=day_amount_matrix,\n",
|
| 657 |
+
" validation_payload=validation_payload,\n",
|
| 658 |
+
" capital_cost_duration_matrix=capital_cost_duration_matrix,\n",
|
| 659 |
+
" )\n",
|
| 660 |
+
"\n",
|
| 661 |
+
"end_time = time.time()\n",
|
| 662 |
+
"time_usage = end_time - start_time\n",
|
| 663 |
+
"print(f\"Learning Rate: {model_level_lr} / {element_level_lr}\")\n",
|
| 664 |
+
"print(f\"Time Using {time_usage} Second\")\n"
|
| 665 |
+
]
|
| 666 |
+
},
|
| 667 |
+
{
|
| 668 |
+
"cell_type": "markdown",
|
| 669 |
+
"id": "39b9aa25",
|
| 670 |
+
"metadata": {},
|
| 671 |
+
"source": [
|
| 672 |
+
"Get the Error Listing"
|
| 673 |
+
]
|
| 674 |
+
},
|
| 675 |
+
{
|
| 676 |
+
"cell_type": "code",
|
| 677 |
+
"execution_count": null,
|
| 678 |
+
"id": "7fa00c92",
|
| 679 |
+
"metadata": {},
|
| 680 |
+
"outputs": [],
|
| 681 |
+
"source": [
|
| 682 |
+
"try:\n",
|
| 683 |
+
" os.mkdir(f\"{output_folder_path}\")\n",
|
| 684 |
+
"except FileExistsError:\n",
|
| 685 |
+
" print(\"Folder is Exist\")\n",
|
| 686 |
+
" pass"
|
| 687 |
+
]
|
| 688 |
+
},
|
| 689 |
+
{
|
| 690 |
+
"cell_type": "code",
|
| 691 |
+
"execution_count": null,
|
| 692 |
+
"id": "8861bedb",
|
| 693 |
+
"metadata": {},
|
| 694 |
+
"outputs": [],
|
| 695 |
+
"source": [
|
| 696 |
+
"error_list = tdce_model.get_epoch_error()\n",
|
| 697 |
+
"sample_error_list = tdce_model.get_sample_error()\n",
|
| 698 |
+
"error_df = pd.DataFrame(error_list)\n",
|
| 699 |
+
"sample_error_df = pd.DataFrame(sample_error_list)\n",
|
| 700 |
+
"\n",
|
| 701 |
+
"\n",
|
| 702 |
+
"# Get Overall Output\n",
|
| 703 |
+
"minimum_error = error_df[\"error\"].min()\n",
|
| 704 |
+
"minimum_percent_error = error_df[\"error_percent\"].min()\n",
|
| 705 |
+
"minimum_validate_error = error_df[\"validate_error\"].min()\n",
|
| 706 |
+
"minimum_validate_percent_error = error_df[\"validate_error_percent\"].min()\n",
|
| 707 |
+
"\n",
|
| 708 |
+
"# Export the Output Result\n",
|
| 709 |
+
"error_df.to_csv(f\"{output_folder_path}/{epoch_number}-{element_level_lr}.csv\")\n",
|
| 710 |
+
"sample_error_df.to_csv(\n",
|
| 711 |
+
" f\"{output_folder_path}/error-list-{epoch_number}-{element_level_lr}.csv\"\n",
|
| 712 |
+
")\n",
|
| 713 |
+
"sample_payload = tdce_model.get_sample_payload()\n",
|
| 714 |
+
"sample_payload_df = pd.DataFrame(sample_payload)\n",
|
| 715 |
+
"\n",
|
| 716 |
+
"# Export all the sample / history of all training\n",
|
| 717 |
+
"sample_payload_df.to_csv(\n",
|
| 718 |
+
" f\"{output_folder_path}/sample-payload-list-{epoch_number}-{element_level_lr}.csv\",\n",
|
| 719 |
+
" index=False,\n",
|
| 720 |
+
")\n",
|
| 721 |
+
"\n",
|
| 722 |
+
"print(f\"Minimum Error: {minimum_error}\"\n",
|
| 723 |
+
" f\" / Minimum Percent Error (RMSPE): {minimum_percent_error}\"\n",
|
| 724 |
+
" f\" / Minimum Validate Error: {minimum_validate_error}\"\n",
|
| 725 |
+
" f\" / Minimum Validate Percent Error (RMSEP): {minimum_validate_percent_error}\")\n"
|
| 726 |
+
]
|
| 727 |
+
},
|
| 728 |
+
{
|
| 729 |
+
"cell_type": "markdown",
|
| 730 |
+
"id": "639b9b91",
|
| 731 |
+
"metadata": {},
|
| 732 |
+
"source": [
|
| 733 |
+
"## Visualization\n",
|
| 734 |
+
"Display the model training behavior"
|
| 735 |
+
]
|
| 736 |
+
},
|
| 737 |
+
{
|
| 738 |
+
"cell_type": "code",
|
| 739 |
+
"execution_count": null,
|
| 740 |
+
"id": "dc3bdc9d",
|
| 741 |
+
"metadata": {},
|
| 742 |
+
"outputs": [],
|
| 743 |
+
"source": [
|
| 744 |
+
"mp.plotting_learning_curve(epoch_error=error_df,element_learning_rate=element_level_lr,\n",
|
| 745 |
+
" model_learning_rate=model_level_lr)"
|
| 746 |
]
|
| 747 |
}
|
| 748 |
],
|
functions/mini_plot.py
ADDED
|
@@ -0,0 +1,51 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import pandas as pd
|
| 2 |
+
import matplotlib.pyplot as plt
|
| 3 |
+
import seaborn as sns
|
| 4 |
+
|
| 5 |
+
sns.set_theme(style="whitegrid", font="Noto Sans",
|
| 6 |
+
font_scale=1)
|
| 7 |
+
this_graph_palette = sns.color_palette("husl", 4)
|
| 8 |
+
sns.set_palette(this_graph_palette)
|
| 9 |
+
|
| 10 |
+
|
| 11 |
+
def plotting_learning_curve(epoch_error, element_learning_rate, model_learning_rate):
|
| 12 |
+
fig, ax = plt.subplots(1, 2, figsize=(8, 4))
|
| 13 |
+
|
| 14 |
+
ax[0].plot(
|
| 15 |
+
epoch_error["epoch"],
|
| 16 |
+
epoch_error["error"],
|
| 17 |
+
label="Training",
|
| 18 |
+
linewidth=2
|
| 19 |
+
)
|
| 20 |
+
ax[0].plot(
|
| 21 |
+
epoch_error["epoch"],
|
| 22 |
+
epoch_error["validate_error"],
|
| 23 |
+
label="Validation", linewidth=2
|
| 24 |
+
)
|
| 25 |
+
ax[0].set_title(
|
| 26 |
+
f"Learning Curve {model_learning_rate}/{element_learning_rate} in MSE",
|
| 27 |
+
fontdict={"fontsize": 12},
|
| 28 |
+
)
|
| 29 |
+
ax[1].plot(
|
| 30 |
+
epoch_error["epoch"],
|
| 31 |
+
epoch_error["error_percent"],
|
| 32 |
+
label="Training",
|
| 33 |
+
linewidth=2
|
| 34 |
+
)
|
| 35 |
+
ax[1].plot(
|
| 36 |
+
epoch_error["epoch"],
|
| 37 |
+
epoch_error["validate_error_percent"],
|
| 38 |
+
label="Validation", linewidth=2
|
| 39 |
+
)
|
| 40 |
+
ax[1].set_ylim(
|
| 41 |
+
0, 100)
|
| 42 |
+
ax[1].set_xlim(
|
| 43 |
+
0, 100)
|
| 44 |
+
|
| 45 |
+
ax[1].set_title(
|
| 46 |
+
f"Learning Curve {model_learning_rate}/{element_learning_rate} in RMSPE",
|
| 47 |
+
fontdict={"fontsize": 12},
|
| 48 |
+
)
|
| 49 |
+
ax[0].legend(loc="lower right")
|
| 50 |
+
ax[1].legend(loc="lower right")
|
| 51 |
+
fig.tight_layout(pad=3.0)
|