-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #15 from jacquelinegarrahan/keras
Keras
- Loading branch information
Showing
9 changed files
with
278 additions
and
59 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,198 @@ | ||
{ | ||
"cells": [ | ||
{ | ||
"cell_type": "markdown", | ||
"metadata": {}, | ||
"source": [ | ||
"# iris example" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": null, | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [ | ||
"from sklearn.datasets import load_iris\n", | ||
"from tensorflow import keras\n", | ||
"import tensorflow as tf\n", | ||
"\n", | ||
"from tensorflow.keras.models import Sequential\n", | ||
"from tensorflow.keras.layers import Dense, Flatten\n", | ||
"from tensorflow.keras.utils import to_categorical\n", | ||
"from sklearn.preprocessing import LabelEncoder\n", | ||
"import pandas as pd\n", | ||
"iris = load_iris()" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": null, | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [ | ||
"iris[\"data\"][0].shape" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": null, | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [ | ||
"data = pd.DataFrame(iris.data, columns=iris.feature_names)\n", | ||
"data.columns = [\"SepalLength\", \"SepalWidth\", \"PetalLength\", \"PetalWidth\"]\n", | ||
"\n", | ||
"data[\"Species\"] = iris.target\n", | ||
"data" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": null, | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [ | ||
"train_dataset = data.sample(frac=0.8,random_state=0)\n", | ||
"test_dataset = data.drop(train_dataset.index)\n", | ||
"train_labels = train_dataset.pop('Species')\n", | ||
"test_labels = test_dataset.pop('Species')\n", | ||
"train_dataset.keys()" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": null, | ||
"metadata": { | ||
"scrolled": true | ||
}, | ||
"outputs": [], | ||
"source": [ | ||
"# encode class values as integers\n", | ||
"encoder = LabelEncoder()\n", | ||
"encoder.fit(train_labels)\n", | ||
"encoded_Y = encoder.transform(train_labels)\n", | ||
"\n", | ||
"# convert integers to dummy variables (i.e. one hot encoded)\n", | ||
"dummy_y = to_categorical(encoded_Y)" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": null, | ||
"metadata": { | ||
"scrolled": true | ||
}, | ||
"outputs": [], | ||
"source": [ | ||
" # define model\n", | ||
"def build_model():\n", | ||
" # create model\n", | ||
" sepal_length_input = keras.Input(shape=(1,), name=\"SepalLength\")\n", | ||
" sepal_width_input = keras.Input(shape=(1,), name=\"SepalWidth\")\n", | ||
" petal_length_input = keras.Input(shape=(1,), name=\"PetalLength\")\n", | ||
" petal_width_input = keras.Input(shape=(1,), name=\"PetalWidth\")\n", | ||
" inputs = [sepal_length_input, sepal_width_input, petal_length_input, petal_width_input]\n", | ||
" merged = keras.layers.concatenate(inputs)\n", | ||
" dense1 = Dense(8, activation='relu')(merged)\n", | ||
" output = Dense(3, activation='softmax', name=\"Species\")(dense1)\n", | ||
"\n", | ||
" # Compile model\n", | ||
" model = keras.Model(inputs=inputs, outputs=[output])\n", | ||
" optimizer = tf.keras.optimizers.Adam()\n", | ||
" model.compile(loss='categorical_crossentropy', optimizer=optimizer, metrics=['accuracy'])\n", | ||
" return model\n", | ||
"\n", | ||
"model = build_model()\n", | ||
"keras.utils.plot_model(model, \"my_first_model_with_shape_info.png\", show_shapes=True)" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": null, | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [ | ||
"train_stats = train_dataset.describe()\n", | ||
"train_stats = train_stats.transpose()\n", | ||
"train_stats" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": null, | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [ | ||
"train_x = train_dataset.to_dict(\"series\")" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": null, | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [ | ||
"early_stop = keras.callbacks.EarlyStopping(monitor='val_loss', patience=15)\n", | ||
"\n", | ||
"history = model.fit(train_x, dummy_y, epochs=1000,\n", | ||
" validation_split = 0.2, verbose=1, callbacks=[early_stop])" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": null, | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [ | ||
"model.save(\"files/iris_model.h5\")" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": null, | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [ | ||
"model.input_names" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": null, | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [ | ||
"model.output_names" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": null, | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [] | ||
} | ||
], | ||
"metadata": { | ||
"kernelspec": { | ||
"display_name": "Python 3", | ||
"language": "python", | ||
"name": "python3" | ||
}, | ||
"language_info": { | ||
"codemirror_mode": { | ||
"name": "ipython", | ||
"version": 3 | ||
}, | ||
"file_extension": ".py", | ||
"mimetype": "text/x-python", | ||
"name": "python", | ||
"nbconvert_exporter": "python", | ||
"pygments_lexer": "ipython3", | ||
"version": "3.7.9" | ||
} | ||
}, | ||
"nbformat": 4, | ||
"nbformat_minor": 4 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.