Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions notebooks/dl/dl-cnn-image-classification.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,7 @@
"metadata": {},
"outputs": [],
"source": [
"seaborn.distplot(img_a.flatten())"
"seaborn.histplot(img_a.flatten(), kde=True)\n"
]
},
{
Expand Down Expand Up @@ -205,7 +205,7 @@
"metadata": {},
"outputs": [],
"source": [
"seaborn.distplot(img_a_conv.flatten())"
"seaborn.histplot(img_a_conv.flatten(), kde=True)\n"
]
},
{
Expand Down Expand Up @@ -471,15 +471,15 @@
"\n",
"net = keras.models.Sequential(\n",
" [\n",
" keras.layers.Convolution2D(\n",
" keras.layers.Conv2D(\n",
" filters=n1,\n",
" kernel_size=(3,3),\n",
" activation=\"relu\",\n",
" ),\n",
" keras.layers.MaxPooling2D(\n",
" pool_size=(2,2)\n",
" ),\n",
" keras.layers.Convolution2D(\n",
" keras.layers.Conv2D(\n",
" filters=n2,\n",
" kernel_size=(3,3),\n",
" activation=\"relu\"\n",
Expand Down
7 changes: 5 additions & 2 deletions notebooks/dl/dl-explainable.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -115,13 +115,16 @@
"metadata": {},
"outputs": [],
"source": [
"from tensorflow.keras.utils import load_img, img_to_array\n",
"\n",
"\n",
"def preprocess_image(img_path):\n",
" \"\"\"Preprocess the \"\"\"\n",
" img = keras.preprocessing.image.load_img(\n",
" img = load_img(\n",
" img_path, \n",
" target_size=(299, 299)\n",
" )\n",
" img_array = keras.preprocessing.image.img_to_array(img)\n",
" img_array = img_to_array(img)\n",
" #img_array = numpy.expand_dims(img_array, axis=0)\n",
" img_array = inception_v3.preprocess_input(img_array)\n",
" img_array = img_array / 2 + 0.5 # brighten\n",
Expand Down
26 changes: 15 additions & 11 deletions notebooks/dl/dl-house-price-regression.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -493,12 +493,14 @@
"cell_type": "markdown",
"metadata": {},
"source": [
"This function is now passed to a wrapper class that aims to implement the `sklearn` estimator interface. We can now pass the data in the shape usual for `sklearn` to train the classifier..."
"This function is now passed to a wrapper class that aims to implement the `sklearn` estimator interface.\n",
"\n",
"First, we need to import the `KerasRegressor` from `scikeras`. We can now pass the data in the shape usual for `sklearn` to train the regressor..."
]
},
{
"cell_type": "code",
"execution_count": 17,
"execution_count": null,
"metadata": {},
"outputs": [
{
Expand Down Expand Up @@ -532,15 +534,17 @@
}
],
"source": [
"model = keras.wrappers.scikit_learn.KerasRegressor(\n",
" build_fn=build_regressor_network,\n",
"from scikeras.wrappers import KerasRegressor\n",
"\n",
"model = KerasRegressor(\n",
" model=build_regressor_network,\n",
" epochs=25, \n",
" batch_size=10,\n",
")\n",
"\n",
"history = model.fit(\n",
" x=X_train,\n",
" y=y_train,\n",
" X_train,\n",
" y_train,\n",
" validation_split=0.2\n",
")"
]
Expand All @@ -559,7 +563,7 @@
"cell_type": "markdown",
"metadata": {},
"source": [
"... and ask for class predictions via the `predict` method. However, we get the predictions in the nested array shape that is usual for TensorFlow, so we need to flatten the array to get a 1D-vector."
"... and ask for predictions via the `predict` method:"
]
},
{
Expand All @@ -568,7 +572,7 @@
"metadata": {},
"outputs": [],
"source": [
"y_pred = model.predict(X_test).flatten()"
"y_pred = model.predict(X_test)"
]
},
{
Expand All @@ -577,8 +581,8 @@
"metadata": {},
"outputs": [],
"source": [
"seaborn.distplot(data[target_col])\n",
"seaborn.distplot(y_pred)\n",
"seaborn.histplot(data[target_col], kde=True)\n",
"seaborn.histplot(y_pred, kde=True)\n",
"plt.title(\"distribution of target variable and predictions\")"
]
},
Expand All @@ -588,7 +592,7 @@
"metadata": {},
"outputs": [],
"source": [
"seaborn.distplot(y_pred - y_test)\n",
"seaborn.histplot(y_pred - y_test, kde=True)\n",
"plt.title(\"distribution of error\")"
]
},
Expand Down
22 changes: 13 additions & 9 deletions notebooks/dl/dl-iris-classifier.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -212,7 +212,9 @@
"cell_type": "markdown",
"metadata": {},
"source": [
"This function is now passed to a wrapper class that aims to implement the `sklearn` estimator interface:"
"This function is now passed to a wrapper class that aims to implement the `sklearn` estimator interface.\n",
"\n",
"First, we need to import the `KerasClassifier` from `scikeras`:"
]
},
{
Expand All @@ -221,8 +223,10 @@
"metadata": {},
"outputs": [],
"source": [
"keras.wrappers.scikit_learn.KerasClassifier(\n",
" build_fn=build_binary_classifier,\n",
"from scikeras.wrappers import KerasClassifier\n",
"\n",
"KerasClassifier(\n",
" model=build_binary_classifier,\n",
" epochs=100, \n",
" batch_size=5,\n",
")\n"
Expand All @@ -241,22 +245,22 @@
"metadata": {},
"outputs": [],
"source": [
"model = keras.wrappers.scikit_learn.KerasClassifier(\n",
" build_fn=build_binary_classifier,\n",
"model = KerasClassifier(\n",
" model=build_binary_classifier,\n",
" epochs=20, \n",
" batch_size=5,\n",
")\n",
"model.fit(\n",
" x=X_train,\n",
" y=y_train\n",
" X_train,\n",
" y_train\n",
")"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"... and ask for class predictions via the `predict` method. However, we get the predictions in the nested array shape that is usual for TensorFlow, so we need to flatten the array to get a 1D-vector."
"... and ask for class predictions via the `predict` method:"
]
},
{
Expand All @@ -265,7 +269,7 @@
"metadata": {},
"outputs": [],
"source": [
"y_pred = model.predict(X_test).flatten()"
"y_pred = model.predict(X_test)"
]
},
{
Expand Down
4 changes: 2 additions & 2 deletions notebooks/dl/dl-learning-process.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -237,7 +237,7 @@
"\n",
"net = keras.models.Sequential(\n",
" [\n",
" keras.layers.Convolution2D(\n",
" keras.layers.Conv2D(\n",
" input_shape=input_shape,\n",
" filters=n1,\n",
" kernel_size=(5,5),\n",
Expand All @@ -246,7 +246,7 @@
" keras.layers.MaxPooling2D(\n",
" pool_size=(2,2)\n",
" ),\n",
" keras.layers.Convolution2D(\n",
" keras.layers.Conv2D(\n",
" filters=n2,\n",
" kernel_size=(5,5),\n",
" activation=\"relu\"\n",
Expand Down
9 changes: 5 additions & 4 deletions notebooks/dl/dl-pretrained.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -175,7 +175,8 @@
"metadata": {},
"outputs": [],
"source": [
"img_a = keras.preprocessing.image.img_to_array(img)\n",
"from tensorflow.keras.utils import img_to_array\n",
"img_a = img_to_array(img)\n",
"img_a.shape"
]
},
Expand All @@ -185,7 +186,7 @@
"metadata": {},
"outputs": [],
"source": [
"seaborn.distplot(img_a.flatten())"
"seaborn.histplot(img_a.flatten(), kde=True)\n"
]
},
{
Expand All @@ -211,7 +212,7 @@
"metadata": {},
"outputs": [],
"source": [
"seaborn.distplot(img_a.flatten())"
"seaborn.histplot(img_a.flatten(), kde=True)\n"
]
},
{
Expand Down Expand Up @@ -318,7 +319,7 @@
" response = requests.get(img_url)\n",
" img = PIL.Image.open(io.BytesIO(response.content))\n",
" img = img.resize(inception_dim)\n",
" img_a = keras.preprocessing.image.img_to_array(img)\n",
" img_a = img_to_array(img)\n",
" img_a = inception_v3.preprocess_input(img_a)\n",
" predictions = self.model.predict(\n",
" numpy.array([img_a])\n",
Expand Down
10 changes: 5 additions & 5 deletions notebooks/dl/dl-save-models.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@
"cell_type": "markdown",
"metadata": {},
"source": [
"This method of the Keras model saves the network - including its weights - to a [HDF5](https://en.m.wikipedia.org/wiki/Hierarchical_Data_Format) file:"
"This method of the Keras model saves the network - including its weights - to a file using the native Keras v3 format (`.keras`):"
]
},
{
Expand All @@ -127,7 +127,7 @@
"metadata": {},
"outputs": [],
"source": [
"net.save(\"saved-model.h5\")"
"net.save(\"saved-model.keras\")"
]
},
{
Expand All @@ -136,7 +136,7 @@
"metadata": {},
"outputs": [],
"source": [
"!ls -lah saved-model.h5"
"!ls -lah saved-model.keras"
]
},
{
Expand All @@ -152,7 +152,7 @@
"metadata": {},
"outputs": [],
"source": [
"net_reloaded = keras.models.load_model(\"saved-model.h5\")"
"net_reloaded = keras.models.load_model(\"saved-model.keras\")"
]
},
{
Expand Down Expand Up @@ -232,7 +232,7 @@
"metadata": {},
"outputs": [],
"source": [
"!rm saved-model.h5"
"!rm saved-model.keras"
]
},
{
Expand Down
8 changes: 4 additions & 4 deletions notebooks/exercises/ml-exercise-house-prices.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -929,9 +929,9 @@
"source": [
"if ready:\n",
" predicted_pd = predicted[[\"SalePrice\", \"prediction\"]]\n",
" seaborn.distplot(predicted_pd[\"SalePrice\"])\n",
" seaborn.distplot(predicted_pd[\"prediction\"])\n",
" seaborn.distplot(predicted_pd[\"SalePrice\"] - predicted_pd[\"prediction\"]) "
" seaborn.histplot(predicted_pd[\"SalePrice\"], kde=True)\n",
" seaborn.histplot(predicted_pd[\"prediction\"], kde=True)\n",
" seaborn.histplot(predicted_pd[\"SalePrice\"] - predicted_pd[\"prediction\"], kde=True)\n"
]
},
{
Expand Down Expand Up @@ -964,4 +964,4 @@
},
"nbformat": 4,
"nbformat_minor": 4
}
}
14 changes: 7 additions & 7 deletions notebooks/exercises/spark-exercise-house-prices-solution.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -316,7 +316,7 @@
"metadata": {},
"outputs": [],
"source": [
"seaborn.distplot(predicted_pd[\"SalePrice\"])"
"seaborn.histplot(predicted_pd[\"SalePrice\"], kde=True)\n"
]
},
{
Expand All @@ -325,7 +325,7 @@
"metadata": {},
"outputs": [],
"source": [
"seaborn.distplot(predicted_pd[\"prediction\"])"
"seaborn.histplot(predicted_pd[\"prediction\"], kde=True)\n"
]
},
{
Expand All @@ -334,7 +334,7 @@
"metadata": {},
"outputs": [],
"source": [
"seaborn.distplot(predicted_pd[\"SalePrice\"] - predicted_pd[\"prediction\"])"
"seaborn.histplot(predicted_pd[\"SalePrice\"] - predicted_pd[\"prediction\"], kde=True)\n"
]
},
{
Expand Down Expand Up @@ -914,7 +914,7 @@
"metadata": {},
"outputs": [],
"source": [
"seaborn.distplot(predicted_pd[\"label\"])"
"seaborn.histplot(predicted_pd[\"label\"], kde=True)\n"
]
},
{
Expand All @@ -923,7 +923,7 @@
"metadata": {},
"outputs": [],
"source": [
"seaborn.distplot(predicted_pd[\"prediction\"])"
"seaborn.histplot(predicted_pd[\"prediction\"], kde=True)\n"
]
},
{
Expand All @@ -932,7 +932,7 @@
"metadata": {},
"outputs": [],
"source": [
"seaborn.distplot(predicted_pd[\"label\"] - predicted_pd[\"prediction\"])"
"seaborn.histplot(predicted_pd[\"label\"] - predicted_pd[\"prediction\"], kde=True)\n"
]
},
{
Expand Down Expand Up @@ -965,4 +965,4 @@
},
"nbformat": 4,
"nbformat_minor": 2
}
}
8 changes: 4 additions & 4 deletions notebooks/exercises/spark-exercise-house-prices.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -341,9 +341,9 @@
"source": [
"if ready:\n",
" predicted_pd = predicted[[\"SalePrice\", \"prediction\"]].toPandas()\n",
" seaborn.distplot(predicted_pd[\"SalePrice\"])\n",
" seaborn.distplot(predicted_pd[\"prediction\"])\n",
" seaborn.distplot(predicted_pd[\"SalePrice\"] - predicted_pd[\"prediction\"]) "
" seaborn.histplot(predicted_pd[\"SalePrice\"], kde=True)\n",
" seaborn.histplot(predicted_pd[\"prediction\"], kde=True)\n",
" seaborn.histplot(predicted_pd[\"SalePrice\"] - predicted_pd[\"prediction\"], kde=True)\n"
]
},
{
Expand Down Expand Up @@ -376,4 +376,4 @@
},
"nbformat": 4,
"nbformat_minor": 2
}
}
Loading