Created using Colaboratory

This commit is contained in:
George Mihaila
2019-01-22 09:31:09 -06:00
parent b544c6b80a
commit 758b72c586

View File

@@ -41,9 +41,9 @@
"colab_type": "code",
"colab": {
"base_uri": "https://localhost:8080/",
"height": 35
"height": 53
},
"outputId": "e6a2fb89-1ffc-4cc8-b34a-459f0f58a4fd"
"outputId": "539c2842-f33b-4fdf-ced5-90f1e7ff88bb"
},
"cell_type": "code",
"source": [
@@ -64,7 +64,8 @@
{
"output_type": "stream",
"text": [
"pima-indians-diabetes.data.csv\tsample_data\n"
"model.json model_weights.h5 pima-indians-diabetes.data.csv\tsample_data\n",
"model.png model.yaml\t pima-indians-diabetes.data.csv.1\twhole_model.h5\n"
],
"name": "stdout"
}
@@ -74,7 +75,11 @@
"metadata": {
"id": "cVCl1YndhcsL",
"colab_type": "code",
"colab": {}
"colab": {
"base_uri": "https://localhost:8080/",
"height": 35
},
"outputId": "22b17207-cbe3-48f9-93e4-fb59bf515d57"
},
"cell_type": "code",
"source": [
@@ -87,8 +92,16 @@
"\n",
"from IPython.display import Image"
],
"execution_count": 0,
"outputs": []
"execution_count": 2,
"outputs": [
{
"output_type": "stream",
"text": [
"Using TensorFlow backend.\n"
],
"name": "stderr"
}
]
},
{
"metadata": {
@@ -98,7 +111,7 @@
"base_uri": "https://localhost:8080/",
"height": 53
},
"outputId": "06505c42-db86-4cee-bf05-717b9736b768"
"outputId": "2f2aa79d-21db-49e0-f5cc-3c32e9ab439e"
},
"cell_type": "code",
"source": [
@@ -159,7 +172,7 @@
"base_uri": "https://localhost:8080/",
"height": 312
},
"outputId": "1d700361-332f-4c56-fa32-42f8f3c42678"
"outputId": "449a81f1-fdfc-4c08-b079-a2f731f13bb6"
},
"cell_type": "code",
"source": [
@@ -192,7 +205,7 @@
"base_uri": "https://localhost:8080/",
"height": 395
},
"outputId": "bfd006ea-791a-4f06-c4ad-c0c25560b243"
"outputId": "9fdd9f6f-b427-45b9-b524-b88f864f68b6"
},
"cell_type": "code",
"source": [
@@ -205,25 +218,25 @@
"output_type": "stream",
"text": [
"Epoch 1/10\n",
"768/768 [==============================] - 1s 787us/step - loss: 5.0707 - acc: 0.6510\n",
"768/768 [==============================] - 0s 295us/step - loss: 6.0555 - acc: 0.5768\n",
"Epoch 2/10\n",
"768/768 [==============================] - 0s 101us/step - loss: 3.5597 - acc: 0.5807\n",
"768/768 [==============================] - 0s 97us/step - loss: 5.8343 - acc: 0.5990\n",
"Epoch 3/10\n",
"768/768 [==============================] - 0s 99us/step - loss: 3.3356 - acc: 0.5911\n",
"768/768 [==============================] - 0s 95us/step - loss: 5.7260 - acc: 0.6237\n",
"Epoch 4/10\n",
"768/768 [==============================] - 0s 95us/step - loss: 3.2350 - acc: 0.5964\n",
"768/768 [==============================] - 0s 93us/step - loss: 5.7296 - acc: 0.6250\n",
"Epoch 5/10\n",
"768/768 [==============================] - 0s 101us/step - loss: 1.2971 - acc: 0.5859\n",
"768/768 [==============================] - 0s 100us/step - loss: 5.6708 - acc: 0.6302\n",
"Epoch 6/10\n",
"768/768 [==============================] - 0s 96us/step - loss: 0.9998 - acc: 0.6107\n",
"768/768 [==============================] - 0s 100us/step - loss: 5.6364 - acc: 0.6224\n",
"Epoch 7/10\n",
"768/768 [==============================] - 0s 97us/step - loss: 0.9333 - acc: 0.6341\n",
"768/768 [==============================] - 0s 94us/step - loss: 5.6071 - acc: 0.6237\n",
"Epoch 8/10\n",
"768/768 [==============================] - 0s 101us/step - loss: 0.8510 - acc: 0.6445\n",
"768/768 [==============================] - 0s 99us/step - loss: 5.4702 - acc: 0.6341\n",
"Epoch 9/10\n",
"768/768 [==============================] - 0s 104us/step - loss: 0.8148 - acc: 0.6497\n",
"768/768 [==============================] - 0s 90us/step - loss: 5.4395 - acc: 0.6354\n",
"Epoch 10/10\n",
"768/768 [==============================] - 0s 98us/step - loss: 0.7673 - acc: 0.6406\n"
"768/768 [==============================] - 0s 98us/step - loss: 5.4271 - acc: 0.6250\n"
],
"name": "stdout"
},
@@ -231,7 +244,7 @@
"output_type": "execute_result",
"data": {
"text/plain": [
"<keras.callbacks.History at 0x7f848c603c50>"
"<keras.callbacks.History at 0x7f487eaccb70>"
]
},
"metadata": {
@@ -249,7 +262,7 @@
"base_uri": "https://localhost:8080/",
"height": 71
},
"outputId": "e70237f1-8cd2-4667-bc25-a4bdae879518"
"outputId": "c195367a-9ccb-4442-9123-09d7ab511c68"
},
"cell_type": "code",
"source": [
@@ -262,9 +275,9 @@
{
"output_type": "stream",
"text": [
"768/768 [==============================] - 0s 62us/step\n",
"768/768 [==============================] - 0s 51us/step\n",
"\n",
"acc: 62.11%\n"
"acc: 62.37%\n"
],
"name": "stdout"
}
@@ -288,7 +301,7 @@
"base_uri": "https://localhost:8080/",
"height": 323
},
"outputId": "3b69713d-7233-4e4e-f88d-17d4f555659d"
"outputId": "d9e6df06-00a9-433a-d1ed-dd291be53922"
},
"cell_type": "code",
"source": [
@@ -300,9 +313,9 @@
"\n",
"# evaluate the model\n",
"scores = model_1.evaluate(x, y)\n",
"print(\"\\n%s: %.2f%%\" % (model.metrics_names[1], scores[1]*100))"
"print(\"\\n%s: %.2f%%\" % (model_1.metrics_names[1], scores[1]*100))"
],
"execution_count": 13,
"execution_count": 8,
"outputs": [
{
"output_type": "stream",
@@ -321,9 +334,9 @@
"Non-trainable params: 0\n",
"_________________________________________________________________\n",
"None\n",
"768/768 [==============================] - 0s 210us/step\n",
"768/768 [==============================] - 0s 66us/step\n",
"\n",
"acc: 62.11%\n"
"acc: 62.37%\n"
],
"name": "stdout"
}
@@ -336,7 +349,7 @@
},
"cell_type": "markdown",
"source": [
"## Save - Load weights and architecture"
"## Save - Load weights and architecture [JSON format]"
]
},
{
@@ -367,7 +380,7 @@
"base_uri": "https://localhost:8080/",
"height": 323
},
"outputId": "f66defda-df92-4401-e950-b376229ff129"
"outputId": "1ad319f4-c648-41a7-89fd-5bc00b6aefec"
},
"cell_type": "code",
"source": [
@@ -388,9 +401,9 @@
"\n",
"# evaluate the model\n",
"scores = model_2.evaluate(x, y)\n",
"print(\"\\n%s: %.2f%%\" % (model.metrics_names[1], scores[1]*100))"
"print(\"\\n%s: %.2f%%\" % (model_2.metrics_names[1], scores[1]*100))"
],
"execution_count": 20,
"execution_count": 10,
"outputs": [
{
"output_type": "stream",
@@ -409,9 +422,100 @@
"Non-trainable params: 0\n",
"_________________________________________________________________\n",
"None\n",
"768/768 [==============================] - 0s 176us/step\n",
"768/768 [==============================] - 0s 70us/step\n",
"\n",
"acc: 62.11%\n"
"acc: 62.37%\n"
],
"name": "stdout"
}
]
},
{
"metadata": {
"id": "m51EU-kxJNVK",
"colab_type": "text"
},
"cell_type": "markdown",
"source": [
"## Save - Load weights and architecture [YAML format]"
]
},
{
"metadata": {
"id": "v6K6YsraJQwj",
"colab_type": "code",
"colab": {}
},
"cell_type": "code",
"source": [
"from keras.models import model_from_yaml\n",
"\n",
"# serialize model to YAML\n",
"model_yaml = model.to_yaml()\n",
"\n",
"with open(\"model.yaml\", \"w\") as yaml_file:\n",
" yaml_file.write(model_yaml)\n",
" \n",
"# serialize weights to HDF5\n",
"model.save_weights(\"model_weights.h5\")"
],
"execution_count": 0,
"outputs": []
},
{
"metadata": {
"id": "PWZx55j7JS8L",
"colab_type": "code",
"colab": {
"base_uri": "https://localhost:8080/",
"height": 323
},
"outputId": "4fe9725f-1cc2-4692-b187-b47a3a92158d"
},
"cell_type": "code",
"source": [
"# load YAML and create model\n",
"yaml_file = open('model.yaml', 'r')\n",
"\n",
"with open(\"model.yaml\", \"r\") as yaml_file:\n",
" loaded_model_yaml = yaml_file.read()\n",
"\n",
"model_3 = model_from_yaml(loaded_model_yaml)\n",
"\n",
"# compile model\n",
"model_3.compile(loss='binary_crossentropy', optimizer='adam', metrics=['accuracy'])\n",
"\n",
"# load weights into new model\n",
"model_3.load_weights(\"model_weights.h5\")\n",
"\n",
"print(model_3.summary())\n",
"\n",
"# evaluate the model\n",
"scores = model_3.evaluate(x, y)\n",
"print(\"\\n%s: %.2f%%\" % (model_3.metrics_names[1], scores[1]*100))"
],
"execution_count": 12,
"outputs": [
{
"output_type": "stream",
"text": [
"_________________________________________________________________\n",
"Layer (type) Output Shape Param # \n",
"=================================================================\n",
"INPUT (InputLayer) (None, 8) 0 \n",
"_________________________________________________________________\n",
"DENSE1 (Dense) (None, 8) 72 \n",
"_________________________________________________________________\n",
"OUTPUT (Dense) (None, 1) 9 \n",
"=================================================================\n",
"Total params: 81\n",
"Trainable params: 81\n",
"Non-trainable params: 0\n",
"_________________________________________________________________\n",
"None\n",
"768/768 [==============================] - 0s 75us/step\n",
"\n",
"acc: 62.37%\n"
],
"name": "stdout"
}