diff --git a/your-code/challenge-1.ipynb b/your-code/challenge-1.ipynb index 2487c5f..869a07b 100644 --- a/your-code/challenge-1.ipynb +++ b/your-code/challenge-1.ipynb @@ -34,11 +34,57 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 7, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + " TL TM TR ML MM MR BL BM BR class\n", + "0 2 2 2 2 1 1 2 1 1 1\n", + "1 2 2 2 2 1 1 1 2 1 1\n", + "2 2 2 2 2 1 1 1 1 2 1\n", + "3 2 2 2 2 1 1 1 0 0 1\n", + "4 2 2 2 2 1 1 0 1 0 1\n" + ] + } + ], "source": [ - "# your code here" + "# your code here\n", + "\n", + "# import libraries\n", + "import pandas as pd\n", + "from sklearn import preprocessing\n", + "le = preprocessing.LabelEncoder()\n", + "\n", + "# read data\n", + "tictac = pd.read_csv(\"/Users/rickardramhoj/ironhack_assignments/lab-deep-learning/your-code/tic-tac-toe.csv\")\n", + "\n", + "# convert to numerical\n", + "tictac[\"TL\"] = le.fit_transform(tictac[\"TL\"])\n", + "tictac[\"TM\"] = le.fit_transform(tictac[\"TM\"])\n", + "tictac[\"TR\"] = le.fit_transform(tictac[\"TR\"])\n", + "tictac[\"ML\"] = le.fit_transform(tictac[\"ML\"])\n", + "tictac[\"MM\"] = le.fit_transform(tictac[\"MM\"])\n", + "tictac[\"MR\"] = le.fit_transform(tictac[\"MR\"])\n", + "tictac[\"BL\"] = le.fit_transform(tictac[\"BL\"])\n", + "tictac[\"BM\"] = le.fit_transform(tictac[\"BM\"])\n", + "tictac[\"BR\"] = le.fit_transform(tictac[\"BR\"])\n", + "tictac[\"class\"] = le.fit_transform(tictac[\"class\"])\n", + "\n", + "# define input\n", + "features = tictac.drop([\"class\"], axis=1)\n", + "\n", + "# define output\n", + "target = tictac[\"class\"]\n", + "\n", + "# normalize input\n", + "scaler = preprocessing.StandardScaler()\n", + "features = scaler.fit_transform(features)\n", + "\n", + "# print head\n", + "print(tictac.head())\n" ] }, { @@ -60,11 +106,72 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 11, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Epoch 1/3\n", + "23/23 [==============================] - 3s 25ms/step - loss: 0.6181 - accuracy: 0.6630\n", + "Epoch 2/3\n", + "23/23 [==============================] - 0s 21ms/step - loss: 0.5508 - accuracy: 0.7312\n", + "Epoch 3/3\n", + "23/23 [==============================] - 0s 12ms/step - loss: 0.5080 - accuracy: 0.7855 0s - loss: 0.5403 - accuracy: \n", + "8/8 [==============================] - 0s 3ms/step - loss: 0.5051 - accuracy: 0.7542\n", + "0.5050691366195679\n", + "0.7541666626930237\n" + ] + }, + { + "name": "stderr", + "output_type": "stream", + "text": [ + "2021-09-28 21:11:03.580642: W tensorflow/python/util/util.cc:348] Sets are not currently considered sequences, but this may change in the future, so consider avoiding using them.\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "text": [ + "INFO:tensorflow:Assets written to: tic-tac-toe.model/assets\n" + ] + } + ], "source": [ - "# your code here" + "# your code here\n", + "\n", + "# import libraries\n", + "from sklearn.model_selection import train_test_split\n", + "import tensorflow as tf\n", + "\n", + "# split into train and test\n", + "X_train, X_test, y_train, y_test = train_test_split(features, target, random_state=1)\n", + "\n", + "# define model\n", + "model = tf.keras.models.Sequential()\n", + "\n", + "# add layers\n", + "model.add(tf.keras.layers.Dense(128, activation=tf.nn.relu))\n", + "model.add(tf.keras.layers.Dense(128, activation=tf.nn.relu))\n", + "model.add(tf.keras.layers.Dense(2, activation=tf.nn.softmax))\n", + "\n", + "# compile model\n", + "model.compile(optimizer='adam',\n", + " loss='sparse_categorical_crossentropy',\n", + " metrics=['accuracy'])\n", + "\n", + "# fit model\n", + "model.fit(X_train, y_train, epochs=3)\n", + "\n", + "# evaluate model\n", + "val_loss, val_acc = model.evaluate(X_test, y_test)\n", + "print(val_loss)\n", + "print(val_acc)\n", + "\n", + "# save model\n", + "model.save('tic-tac-toe.model')" ] }, { @@ -78,11 +185,39 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 12, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "[[0.35851267 0.64148736]\n", + " [0.25878274 0.7412172 ]\n", + " [0.2545635 0.74543643]\n", + " [0.22912289 0.7708771 ]\n", + " [0.29479566 0.70520437]]\n", + "241 1\n", + "849 0\n", + "436 1\n", + "386 1\n", + "345 1\n", + "Name: class, dtype: int64\n" + ] + } + ], "source": [ - "# your code here" + "# your code here\n", + "\n", + "# load model\n", + "model = tf.keras.models.load_model('tic-tac-toe.model')\n", + "\n", + "# make predictions\n", + "predictions = model.predict(X_test[:5])\n", + "\n", + "# compare predictions with true values\n", + "print(predictions)\n", + "print(y_test[:5])\n" ] }, { @@ -104,11 +239,59 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 24, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Epoch 1/5\n", + "23/23 [==============================] - 1s 5ms/step - loss: 0.6424 - accuracy: 0.6435\n", + "Epoch 2/5\n", + "23/23 [==============================] - 0s 3ms/step - loss: 0.5486 - accuracy: 0.7159\n", + "Epoch 3/5\n", + "23/23 [==============================] - 0s 4ms/step - loss: 0.4277 - accuracy: 0.8148\n", + "Epoch 4/5\n", + "23/23 [==============================] - 0s 6ms/step - loss: 0.3145 - accuracy: 0.8677\n", + "Epoch 5/5\n", + "23/23 [==============================] - 0s 5ms/step - loss: 0.1806 - accuracy: 0.9415\n", + "8/8 [==============================] - 0s 3ms/step - loss: 0.2420 - accuracy: 0.8958\n", + "0.2419986128807068\n", + "0.8958333134651184\n" + ] + } + ], "source": [ - "# your code here" + "# your code here\n", + "\n", + "# define model\n", + "model = tf.keras.models.Sequential()\n", + "\n", + "# add layers\n", + "model.add(tf.keras.layers.Dense(128, activation=tf.nn.relu))\n", + "model.add(tf.keras.layers.Dense(128, activation=tf.nn.relu))\n", + "model.add(tf.keras.layers.Dense(128, activation=tf.nn.relu))\n", + "model.add(tf.keras.layers.Dense(128, activation=tf.nn.relu))\n", + "model.add(tf.keras.layers.Dense(128, activation=tf.nn.relu))\n", + "model.add(tf.keras.layers.Dense(2, activation=tf.nn.softmax))\n", + "\n", + "opt = tf.keras.optimizers.Adam(\n", + " learning_rate=0.001, beta_1=0.9, beta_2=0.999, epsilon=1e-07, amsgrad=False,\n", + " name='Adam'\n", + ")\n", + "# compile model\n", + "model.compile(optimizer=opt,\n", + " loss='sparse_categorical_crossentropy',\n", + " metrics=['accuracy'])\n", + "\n", + "# fit model\n", + "model.fit(X_train, y_train, epochs=5)\n", + "\n", + "# evaluate model\n", + "val_loss, val_acc = model.evaluate(X_test, y_test)\n", + "print(val_loss)\n", + "print(val_acc)" ] }, { @@ -120,17 +303,20 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 21, "metadata": {}, "outputs": [], "source": [ - "# your answer here" + "# your answer here\n", + "\n", + "# Adding more epochs increased the score, but leads to overfitting if too many are used. \n", + "# Adding extra layers also increased the score, as well as changing the learning rate. " ] } ], "metadata": { "kernelspec": { - "display_name": "Python 3", + "display_name": "Python 3 (ipykernel)", "language": "python", "name": "python3" }, @@ -144,7 +330,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.7.3" + "version": "3.7.6" } }, "nbformat": 4, diff --git a/your-code/challenge2_solutions.png b/your-code/challenge2_solutions.png new file mode 100644 index 0000000..3659d3d Binary files /dev/null and b/your-code/challenge2_solutions.png differ diff --git a/your-code/tic-tac-toe.model/keras_metadata.pb b/your-code/tic-tac-toe.model/keras_metadata.pb new file mode 100644 index 0000000..421166e --- /dev/null +++ b/your-code/tic-tac-toe.model/keras_metadata.pb @@ -0,0 +1,7 @@ + +%root"_tf_keras_sequential*${"name": "sequential_3", "trainable": true, "expects_training_arg": true, "dtype": "float32", "batch_input_shape": null, "must_restore_from_config": false, "class_name": "Sequential", "config": {"name": "sequential_3", "layers": [{"class_name": "InputLayer", "config": {"batch_input_shape": {"class_name": "__tuple__", "items": [null, 9]}, "dtype": "float32", "sparse": false, "ragged": false, "name": "dense_9_input"}}, {"class_name": "Dense", "config": {"name": "dense_9", "trainable": true, "dtype": "float32", "units": 128, "activation": "relu", "use_bias": true, "kernel_initializer": {"class_name": "GlorotUniform", "config": {"seed": null}}, "bias_initializer": {"class_name": "Zeros", "config": {}}, "kernel_regularizer": null, "bias_regularizer": null, "activity_regularizer": null, "kernel_constraint": null, "bias_constraint": null}}, {"class_name": "Dense", "config": {"name": "dense_10", "trainable": true, "dtype": "float32", "units": 128, "activation": "relu", "use_bias": true, "kernel_initializer": {"class_name": "GlorotUniform", "config": {"seed": null}}, "bias_initializer": {"class_name": "Zeros", "config": {}}, "kernel_regularizer": null, "bias_regularizer": null, "activity_regularizer": null, "kernel_constraint": null, "bias_constraint": null}}, {"class_name": "Dense", "config": {"name": "dense_11", "trainable": true, "dtype": "float32", "units": 2, "activation": "softmax", "use_bias": true, "kernel_initializer": {"class_name": "GlorotUniform", "config": {"seed": null}}, "bias_initializer": {"class_name": "Zeros", "config": {}}, "kernel_regularizer": null, "bias_regularizer": null, "activity_regularizer": null, "kernel_constraint": null, "bias_constraint": null}}]}, "shared_object_id": 10, "input_spec": {"class_name": "InputSpec", "config": {"dtype": null, "shape": null, "ndim": null, "max_ndim": null, "min_ndim": 2, "axes": {"-1": 9}}, "shared_object_id": 11}, "build_input_shape": {"class_name": "TensorShape", "items": [null, 9]}, "is_graph_network": true, "full_save_spec": {"class_name": "__tuple__", "items": [[{"class_name": "TypeSpec", "type_spec": "tf.TensorSpec", "serialized": [{"class_name": "TensorShape", "items": [null, 9]}, "float32", "dense_9_input"]}], {}]}, "save_spec": {"class_name": "TypeSpec", "type_spec": "tf.TensorSpec", "serialized": [{"class_name": "TensorShape", "items": [null, 9]}, "float32", "dense_9_input"]}, "keras_version": "2.6.0", "backend": "tensorflow", "model_config": {"class_name": "Sequential", "config": {"name": "sequential_3", "layers": [{"class_name": "InputLayer", "config": {"batch_input_shape": {"class_name": "__tuple__", "items": [null, 9]}, "dtype": "float32", "sparse": false, "ragged": false, "name": "dense_9_input"}, "shared_object_id": 0}, {"class_name": "Dense", "config": {"name": "dense_9", "trainable": true, "dtype": "float32", "units": 128, "activation": "relu", "use_bias": true, "kernel_initializer": {"class_name": "GlorotUniform", "config": {"seed": null}, "shared_object_id": 1}, "bias_initializer": {"class_name": "Zeros", "config": {}, "shared_object_id": 2}, "kernel_regularizer": null, "bias_regularizer": null, "activity_regularizer": null, "kernel_constraint": null, "bias_constraint": null}, "shared_object_id": 3}, {"class_name": "Dense", "config": {"name": "dense_10", "trainable": true, "dtype": "float32", "units": 128, "activation": "relu", "use_bias": true, "kernel_initializer": {"class_name": "GlorotUniform", "config": {"seed": null}, "shared_object_id": 4}, "bias_initializer": {"class_name": "Zeros", "config": {}, "shared_object_id": 5}, "kernel_regularizer": null, "bias_regularizer": null, "activity_regularizer": null, "kernel_constraint": null, "bias_constraint": null}, "shared_object_id": 6}, {"class_name": "Dense", "config": {"name": "dense_11", "trainable": true, "dtype": "float32", "units": 2, "activation": "softmax", "use_bias": true, "kernel_initializer": {"class_name": "GlorotUniform", "config": {"seed": null}, "shared_object_id": 7}, "bias_initializer": {"class_name": "Zeros", "config": {}, "shared_object_id": 8}, "kernel_regularizer": null, "bias_regularizer": null, "activity_regularizer": null, "kernel_constraint": null, "bias_constraint": null}, "shared_object_id": 9}]}}, "training_config": {"loss": "sparse_categorical_crossentropy", "metrics": [[{"class_name": "MeanMetricWrapper", "config": {"name": "accuracy", "dtype": "float32", "fn": "sparse_categorical_accuracy"}, "shared_object_id": 12}]], "weighted_metrics": null, "loss_weights": null, "optimizer_config": {"class_name": "Adam", "config": {"name": "Adam", "learning_rate": 0.0010000000474974513, "decay": 0.0, "beta_1": 0.8999999761581421, "beta_2": 0.9990000128746033, "epsilon": 1e-07, "amsgrad": false}}}}2 +root.layer_with_weights-0"_tf_keras_layer*{"name": "dense_9", "trainable": true, "expects_training_arg": false, "dtype": "float32", "batch_input_shape": null, "stateful": false, "must_restore_from_config": false, "class_name": "Dense", "config": {"name": "dense_9", "trainable": true, "dtype": "float32", "units": 128, "activation": "relu", "use_bias": true, "kernel_initializer": {"class_name": "GlorotUniform", "config": {"seed": null}, "shared_object_id": 1}, "bias_initializer": {"class_name": "Zeros", "config": {}, "shared_object_id": 2}, "kernel_regularizer": null, "bias_regularizer": null, "activity_regularizer": null, "kernel_constraint": null, "bias_constraint": null}, "shared_object_id": 3, "input_spec": {"class_name": "InputSpec", "config": {"dtype": null, "shape": null, "ndim": null, "max_ndim": null, "min_ndim": 2, "axes": {"-1": 9}}, "shared_object_id": 11}, "build_input_shape": {"class_name": "TensorShape", "items": [null, 9]}}2 +root.layer_with_weights-1"_tf_keras_layer*{"name": "dense_10", "trainable": true, "expects_training_arg": false, "dtype": "float32", "batch_input_shape": null, "stateful": false, "must_restore_from_config": false, "class_name": "Dense", "config": {"name": "dense_10", "trainable": true, "dtype": "float32", "units": 128, "activation": "relu", "use_bias": true, "kernel_initializer": {"class_name": "GlorotUniform", "config": {"seed": null}, "shared_object_id": 4}, "bias_initializer": {"class_name": "Zeros", "config": {}, "shared_object_id": 5}, "kernel_regularizer": null, "bias_regularizer": null, "activity_regularizer": null, "kernel_constraint": null, "bias_constraint": null}, "shared_object_id": 6, "input_spec": {"class_name": "InputSpec", "config": {"dtype": null, "shape": null, "ndim": null, "max_ndim": null, "min_ndim": 2, "axes": {"-1": 128}}, "shared_object_id": 13}, "build_input_shape": {"class_name": "TensorShape", "items": [null, 128]}}2 +root.layer_with_weights-2"_tf_keras_layer*{"name": "dense_11", "trainable": true, "expects_training_arg": false, "dtype": "float32", "batch_input_shape": null, "stateful": false, "must_restore_from_config": false, "class_name": "Dense", "config": {"name": "dense_11", "trainable": true, "dtype": "float32", "units": 2, "activation": "softmax", "use_bias": true, "kernel_initializer": {"class_name": "GlorotUniform", "config": {"seed": null}, "shared_object_id": 7}, "bias_initializer": {"class_name": "Zeros", "config": {}, "shared_object_id": 8}, "kernel_regularizer": null, "bias_regularizer": null, "activity_regularizer": null, "kernel_constraint": null, "bias_constraint": null}, "shared_object_id": 9, "input_spec": {"class_name": "InputSpec", "config": {"dtype": null, "shape": null, "ndim": null, "max_ndim": null, "min_ndim": 2, "axes": {"-1": 128}}, "shared_object_id": 14}, "build_input_shape": {"class_name": "TensorShape", "items": [null, 128]}}2 +5root.keras_api.metrics.0"_tf_keras_metric*{"class_name": "Mean", "name": "loss", "dtype": "float32", "config": {"name": "loss", "dtype": "float32"}, "shared_object_id": 15}2 +6root.keras_api.metrics.1"_tf_keras_metric*{"class_name": "MeanMetricWrapper", "name": "accuracy", "dtype": "float32", "config": {"name": "accuracy", "dtype": "float32", "fn": "sparse_categorical_accuracy"}, "shared_object_id": 12}2 \ No newline at end of file diff --git a/your-code/tic-tac-toe.model/saved_model.pb b/your-code/tic-tac-toe.model/saved_model.pb new file mode 100644 index 0000000..0ddcfe4 Binary files /dev/null and b/your-code/tic-tac-toe.model/saved_model.pb differ diff --git a/your-code/tic-tac-toe.model/variables/variables.data-00000-of-00001 b/your-code/tic-tac-toe.model/variables/variables.data-00000-of-00001 new file mode 100644 index 0000000..aa684dd Binary files /dev/null and b/your-code/tic-tac-toe.model/variables/variables.data-00000-of-00001 differ diff --git a/your-code/tic-tac-toe.model/variables/variables.index b/your-code/tic-tac-toe.model/variables/variables.index new file mode 100644 index 0000000..0ba66ef Binary files /dev/null and b/your-code/tic-tac-toe.model/variables/variables.index differ