Spaces:
Runtime error
Runtime error
save whole model
Browse files- stock_predictor.ipynb +197 -221
stock_predictor.ipynb
CHANGED
|
@@ -4,7 +4,7 @@
|
|
| 4 |
"metadata": {
|
| 5 |
"colab": {
|
| 6 |
"provenance": [],
|
| 7 |
-
"authorship_tag": "
|
| 8 |
"include_colab_link": true
|
| 9 |
},
|
| 10 |
"kernelspec": {
|
|
@@ -33,7 +33,7 @@
|
|
| 33 |
"source": [
|
| 34 |
"from google.colab import drive\n",
|
| 35 |
"drive.mount('/content/drive')\n",
|
| 36 |
-
"project_path = '/content/drive/MyDrive/projects/Stock_Predicter'\n",
|
| 37 |
"%cd $project_path"
|
| 38 |
],
|
| 39 |
"metadata": {
|
|
@@ -41,23 +41,23 @@
|
|
| 41 |
"base_uri": "https://localhost:8080/"
|
| 42 |
},
|
| 43 |
"id": "Xr3Qozgfktoc",
|
| 44 |
-
"outputId": "
|
| 45 |
},
|
| 46 |
-
"execution_count":
|
| 47 |
"outputs": [
|
| 48 |
{
|
| 49 |
"output_type": "stream",
|
| 50 |
"name": "stdout",
|
| 51 |
"text": [
|
| 52 |
-
"
|
| 53 |
-
"/content/drive/MyDrive/projects/Stock_Predicter\n"
|
| 54 |
]
|
| 55 |
}
|
| 56 |
]
|
| 57 |
},
|
| 58 |
{
|
| 59 |
"cell_type": "code",
|
| 60 |
-
"execution_count":
|
| 61 |
"metadata": {
|
| 62 |
"id": "e8SQqogMQYLh"
|
| 63 |
},
|
|
@@ -99,7 +99,7 @@
|
|
| 99 |
"metadata": {
|
| 100 |
"id": "O6dtJpJwS5Eg"
|
| 101 |
},
|
| 102 |
-
"execution_count":
|
| 103 |
"outputs": []
|
| 104 |
},
|
| 105 |
{
|
|
@@ -113,9 +113,9 @@
|
|
| 113 |
"base_uri": "https://localhost:8080/"
|
| 114 |
},
|
| 115 |
"id": "LwPyk8Uh-Zz_",
|
| 116 |
-
"outputId": "
|
| 117 |
},
|
| 118 |
-
"execution_count":
|
| 119 |
"outputs": [
|
| 120 |
{
|
| 121 |
"output_type": "stream",
|
|
@@ -150,7 +150,7 @@
|
|
| 150 |
"metadata": {
|
| 151 |
"id": "Bpym8x-Kxf0p"
|
| 152 |
},
|
| 153 |
-
"execution_count":
|
| 154 |
"outputs": []
|
| 155 |
},
|
| 156 |
{
|
|
@@ -192,7 +192,7 @@
|
|
| 192 |
"metadata": {
|
| 193 |
"id": "v9RoqzBvtrOb"
|
| 194 |
},
|
| 195 |
-
"execution_count":
|
| 196 |
"outputs": []
|
| 197 |
},
|
| 198 |
{
|
|
@@ -203,10 +203,12 @@
|
|
| 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
|
| 210 |
" \n",
|
| 211 |
" x_train = np.array(x_train_list)\n",
|
| 212 |
" y_train = np.array(y_train_list)\n",
|
|
@@ -215,9 +217,40 @@
|
|
| 215 |
"metadata": {
|
| 216 |
"id": "jMXkRAYFomHM"
|
| 217 |
},
|
| 218 |
-
"execution_count":
|
| 219 |
"outputs": []
|
| 220 |
},
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 221 |
{
|
| 222 |
"cell_type": "code",
|
| 223 |
"source": [
|
|
@@ -236,7 +269,7 @@
|
|
| 236 |
"metadata": {
|
| 237 |
"id": "YZWMfusT-I7Z"
|
| 238 |
},
|
| 239 |
-
"execution_count":
|
| 240 |
"outputs": []
|
| 241 |
},
|
| 242 |
{
|
|
@@ -249,16 +282,18 @@
|
|
| 249 |
"base_uri": "https://localhost:8080/"
|
| 250 |
},
|
| 251 |
"id": "PeJjDC0VBG_6",
|
| 252 |
-
"outputId": "
|
| 253 |
},
|
| 254 |
-
"execution_count":
|
| 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 |
]
|
|
@@ -267,72 +302,23 @@
|
|
| 267 |
"cell_type": "code",
|
| 268 |
"source": [
|
| 269 |
"print(x_train.shape)\n",
|
| 270 |
-
"
|
| 271 |
],
|
| 272 |
"metadata": {
|
| 273 |
"colab": {
|
| 274 |
"base_uri": "https://localhost:8080/"
|
| 275 |
},
|
| 276 |
"id": "YkI8vSguuS8A",
|
| 277 |
-
"outputId": "
|
| 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":
|
| 318 |
"outputs": [
|
| 319 |
{
|
| 320 |
"output_type": "stream",
|
| 321 |
"name": "stdout",
|
| 322 |
"text": [
|
| 323 |
-
"
|
| 324 |
-
"
|
| 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 |
]
|
|
@@ -360,14 +346,12 @@
|
|
| 360 |
"source": [
|
| 361 |
"def create_model():\n",
|
| 362 |
" model = Sequential()\n",
|
| 363 |
-
"
|
| 364 |
-
" model.add(
|
| 365 |
-
" model.add(
|
| 366 |
-
" model.add(
|
| 367 |
-
" model.add(
|
| 368 |
-
" model.add(
|
| 369 |
-
" model.add(Dropout(0.2))\n",
|
| 370 |
-
" model.add(Dense(units=4))\n",
|
| 371 |
" return model\n",
|
| 372 |
"\n",
|
| 373 |
"model = create_model()\n",
|
|
@@ -378,35 +362,29 @@
|
|
| 378 |
"base_uri": "https://localhost:8080/"
|
| 379 |
},
|
| 380 |
"id": "GXhYAKzXVfku",
|
| 381 |
-
"outputId": "
|
| 382 |
},
|
| 383 |
-
"execution_count":
|
| 384 |
"outputs": [
|
| 385 |
{
|
| 386 |
"output_type": "stream",
|
| 387 |
"name": "stdout",
|
| 388 |
"text": [
|
| 389 |
-
"Model: \"
|
| 390 |
"_________________________________________________________________\n",
|
| 391 |
" Layer (type) Output Shape Param # \n",
|
| 392 |
"=================================================================\n",
|
| 393 |
-
"
|
| 394 |
-
" \n",
|
| 395 |
-
" dropout_6 (Dropout) (None, None, 1000) 0 \n",
|
| 396 |
" \n",
|
| 397 |
-
"
|
| 398 |
" \n",
|
| 399 |
-
"
|
| 400 |
" \n",
|
| 401 |
-
"
|
| 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:
|
| 409 |
-
"Trainable params:
|
| 410 |
"Non-trainable params: 0\n",
|
| 411 |
"_________________________________________________________________\n",
|
| 412 |
"None\n"
|
|
@@ -417,12 +395,25 @@
|
|
| 417 |
{
|
| 418 |
"cell_type": "code",
|
| 419 |
"source": [
|
| 420 |
-
"model.compile(optimizer='adam', loss='mean_squared_error')"
|
|
|
|
| 421 |
],
|
| 422 |
"metadata": {
|
| 423 |
"id": "ZhoWj_XeXQws"
|
| 424 |
},
|
| 425 |
-
"execution_count":
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 426 |
"outputs": []
|
| 427 |
},
|
| 428 |
{
|
|
@@ -445,16 +436,16 @@
|
|
| 445 |
"base_uri": "https://localhost:8080/"
|
| 446 |
},
|
| 447 |
"id": "HDT9XPXHvqyN",
|
| 448 |
-
"outputId": "
|
| 449 |
},
|
| 450 |
-
"execution_count":
|
| 451 |
"outputs": [
|
| 452 |
{
|
| 453 |
"output_type": "stream",
|
| 454 |
"name": "stdout",
|
| 455 |
"text": [
|
| 456 |
"(2082, 500, 6)\n",
|
| 457 |
-
"(2082, 4)\n"
|
| 458 |
]
|
| 459 |
}
|
| 460 |
]
|
|
@@ -463,97 +454,28 @@
|
|
| 463 |
"cell_type": "code",
|
| 464 |
"source": [
|
| 465 |
"# Change to False to avoid trainging the model\n",
|
| 466 |
-
"
|
| 467 |
-
"if
|
|
|
|
| 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 |
-
"
|
| 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":
|
| 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 |
},
|
| 558 |
{
|
| 559 |
"cell_type": "markdown",
|
|
@@ -571,20 +493,32 @@
|
|
| 571 |
"!ls -ld training_checkpoints_*/"
|
| 572 |
],
|
| 573 |
"metadata": {
|
| 574 |
-
"id": "59CDDB0i4yTx"
|
|
|
|
|
|
|
|
|
|
|
|
|
| 575 |
},
|
| 576 |
-
"execution_count":
|
| 577 |
-
"outputs": [
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 578 |
},
|
| 579 |
{
|
| 580 |
"cell_type": "code",
|
| 581 |
"source": [
|
| 582 |
-
"
|
| 583 |
],
|
| 584 |
"metadata": {
|
| 585 |
"id": "tpmru7nG9kbW"
|
| 586 |
},
|
| 587 |
-
"execution_count":
|
| 588 |
"outputs": []
|
| 589 |
},
|
| 590 |
{
|
|
@@ -594,9 +528,7 @@
|
|
| 594 |
"try:\n",
|
| 595 |
" checkpoint_dir\n",
|
| 596 |
"except NameError: \n",
|
| 597 |
-
" checkpoint_dir = '
|
| 598 |
-
"\n",
|
| 599 |
-
"print(checkpoint_dir)\n",
|
| 600 |
"\n",
|
| 601 |
"def load_weights(epoch=None):\n",
|
| 602 |
" if epoch is None:\n",
|
|
@@ -607,27 +539,44 @@
|
|
| 607 |
" if re.search(f'^ckpt_epoch{epoch}_.*\\.index', entry.name):\n",
|
| 608 |
" weights_file = checkpoint_dir + '/'+ entry.name[:-6]\n",
|
| 609 |
"\n",
|
|
|
|
| 610 |
" print(weights_file)\n",
|
| 611 |
-
"
|
| 612 |
-
" return
|
| 613 |
"\n",
|
| 614 |
-
"
|
|
|
|
|
|
|
| 615 |
],
|
| 616 |
"metadata": {
|
| 617 |
"colab": {
|
| 618 |
"base_uri": "https://localhost:8080/"
|
| 619 |
},
|
| 620 |
"id": "wQ0JTXsp4VKF",
|
| 621 |
-
"outputId": "
|
| 622 |
},
|
| 623 |
-
"execution_count":
|
| 624 |
"outputs": [
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 625 |
{
|
| 626 |
"output_type": "stream",
|
| 627 |
"name": "stdout",
|
| 628 |
"text": [
|
| 629 |
-
"
|
| 630 |
-
"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 631 |
]
|
| 632 |
}
|
| 633 |
]
|
|
@@ -635,8 +584,9 @@
|
|
| 635 |
{
|
| 636 |
"cell_type": "code",
|
| 637 |
"source": [
|
| 638 |
-
"test_start = dt.datetime(
|
| 639 |
-
"
|
|
|
|
| 640 |
"\n",
|
| 641 |
"yfin.pdr_override()\n",
|
| 642 |
"test_data = web.data.get_data_yahoo(ticker, test_start, test_end)"
|
|
@@ -646,9 +596,9 @@
|
|
| 646 |
"base_uri": "https://localhost:8080/"
|
| 647 |
},
|
| 648 |
"id": "Mf4q97pfaSCA",
|
| 649 |
-
"outputId": "
|
| 650 |
},
|
| 651 |
-
"execution_count":
|
| 652 |
"outputs": [
|
| 653 |
{
|
| 654 |
"output_type": "stream",
|
|
@@ -659,13 +609,37 @@
|
|
| 659 |
}
|
| 660 |
]
|
| 661 |
},
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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(
|
| 669 |
"print(test_x_train.shape)\n",
|
| 670 |
"print(test_y_train.shape)\n",
|
| 671 |
"results = model.predict(test_x_train)\n",
|
|
@@ -678,9 +652,9 @@
|
|
| 678 |
"base_uri": "https://localhost:8080/"
|
| 679 |
},
|
| 680 |
"id": "MqCeMf3UoxZm",
|
| 681 |
-
"outputId": "
|
| 682 |
},
|
| 683 |
-
"execution_count":
|
| 684 |
"outputs": [
|
| 685 |
{
|
| 686 |
"output_type": "stream",
|
|
@@ -688,9 +662,11 @@
|
|
| 688 |
"text": [
|
| 689 |
"max_value: 10.589996337890625\n",
|
| 690 |
"max_volume: 1460852400.0\n",
|
| 691 |
-
"
|
| 692 |
-
"(
|
| 693 |
-
"
|
|
|
|
|
|
|
| 694 |
]
|
| 695 |
}
|
| 696 |
]
|
|
@@ -705,7 +681,7 @@
|
|
| 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",
|
|
@@ -726,18 +702,18 @@
|
|
| 726 |
"base_uri": "https://localhost:8080/"
|
| 727 |
},
|
| 728 |
"id": "AVYFQZnqEqhx",
|
| 729 |
-
"outputId": "
|
| 730 |
},
|
| 731 |
-
"execution_count":
|
| 732 |
"outputs": [
|
| 733 |
{
|
| 734 |
"output_type": "stream",
|
| 735 |
"name": "stdout",
|
| 736 |
"text": [
|
| 737 |
-
"right_counter :
|
| 738 |
"no_action_counter : 0\n",
|
| 739 |
-
"wrong_counter :
|
| 740 |
-
"success rate:
|
| 741 |
]
|
| 742 |
}
|
| 743 |
]
|
|
@@ -752,25 +728,25 @@
|
|
| 752 |
"base_uri": "https://localhost:8080/"
|
| 753 |
},
|
| 754 |
"id": "gyhzy_l6sAvi",
|
| 755 |
-
"outputId": "
|
| 756 |
},
|
| 757 |
-
"execution_count":
|
| 758 |
"outputs": [
|
| 759 |
{
|
| 760 |
"output_type": "execute_result",
|
| 761 |
"data": {
|
| 762 |
"text/plain": [
|
| 763 |
-
"Open
|
| 764 |
-
"High
|
| 765 |
-
"Low
|
| 766 |
-
"Close
|
| 767 |
-
"Adj Close
|
| 768 |
-
"Volume
|
| 769 |
-
"Name:
|
| 770 |
]
|
| 771 |
},
|
| 772 |
"metadata": {},
|
| 773 |
-
"execution_count":
|
| 774 |
}
|
| 775 |
]
|
| 776 |
}
|
|
|
|
| 4 |
"metadata": {
|
| 5 |
"colab": {
|
| 6 |
"provenance": [],
|
| 7 |
+
"authorship_tag": "ABX9TyO1k4BL8RYqJqf+FTqQWuh+",
|
| 8 |
"include_colab_link": true
|
| 9 |
},
|
| 10 |
"kernelspec": {
|
|
|
|
| 33 |
"source": [
|
| 34 |
"from google.colab import drive\n",
|
| 35 |
"drive.mount('/content/drive')\n",
|
| 36 |
+
"project_path = '/content/drive/MyDrive/projects/Stock_Predicter/v1'\n",
|
| 37 |
"%cd $project_path"
|
| 38 |
],
|
| 39 |
"metadata": {
|
|
|
|
| 41 |
"base_uri": "https://localhost:8080/"
|
| 42 |
},
|
| 43 |
"id": "Xr3Qozgfktoc",
|
| 44 |
+
"outputId": "7d93f2be-afa1-402c-ec77-24e5d1cf5f8e"
|
| 45 |
},
|
| 46 |
+
"execution_count": 1,
|
| 47 |
"outputs": [
|
| 48 |
{
|
| 49 |
"output_type": "stream",
|
| 50 |
"name": "stdout",
|
| 51 |
"text": [
|
| 52 |
+
"Mounted at /content/drive\n",
|
| 53 |
+
"/content/drive/MyDrive/projects/Stock_Predicter/v1\n"
|
| 54 |
]
|
| 55 |
}
|
| 56 |
]
|
| 57 |
},
|
| 58 |
{
|
| 59 |
"cell_type": "code",
|
| 60 |
+
"execution_count": 2,
|
| 61 |
"metadata": {
|
| 62 |
"id": "e8SQqogMQYLh"
|
| 63 |
},
|
|
|
|
| 99 |
"metadata": {
|
| 100 |
"id": "O6dtJpJwS5Eg"
|
| 101 |
},
|
| 102 |
+
"execution_count": 3,
|
| 103 |
"outputs": []
|
| 104 |
},
|
| 105 |
{
|
|
|
|
| 113 |
"base_uri": "https://localhost:8080/"
|
| 114 |
},
|
| 115 |
"id": "LwPyk8Uh-Zz_",
|
| 116 |
+
"outputId": "5a636265-2f20-46ad-bc0d-30adbf2b630b"
|
| 117 |
},
|
| 118 |
+
"execution_count": 4,
|
| 119 |
"outputs": [
|
| 120 |
{
|
| 121 |
"output_type": "stream",
|
|
|
|
| 150 |
"metadata": {
|
| 151 |
"id": "Bpym8x-Kxf0p"
|
| 152 |
},
|
| 153 |
+
"execution_count": 5,
|
| 154 |
"outputs": []
|
| 155 |
},
|
| 156 |
{
|
|
|
|
| 192 |
"metadata": {
|
| 193 |
"id": "v9RoqzBvtrOb"
|
| 194 |
},
|
| 195 |
+
"execution_count": 6,
|
| 196 |
"outputs": []
|
| 197 |
},
|
| 198 |
{
|
|
|
|
| 203 |
" \n",
|
| 204 |
" x_train_list = []\n",
|
| 205 |
" y_train_list = []\n",
|
| 206 |
+
" print('shape norm_data')\n",
|
| 207 |
+
" print(norm_data.shape)\n",
|
| 208 |
" \n",
|
| 209 |
" for i in range(prediction_days, len(norm_data)):\n",
|
| 210 |
+
" x_train_list.append(norm_data.iloc[i-prediction_days:i])\n",
|
| 211 |
+
" y_train_list.append(norm_data.iloc[i-prediction_days+1:i+1,0:4])\n",
|
| 212 |
" \n",
|
| 213 |
" x_train = np.array(x_train_list)\n",
|
| 214 |
" y_train = np.array(y_train_list)\n",
|
|
|
|
| 217 |
"metadata": {
|
| 218 |
"id": "jMXkRAYFomHM"
|
| 219 |
},
|
| 220 |
+
"execution_count": 7,
|
| 221 |
"outputs": []
|
| 222 |
},
|
| 223 |
+
{
|
| 224 |
+
"cell_type": "code",
|
| 225 |
+
"source": [
|
| 226 |
+
"x = np.random.randint(5, size=(5,6))\n",
|
| 227 |
+
"print(x)\n",
|
| 228 |
+
"print(x[2:4, 0:4])"
|
| 229 |
+
],
|
| 230 |
+
"metadata": {
|
| 231 |
+
"colab": {
|
| 232 |
+
"base_uri": "https://localhost:8080/"
|
| 233 |
+
},
|
| 234 |
+
"id": "TX8fsigbOOzE",
|
| 235 |
+
"outputId": "a5e4e0f3-8814-4e28-d5e2-33f4e80954b5"
|
| 236 |
+
},
|
| 237 |
+
"execution_count": 8,
|
| 238 |
+
"outputs": [
|
| 239 |
+
{
|
| 240 |
+
"output_type": "stream",
|
| 241 |
+
"name": "stdout",
|
| 242 |
+
"text": [
|
| 243 |
+
"[[0 3 4 2 4 1]\n",
|
| 244 |
+
" [4 4 1 0 3 4]\n",
|
| 245 |
+
" [0 3 3 3 2 0]\n",
|
| 246 |
+
" [3 1 3 3 0 4]\n",
|
| 247 |
+
" [1 0 0 1 0 3]]\n",
|
| 248 |
+
"[[0 3 3 3]\n",
|
| 249 |
+
" [3 1 3 3]]\n"
|
| 250 |
+
]
|
| 251 |
+
}
|
| 252 |
+
]
|
| 253 |
+
},
|
| 254 |
{
|
| 255 |
"cell_type": "code",
|
| 256 |
"source": [
|
|
|
|
| 269 |
"metadata": {
|
| 270 |
"id": "YZWMfusT-I7Z"
|
| 271 |
},
|
| 272 |
+
"execution_count": 9,
|
| 273 |
"outputs": []
|
| 274 |
},
|
| 275 |
{
|
|
|
|
| 282 |
"base_uri": "https://localhost:8080/"
|
| 283 |
},
|
| 284 |
"id": "PeJjDC0VBG_6",
|
| 285 |
+
"outputId": "ec7bf037-3d03-47b5-b97b-43e19b9ac9ea"
|
| 286 |
},
|
| 287 |
+
"execution_count": 10,
|
| 288 |
"outputs": [
|
| 289 |
{
|
| 290 |
"output_type": "stream",
|
| 291 |
"name": "stdout",
|
| 292 |
"text": [
|
| 293 |
"max_value: 10.589996337890625\n",
|
| 294 |
+
"max_volume: 1460852400.0\n",
|
| 295 |
+
"shape norm_data\n",
|
| 296 |
+
"(2582, 6)\n"
|
| 297 |
]
|
| 298 |
}
|
| 299 |
]
|
|
|
|
| 302 |
"cell_type": "code",
|
| 303 |
"source": [
|
| 304 |
"print(x_train.shape)\n",
|
| 305 |
+
"print(y_train.shape)"
|
| 306 |
],
|
| 307 |
"metadata": {
|
| 308 |
"colab": {
|
| 309 |
"base_uri": "https://localhost:8080/"
|
| 310 |
},
|
| 311 |
"id": "YkI8vSguuS8A",
|
| 312 |
+
"outputId": "51e71e32-7e5b-4686-8e01-872ea97f976f"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 313 |
},
|
| 314 |
+
"execution_count": 11,
|
| 315 |
"outputs": [
|
| 316 |
{
|
| 317 |
"output_type": "stream",
|
| 318 |
"name": "stdout",
|
| 319 |
"text": [
|
| 320 |
+
"(2082, 500, 6)\n",
|
| 321 |
+
"(2082, 500, 4)\n"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 322 |
]
|
| 323 |
}
|
| 324 |
]
|
|
|
|
| 346 |
"source": [
|
| 347 |
"def create_model():\n",
|
| 348 |
" model = Sequential()\n",
|
| 349 |
+
" model.add(LSTM(units=1024, return_sequences=True, input_shape=(None,x_train.shape[-1],)))\n",
|
| 350 |
+
" # model.add(Dropout(0.2))\n",
|
| 351 |
+
" model.add(LSTM(units=1024, return_sequences=True))\n",
|
| 352 |
+
" # model.add(Dropout(0.2))\n",
|
| 353 |
+
" model.add(LSTM(units=1024, return_sequences=True))\n",
|
| 354 |
+
" model.add(Dense(4))\n",
|
|
|
|
|
|
|
| 355 |
" return model\n",
|
| 356 |
"\n",
|
| 357 |
"model = create_model()\n",
|
|
|
|
| 362 |
"base_uri": "https://localhost:8080/"
|
| 363 |
},
|
| 364 |
"id": "GXhYAKzXVfku",
|
| 365 |
+
"outputId": "0a471170-8a4b-416e-874c-ec5061a21744"
|
| 366 |
},
|
| 367 |
+
"execution_count": 12,
|
| 368 |
"outputs": [
|
| 369 |
{
|
| 370 |
"output_type": "stream",
|
| 371 |
"name": "stdout",
|
| 372 |
"text": [
|
| 373 |
+
"Model: \"sequential\"\n",
|
| 374 |
"_________________________________________________________________\n",
|
| 375 |
" Layer (type) Output Shape Param # \n",
|
| 376 |
"=================================================================\n",
|
| 377 |
+
" lstm (LSTM) (None, None, 1024) 4222976 \n",
|
|
|
|
|
|
|
| 378 |
" \n",
|
| 379 |
+
" lstm_1 (LSTM) (None, None, 1024) 8392704 \n",
|
| 380 |
" \n",
|
| 381 |
+
" lstm_2 (LSTM) (None, None, 1024) 8392704 \n",
|
| 382 |
" \n",
|
| 383 |
+
" dense (Dense) (None, None, 4) 4100 \n",
|
|
|
|
|
|
|
|
|
|
|
|
|
| 384 |
" \n",
|
| 385 |
"=================================================================\n",
|
| 386 |
+
"Total params: 21,012,484\n",
|
| 387 |
+
"Trainable params: 21,012,484\n",
|
| 388 |
"Non-trainable params: 0\n",
|
| 389 |
"_________________________________________________________________\n",
|
| 390 |
"None\n"
|
|
|
|
| 395 |
{
|
| 396 |
"cell_type": "code",
|
| 397 |
"source": [
|
| 398 |
+
"# model.compile(optimizer='adam', loss='mean_squared_error')\n",
|
| 399 |
+
"model.compile(optimizer='adam', loss=tf.keras.losses.MeanSquaredError())"
|
| 400 |
],
|
| 401 |
"metadata": {
|
| 402 |
"id": "ZhoWj_XeXQws"
|
| 403 |
},
|
| 404 |
+
"execution_count": 13,
|
| 405 |
+
"outputs": []
|
| 406 |
+
},
|
| 407 |
+
{
|
| 408 |
+
"cell_type": "code",
|
| 409 |
+
"source": [
|
| 410 |
+
"if False:\n",
|
| 411 |
+
" model.load_weights('./training_checkpoints_20230408062729/ckpt_epoch24_loss0.00024580140598118305')"
|
| 412 |
+
],
|
| 413 |
+
"metadata": {
|
| 414 |
+
"id": "jYCXXkRZraaF"
|
| 415 |
+
},
|
| 416 |
+
"execution_count": 14,
|
| 417 |
"outputs": []
|
| 418 |
},
|
| 419 |
{
|
|
|
|
| 436 |
"base_uri": "https://localhost:8080/"
|
| 437 |
},
|
| 438 |
"id": "HDT9XPXHvqyN",
|
| 439 |
+
"outputId": "041baee8-f87c-47aa-af07-8fb6dc56186f"
|
| 440 |
},
|
| 441 |
+
"execution_count": 15,
|
| 442 |
"outputs": [
|
| 443 |
{
|
| 444 |
"output_type": "stream",
|
| 445 |
"name": "stdout",
|
| 446 |
"text": [
|
| 447 |
"(2082, 500, 6)\n",
|
| 448 |
+
"(2082, 500, 4)\n"
|
| 449 |
]
|
| 450 |
}
|
| 451 |
]
|
|
|
|
| 454 |
"cell_type": "code",
|
| 455 |
"source": [
|
| 456 |
"# Change to False to avoid trainging the model\n",
|
| 457 |
+
"to_train = True\n",
|
| 458 |
+
"if to_train:\n",
|
| 459 |
+
"# if True:\n",
|
| 460 |
" # Directory where the checkpoints will be saved\n",
|
| 461 |
" checkpoint_dir = './training_checkpoints_'+dt.datetime.now().strftime(\"%Y%m%d%H%M%S\")\n",
|
| 462 |
" # Name of the checkpoint files\n",
|
| 463 |
+
" # checkpoint_prefix = os.path.join(checkpoint_dir, \"ckpt_epoch{epoch}_loss{loss}\")\n",
|
| 464 |
+
" checkpoint_prefix = os.path.join(checkpoint_dir, \"ckpt\")\n",
|
| 465 |
" \n",
|
| 466 |
" checkpoint_callback=tf.keras.callbacks.ModelCheckpoint(\n",
|
| 467 |
" filepath=checkpoint_prefix,\n",
|
| 468 |
" save_weights_only=True,\n",
|
| 469 |
" monitor=\"loss\", mode=\"min\",\n",
|
| 470 |
" save_best_only=True)\n",
|
| 471 |
+
"\n",
|
| 472 |
" model.fit(x_train, y_train, epochs=25, batch_size=32, callbacks=[checkpoint_callback])\n"
|
| 473 |
],
|
| 474 |
"metadata": {
|
| 475 |
+
"id": "9Ccc_Ej2TmYO"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 476 |
},
|
| 477 |
+
"execution_count": 16,
|
| 478 |
+
"outputs": []
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 479 |
},
|
| 480 |
{
|
| 481 |
"cell_type": "markdown",
|
|
|
|
| 493 |
"!ls -ld training_checkpoints_*/"
|
| 494 |
],
|
| 495 |
"metadata": {
|
| 496 |
+
"id": "59CDDB0i4yTx",
|
| 497 |
+
"colab": {
|
| 498 |
+
"base_uri": "https://localhost:8080/"
|
| 499 |
+
},
|
| 500 |
+
"outputId": "fa0788e1-b75e-45dc-a95d-ec3e24d00d70"
|
| 501 |
},
|
| 502 |
+
"execution_count": 17,
|
| 503 |
+
"outputs": [
|
| 504 |
+
{
|
| 505 |
+
"output_type": "stream",
|
| 506 |
+
"name": "stdout",
|
| 507 |
+
"text": [
|
| 508 |
+
"drwx------ 2 root root 4096 Apr 8 07:34 training_checkpoints_20230408073359/\n"
|
| 509 |
+
]
|
| 510 |
+
}
|
| 511 |
+
]
|
| 512 |
},
|
| 513 |
{
|
| 514 |
"cell_type": "code",
|
| 515 |
"source": [
|
| 516 |
+
"model = create_model()"
|
| 517 |
],
|
| 518 |
"metadata": {
|
| 519 |
"id": "tpmru7nG9kbW"
|
| 520 |
},
|
| 521 |
+
"execution_count": 18,
|
| 522 |
"outputs": []
|
| 523 |
},
|
| 524 |
{
|
|
|
|
| 528 |
"try:\n",
|
| 529 |
" checkpoint_dir\n",
|
| 530 |
"except NameError: \n",
|
| 531 |
+
" checkpoint_dir = 'training_checkpoints_20230408073359'\n",
|
|
|
|
|
|
|
| 532 |
"\n",
|
| 533 |
"def load_weights(epoch=None):\n",
|
| 534 |
" if epoch is None:\n",
|
|
|
|
| 539 |
" if re.search(f'^ckpt_epoch{epoch}_.*\\.index', entry.name):\n",
|
| 540 |
" weights_file = checkpoint_dir + '/'+ entry.name[:-6]\n",
|
| 541 |
"\n",
|
| 542 |
+
" print('weights_file')\n",
|
| 543 |
" print(weights_file)\n",
|
| 544 |
+
" model.load_weights(weights_file)\n",
|
| 545 |
+
" return model\n",
|
| 546 |
"\n",
|
| 547 |
+
"model = load_weights(epoch=None)\n",
|
| 548 |
+
"model_filepath = 'saved_model'\n",
|
| 549 |
+
"model.save(model_filepath)"
|
| 550 |
],
|
| 551 |
"metadata": {
|
| 552 |
"colab": {
|
| 553 |
"base_uri": "https://localhost:8080/"
|
| 554 |
},
|
| 555 |
"id": "wQ0JTXsp4VKF",
|
| 556 |
+
"outputId": "57440f25-6468-404c-f0a6-e99f004f9e67"
|
| 557 |
},
|
| 558 |
+
"execution_count": 28,
|
| 559 |
"outputs": [
|
| 560 |
+
{
|
| 561 |
+
"output_type": "stream",
|
| 562 |
+
"name": "stderr",
|
| 563 |
+
"text": [
|
| 564 |
+
"WARNING:tensorflow:Compiled the loaded model, but the compiled metrics have yet to be built. `model.compile_metrics` will be empty until you train or evaluate the model.\n"
|
| 565 |
+
]
|
| 566 |
+
},
|
| 567 |
{
|
| 568 |
"output_type": "stream",
|
| 569 |
"name": "stdout",
|
| 570 |
"text": [
|
| 571 |
+
"weights_file\n",
|
| 572 |
+
"training_checkpoints_20230408073359/ckpt\n"
|
| 573 |
+
]
|
| 574 |
+
},
|
| 575 |
+
{
|
| 576 |
+
"output_type": "stream",
|
| 577 |
+
"name": "stderr",
|
| 578 |
+
"text": [
|
| 579 |
+
"WARNING:absl:Found untraced functions such as lstm_cell_3_layer_call_fn, lstm_cell_3_layer_call_and_return_conditional_losses, lstm_cell_4_layer_call_fn, lstm_cell_4_layer_call_and_return_conditional_losses, lstm_cell_5_layer_call_fn while saving (showing 5 of 6). These functions will not be directly callable after loading.\n"
|
| 580 |
]
|
| 581 |
}
|
| 582 |
]
|
|
|
|
| 584 |
{
|
| 585 |
"cell_type": "code",
|
| 586 |
"source": [
|
| 587 |
+
"test_start = dt.datetime(2016,1,1)\n",
|
| 588 |
+
"test_end = dt.datetime(2023,4,5)\n",
|
| 589 |
+
"ticker = 'AAPL'\n",
|
| 590 |
"\n",
|
| 591 |
"yfin.pdr_override()\n",
|
| 592 |
"test_data = web.data.get_data_yahoo(ticker, test_start, test_end)"
|
|
|
|
| 596 |
"base_uri": "https://localhost:8080/"
|
| 597 |
},
|
| 598 |
"id": "Mf4q97pfaSCA",
|
| 599 |
+
"outputId": "5682a5dd-6968-4098-c203-31bc6c14e47c"
|
| 600 |
},
|
| 601 |
+
"execution_count": 24,
|
| 602 |
"outputs": [
|
| 603 |
{
|
| 604 |
"output_type": "stream",
|
|
|
|
| 609 |
}
|
| 610 |
]
|
| 611 |
},
|
| 612 |
+
{
|
| 613 |
+
"cell_type": "code",
|
| 614 |
+
"source": [
|
| 615 |
+
"load_whole_model = False\n",
|
| 616 |
+
"if load_whole_model:\n",
|
| 617 |
+
" model = tf.keras.models.load_model(model_filepath)"
|
| 618 |
+
],
|
| 619 |
+
"metadata": {
|
| 620 |
+
"colab": {
|
| 621 |
+
"base_uri": "https://localhost:8080/"
|
| 622 |
+
},
|
| 623 |
+
"id": "uloiDJXRbrJs",
|
| 624 |
+
"outputId": "a3aeb021-5555-413f-eb40-cce7ebe60ed5"
|
| 625 |
+
},
|
| 626 |
+
"execution_count": 29,
|
| 627 |
+
"outputs": [
|
| 628 |
+
{
|
| 629 |
+
"output_type": "stream",
|
| 630 |
+
"name": "stderr",
|
| 631 |
+
"text": [
|
| 632 |
+
"WARNING:tensorflow:No training configuration found in save file, so the model was *not* compiled. Compile it manually.\n"
|
| 633 |
+
]
|
| 634 |
+
}
|
| 635 |
+
]
|
| 636 |
+
},
|
| 637 |
{
|
| 638 |
"cell_type": "code",
|
| 639 |
"source": [
|
| 640 |
"# def close_tester(model, test_data, scaler=None):\n",
|
|
|
|
| 641 |
"scaler = scaler\n",
|
| 642 |
+
"test_x_train, test_y_train, _, _ = preprocessing(test_data, scaler=scaler)\n",
|
| 643 |
"print(test_x_train.shape)\n",
|
| 644 |
"print(test_y_train.shape)\n",
|
| 645 |
"results = model.predict(test_x_train)\n",
|
|
|
|
| 652 |
"base_uri": "https://localhost:8080/"
|
| 653 |
},
|
| 654 |
"id": "MqCeMf3UoxZm",
|
| 655 |
+
"outputId": "1a448123-6253-4585-c7af-5ff7491c1628"
|
| 656 |
},
|
| 657 |
+
"execution_count": 30,
|
| 658 |
"outputs": [
|
| 659 |
{
|
| 660 |
"output_type": "stream",
|
|
|
|
| 662 |
"text": [
|
| 663 |
"max_value: 10.589996337890625\n",
|
| 664 |
"max_volume: 1460852400.0\n",
|
| 665 |
+
"shape norm_data\n",
|
| 666 |
+
"(1826, 6)\n",
|
| 667 |
+
"(1326, 500, 6)\n",
|
| 668 |
+
"(1326, 500, 4)\n",
|
| 669 |
+
"42/42 [==============================] - 12s 270ms/step\n"
|
| 670 |
]
|
| 671 |
}
|
| 672 |
]
|
|
|
|
| 681 |
"for result, expected in zip(results[:], test_y_train[:]):\n",
|
| 682 |
" # print(result)\n",
|
| 683 |
" # print(expected)\n",
|
| 684 |
+
" comparer = result[-1][3] * expected[-1][3]\n",
|
| 685 |
" if comparer > 0:\n",
|
| 686 |
" right_counter += 1\n",
|
| 687 |
" elif comparer == 0:\n",
|
|
|
|
| 702 |
"base_uri": "https://localhost:8080/"
|
| 703 |
},
|
| 704 |
"id": "AVYFQZnqEqhx",
|
| 705 |
+
"outputId": "8110f58a-7344-42b0-ed9a-ba77a4999e30"
|
| 706 |
},
|
| 707 |
+
"execution_count": 31,
|
| 708 |
"outputs": [
|
| 709 |
{
|
| 710 |
"output_type": "stream",
|
| 711 |
"name": "stdout",
|
| 712 |
"text": [
|
| 713 |
+
"right_counter : 1300\n",
|
| 714 |
"no_action_counter : 0\n",
|
| 715 |
+
"wrong_counter : 22\n",
|
| 716 |
+
"success rate: 98.03921568627452%\n"
|
| 717 |
]
|
| 718 |
}
|
| 719 |
]
|
|
|
|
| 728 |
"base_uri": "https://localhost:8080/"
|
| 729 |
},
|
| 730 |
"id": "gyhzy_l6sAvi",
|
| 731 |
+
"outputId": "6bd3c88a-b61b-44e8-d16d-0880a17ebab2"
|
| 732 |
},
|
| 733 |
+
"execution_count": 32,
|
| 734 |
"outputs": [
|
| 735 |
{
|
| 736 |
"output_type": "execute_result",
|
| 737 |
"data": {
|
| 738 |
"text/plain": [
|
| 739 |
+
"Open 4.252500e+01\n",
|
| 740 |
+
"High 4.269500e+01\n",
|
| 741 |
+
"Low 4.242750e+01\n",
|
| 742 |
+
"Close 4.265000e+01\n",
|
| 743 |
+
"Adj Close 4.049405e+01\n",
|
| 744 |
+
"Volume 8.599280e+07\n",
|
| 745 |
+
"Name: 2017-12-27 00:00:00, dtype: float64"
|
| 746 |
]
|
| 747 |
},
|
| 748 |
"metadata": {},
|
| 749 |
+
"execution_count": 32
|
| 750 |
}
|
| 751 |
]
|
| 752 |
}
|