diff --git a/1_to_1_multi_layer.ipynb b/1_to_1_multi_layer.ipynb index 839daa0..ce6cd94 100644 --- a/1_to_1_multi_layer.ipynb +++ b/1_to_1_multi_layer.ipynb @@ -208,7 +208,7 @@ }, { "cell_type": "code", - "execution_count": 24, + "execution_count": 10, "metadata": {}, "outputs": [], "source": [ @@ -223,7 +223,7 @@ }, { "cell_type": "code", - "execution_count": 25, + "execution_count": 11, "metadata": {}, "outputs": [], "source": [ @@ -259,7 +259,7 @@ }, { "cell_type": "code", - "execution_count": 26, + "execution_count": 12, "metadata": {}, "outputs": [], "source": [ @@ -343,7 +343,7 @@ " \n", " \n", " \n", - " def fit(self, minibatches, epochs, print_step, checkpoint = 10, patience = 80):\n", + " def fit(self, minibatches, epochs, print_step, checkpoint = 10, patience = 15):\n", " self.loss_list = []\n", " patience_cnt = 0\n", " epoche_save = 0\n", @@ -353,8 +353,9 @@ " for iep in range(epochs):\n", " loss = 0\n", " \n", - " #Here I iterate through the batches\n", - " for batch in range(len(minibatches)):\n", + " batches = len(minibatches)\n", + " #Here I iterate over the batches\n", + " for batch in range(batches):\n", " #### Here I train the RNNcell\n", " #### The X is the time series, the Y is shifted by 1 time step\n", " train, target = minibatches[batch]\n", @@ -363,6 +364,8 @@ " \n", " loss += self.sess.run(self.cost, feed_dict={self.X:train, self.Y:target})\n", " \n", + " #Normalize loss over number of batches\n", + " loss /= batches\n", " self.loss_list.append(loss)\n", " \n", " #print(loss)\n", @@ -374,17 +377,13 @@ " epoche_save = iep\n", " \n", " #early stopping with patience\n", - " if iep > 1 and abs(self.loss_list[iep]-self.loss_list[iep-1]) < 0.05:\n", + " if iep > 1 and abs(self.loss_list[iep]-self.loss_list[iep-1]) < 0.005:\n", " patience_cnt += 1\n", " #print(\"Patience now at: \", patience_cnt, \" of \", patience)\n", + " \n", " if patience_cnt + 1 > patience:\n", " print(\"Early stopping at epoch \", iep, \", difference: \", abs(self.loss_list[iep]-self.loss_list[iep-1]))\n", - " \n", - " #Set model back to the last checkpoint if performance was better\n", - " if self.loss_list[epoche_save] < self.loss_list[iep]:\n", - " self.load(folder)\n", - " print(\"Last checkpoint at epoch \", epoche_save, \" loaded\")\n", - " print(\"Performance at last checkpoint is \" ,self.loss_list[iep] - self.loss_list[epoche_save], \" better\" )\n", + " print(\"Cost: \",loss)\n", " break\n", " \n", " if iep%print_step==0:\n", @@ -392,6 +391,12 @@ " print(\"Cost: \",loss)\n", " print(\"Patience: \",patience_cnt, \"/\", patience)\n", " print(\"Last checkpoint at: Epoch \", epoche_save, \"\\n\")\n", + " \n", + " #Set model back to the last checkpoint if performance was better\n", + " if self.loss_list[epoche_save] < self.loss_list[iep]:\n", + " self.load(folder)\n", + " print(\"Last checkpoint at epoch \", epoche_save, \" loaded\")\n", + " print(\"Performance at last checkpoint is \" ,self.loss_list[iep] - self.loss_list[epoche_save], \" better\" )\n", " \n", " \n", " \n", @@ -410,7 +415,7 @@ }, { "cell_type": "code", - "execution_count": 27, + "execution_count": 13, "metadata": {}, "outputs": [], "source": [ @@ -427,9 +432,19 @@ }, { "cell_type": "code", - "execution_count": 28, + "execution_count": 14, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "WARNING:tensorflow:From c:\\users\\sa_li\\anaconda3\\envs\\rnn-tf-ker\\lib\\site-packages\\tensorflow\\contrib\\learn\\python\\learn\\datasets\\base.py:198: retry (from tensorflow.contrib.learn.python.learn.datasets.base) is deprecated and will be removed in a future version.\n", + "Instructions for updating:\n", + "Use the retry module or similar alternatives.\n" + ] + } + ], "source": [ "tf.reset_default_graph()\n", "rnn = RNNPlacePrediction(time_steps=timesteps, future_steps=future_steps, ninputs=ninputs, \n", @@ -438,7 +453,7 @@ }, { "cell_type": "code", - "execution_count": 29, + "execution_count": 15, "metadata": {}, "outputs": [], "source": [ @@ -447,7 +462,7 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 19, "metadata": { "scrolled": true }, @@ -457,15 +472,20 @@ "output_type": "stream", "text": [ "Epoch number 0\n", - "Cost: 65161.071533203125\n", - "Patience: 0 / 80\n", + "Cost: 3.893127314587857\n", + "Patience: 0 / 5\n", "Last checkpoint at: Epoch 0 \n", - "\n" + "\n", + "Early stopping at epoch 10 , difference: 0.002693013941988287\n", + "Cost: 3.91306800537921\n", + "INFO:tensorflow:Restoring parameters from ./rnn_model_lstm[50,40,30,20,10]c_checkpoint/rnn_basic\n", + "Last checkpoint at epoch 0 loaded\n", + "Performance at last checkpoint is 0.019940690791353077 better\n" ] } ], "source": [ - "rnn.fit(minibatches, epochs=5000, print_step=500)" + "rnn.fit(minibatches, epochs=22, print_step=10)" ] }, { @@ -474,6 +494,18 @@ "metadata": {}, "outputs": [], "source": [ + "plt.plot(rnn.loss_list)\n", + "plt.xlabel(\"Epoch\")\n", + "plt.ylabel(\"Cost\")\n", + "plt.show()" + ] + }, + { + "cell_type": "code", + "execution_count": 20, + "metadata": {}, + "outputs": [], + "source": [ "#save in a folder that describes the model\n", "folder = \"./rnn_model_\" + str(rnn._) + \"_\" + str(len(rnn.ncells)) + \"l_\" + str(rnn.ncells).replace(\" \",\"\") + \"c/rnn_basic\"\n", "rnn.save(folder)" @@ -481,16 +513,17 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 21, "metadata": {}, "outputs": [], "source": [ - "###rnn.load(folder)###" + "#folder = \"./trained_models/rnn_model_\" + str(rnn._) + \"_\" + str(len(rnn.ncells)) + \"l_\" + str(rnn.ncells).replace(\" \",\"\") + \"c/rnn_basic\"\n", + "#rnn.load(folder)" ] }, { "cell_type": "code", - "execution_count": null, + "execution_count": 22, "metadata": {}, "outputs": [], "source": [ @@ -499,18 +532,18 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 23, "metadata": {}, "outputs": [], "source": [ "#Here I predict based on my test set\n", "\n", - "#test_pred = rnn.predict(test_input)" + "test_pred = rnn.predict(test_input)" ] }, { "cell_type": "code", - "execution_count": null, + "execution_count": 24, "metadata": {}, "outputs": [], "source": [ @@ -521,13 +554,24 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 25, "metadata": {}, - "outputs": [], + "outputs": [ + { + "data": { + "text/plain": [ + "4.3867254" + ] + }, + "execution_count": 25, + "metadata": {}, + "output_type": "execute_result" + } + ], "source": [ "#Here I evaluate my model on the test set based on mean_squared_error\n", "\n", - "#rnn.sess.run(rnn.cost, feed_dict={rnn.X:test_input, rnn.Y:test_target})" + "rnn.sess.run(rnn.cost, feed_dict={rnn.X:test_input, rnn.Y:test_target})" ] }, { @@ -535,9 +579,7 @@ "execution_count": null, "metadata": {}, "outputs": [], - "source": [ - "print(1%10)" - ] + "source": [] }, { "cell_type": "code", @@ -546,15 +588,7 @@ "scrolled": true }, "outputs": [], - "source": [ - "z = 0\n", - "for _ in range(len(rnn.loss_list)):\n", - " if abs(rnn.loss_list[_]-rnn.loss_list[_-1]) < 0.5:\n", - " z += 1\n", - " print(_)\n", - "\n", - "print(z)" - ] + "source": [] }, { "cell_type": "code", diff --git "a/trained_models/rnn_model_lstm_5l_\13350,40,30,20,10\135c/checkpoint" "b/trained_models/rnn_model_lstm_5l_\13350,40,30,20,10\135c/checkpoint" new file mode 100644 index 0000000..774833f --- /dev/null +++ "b/trained_models/rnn_model_lstm_5l_\13350,40,30,20,10\135c/checkpoint" @@ -0,0 +1,3 @@ +model_checkpoint_path: "rnn_basic" +all_model_checkpoint_paths: "..\\rnn_model_lstm[50,40,30,20,10]c_checkpoint\\rnn_basic" +all_model_checkpoint_paths: "rnn_basic" diff --git "a/trained_models/rnn_model_lstm_5l_\13350,40,30,20,10\135c/rnn_basic.data-00000-of-00001" "b/trained_models/rnn_model_lstm_5l_\13350,40,30,20,10\135c/rnn_basic.data-00000-of-00001" new file mode 100644 index 0000000..d91c1ce --- /dev/null +++ "b/trained_models/rnn_model_lstm_5l_\13350,40,30,20,10\135c/rnn_basic.data-00000-of-00001" Binary files differ diff --git "a/trained_models/rnn_model_lstm_5l_\13350,40,30,20,10\135c/rnn_basic.index" "b/trained_models/rnn_model_lstm_5l_\13350,40,30,20,10\135c/rnn_basic.index" new file mode 100644 index 0000000..361718e --- /dev/null +++ "b/trained_models/rnn_model_lstm_5l_\13350,40,30,20,10\135c/rnn_basic.index" Binary files differ diff --git "a/trained_models/rnn_model_lstm_5l_\13350,40,30,20,10\135c/rnn_basic.meta" "b/trained_models/rnn_model_lstm_5l_\13350,40,30,20,10\135c/rnn_basic.meta" new file mode 100644 index 0000000..b941ef6 --- /dev/null +++ "b/trained_models/rnn_model_lstm_5l_\13350,40,30,20,10\135c/rnn_basic.meta" Binary files differ