Skip to content

Commit

Permalink
separate imports in save_restore
Browse files Browse the repository at this point in the history
  • Loading branch information
jjahanip committed Feb 21, 2019
1 parent 82bd696 commit 7eb095a
Showing 1 changed file with 64 additions and 52 deletions.
116 changes: 64 additions & 52 deletions 1_TensorFlow_Basics/Tutorials/4_Save_and_Restore.ipynb
Expand Up @@ -56,6 +56,26 @@
"```\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## 0. Import the required libraries:\n",
"\n",
"We will start with importing the required Python libraries."
]
},
{
"cell_type": "code",
"execution_count": 1,
"metadata": {},
"outputs": [],
"source": [
"#imports\n",
"import tensorflow as tf\n",
"import os"
]
},
{
"cell_type": "markdown",
"metadata": {},
Expand All @@ -67,13 +87,10 @@
},
{
"cell_type": "code",
"execution_count": 1,
"metadata": {
"collapsed": true
},
"execution_count": 2,
"metadata": {},
"outputs": [],
"source": [
"import tensorflow as tf\n",
"# create variables a and b\n",
"a = tf.get_variable(\"A\", initializer=tf.constant(3, shape=[2]))\n",
"b = tf.get_variable(\"B\", initializer=tf.constant(5, shape=[3]))"
Expand All @@ -95,10 +112,8 @@
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {
"collapsed": true
},
"execution_count": 3,
"metadata": {},
"outputs": [],
"source": [
"# initialize all of the variables\n",
Expand All @@ -114,7 +129,7 @@
},
{
"cell_type": "code",
"execution_count": 3,
"execution_count": 4,
"metadata": {},
"outputs": [
{
Expand Down Expand Up @@ -148,7 +163,7 @@
},
{
"cell_type": "code",
"execution_count": 4,
"execution_count": 5,
"metadata": {},
"outputs": [
{
Expand Down Expand Up @@ -180,7 +195,7 @@
},
{
"cell_type": "code",
"execution_count": 5,
"execution_count": 6,
"metadata": {},
"outputs": [
{
Expand Down Expand Up @@ -211,21 +226,20 @@
},
{
"cell_type": "code",
"execution_count": 6,
"execution_count": 7,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"saved_variable.data-00000-of-00001\n",
"saved_variable.meta\n",
"saved_variable.index\n"
"saved_variable.index\n",
"saved_variable.meta\n"
]
}
],
"source": [
"import os\n",
"for file in os.listdir('.'):\n",
" if 'saved_variable' in file:\n",
" print(file)"
Expand All @@ -252,7 +266,7 @@
},
{
"cell_type": "code",
"execution_count": 7,
"execution_count": 8,
"metadata": {},
"outputs": [
{
Expand Down Expand Up @@ -292,7 +306,7 @@
},
{
"cell_type": "code",
"execution_count": 8,
"execution_count": 9,
"metadata": {},
"outputs": [
{
Expand Down Expand Up @@ -336,7 +350,7 @@
},
{
"cell_type": "code",
"execution_count": 9,
"execution_count": 10,
"metadata": {},
"outputs": [
{
Expand Down Expand Up @@ -392,7 +406,7 @@
},
{
"cell_type": "code",
"execution_count": 10,
"execution_count": 11,
"metadata": {},
"outputs": [
{
Expand Down Expand Up @@ -446,7 +460,7 @@
},
{
"cell_type": "code",
"execution_count": 11,
"execution_count": 12,
"metadata": {},
"outputs": [
{
Expand Down Expand Up @@ -489,7 +503,7 @@
},
{
"cell_type": "code",
"execution_count": 12,
"execution_count": 13,
"metadata": {},
"outputs": [
{
Expand Down Expand Up @@ -554,10 +568,8 @@
},
{
"cell_type": "code",
"execution_count": 13,
"metadata": {
"collapsed": true
},
"execution_count": 14,
"metadata": {},
"outputs": [],
"source": [
"# create saver object\n",
Expand All @@ -573,15 +585,15 @@
},
{
"cell_type": "code",
"execution_count": 14,
"execution_count": 15,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"---------------------------------------------------------\n",
"Validation loss: 0.31, Validation accuracy: 91.7%\n",
"Validation loss: 0.32, Validation accuracy: 91.4%\n",
"---------------------------------------------------------\n",
"model saved in ./linear_model\n"
]
Expand Down Expand Up @@ -619,16 +631,16 @@
},
{
"cell_type": "code",
"execution_count": 15,
"execution_count": 16,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"linear_model.data-00000-of-00001\n",
"linear_model.meta\n",
"linear_model.index\n"
"linear_model.index\n",
"linear_model.meta\n"
]
}
],
Expand All @@ -647,7 +659,7 @@
},
{
"cell_type": "code",
"execution_count": 16,
"execution_count": 17,
"metadata": {},
"outputs": [
{
Expand All @@ -656,24 +668,24 @@
"text": [
"INFO:tensorflow:Restoring parameters from ./linear_model\n",
"---------------------------------------------------------\n",
"Test loss: 0.32, test accuracy: 91.4%\n",
"Test loss: 0.32, test accuracy: 91.2%\n",
"---------------------------------------------------------\n",
"\n",
"W = [[ 0.01166079 0.00214 0.01031407 ... 0.00138819 -0.00628771\n",
" 0.00202227]\n",
" [ 0.00583515 -0.0049552 0.00155843 ... -0.00023047 -0.00729254\n",
" -0.00405982]\n",
" [ 0.01768233 -0.00875011 -0.01097609 ... -0.00147737 -0.00018261\n",
" -0.00989295]\n",
"W = [[-0.00795777 0.00179533 -0.01632378 ... 0.01127853 -0.0165657\n",
" -0.01905626]\n",
" [-0.00036186 -0.01072511 0.01412622 ... -0.01868354 0.0091197\n",
" -0.01899963]\n",
" [ 0.01169185 0.00378057 -0.00452419 ... 0.01239008 0.00784524\n",
" -0.00559599]\n",
" ...\n",
" [ 0.00241719 0.00672442 -0.00274725 ... 0.01378055 -0.00826664\n",
" -0.00141329]\n",
" [-0.01537914 -0.00139711 0.0020227 ... -0.00358674 0.00436257\n",
" -0.00737171]\n",
" [ 0.00428195 0.00458214 -0.01005485 ... 0.00464132 0.00323565\n",
" -0.00285462]]\n",
"b = [-0.14392368 0.2330784 -0.04319457 -0.10059926 0.06679667 0.21008638\n",
" -0.0314009 0.14653793 -0.30130398 -0.05041829]\n"
" [ 0.00589321 -0.01055128 0.00599118 ... -0.0021277 0.00593736\n",
" 0.00538401]\n",
" [ 0.0050825 0.00028797 -0.00596465 ... -0.00036289 0.00454178\n",
" -0.00049127]\n",
" [-0.00797494 -0.00345959 0.002974 ... 0.01462011 -0.00961047\n",
" 0.00482459]]\n",
"b = [-0.13907638 0.24205 -0.04328492 -0.09594144 0.0636861 0.19795758\n",
" -0.03274642 0.12884071 -0.2828767 -0.04605722]\n"
]
}
],
Expand All @@ -683,7 +695,7 @@
"# run the session\n",
"with tf.Session() as sess:\n",
" # restore the saved vairable\n",
" imported_graph.restore(sess, './linear_model')\n",
" saver.restore(sess, './linear_model')\n",
" \n",
" # Accuracy\n",
" feed_dict_test = {x: data.test.images, y: data.test.labels}\n",
Expand All @@ -710,7 +722,7 @@
},
{
"cell_type": "code",
"execution_count": 17,
"execution_count": 18,
"metadata": {},
"outputs": [
{
Expand Down Expand Up @@ -923,7 +935,7 @@
},
{
"cell_type": "code",
"execution_count": 18,
"execution_count": 19,
"metadata": {},
"outputs": [
{
Expand All @@ -932,7 +944,7 @@
"text": [
"INFO:tensorflow:Restoring parameters from ./linear_model\n",
"---------------------------------------------------------\n",
"Test loss: 0.32, test accuracy: 91.4%\n",
"Test loss: 0.32, test accuracy: 91.2%\n",
"---------------------------------------------------------\n",
"\n"
]
Expand Down Expand Up @@ -977,7 +989,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.5.4"
"version": "3.6.8"
}
},
"nbformat": 4,
Expand Down

0 comments on commit 7eb095a

Please sign in to comment.