Spaces:
Runtime error
Runtime error
Created using Colaboratory
Browse files- stock_predictor.ipynb +299 -477
stock_predictor.ipynb
CHANGED
|
@@ -4,10 +4,7 @@
|
|
| 4 |
"metadata": {
|
| 5 |
"colab": {
|
| 6 |
"provenance": [],
|
| 7 |
-
"
|
| 8 |
-
"Z3N2WMYNV-qX"
|
| 9 |
-
],
|
| 10 |
-
"authorship_tag": "ABX9TyOuk8MIfThoeWnRbBQlPf+h",
|
| 11 |
"include_colab_link": true
|
| 12 |
},
|
| 13 |
"kernelspec": {
|
|
@@ -17,7 +14,8 @@
|
|
| 17 |
"language_info": {
|
| 18 |
"name": "python"
|
| 19 |
},
|
| 20 |
-
"gpuClass": "standard"
|
|
|
|
| 21 |
},
|
| 22 |
"cells": [
|
| 23 |
{
|
|
@@ -43,15 +41,15 @@
|
|
| 43 |
"base_uri": "https://localhost:8080/"
|
| 44 |
},
|
| 45 |
"id": "Xr3Qozgfktoc",
|
| 46 |
-
"outputId": "
|
| 47 |
},
|
| 48 |
-
"execution_count":
|
| 49 |
"outputs": [
|
| 50 |
{
|
| 51 |
"output_type": "stream",
|
| 52 |
"name": "stdout",
|
| 53 |
"text": [
|
| 54 |
-
"
|
| 55 |
"/content/drive/MyDrive/projects/Stock_Predicter\n"
|
| 56 |
]
|
| 57 |
}
|
|
@@ -59,7 +57,7 @@
|
|
| 59 |
},
|
| 60 |
{
|
| 61 |
"cell_type": "code",
|
| 62 |
-
"execution_count":
|
| 63 |
"metadata": {
|
| 64 |
"id": "e8SQqogMQYLh"
|
| 65 |
},
|
|
@@ -101,7 +99,7 @@
|
|
| 101 |
"metadata": {
|
| 102 |
"id": "O6dtJpJwS5Eg"
|
| 103 |
},
|
| 104 |
-
"execution_count":
|
| 105 |
"outputs": []
|
| 106 |
},
|
| 107 |
{
|
|
@@ -115,9 +113,9 @@
|
|
| 115 |
"base_uri": "https://localhost:8080/"
|
| 116 |
},
|
| 117 |
"id": "LwPyk8Uh-Zz_",
|
| 118 |
-
"outputId": "
|
| 119 |
},
|
| 120 |
-
"execution_count":
|
| 121 |
"outputs": [
|
| 122 |
{
|
| 123 |
"output_type": "stream",
|
|
@@ -140,136 +138,201 @@
|
|
| 140 |
{
|
| 141 |
"cell_type": "code",
|
| 142 |
"source": [
|
| 143 |
-
"def
|
| 144 |
-
"
|
| 145 |
-
"
|
| 146 |
-
"
|
| 147 |
-
"
|
| 148 |
-
"
|
| 149 |
-
"
|
| 150 |
-
"
|
| 151 |
-
|
| 152 |
-
|
| 153 |
-
"
|
| 154 |
-
|
| 155 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 156 |
" if scaler is None:\n",
|
| 157 |
" # Create the scaler\n",
|
| 158 |
-
" values = the_data.values\n",
|
| 159 |
-
" # print('values')\n",
|
| 160 |
-
" # print(values)\n",
|
| 161 |
" max_value = np.max(values[:,:-1])\n",
|
| 162 |
-
" # print(max_value)\n",
|
| 163 |
-
" min_value = np.min(values[:,:-1])\n",
|
| 164 |
-
" # print(min_value)\n",
|
| 165 |
" max_volume = np.max(values[:,-1])\n",
|
| 166 |
-
"
|
| 167 |
-
"
|
| 168 |
-
"
|
| 169 |
-
"
|
| 170 |
-
"
|
| 171 |
-
"
|
| 172 |
-
" values[:,-1] = (values[:,-1] - min_volume) / (max_volume-min_volume) * 2 - 1\n",
|
| 173 |
-
" # print(values)\n",
|
| 174 |
" return data\n",
|
| 175 |
-
" def
|
| 176 |
-
" decoded_values =
|
| 177 |
" return decoded_values\n",
|
|
|
|
|
|
|
| 178 |
" \n",
|
| 179 |
" normalized_data = scaler(the_data)\n",
|
| 180 |
"\n",
|
| 181 |
-
" return normalized_data, scaler,
|
| 182 |
"\n",
|
| 183 |
"\n"
|
| 184 |
],
|
| 185 |
"metadata": {
|
| 186 |
"id": "v9RoqzBvtrOb"
|
| 187 |
},
|
| 188 |
-
"execution_count":
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 189 |
"outputs": []
|
| 190 |
},
|
| 191 |
{
|
| 192 |
"cell_type": "code",
|
| 193 |
"source": [
|
| 194 |
-
"
|
| 195 |
-
"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 196 |
],
|
| 197 |
"metadata": {
|
| 198 |
-
"id": "-
|
| 199 |
},
|
| 200 |
-
"execution_count":
|
| 201 |
"outputs": []
|
| 202 |
},
|
| 203 |
{
|
| 204 |
"cell_type": "code",
|
| 205 |
"source": [
|
| 206 |
-
"
|
| 207 |
],
|
| 208 |
"metadata": {
|
| 209 |
"colab": {
|
| 210 |
"base_uri": "https://localhost:8080/"
|
| 211 |
},
|
| 212 |
-
"id": "
|
| 213 |
-
"outputId": "
|
| 214 |
},
|
| 215 |
-
"execution_count":
|
| 216 |
"outputs": [
|
| 217 |
{
|
| 218 |
-
"output_type": "
|
| 219 |
-
"
|
| 220 |
-
|
| 221 |
-
|
| 222 |
-
|
| 223 |
-
|
| 224 |
-
"metadata": {},
|
| 225 |
-
"execution_count": 41
|
| 226 |
}
|
| 227 |
]
|
| 228 |
},
|
| 229 |
{
|
| 230 |
"cell_type": "code",
|
| 231 |
"source": [
|
| 232 |
-
"
|
| 233 |
-
"
|
| 234 |
-
"x_train_list = []\n",
|
| 235 |
-
"y_train_list = []\n",
|
| 236 |
-
"\n",
|
| 237 |
-
"for i in range(prediction_days, len(norm_data)):\n",
|
| 238 |
-
" x_train_list.append(norm_data[i-prediction_days:i])\n",
|
| 239 |
-
" y_train_list.append(norm_data.iloc[i].values[0:4])\n",
|
| 240 |
-
"\n",
|
| 241 |
-
"x_train = np.array(x_train_list)\n",
|
| 242 |
-
"y_train = np.array(y_train_list)"
|
| 243 |
],
|
| 244 |
"metadata": {
|
| 245 |
-
"
|
|
|
|
|
|
|
|
|
|
|
|
|
| 246 |
},
|
| 247 |
-
"execution_count":
|
| 248 |
-
"outputs": [
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 249 |
},
|
| 250 |
{
|
| 251 |
"cell_type": "code",
|
| 252 |
"source": [
|
| 253 |
-
"
|
| 254 |
-
"print(
|
| 255 |
-
"
|
|
|
|
|
|
|
| 256 |
],
|
| 257 |
"metadata": {
|
| 258 |
"colab": {
|
| 259 |
"base_uri": "https://localhost:8080/"
|
| 260 |
},
|
| 261 |
-
"id": "
|
| 262 |
-
"outputId": "
|
| 263 |
},
|
| 264 |
-
"execution_count":
|
| 265 |
"outputs": [
|
| 266 |
{
|
| 267 |
"output_type": "stream",
|
| 268 |
"name": "stdout",
|
| 269 |
"text": [
|
| 270 |
-
"
|
| 271 |
-
"
|
| 272 |
-
"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 273 |
]
|
| 274 |
}
|
| 275 |
]
|
|
@@ -298,11 +361,11 @@
|
|
| 298 |
"def create_model():\n",
|
| 299 |
" model = Sequential()\n",
|
| 300 |
" # model.add(LSTM(units=112, return_sequences=True, input_shape=(x_train.shape[1:])))\n",
|
| 301 |
-
" model.add(LSTM(units=
|
| 302 |
" model.add(Dropout(0.2))\n",
|
| 303 |
-
" model.add(LSTM(units=
|
| 304 |
" model.add(Dropout(0.2))\n",
|
| 305 |
-
" model.add(LSTM(units=
|
| 306 |
" model.add(Dropout(0.2))\n",
|
| 307 |
" model.add(Dense(units=4))\n",
|
| 308 |
" return model\n",
|
|
@@ -315,35 +378,35 @@
|
|
| 315 |
"base_uri": "https://localhost:8080/"
|
| 316 |
},
|
| 317 |
"id": "GXhYAKzXVfku",
|
| 318 |
-
"outputId": "
|
| 319 |
},
|
| 320 |
-
"execution_count":
|
| 321 |
"outputs": [
|
| 322 |
{
|
| 323 |
"output_type": "stream",
|
| 324 |
"name": "stdout",
|
| 325 |
"text": [
|
| 326 |
-
"Model: \"
|
| 327 |
"_________________________________________________________________\n",
|
| 328 |
" Layer (type) Output Shape Param # \n",
|
| 329 |
"=================================================================\n",
|
| 330 |
-
"
|
| 331 |
" \n",
|
| 332 |
-
"
|
| 333 |
" \n",
|
| 334 |
-
"
|
| 335 |
" \n",
|
| 336 |
-
"
|
| 337 |
" \n",
|
| 338 |
-
"
|
| 339 |
" \n",
|
| 340 |
-
"
|
| 341 |
" \n",
|
| 342 |
-
"
|
| 343 |
" \n",
|
| 344 |
"=================================================================\n",
|
| 345 |
-
"Total params:
|
| 346 |
-
"Trainable params:
|
| 347 |
"Non-trainable params: 0\n",
|
| 348 |
"_________________________________________________________________\n",
|
| 349 |
"None\n"
|
|
@@ -359,34 +422,7 @@
|
|
| 359 |
"metadata": {
|
| 360 |
"id": "ZhoWj_XeXQws"
|
| 361 |
},
|
| 362 |
-
"execution_count":
|
| 363 |
-
"outputs": []
|
| 364 |
-
},
|
| 365 |
-
{
|
| 366 |
-
"cell_type": "markdown",
|
| 367 |
-
"source": [
|
| 368 |
-
"## Create checkpoint callback"
|
| 369 |
-
],
|
| 370 |
-
"metadata": {
|
| 371 |
-
"id": "XU0vc4n8p92L"
|
| 372 |
-
}
|
| 373 |
-
},
|
| 374 |
-
{
|
| 375 |
-
"cell_type": "code",
|
| 376 |
-
"source": [
|
| 377 |
-
"# Directory where the checkpoints will be saved\n",
|
| 378 |
-
"checkpoint_dir = './training_checkpoints_'+dt.datetime.now().strftime(\"%Y%m%d%H%M%S\")\n",
|
| 379 |
-
"# Name of the checkpoint files\n",
|
| 380 |
-
"checkpoint_prefix = os.path.join(checkpoint_dir, \"ckpt_epoch{epoch}_loss{loss}\")\n",
|
| 381 |
-
"\n",
|
| 382 |
-
"checkpoint_callback=tf.keras.callbacks.ModelCheckpoint(\n",
|
| 383 |
-
" filepath=checkpoint_prefix,\n",
|
| 384 |
-
" save_weights_only=True)"
|
| 385 |
-
],
|
| 386 |
-
"metadata": {
|
| 387 |
-
"id": "M5MBAB1-qCZr"
|
| 388 |
-
},
|
| 389 |
-
"execution_count": 35,
|
| 390 |
"outputs": []
|
| 391 |
},
|
| 392 |
{
|
|
@@ -409,16 +445,16 @@
|
|
| 409 |
"base_uri": "https://localhost:8080/"
|
| 410 |
},
|
| 411 |
"id": "HDT9XPXHvqyN",
|
| 412 |
-
"outputId": "
|
| 413 |
},
|
| 414 |
-
"execution_count":
|
| 415 |
"outputs": [
|
| 416 |
{
|
| 417 |
"output_type": "stream",
|
| 418 |
"name": "stdout",
|
| 419 |
"text": [
|
| 420 |
-
"(
|
| 421 |
-
"(
|
| 422 |
]
|
| 423 |
}
|
| 424 |
]
|
|
@@ -426,108 +462,96 @@
|
|
| 426 |
{
|
| 427 |
"cell_type": "code",
|
| 428 |
"source": [
|
| 429 |
-
"
|
| 430 |
-
|
| 431 |
-
|
| 432 |
-
"
|
| 433 |
-
|
| 434 |
-
|
| 435 |
-
"
|
| 436 |
-
"
|
| 437 |
-
|
| 438 |
-
"
|
| 439 |
-
"
|
| 440 |
-
|
| 441 |
-
|
| 442 |
-
|
| 443 |
-
"text/plain": [
|
| 444 |
-
"array([ 0.02002301, 0.0391905 , -0.09898045, -0.05744885])"
|
| 445 |
-
]
|
| 446 |
-
},
|
| 447 |
-
"metadata": {},
|
| 448 |
-
"execution_count": 40
|
| 449 |
-
}
|
| 450 |
-
]
|
| 451 |
-
},
|
| 452 |
-
{
|
| 453 |
-
"cell_type": "code",
|
| 454 |
-
"source": [
|
| 455 |
-
"model.fit(x_train, y_train, epochs=25, batch_size=32, callbacks=[checkpoint_callback])\n"
|
| 456 |
],
|
| 457 |
"metadata": {
|
| 458 |
"colab": {
|
| 459 |
-
"base_uri": "https://localhost:8080/"
|
|
|
|
| 460 |
},
|
| 461 |
"id": "9Ccc_Ej2TmYO",
|
| 462 |
-
"outputId": "
|
| 463 |
},
|
| 464 |
-
"execution_count":
|
| 465 |
"outputs": [
|
| 466 |
{
|
| 467 |
"output_type": "stream",
|
| 468 |
"name": "stdout",
|
| 469 |
"text": [
|
| 470 |
"Epoch 1/25\n",
|
| 471 |
-
"
|
| 472 |
"Epoch 2/25\n",
|
| 473 |
-
"
|
| 474 |
"Epoch 3/25\n",
|
| 475 |
-
"
|
| 476 |
"Epoch 4/25\n",
|
| 477 |
-
"
|
| 478 |
"Epoch 5/25\n",
|
| 479 |
-
"
|
| 480 |
"Epoch 6/25\n",
|
| 481 |
-
"
|
| 482 |
"Epoch 7/25\n",
|
| 483 |
-
"
|
| 484 |
"Epoch 8/25\n",
|
| 485 |
-
"
|
| 486 |
"Epoch 9/25\n",
|
| 487 |
-
"
|
| 488 |
"Epoch 10/25\n",
|
| 489 |
-
"
|
| 490 |
"Epoch 11/25\n",
|
| 491 |
-
"
|
| 492 |
"Epoch 12/25\n",
|
| 493 |
-
"
|
| 494 |
"Epoch 13/25\n",
|
| 495 |
-
"
|
| 496 |
"Epoch 14/25\n",
|
| 497 |
-
"
|
| 498 |
"Epoch 15/25\n",
|
| 499 |
-
"
|
| 500 |
"Epoch 16/25\n",
|
| 501 |
-
"
|
| 502 |
"Epoch 17/25\n",
|
| 503 |
-
"
|
| 504 |
"Epoch 18/25\n",
|
| 505 |
-
"
|
| 506 |
"Epoch 19/25\n",
|
| 507 |
-
"
|
| 508 |
"Epoch 20/25\n",
|
| 509 |
-
"
|
| 510 |
-
"Epoch 21/25\n",
|
| 511 |
-
"78/78 [==============================] - 30s 384ms/step - loss: 0.0107\n",
|
| 512 |
-
"Epoch 22/25\n",
|
| 513 |
-
"78/78 [==============================] - 30s 381ms/step - loss: 0.0106\n",
|
| 514 |
-
"Epoch 23/25\n",
|
| 515 |
-
"78/78 [==============================] - 30s 380ms/step - loss: 0.0106\n",
|
| 516 |
-
"Epoch 24/25\n",
|
| 517 |
-
"78/78 [==============================] - 30s 385ms/step - loss: 0.0107\n",
|
| 518 |
-
"Epoch 25/25\n",
|
| 519 |
-
"78/78 [==============================] - 30s 383ms/step - loss: 0.0106\n"
|
| 520 |
]
|
| 521 |
},
|
| 522 |
{
|
| 523 |
-
"output_type": "
|
| 524 |
-
"
|
| 525 |
-
|
| 526 |
-
|
| 527 |
-
|
| 528 |
-
|
| 529 |
-
|
| 530 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 531 |
}
|
| 532 |
]
|
| 533 |
},
|
|
@@ -544,25 +568,13 @@
|
|
| 544 |
"cell_type": "code",
|
| 545 |
"source": [
|
| 546 |
"#print trainings directories to pick one\n",
|
| 547 |
-
"!ls -
|
| 548 |
],
|
| 549 |
"metadata": {
|
| 550 |
-
"
|
| 551 |
-
"base_uri": "https://localhost:8080/"
|
| 552 |
-
},
|
| 553 |
-
"id": "59CDDB0i4yTx",
|
| 554 |
-
"outputId": "497ae253-e3ac-47d0-d066-8e508f55782c"
|
| 555 |
},
|
| 556 |
-
"execution_count":
|
| 557 |
-
"outputs": [
|
| 558 |
-
{
|
| 559 |
-
"output_type": "stream",
|
| 560 |
-
"name": "stdout",
|
| 561 |
-
"text": [
|
| 562 |
-
"training_checkpoints_20230406041748/\n"
|
| 563 |
-
]
|
| 564 |
-
}
|
| 565 |
-
]
|
| 566 |
},
|
| 567 |
{
|
| 568 |
"cell_type": "code",
|
|
@@ -572,13 +584,19 @@
|
|
| 572 |
"metadata": {
|
| 573 |
"id": "tpmru7nG9kbW"
|
| 574 |
},
|
| 575 |
-
"execution_count":
|
| 576 |
"outputs": []
|
| 577 |
},
|
| 578 |
{
|
| 579 |
"cell_type": "code",
|
| 580 |
"source": [
|
| 581 |
-
"checkpoint_dir
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 582 |
"\n",
|
| 583 |
"def load_weights(epoch=None):\n",
|
| 584 |
" if epoch is None:\n",
|
|
@@ -600,15 +618,16 @@
|
|
| 600 |
"base_uri": "https://localhost:8080/"
|
| 601 |
},
|
| 602 |
"id": "wQ0JTXsp4VKF",
|
| 603 |
-
"outputId": "
|
| 604 |
},
|
| 605 |
-
"execution_count":
|
| 606 |
"outputs": [
|
| 607 |
{
|
| 608 |
"output_type": "stream",
|
| 609 |
"name": "stdout",
|
| 610 |
"text": [
|
| 611 |
-
"
|
|
|
|
| 612 |
]
|
| 613 |
}
|
| 614 |
]
|
|
@@ -616,8 +635,8 @@
|
|
| 616 |
{
|
| 617 |
"cell_type": "code",
|
| 618 |
"source": [
|
| 619 |
-
"test_start = dt.
|
| 620 |
-
"
|
| 621 |
"\n",
|
| 622 |
"yfin.pdr_override()\n",
|
| 623 |
"test_data = web.data.get_data_yahoo(ticker, test_start, test_end)"
|
|
@@ -627,9 +646,9 @@
|
|
| 627 |
"base_uri": "https://localhost:8080/"
|
| 628 |
},
|
| 629 |
"id": "Mf4q97pfaSCA",
|
| 630 |
-
"outputId": "
|
| 631 |
},
|
| 632 |
-
"execution_count":
|
| 633 |
"outputs": [
|
| 634 |
{
|
| 635 |
"output_type": "stream",
|
|
@@ -643,57 +662,35 @@
|
|
| 643 |
{
|
| 644 |
"cell_type": "code",
|
| 645 |
"source": [
|
| 646 |
-
"
|
| 647 |
-
|
| 648 |
-
|
| 649 |
-
"
|
| 650 |
-
|
| 651 |
-
|
| 652 |
-
|
| 653 |
-
|
| 654 |
-
|
| 655 |
-
|
| 656 |
-
"source": [
|
| 657 |
-
"print(type(test_data_norm))"
|
| 658 |
-
],
|
| 659 |
-
"metadata": {
|
| 660 |
-
"colab": {
|
| 661 |
-
"base_uri": "https://localhost:8080/"
|
| 662 |
-
},
|
| 663 |
-
"id": "mhbqRZ6cDhd6",
|
| 664 |
-
"outputId": "8b40a738-e143-4920-de03-8e8572f4389a"
|
| 665 |
-
},
|
| 666 |
-
"execution_count": 102,
|
| 667 |
-
"outputs": [
|
| 668 |
-
{
|
| 669 |
-
"output_type": "stream",
|
| 670 |
-
"name": "stdout",
|
| 671 |
-
"text": [
|
| 672 |
-
"<class 'pandas.core.frame.DataFrame'>\n"
|
| 673 |
-
]
|
| 674 |
-
}
|
| 675 |
-
]
|
| 676 |
-
},
|
| 677 |
-
{
|
| 678 |
-
"cell_type": "code",
|
| 679 |
-
"source": [
|
| 680 |
-
"input_data = np.expand_dims(test_data_norm.values, axis=0)\n",
|
| 681 |
-
"print(input_data.shape)"
|
| 682 |
],
|
| 683 |
"metadata": {
|
| 684 |
"colab": {
|
| 685 |
"base_uri": "https://localhost:8080/"
|
| 686 |
},
|
| 687 |
-
"id": "
|
| 688 |
-
"outputId": "
|
| 689 |
},
|
| 690 |
-
"execution_count":
|
| 691 |
"outputs": [
|
| 692 |
{
|
| 693 |
"output_type": "stream",
|
| 694 |
"name": "stdout",
|
| 695 |
"text": [
|
| 696 |
-
"
|
|
|
|
|
|
|
|
|
|
|
|
|
| 697 |
]
|
| 698 |
}
|
| 699 |
]
|
|
@@ -701,22 +698,46 @@
|
|
| 701 |
{
|
| 702 |
"cell_type": "code",
|
| 703 |
"source": [
|
| 704 |
-
"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 705 |
],
|
| 706 |
"metadata": {
|
| 707 |
"colab": {
|
| 708 |
"base_uri": "https://localhost:8080/"
|
| 709 |
},
|
| 710 |
"id": "AVYFQZnqEqhx",
|
| 711 |
-
"outputId": "
|
| 712 |
},
|
| 713 |
-
"execution_count":
|
| 714 |
"outputs": [
|
| 715 |
{
|
| 716 |
"output_type": "stream",
|
| 717 |
"name": "stdout",
|
| 718 |
"text": [
|
| 719 |
-
"
|
|
|
|
|
|
|
|
|
|
| 720 |
]
|
| 721 |
}
|
| 722 |
]
|
|
@@ -724,231 +745,32 @@
|
|
| 724 |
{
|
| 725 |
"cell_type": "code",
|
| 726 |
"source": [
|
| 727 |
-
"
|
| 728 |
-
"print(the_decoder(results))"
|
| 729 |
],
|
| 730 |
"metadata": {
|
| 731 |
"colab": {
|
| 732 |
"base_uri": "https://localhost:8080/"
|
| 733 |
},
|
| 734 |
-
"id": "
|
| 735 |
-
"outputId": "
|
| 736 |
-
},
|
| 737 |
-
"execution_count": 113,
|
| 738 |
-
"outputs": [
|
| 739 |
-
{
|
| 740 |
-
"output_type": "stream",
|
| 741 |
-
"name": "stdout",
|
| 742 |
-
"text": [
|
| 743 |
-
"[[-0.01962117 0.09634934 -0.10176479 -0.00849891]]\n",
|
| 744 |
-
"[[-0.06636524 1.3856668 -1.0948591 0.0728941 ]]\n"
|
| 745 |
-
]
|
| 746 |
-
}
|
| 747 |
-
]
|
| 748 |
-
},
|
| 749 |
-
{
|
| 750 |
-
"cell_type": "code",
|
| 751 |
-
"source": [
|
| 752 |
-
"test_data.head()"
|
| 753 |
-
],
|
| 754 |
-
"metadata": {
|
| 755 |
-
"colab": {
|
| 756 |
-
"base_uri": "https://localhost:8080/",
|
| 757 |
-
"height": 237
|
| 758 |
-
},
|
| 759 |
-
"id": "m0k7toG3E2_9",
|
| 760 |
-
"outputId": "38ab6e43-1321-4028-9482-8e6687802a7d"
|
| 761 |
},
|
| 762 |
-
"execution_count":
|
| 763 |
"outputs": [
|
| 764 |
{
|
| 765 |
"output_type": "execute_result",
|
| 766 |
"data": {
|
| 767 |
"text/plain": [
|
| 768 |
-
"
|
| 769 |
-
"
|
| 770 |
-
"
|
| 771 |
-
"
|
| 772 |
-
"
|
| 773 |
-
"
|
| 774 |
-
"
|
| 775 |
-
"\n",
|
| 776 |
-
" Volume \n",
|
| 777 |
-
"Date \n",
|
| 778 |
-
"2022-09-19 81474200 \n",
|
| 779 |
-
"2022-09-20 107689800 \n",
|
| 780 |
-
"2022-09-21 101696800 \n",
|
| 781 |
-
"2022-09-22 86652500 \n",
|
| 782 |
-
"2022-09-23 96029900 "
|
| 783 |
-
],
|
| 784 |
-
"text/html": [
|
| 785 |
-
"\n",
|
| 786 |
-
" <div id=\"df-51b6b5ba-2841-4317-9ce2-b32b40e2e9fc\">\n",
|
| 787 |
-
" <div class=\"colab-df-container\">\n",
|
| 788 |
-
" <div>\n",
|
| 789 |
-
"<style scoped>\n",
|
| 790 |
-
" .dataframe tbody tr th:only-of-type {\n",
|
| 791 |
-
" vertical-align: middle;\n",
|
| 792 |
-
" }\n",
|
| 793 |
-
"\n",
|
| 794 |
-
" .dataframe tbody tr th {\n",
|
| 795 |
-
" vertical-align: top;\n",
|
| 796 |
-
" }\n",
|
| 797 |
-
"\n",
|
| 798 |
-
" .dataframe thead th {\n",
|
| 799 |
-
" text-align: right;\n",
|
| 800 |
-
" }\n",
|
| 801 |
-
"</style>\n",
|
| 802 |
-
"<table border=\"1\" class=\"dataframe\">\n",
|
| 803 |
-
" <thead>\n",
|
| 804 |
-
" <tr style=\"text-align: right;\">\n",
|
| 805 |
-
" <th></th>\n",
|
| 806 |
-
" <th>Open</th>\n",
|
| 807 |
-
" <th>High</th>\n",
|
| 808 |
-
" <th>Low</th>\n",
|
| 809 |
-
" <th>Close</th>\n",
|
| 810 |
-
" <th>Adj Close</th>\n",
|
| 811 |
-
" <th>Volume</th>\n",
|
| 812 |
-
" </tr>\n",
|
| 813 |
-
" <tr>\n",
|
| 814 |
-
" <th>Date</th>\n",
|
| 815 |
-
" <th></th>\n",
|
| 816 |
-
" <th></th>\n",
|
| 817 |
-
" <th></th>\n",
|
| 818 |
-
" <th></th>\n",
|
| 819 |
-
" <th></th>\n",
|
| 820 |
-
" <th></th>\n",
|
| 821 |
-
" </tr>\n",
|
| 822 |
-
" </thead>\n",
|
| 823 |
-
" <tbody>\n",
|
| 824 |
-
" <tr>\n",
|
| 825 |
-
" <th>2022-09-19</th>\n",
|
| 826 |
-
" <td>149.309998</td>\n",
|
| 827 |
-
" <td>154.559998</td>\n",
|
| 828 |
-
" <td>149.100006</td>\n",
|
| 829 |
-
" <td>154.479996</td>\n",
|
| 830 |
-
" <td>153.989029</td>\n",
|
| 831 |
-
" <td>81474200</td>\n",
|
| 832 |
-
" </tr>\n",
|
| 833 |
-
" <tr>\n",
|
| 834 |
-
" <th>2022-09-20</th>\n",
|
| 835 |
-
" <td>153.399994</td>\n",
|
| 836 |
-
" <td>158.080002</td>\n",
|
| 837 |
-
" <td>153.080002</td>\n",
|
| 838 |
-
" <td>156.899994</td>\n",
|
| 839 |
-
" <td>156.401352</td>\n",
|
| 840 |
-
" <td>107689800</td>\n",
|
| 841 |
-
" </tr>\n",
|
| 842 |
-
" <tr>\n",
|
| 843 |
-
" <th>2022-09-21</th>\n",
|
| 844 |
-
" <td>157.339996</td>\n",
|
| 845 |
-
" <td>158.740005</td>\n",
|
| 846 |
-
" <td>153.600006</td>\n",
|
| 847 |
-
" <td>153.720001</td>\n",
|
| 848 |
-
" <td>153.231461</td>\n",
|
| 849 |
-
" <td>101696800</td>\n",
|
| 850 |
-
" </tr>\n",
|
| 851 |
-
" <tr>\n",
|
| 852 |
-
" <th>2022-09-22</th>\n",
|
| 853 |
-
" <td>152.380005</td>\n",
|
| 854 |
-
" <td>154.470001</td>\n",
|
| 855 |
-
" <td>150.910004</td>\n",
|
| 856 |
-
" <td>152.740005</td>\n",
|
| 857 |
-
" <td>152.254578</td>\n",
|
| 858 |
-
" <td>86652500</td>\n",
|
| 859 |
-
" </tr>\n",
|
| 860 |
-
" <tr>\n",
|
| 861 |
-
" <th>2022-09-23</th>\n",
|
| 862 |
-
" <td>151.190002</td>\n",
|
| 863 |
-
" <td>151.470001</td>\n",
|
| 864 |
-
" <td>148.559998</td>\n",
|
| 865 |
-
" <td>150.429993</td>\n",
|
| 866 |
-
" <td>149.951904</td>\n",
|
| 867 |
-
" <td>96029900</td>\n",
|
| 868 |
-
" </tr>\n",
|
| 869 |
-
" </tbody>\n",
|
| 870 |
-
"</table>\n",
|
| 871 |
-
"</div>\n",
|
| 872 |
-
" <button class=\"colab-df-convert\" onclick=\"convertToInteractive('df-51b6b5ba-2841-4317-9ce2-b32b40e2e9fc')\"\n",
|
| 873 |
-
" title=\"Convert this dataframe to an interactive table.\"\n",
|
| 874 |
-
" style=\"display:none;\">\n",
|
| 875 |
-
" \n",
|
| 876 |
-
" <svg xmlns=\"http://www.w3.org/2000/svg\" height=\"24px\"viewBox=\"0 0 24 24\"\n",
|
| 877 |
-
" width=\"24px\">\n",
|
| 878 |
-
" <path d=\"M0 0h24v24H0V0z\" fill=\"none\"/>\n",
|
| 879 |
-
" <path d=\"M18.56 5.44l.94 2.06.94-2.06 2.06-.94-2.06-.94-.94-2.06-.94 2.06-2.06.94zm-11 1L8.5 8.5l.94-2.06 2.06-.94-2.06-.94L8.5 2.5l-.94 2.06-2.06.94zm10 10l.94 2.06.94-2.06 2.06-.94-2.06-.94-.94-2.06-.94 2.06-2.06.94z\"/><path d=\"M17.41 7.96l-1.37-1.37c-.4-.4-.92-.59-1.43-.59-.52 0-1.04.2-1.43.59L10.3 9.45l-7.72 7.72c-.78.78-.78 2.05 0 2.83L4 21.41c.39.39.9.59 1.41.59.51 0 1.02-.2 1.41-.59l7.78-7.78 2.81-2.81c.8-.78.8-2.07 0-2.86zM5.41 20L4 18.59l7.72-7.72 1.47 1.35L5.41 20z\"/>\n",
|
| 880 |
-
" </svg>\n",
|
| 881 |
-
" </button>\n",
|
| 882 |
-
" \n",
|
| 883 |
-
" <style>\n",
|
| 884 |
-
" .colab-df-container {\n",
|
| 885 |
-
" display:flex;\n",
|
| 886 |
-
" flex-wrap:wrap;\n",
|
| 887 |
-
" gap: 12px;\n",
|
| 888 |
-
" }\n",
|
| 889 |
-
"\n",
|
| 890 |
-
" .colab-df-convert {\n",
|
| 891 |
-
" background-color: #E8F0FE;\n",
|
| 892 |
-
" border: none;\n",
|
| 893 |
-
" border-radius: 50%;\n",
|
| 894 |
-
" cursor: pointer;\n",
|
| 895 |
-
" display: none;\n",
|
| 896 |
-
" fill: #1967D2;\n",
|
| 897 |
-
" height: 32px;\n",
|
| 898 |
-
" padding: 0 0 0 0;\n",
|
| 899 |
-
" width: 32px;\n",
|
| 900 |
-
" }\n",
|
| 901 |
-
"\n",
|
| 902 |
-
" .colab-df-convert:hover {\n",
|
| 903 |
-
" background-color: #E2EBFA;\n",
|
| 904 |
-
" box-shadow: 0px 1px 2px rgba(60, 64, 67, 0.3), 0px 1px 3px 1px rgba(60, 64, 67, 0.15);\n",
|
| 905 |
-
" fill: #174EA6;\n",
|
| 906 |
-
" }\n",
|
| 907 |
-
"\n",
|
| 908 |
-
" [theme=dark] .colab-df-convert {\n",
|
| 909 |
-
" background-color: #3B4455;\n",
|
| 910 |
-
" fill: #D2E3FC;\n",
|
| 911 |
-
" }\n",
|
| 912 |
-
"\n",
|
| 913 |
-
" [theme=dark] .colab-df-convert:hover {\n",
|
| 914 |
-
" background-color: #434B5C;\n",
|
| 915 |
-
" box-shadow: 0px 1px 3px 1px rgba(0, 0, 0, 0.15);\n",
|
| 916 |
-
" filter: drop-shadow(0px 1px 2px rgba(0, 0, 0, 0.3));\n",
|
| 917 |
-
" fill: #FFFFFF;\n",
|
| 918 |
-
" }\n",
|
| 919 |
-
" </style>\n",
|
| 920 |
-
"\n",
|
| 921 |
-
" <script>\n",
|
| 922 |
-
" const buttonEl =\n",
|
| 923 |
-
" document.querySelector('#df-51b6b5ba-2841-4317-9ce2-b32b40e2e9fc button.colab-df-convert');\n",
|
| 924 |
-
" buttonEl.style.display =\n",
|
| 925 |
-
" google.colab.kernel.accessAllowed ? 'block' : 'none';\n",
|
| 926 |
-
"\n",
|
| 927 |
-
" async function convertToInteractive(key) {\n",
|
| 928 |
-
" const element = document.querySelector('#df-51b6b5ba-2841-4317-9ce2-b32b40e2e9fc');\n",
|
| 929 |
-
" const dataTable =\n",
|
| 930 |
-
" await google.colab.kernel.invokeFunction('convertToInteractive',\n",
|
| 931 |
-
" [key], {});\n",
|
| 932 |
-
" if (!dataTable) return;\n",
|
| 933 |
-
"\n",
|
| 934 |
-
" const docLinkHtml = 'Like what you see? Visit the ' +\n",
|
| 935 |
-
" '<a target=\"_blank\" href=https://colab.research.google.com/notebooks/data_table.ipynb>data table notebook</a>'\n",
|
| 936 |
-
" + ' to learn more about interactive tables.';\n",
|
| 937 |
-
" element.innerHTML = '';\n",
|
| 938 |
-
" dataTable['output_type'] = 'display_data';\n",
|
| 939 |
-
" await google.colab.output.renderOutput(dataTable, element);\n",
|
| 940 |
-
" const docLink = document.createElement('div');\n",
|
| 941 |
-
" docLink.innerHTML = docLinkHtml;\n",
|
| 942 |
-
" element.appendChild(docLink);\n",
|
| 943 |
-
" }\n",
|
| 944 |
-
" </script>\n",
|
| 945 |
-
" </div>\n",
|
| 946 |
-
" </div>\n",
|
| 947 |
-
" "
|
| 948 |
]
|
| 949 |
},
|
| 950 |
"metadata": {},
|
| 951 |
-
"execution_count":
|
| 952 |
}
|
| 953 |
]
|
| 954 |
}
|
|
|
|
| 4 |
"metadata": {
|
| 5 |
"colab": {
|
| 6 |
"provenance": [],
|
| 7 |
+
"authorship_tag": "ABX9TyOc5/oQ0Z2ie5dOI46PpyV0",
|
|
|
|
|
|
|
|
|
|
| 8 |
"include_colab_link": true
|
| 9 |
},
|
| 10 |
"kernelspec": {
|
|
|
|
| 14 |
"language_info": {
|
| 15 |
"name": "python"
|
| 16 |
},
|
| 17 |
+
"gpuClass": "standard",
|
| 18 |
+
"accelerator": "GPU"
|
| 19 |
},
|
| 20 |
"cells": [
|
| 21 |
{
|
|
|
|
| 41 |
"base_uri": "https://localhost:8080/"
|
| 42 |
},
|
| 43 |
"id": "Xr3Qozgfktoc",
|
| 44 |
+
"outputId": "b4ce9a19-4dc1-43e9-b09e-af91a2b07343"
|
| 45 |
},
|
| 46 |
+
"execution_count": 90,
|
| 47 |
"outputs": [
|
| 48 |
{
|
| 49 |
"output_type": "stream",
|
| 50 |
"name": "stdout",
|
| 51 |
"text": [
|
| 52 |
+
"Drive already mounted at /content/drive; to attempt to forcibly remount, call drive.mount(\"/content/drive\", force_remount=True).\n",
|
| 53 |
"/content/drive/MyDrive/projects/Stock_Predicter\n"
|
| 54 |
]
|
| 55 |
}
|
|
|
|
| 57 |
},
|
| 58 |
{
|
| 59 |
"cell_type": "code",
|
| 60 |
+
"execution_count": 91,
|
| 61 |
"metadata": {
|
| 62 |
"id": "e8SQqogMQYLh"
|
| 63 |
},
|
|
|
|
| 99 |
"metadata": {
|
| 100 |
"id": "O6dtJpJwS5Eg"
|
| 101 |
},
|
| 102 |
+
"execution_count": 92,
|
| 103 |
"outputs": []
|
| 104 |
},
|
| 105 |
{
|
|
|
|
| 113 |
"base_uri": "https://localhost:8080/"
|
| 114 |
},
|
| 115 |
"id": "LwPyk8Uh-Zz_",
|
| 116 |
+
"outputId": "2217df50-87e9-48e3-e096-71163331f570"
|
| 117 |
},
|
| 118 |
+
"execution_count": 93,
|
| 119 |
"outputs": [
|
| 120 |
{
|
| 121 |
"output_type": "stream",
|
|
|
|
| 138 |
{
|
| 139 |
"cell_type": "code",
|
| 140 |
"source": [
|
| 141 |
+
"def create_remove_columns(data):\n",
|
| 142 |
+
" # create jump column\n",
|
| 143 |
+
" data = pd.DataFrame.copy(data)\n",
|
| 144 |
+
" data['Jump'] = data['Open'] - data['Close'].shift(1)\n",
|
| 145 |
+
" data['Jump'].fillna(0, inplace=True)\n",
|
| 146 |
+
" # data = data.reindex(columns=['Open', 'High', 'Low', 'Close', 'Adj Close', 'Jump'])\n",
|
| 147 |
+
" data.insert(0,'Jump', data.pop('Jump'))\n",
|
| 148 |
+
" return data"
|
| 149 |
+
],
|
| 150 |
+
"metadata": {
|
| 151 |
+
"id": "Bpym8x-Kxf0p"
|
| 152 |
+
},
|
| 153 |
+
"execution_count": 94,
|
| 154 |
+
"outputs": []
|
| 155 |
+
},
|
| 156 |
+
{
|
| 157 |
+
"cell_type": "code",
|
| 158 |
+
"source": [
|
| 159 |
+
"def normalize_data(data, scaler=None):\n",
|
| 160 |
+
" the_data = pd.DataFrame.copy(data)\n",
|
| 161 |
+
" # substract the open value to all columns but the first one and the last one which are \"Jump\" and \"Volume\"\n",
|
| 162 |
+
" the_data.iloc[:, 1:-1] = the_data.iloc[:,1:-1] - the_data['Open'].values[:, np.newaxis]\n",
|
| 163 |
+
" # print('the_data')\n",
|
| 164 |
+
" # print(the_data)\n",
|
| 165 |
+
"\n",
|
| 166 |
+
" the_data.pop('Open')\n",
|
| 167 |
+
" # todo save an csv with the values for the scaler\n",
|
| 168 |
" if scaler is None:\n",
|
| 169 |
" # Create the scaler\n",
|
| 170 |
+
" values = np.abs(the_data.values)\n",
|
|
|
|
|
|
|
| 171 |
" max_value = np.max(values[:,:-1])\n",
|
|
|
|
|
|
|
|
|
|
| 172 |
" max_volume = np.max(values[:,-1])\n",
|
| 173 |
+
" def scaler(d):\n",
|
| 174 |
+
" data = pd.DataFrame.copy(d)\n",
|
| 175 |
+
" print('max_value: ', max_value)\n",
|
| 176 |
+
" print('max_volume: ', max_volume)\n",
|
| 177 |
+
" data.iloc[:, :-1] = data.iloc[:,:-1].apply(lambda x: x/max_value)\n",
|
| 178 |
+
" data.iloc[:, -1] = data.iloc[:,-1].apply(lambda x: x/max_volume)\n",
|
|
|
|
|
|
|
| 179 |
" return data\n",
|
| 180 |
+
" def decoder(values):\n",
|
| 181 |
+
" decoded_values = values * max_value\n",
|
| 182 |
" return decoded_values\n",
|
| 183 |
+
" else:\n",
|
| 184 |
+
" decoder = None\n",
|
| 185 |
" \n",
|
| 186 |
" normalized_data = scaler(the_data)\n",
|
| 187 |
"\n",
|
| 188 |
+
" return normalized_data, scaler, decoder\n",
|
| 189 |
"\n",
|
| 190 |
"\n"
|
| 191 |
],
|
| 192 |
"metadata": {
|
| 193 |
"id": "v9RoqzBvtrOb"
|
| 194 |
},
|
| 195 |
+
"execution_count": 95,
|
| 196 |
+
"outputs": []
|
| 197 |
+
},
|
| 198 |
+
{
|
| 199 |
+
"cell_type": "code",
|
| 200 |
+
"source": [
|
| 201 |
+
"def create_training_data(norm_data):\n",
|
| 202 |
+
" prediction_days = 500\n",
|
| 203 |
+
" \n",
|
| 204 |
+
" x_train_list = []\n",
|
| 205 |
+
" y_train_list = []\n",
|
| 206 |
+
" \n",
|
| 207 |
+
" for i in range(prediction_days, len(norm_data)):\n",
|
| 208 |
+
" x_train_list.append(norm_data[i-prediction_days:i])\n",
|
| 209 |
+
" y_train_list.append(norm_data.iloc[i].values[0:4])\n",
|
| 210 |
+
" \n",
|
| 211 |
+
" x_train = np.array(x_train_list)\n",
|
| 212 |
+
" y_train = np.array(y_train_list)\n",
|
| 213 |
+
" return x_train, y_train"
|
| 214 |
+
],
|
| 215 |
+
"metadata": {
|
| 216 |
+
"id": "jMXkRAYFomHM"
|
| 217 |
+
},
|
| 218 |
+
"execution_count": 96,
|
| 219 |
"outputs": []
|
| 220 |
},
|
| 221 |
{
|
| 222 |
"cell_type": "code",
|
| 223 |
"source": [
|
| 224 |
+
"#Make all the preprocesing\n",
|
| 225 |
+
"def preprocessing(data, scaler=None):\n",
|
| 226 |
+
" # print(data.head(3))\n",
|
| 227 |
+
" data_0 = create_remove_columns(data)\n",
|
| 228 |
+
" # print(data_0.head(3))\n",
|
| 229 |
+
" #todo: save the_scaler somehow to use in new runtimes\n",
|
| 230 |
+
" norm_data, scaler, decoder = normalize_data(data_0, scaler=scaler)\n",
|
| 231 |
+
" # print(norm_data.head(3))\n",
|
| 232 |
+
" x_train, y_train = create_training_data(norm_data)\n",
|
| 233 |
+
" # print(x_train.shape, y_train.shape)\n",
|
| 234 |
+
" return x_train, y_train, scaler, decoder"
|
| 235 |
],
|
| 236 |
"metadata": {
|
| 237 |
+
"id": "YZWMfusT-I7Z"
|
| 238 |
},
|
| 239 |
+
"execution_count": 97,
|
| 240 |
"outputs": []
|
| 241 |
},
|
| 242 |
{
|
| 243 |
"cell_type": "code",
|
| 244 |
"source": [
|
| 245 |
+
"x_train, y_train, scaler, decoder = preprocessing(data)"
|
| 246 |
],
|
| 247 |
"metadata": {
|
| 248 |
"colab": {
|
| 249 |
"base_uri": "https://localhost:8080/"
|
| 250 |
},
|
| 251 |
+
"id": "PeJjDC0VBG_6",
|
| 252 |
+
"outputId": "aff2cf0b-e630-4727-bbec-3cf5be4e53a0"
|
| 253 |
},
|
| 254 |
+
"execution_count": 98,
|
| 255 |
"outputs": [
|
| 256 |
{
|
| 257 |
+
"output_type": "stream",
|
| 258 |
+
"name": "stdout",
|
| 259 |
+
"text": [
|
| 260 |
+
"max_value: 10.589996337890625\n",
|
| 261 |
+
"max_volume: 1460852400.0\n"
|
| 262 |
+
]
|
|
|
|
|
|
|
| 263 |
}
|
| 264 |
]
|
| 265 |
},
|
| 266 |
{
|
| 267 |
"cell_type": "code",
|
| 268 |
"source": [
|
| 269 |
+
"print(x_train.shape)\n",
|
| 270 |
+
"x_train[1,499,:]"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 271 |
],
|
| 272 |
"metadata": {
|
| 273 |
+
"colab": {
|
| 274 |
+
"base_uri": "https://localhost:8080/"
|
| 275 |
+
},
|
| 276 |
+
"id": "YkI8vSguuS8A",
|
| 277 |
+
"outputId": "6e5eeaa2-12de-4dd2-b17c-1d61e415fbd8"
|
| 278 |
},
|
| 279 |
+
"execution_count": 99,
|
| 280 |
+
"outputs": [
|
| 281 |
+
{
|
| 282 |
+
"output_type": "stream",
|
| 283 |
+
"name": "stdout",
|
| 284 |
+
"text": [
|
| 285 |
+
"(2082, 500, 6)\n"
|
| 286 |
+
]
|
| 287 |
+
},
|
| 288 |
+
{
|
| 289 |
+
"output_type": "execute_result",
|
| 290 |
+
"data": {
|
| 291 |
+
"text/plain": [
|
| 292 |
+
"array([ 0.00212456, 0.05712934, -0.00212456, 0.04461756, -0.22778379,\n",
|
| 293 |
+
" 0.09233239])"
|
| 294 |
+
]
|
| 295 |
+
},
|
| 296 |
+
"metadata": {},
|
| 297 |
+
"execution_count": 99
|
| 298 |
+
}
|
| 299 |
+
]
|
| 300 |
},
|
| 301 |
{
|
| 302 |
"cell_type": "code",
|
| 303 |
"source": [
|
| 304 |
+
"td = data.iloc[498:501]\n",
|
| 305 |
+
"# print('td:\\n',td)\n",
|
| 306 |
+
"td0 = create_remove_columns(td)\n",
|
| 307 |
+
"print('td0:\\n',td0)\n",
|
| 308 |
+
"print(decoder(y_train[0]))"
|
| 309 |
],
|
| 310 |
"metadata": {
|
| 311 |
"colab": {
|
| 312 |
"base_uri": "https://localhost:8080/"
|
| 313 |
},
|
| 314 |
+
"id": "QaO34uSds2wJ",
|
| 315 |
+
"outputId": "af5d9a04-214c-4a2d-c706-5af3a2a1ea5a"
|
| 316 |
},
|
| 317 |
+
"execution_count": 100,
|
| 318 |
"outputs": [
|
| 319 |
{
|
| 320 |
"output_type": "stream",
|
| 321 |
"name": "stdout",
|
| 322 |
"text": [
|
| 323 |
+
"td0:\n",
|
| 324 |
+
" Jump Open High Low Close Adj Close \\\n",
|
| 325 |
+
"Date \n",
|
| 326 |
+
"2014-12-23 0.000000 28.307501 28.332500 28.115000 28.135000 25.286961 \n",
|
| 327 |
+
"2014-12-24 0.010000 28.145000 28.177500 28.002501 28.002501 25.167873 \n",
|
| 328 |
+
"2014-12-26 0.022499 28.025000 28.629999 28.002501 28.497499 25.612770 \n",
|
| 329 |
+
"\n",
|
| 330 |
+
" Volume \n",
|
| 331 |
+
"Date \n",
|
| 332 |
+
"2014-12-23 104113600 \n",
|
| 333 |
+
"2014-12-24 57918400 \n",
|
| 334 |
+
"2014-12-26 134884000 \n",
|
| 335 |
+
"[ 0.02249908 0.60499954 -0.02249908 0.47249985]\n"
|
| 336 |
]
|
| 337 |
}
|
| 338 |
]
|
|
|
|
| 361 |
"def create_model():\n",
|
| 362 |
" model = Sequential()\n",
|
| 363 |
" # model.add(LSTM(units=112, return_sequences=True, input_shape=(x_train.shape[1:])))\n",
|
| 364 |
+
" model.add(LSTM(units=1000, return_sequences=True, input_shape=(None,x_train.shape[-1],)))\n",
|
| 365 |
" model.add(Dropout(0.2))\n",
|
| 366 |
+
" model.add(LSTM(units=1000, return_sequences=True))\n",
|
| 367 |
" model.add(Dropout(0.2))\n",
|
| 368 |
+
" model.add(LSTM(units=1000))\n",
|
| 369 |
" model.add(Dropout(0.2))\n",
|
| 370 |
" model.add(Dense(units=4))\n",
|
| 371 |
" return model\n",
|
|
|
|
| 378 |
"base_uri": "https://localhost:8080/"
|
| 379 |
},
|
| 380 |
"id": "GXhYAKzXVfku",
|
| 381 |
+
"outputId": "bbf96ec2-84fc-4246-9003-32c2f8083bb6"
|
| 382 |
},
|
| 383 |
+
"execution_count": 101,
|
| 384 |
"outputs": [
|
| 385 |
{
|
| 386 |
"output_type": "stream",
|
| 387 |
"name": "stdout",
|
| 388 |
"text": [
|
| 389 |
+
"Model: \"sequential_2\"\n",
|
| 390 |
"_________________________________________________________________\n",
|
| 391 |
" Layer (type) Output Shape Param # \n",
|
| 392 |
"=================================================================\n",
|
| 393 |
+
" lstm_6 (LSTM) (None, None, 1000) 4028000 \n",
|
| 394 |
" \n",
|
| 395 |
+
" dropout_6 (Dropout) (None, None, 1000) 0 \n",
|
| 396 |
" \n",
|
| 397 |
+
" lstm_7 (LSTM) (None, None, 1000) 8004000 \n",
|
| 398 |
" \n",
|
| 399 |
+
" dropout_7 (Dropout) (None, None, 1000) 0 \n",
|
| 400 |
" \n",
|
| 401 |
+
" lstm_8 (LSTM) (None, 1000) 8004000 \n",
|
| 402 |
" \n",
|
| 403 |
+
" dropout_8 (Dropout) (None, 1000) 0 \n",
|
| 404 |
" \n",
|
| 405 |
+
" dense_2 (Dense) (None, 4) 4004 \n",
|
| 406 |
" \n",
|
| 407 |
"=================================================================\n",
|
| 408 |
+
"Total params: 20,040,004\n",
|
| 409 |
+
"Trainable params: 20,040,004\n",
|
| 410 |
"Non-trainable params: 0\n",
|
| 411 |
"_________________________________________________________________\n",
|
| 412 |
"None\n"
|
|
|
|
| 422 |
"metadata": {
|
| 423 |
"id": "ZhoWj_XeXQws"
|
| 424 |
},
|
| 425 |
+
"execution_count": 102,
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 426 |
"outputs": []
|
| 427 |
},
|
| 428 |
{
|
|
|
|
| 445 |
"base_uri": "https://localhost:8080/"
|
| 446 |
},
|
| 447 |
"id": "HDT9XPXHvqyN",
|
| 448 |
+
"outputId": "6bc3a3e9-a7ae-48ff-e64a-fb529c5e1f75"
|
| 449 |
},
|
| 450 |
+
"execution_count": 103,
|
| 451 |
"outputs": [
|
| 452 |
{
|
| 453 |
"output_type": "stream",
|
| 454 |
"name": "stdout",
|
| 455 |
"text": [
|
| 456 |
+
"(2082, 500, 6)\n",
|
| 457 |
+
"(2082, 4)\n"
|
| 458 |
]
|
| 459 |
}
|
| 460 |
]
|
|
|
|
| 462 |
{
|
| 463 |
"cell_type": "code",
|
| 464 |
"source": [
|
| 465 |
+
"# Change to False to avoid trainging the model\n",
|
| 466 |
+
"# if False:\n",
|
| 467 |
+
"if True:\n",
|
| 468 |
+
" # Directory where the checkpoints will be saved\n",
|
| 469 |
+
" checkpoint_dir = './training_checkpoints_'+dt.datetime.now().strftime(\"%Y%m%d%H%M%S\")\n",
|
| 470 |
+
" # Name of the checkpoint files\n",
|
| 471 |
+
" checkpoint_prefix = os.path.join(checkpoint_dir, \"ckpt_epoch{epoch}_loss{loss}\")\n",
|
| 472 |
+
" \n",
|
| 473 |
+
" checkpoint_callback=tf.keras.callbacks.ModelCheckpoint(\n",
|
| 474 |
+
" filepath=checkpoint_prefix,\n",
|
| 475 |
+
" save_weights_only=True,\n",
|
| 476 |
+
" monitor=\"loss\", mode=\"min\",\n",
|
| 477 |
+
" save_best_only=True)\n",
|
| 478 |
+
" model.fit(x_train, y_train, epochs=25, batch_size=32, callbacks=[checkpoint_callback])\n"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 479 |
],
|
| 480 |
"metadata": {
|
| 481 |
"colab": {
|
| 482 |
+
"base_uri": "https://localhost:8080/",
|
| 483 |
+
"height": 1000
|
| 484 |
},
|
| 485 |
"id": "9Ccc_Ej2TmYO",
|
| 486 |
+
"outputId": "4e7fe210-6cbb-4a9d-f856-829cfa6bced5"
|
| 487 |
},
|
| 488 |
+
"execution_count": 104,
|
| 489 |
"outputs": [
|
| 490 |
{
|
| 491 |
"output_type": "stream",
|
| 492 |
"name": "stdout",
|
| 493 |
"text": [
|
| 494 |
"Epoch 1/25\n",
|
| 495 |
+
"66/66 [==============================] - 58s 773ms/step - loss: 0.0125\n",
|
| 496 |
"Epoch 2/25\n",
|
| 497 |
+
"66/66 [==============================] - 54s 816ms/step - loss: 0.0115\n",
|
| 498 |
"Epoch 3/25\n",
|
| 499 |
+
"66/66 [==============================] - 55s 841ms/step - loss: 0.0113\n",
|
| 500 |
"Epoch 4/25\n",
|
| 501 |
+
"66/66 [==============================] - 56s 845ms/step - loss: 0.0114\n",
|
| 502 |
"Epoch 5/25\n",
|
| 503 |
+
"66/66 [==============================] - 57s 859ms/step - loss: 0.0113\n",
|
| 504 |
"Epoch 6/25\n",
|
| 505 |
+
"66/66 [==============================] - 58s 886ms/step - loss: 0.0112\n",
|
| 506 |
"Epoch 7/25\n",
|
| 507 |
+
"66/66 [==============================] - 59s 889ms/step - loss: 0.0112\n",
|
| 508 |
"Epoch 8/25\n",
|
| 509 |
+
"66/66 [==============================] - 59s 890ms/step - loss: 0.0111\n",
|
| 510 |
"Epoch 9/25\n",
|
| 511 |
+
"66/66 [==============================] - 58s 875ms/step - loss: 0.0112\n",
|
| 512 |
"Epoch 10/25\n",
|
| 513 |
+
"66/66 [==============================] - 58s 880ms/step - loss: 0.0112\n",
|
| 514 |
"Epoch 11/25\n",
|
| 515 |
+
"66/66 [==============================] - 58s 881ms/step - loss: 0.0111\n",
|
| 516 |
"Epoch 12/25\n",
|
| 517 |
+
"66/66 [==============================] - 59s 892ms/step - loss: 0.0111\n",
|
| 518 |
"Epoch 13/25\n",
|
| 519 |
+
"66/66 [==============================] - 59s 895ms/step - loss: 0.0110\n",
|
| 520 |
"Epoch 14/25\n",
|
| 521 |
+
"66/66 [==============================] - 58s 880ms/step - loss: 0.0111\n",
|
| 522 |
"Epoch 15/25\n",
|
| 523 |
+
"66/66 [==============================] - 58s 882ms/step - loss: 0.0111\n",
|
| 524 |
"Epoch 16/25\n",
|
| 525 |
+
"66/66 [==============================] - 59s 896ms/step - loss: 0.0110\n",
|
| 526 |
"Epoch 17/25\n",
|
| 527 |
+
"66/66 [==============================] - 58s 882ms/step - loss: 0.0112\n",
|
| 528 |
"Epoch 18/25\n",
|
| 529 |
+
"66/66 [==============================] - 58s 882ms/step - loss: 0.0110\n",
|
| 530 |
"Epoch 19/25\n",
|
| 531 |
+
"66/66 [==============================] - 58s 882ms/step - loss: 0.0111\n",
|
| 532 |
"Epoch 20/25\n",
|
| 533 |
+
"24/66 [=========>....................] - ETA: 37s - loss: 0.0099"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 534 |
]
|
| 535 |
},
|
| 536 |
{
|
| 537 |
+
"output_type": "error",
|
| 538 |
+
"ename": "KeyboardInterrupt",
|
| 539 |
+
"evalue": "ignored",
|
| 540 |
+
"traceback": [
|
| 541 |
+
"\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
|
| 542 |
+
"\u001b[0;31mKeyboardInterrupt\u001b[0m Traceback (most recent call last)",
|
| 543 |
+
"\u001b[0;32m<ipython-input-104-78bd1a1c9ef9>\u001b[0m in \u001b[0;36m<cell line: 5>\u001b[0;34m()\u001b[0m\n\u001b[1;32m 12\u001b[0m \u001b[0mmonitor\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0;34m\"loss\"\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mmode\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0;34m\"min\"\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 13\u001b[0m save_best_only=True)\n\u001b[0;32m---> 14\u001b[0;31m \u001b[0mmodel\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mfit\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mx_train\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0my_train\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mepochs\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0;36m25\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mbatch_size\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0;36m32\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mcallbacks\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0mcheckpoint_callback\u001b[0m\u001b[0;34m]\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m",
|
| 544 |
+
"\u001b[0;32m/usr/local/lib/python3.9/dist-packages/keras/utils/traceback_utils.py\u001b[0m in \u001b[0;36merror_handler\u001b[0;34m(*args, **kwargs)\u001b[0m\n\u001b[1;32m 63\u001b[0m \u001b[0mfiltered_tb\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0;32mNone\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 64\u001b[0m \u001b[0;32mtry\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m---> 65\u001b[0;31m \u001b[0;32mreturn\u001b[0m \u001b[0mfn\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m*\u001b[0m\u001b[0margs\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;34m**\u001b[0m\u001b[0mkwargs\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 66\u001b[0m \u001b[0;32mexcept\u001b[0m \u001b[0mException\u001b[0m \u001b[0;32mas\u001b[0m \u001b[0me\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 67\u001b[0m \u001b[0mfiltered_tb\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0m_process_traceback_frames\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0me\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0m__traceback__\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n",
|
| 545 |
+
"\u001b[0;32m/usr/local/lib/python3.9/dist-packages/keras/engine/training.py\u001b[0m in \u001b[0;36mfit\u001b[0;34m(self, x, y, batch_size, epochs, verbose, callbacks, validation_split, validation_data, shuffle, class_weight, sample_weight, initial_epoch, steps_per_epoch, validation_steps, validation_batch_size, validation_freq, max_queue_size, workers, use_multiprocessing)\u001b[0m\n\u001b[1;32m 1683\u001b[0m ):\n\u001b[1;32m 1684\u001b[0m \u001b[0mcallbacks\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mon_train_batch_begin\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mstep\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m-> 1685\u001b[0;31m \u001b[0mtmp_logs\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mtrain_function\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0miterator\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 1686\u001b[0m \u001b[0;32mif\u001b[0m \u001b[0mdata_handler\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mshould_sync\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 1687\u001b[0m \u001b[0mcontext\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0masync_wait\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n",
|
| 546 |
+
"\u001b[0;32m/usr/local/lib/python3.9/dist-packages/tensorflow/python/util/traceback_utils.py\u001b[0m in \u001b[0;36merror_handler\u001b[0;34m(*args, **kwargs)\u001b[0m\n\u001b[1;32m 148\u001b[0m \u001b[0mfiltered_tb\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0;32mNone\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 149\u001b[0m \u001b[0;32mtry\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m--> 150\u001b[0;31m \u001b[0;32mreturn\u001b[0m \u001b[0mfn\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m*\u001b[0m\u001b[0margs\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;34m**\u001b[0m\u001b[0mkwargs\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 151\u001b[0m \u001b[0;32mexcept\u001b[0m \u001b[0mException\u001b[0m \u001b[0;32mas\u001b[0m \u001b[0me\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 152\u001b[0m \u001b[0mfiltered_tb\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0m_process_traceback_frames\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0me\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0m__traceback__\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n",
|
| 547 |
+
"\u001b[0;32m/usr/local/lib/python3.9/dist-packages/tensorflow/python/eager/polymorphic_function/polymorphic_function.py\u001b[0m in \u001b[0;36m__call__\u001b[0;34m(self, *args, **kwds)\u001b[0m\n\u001b[1;32m 892\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 893\u001b[0m \u001b[0;32mwith\u001b[0m \u001b[0mOptionalXlaContext\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0m_jit_compile\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m--> 894\u001b[0;31m \u001b[0mresult\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0m_call\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m*\u001b[0m\u001b[0margs\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;34m**\u001b[0m\u001b[0mkwds\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 895\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 896\u001b[0m \u001b[0mnew_tracing_count\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mexperimental_get_tracing_count\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n",
|
| 548 |
+
"\u001b[0;32m/usr/local/lib/python3.9/dist-packages/tensorflow/python/eager/polymorphic_function/polymorphic_function.py\u001b[0m in \u001b[0;36m_call\u001b[0;34m(self, *args, **kwds)\u001b[0m\n\u001b[1;32m 924\u001b[0m \u001b[0;31m# In this case we have created variables on the first call, so we run the\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 925\u001b[0m \u001b[0;31m# defunned version which is guaranteed to never create variables.\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m--> 926\u001b[0;31m \u001b[0;32mreturn\u001b[0m \u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0m_no_variable_creation_fn\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m*\u001b[0m\u001b[0margs\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;34m**\u001b[0m\u001b[0mkwds\u001b[0m\u001b[0;34m)\u001b[0m \u001b[0;31m# pylint: disable=not-callable\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 927\u001b[0m \u001b[0;32melif\u001b[0m \u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0m_variable_creation_fn\u001b[0m \u001b[0;32mis\u001b[0m \u001b[0;32mnot\u001b[0m \u001b[0;32mNone\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 928\u001b[0m \u001b[0;31m# Release the lock early so that multiple threads can perform the call\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n",
|
| 549 |
+
"\u001b[0;32m/usr/local/lib/python3.9/dist-packages/tensorflow/python/eager/polymorphic_function/tracing_compiler.py\u001b[0m in \u001b[0;36m__call__\u001b[0;34m(self, *args, **kwargs)\u001b[0m\n\u001b[1;32m 141\u001b[0m (concrete_function,\n\u001b[1;32m 142\u001b[0m filtered_flat_args) = self._maybe_define_function(args, kwargs)\n\u001b[0;32m--> 143\u001b[0;31m return concrete_function._call_flat(\n\u001b[0m\u001b[1;32m 144\u001b[0m filtered_flat_args, captured_inputs=concrete_function.captured_inputs) # pylint: disable=protected-access\n\u001b[1;32m 145\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n",
|
| 550 |
+
"\u001b[0;32m/usr/local/lib/python3.9/dist-packages/tensorflow/python/eager/polymorphic_function/monomorphic_function.py\u001b[0m in \u001b[0;36m_call_flat\u001b[0;34m(self, args, captured_inputs, cancellation_manager)\u001b[0m\n\u001b[1;32m 1755\u001b[0m and executing_eagerly):\n\u001b[1;32m 1756\u001b[0m \u001b[0;31m# No tape is watching; skip to running the function.\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m-> 1757\u001b[0;31m return self._build_call_outputs(self._inference_function.call(\n\u001b[0m\u001b[1;32m 1758\u001b[0m ctx, args, cancellation_manager=cancellation_manager))\n\u001b[1;32m 1759\u001b[0m forward_backward = self._select_forward_and_backward_functions(\n",
|
| 551 |
+
"\u001b[0;32m/usr/local/lib/python3.9/dist-packages/tensorflow/python/eager/polymorphic_function/monomorphic_function.py\u001b[0m in \u001b[0;36mcall\u001b[0;34m(self, ctx, args, cancellation_manager)\u001b[0m\n\u001b[1;32m 379\u001b[0m \u001b[0;32mwith\u001b[0m \u001b[0m_InterpolateFunctionError\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mself\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 380\u001b[0m \u001b[0;32mif\u001b[0m \u001b[0mcancellation_manager\u001b[0m \u001b[0;32mis\u001b[0m \u001b[0;32mNone\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m--> 381\u001b[0;31m outputs = execute.execute(\n\u001b[0m\u001b[1;32m 382\u001b[0m \u001b[0mstr\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0msignature\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mname\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 383\u001b[0m \u001b[0mnum_outputs\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0m_num_outputs\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n",
|
| 552 |
+
"\u001b[0;32m/usr/local/lib/python3.9/dist-packages/tensorflow/python/eager/execute.py\u001b[0m in \u001b[0;36mquick_execute\u001b[0;34m(op_name, num_outputs, inputs, attrs, ctx, name)\u001b[0m\n\u001b[1;32m 50\u001b[0m \u001b[0;32mtry\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 51\u001b[0m \u001b[0mctx\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mensure_initialized\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m---> 52\u001b[0;31m tensors = pywrap_tfe.TFE_Py_Execute(ctx._handle, device_name, op_name,\n\u001b[0m\u001b[1;32m 53\u001b[0m inputs, attrs, num_outputs)\n\u001b[1;32m 54\u001b[0m \u001b[0;32mexcept\u001b[0m \u001b[0mcore\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0m_NotOkStatusException\u001b[0m \u001b[0;32mas\u001b[0m \u001b[0me\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n",
|
| 553 |
+
"\u001b[0;31mKeyboardInterrupt\u001b[0m: "
|
| 554 |
+
]
|
| 555 |
}
|
| 556 |
]
|
| 557 |
},
|
|
|
|
| 568 |
"cell_type": "code",
|
| 569 |
"source": [
|
| 570 |
"#print trainings directories to pick one\n",
|
| 571 |
+
"!ls -ld training_checkpoints_*/"
|
| 572 |
],
|
| 573 |
"metadata": {
|
| 574 |
+
"id": "59CDDB0i4yTx"
|
|
|
|
|
|
|
|
|
|
|
|
|
| 575 |
},
|
| 576 |
+
"execution_count": null,
|
| 577 |
+
"outputs": []
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 578 |
},
|
| 579 |
{
|
| 580 |
"cell_type": "code",
|
|
|
|
| 584 |
"metadata": {
|
| 585 |
"id": "tpmru7nG9kbW"
|
| 586 |
},
|
| 587 |
+
"execution_count": 105,
|
| 588 |
"outputs": []
|
| 589 |
},
|
| 590 |
{
|
| 591 |
"cell_type": "code",
|
| 592 |
"source": [
|
| 593 |
+
"# if checkpoint_dir does not exists, select the one stated in the except block\n",
|
| 594 |
+
"try:\n",
|
| 595 |
+
" checkpoint_dir\n",
|
| 596 |
+
"except NameError: \n",
|
| 597 |
+
" checkpoint_dir = './training_checkpoints_20230406214431'\n",
|
| 598 |
+
"\n",
|
| 599 |
+
"print(checkpoint_dir)\n",
|
| 600 |
"\n",
|
| 601 |
"def load_weights(epoch=None):\n",
|
| 602 |
" if epoch is None:\n",
|
|
|
|
| 618 |
"base_uri": "https://localhost:8080/"
|
| 619 |
},
|
| 620 |
"id": "wQ0JTXsp4VKF",
|
| 621 |
+
"outputId": "2b25d414-f188-4c14-ef43-f7af5566a3be"
|
| 622 |
},
|
| 623 |
+
"execution_count": 107,
|
| 624 |
"outputs": [
|
| 625 |
{
|
| 626 |
"output_type": "stream",
|
| 627 |
"name": "stdout",
|
| 628 |
"text": [
|
| 629 |
+
"./training_checkpoints_20230406230143\n",
|
| 630 |
+
"./training_checkpoints_20230406230143/ckpt_epoch16_loss0.01097947172820568\n"
|
| 631 |
]
|
| 632 |
}
|
| 633 |
]
|
|
|
|
| 635 |
{
|
| 636 |
"cell_type": "code",
|
| 637 |
"source": [
|
| 638 |
+
"test_start = dt.datetime(2013,1,1)\n",
|
| 639 |
+
"end = dt.datetime(2023,4,5)\n",
|
| 640 |
"\n",
|
| 641 |
"yfin.pdr_override()\n",
|
| 642 |
"test_data = web.data.get_data_yahoo(ticker, test_start, test_end)"
|
|
|
|
| 646 |
"base_uri": "https://localhost:8080/"
|
| 647 |
},
|
| 648 |
"id": "Mf4q97pfaSCA",
|
| 649 |
+
"outputId": "7355b53f-c879-4296-d24a-bb8bd739e5d8"
|
| 650 |
},
|
| 651 |
+
"execution_count": 114,
|
| 652 |
"outputs": [
|
| 653 |
{
|
| 654 |
"output_type": "stream",
|
|
|
|
| 662 |
{
|
| 663 |
"cell_type": "code",
|
| 664 |
"source": [
|
| 665 |
+
"# def close_tester(model, test_data, scaler=None):\n",
|
| 666 |
+
"model = test_model\n",
|
| 667 |
+
"scaler = scaler\n",
|
| 668 |
+
"test_x_train, test_y_train, _, _ = preprocessing(data, scaler=scaler)\n",
|
| 669 |
+
"print(test_x_train.shape)\n",
|
| 670 |
+
"print(test_y_train.shape)\n",
|
| 671 |
+
"results = model.predict(test_x_train)\n",
|
| 672 |
+
"# the results are tensors of 4 numbers, Jump, High, Low, and Close respectively\n",
|
| 673 |
+
"\n",
|
| 674 |
+
"# close_tester(test_model, test_data, scaler=the_scaler)\n"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 675 |
],
|
| 676 |
"metadata": {
|
| 677 |
"colab": {
|
| 678 |
"base_uri": "https://localhost:8080/"
|
| 679 |
},
|
| 680 |
+
"id": "MqCeMf3UoxZm",
|
| 681 |
+
"outputId": "a0591f06-f804-41e9-b973-627a7693ff89"
|
| 682 |
},
|
| 683 |
+
"execution_count": 115,
|
| 684 |
"outputs": [
|
| 685 |
{
|
| 686 |
"output_type": "stream",
|
| 687 |
"name": "stdout",
|
| 688 |
"text": [
|
| 689 |
+
"max_value: 10.589996337890625\n",
|
| 690 |
+
"max_volume: 1460852400.0\n",
|
| 691 |
+
"(2082, 500, 6)\n",
|
| 692 |
+
"(2082, 4)\n",
|
| 693 |
+
"66/66 [==============================] - 18s 275ms/step\n"
|
| 694 |
]
|
| 695 |
}
|
| 696 |
]
|
|
|
|
| 698 |
{
|
| 699 |
"cell_type": "code",
|
| 700 |
"source": [
|
| 701 |
+
"right_counter = 0\n",
|
| 702 |
+
"wrong_counter = 0\n",
|
| 703 |
+
"no_action_counter = 0\n",
|
| 704 |
+
"# for result, expected in zip(results[:2], test_y_train[:2]):\n",
|
| 705 |
+
"for result, expected in zip(results[:], test_y_train[:]):\n",
|
| 706 |
+
" # print(result)\n",
|
| 707 |
+
" # print(expected)\n",
|
| 708 |
+
" comparer = result[3] * expected[3]\n",
|
| 709 |
+
" if comparer > 0:\n",
|
| 710 |
+
" right_counter += 1\n",
|
| 711 |
+
" elif comparer == 0:\n",
|
| 712 |
+
" no_action_counter\n",
|
| 713 |
+
" elif comparer < 0:\n",
|
| 714 |
+
" wrong_counter += 1\n",
|
| 715 |
+
"\n",
|
| 716 |
+
" # print('expected: ', decoder(expected))\n",
|
| 717 |
+
" # print('result: ', decoder(result))\n",
|
| 718 |
+
"\n",
|
| 719 |
+
"print('right_counter :', right_counter)\n",
|
| 720 |
+
"print('no_action_counter :',no_action_counter)\n",
|
| 721 |
+
"print('wrong_counter :', wrong_counter)\n",
|
| 722 |
+
"print('success rate: {}%'.format(right_counter*100/len(results)))"
|
| 723 |
],
|
| 724 |
"metadata": {
|
| 725 |
"colab": {
|
| 726 |
"base_uri": "https://localhost:8080/"
|
| 727 |
},
|
| 728 |
"id": "AVYFQZnqEqhx",
|
| 729 |
+
"outputId": "7353f76c-f2e6-4a48-ba31-74ab67bb73ea"
|
| 730 |
},
|
| 731 |
+
"execution_count": 120,
|
| 732 |
"outputs": [
|
| 733 |
{
|
| 734 |
"output_type": "stream",
|
| 735 |
"name": "stdout",
|
| 736 |
"text": [
|
| 737 |
+
"right_counter : 1118\n",
|
| 738 |
+
"no_action_counter : 0\n",
|
| 739 |
+
"wrong_counter : 959\n",
|
| 740 |
+
"success rate: 53.6983669548511%\n"
|
| 741 |
]
|
| 742 |
}
|
| 743 |
]
|
|
|
|
| 745 |
{
|
| 746 |
"cell_type": "code",
|
| 747 |
"source": [
|
| 748 |
+
"test_data.iloc[500,:]"
|
|
|
|
| 749 |
],
|
| 750 |
"metadata": {
|
| 751 |
"colab": {
|
| 752 |
"base_uri": "https://localhost:8080/"
|
| 753 |
},
|
| 754 |
+
"id": "gyhzy_l6sAvi",
|
| 755 |
+
"outputId": "78f5d2cf-cd21-47b1-b58e-6b3321a802bd"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 756 |
},
|
| 757 |
+
"execution_count": 123,
|
| 758 |
"outputs": [
|
| 759 |
{
|
| 760 |
"output_type": "execute_result",
|
| 761 |
"data": {
|
| 762 |
"text/plain": [
|
| 763 |
+
"Open 2.802500e+01\n",
|
| 764 |
+
"High 2.863000e+01\n",
|
| 765 |
+
"Low 2.800250e+01\n",
|
| 766 |
+
"Close 2.849750e+01\n",
|
| 767 |
+
"Adj Close 2.561277e+01\n",
|
| 768 |
+
"Volume 1.348840e+08\n",
|
| 769 |
+
"Name: 2014-12-26 00:00:00, dtype: float64"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 770 |
]
|
| 771 |
},
|
| 772 |
"metadata": {},
|
| 773 |
+
"execution_count": 123
|
| 774 |
}
|
| 775 |
]
|
| 776 |
}
|