diff --git a/your-code/bonus.ipynb b/your-code/bonus.ipynb index 92e4a72..9f79474 100644 --- a/your-code/bonus.ipynb +++ b/your-code/bonus.ipynb @@ -144,7 +144,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.7.3" + "version": "3.8.2" } }, "nbformat": 4, diff --git a/your-code/challenge-1.ipynb b/your-code/challenge-1.ipynb index 2487c5f..ae4dcf9 100644 --- a/your-code/challenge-1.ipynb +++ b/your-code/challenge-1.ipynb @@ -34,11 +34,346 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 1, + "metadata": {}, + "outputs": [ + { + "data": { + "text/html": [ + "
\n", + "\n", + "\n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + "
TLTMTRMLMMMRBLBMBRclass
0xxxxooxooTrue
1xxxxoooxoTrue
2xxxxooooxTrue
3xxxxooobbTrue
4xxxxoobobTrue
\n", + "
" + ], + "text/plain": [ + " TL TM TR ML MM MR BL BM BR class\n", + "0 x x x x o o x o o True\n", + "1 x x x x o o o x o True\n", + "2 x x x x o o o o x True\n", + "3 x x x x o o o b b True\n", + "4 x x x x o o b o b True" + ] + }, + "metadata": {}, + "output_type": "display_data" + }, + { + "data": { + "text/html": [ + "
\n", + "\n", + "\n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + "
TLTMTRMLMMMRBLBMBRclass
953oxxxoooxxFalse
954oxoxxoxoxFalse
955oxoxoxxoxFalse
956oxooxxxoxFalse
957ooxxxooxxFalse
\n", + "
" + ], + "text/plain": [ + " TL TM TR ML MM MR BL BM BR class\n", + "953 o x x x o o o x x False\n", + "954 o x o x x o x o x False\n", + "955 o x o x o x x o x False\n", + "956 o x o o x x x o x False\n", + "957 o o x x x o o x x False" + ] + }, + "metadata": {}, + "output_type": "display_data" + }, + { + "data": { + "text/plain": [ + "TL 0\n", + "TM 0\n", + "TR 0\n", + "ML 0\n", + "MM 0\n", + "MR 0\n", + "BL 0\n", + "BM 0\n", + "BR 0\n", + "class 0\n", + "dtype: int64" + ] + }, + "metadata": {}, + "output_type": "display_data" + }, + { + "data": { + "text/plain": [ + "TL object\n", + "TM object\n", + "TR object\n", + "ML object\n", + "MM object\n", + "MR object\n", + "BL object\n", + "BM object\n", + "BR object\n", + "class bool\n", + "dtype: object" + ] + }, + "metadata": {}, + "output_type": "display_data" + }, + { + "data": { + "text/plain": [ + "array(['x', 'o', 'b'], dtype=object)" + ] + }, + "execution_count": 1, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "# your code here\n", + "import pandas as pd\n", + "\n", + "ttt = pd.read_csv('tic-tac-toe.csv')\n", + "display(ttt.head())\n", + "display(ttt.tail())\n", + "display(ttt.isnull().sum())\n", + "display(ttt.dtypes)\n", + "ttt['TL'].unique()" + ] + }, + { + "cell_type": "code", + "execution_count": 2, + "metadata": {}, + "outputs": [ + { + "name": "stderr", + "output_type": "stream", + "text": [ + ":4: SettingWithCopyWarning: \n", + "A value is trying to be set on a copy of a slice from a DataFrame\n", + "\n", + "See the caveats in the documentation: https://pandas.pydata.org/pandas-docs/stable/user_guide/indexing.html#returning-a-view-versus-a-copy\n", + " ttt[col][ttt[col]=='x'] = 1\n", + ":5: SettingWithCopyWarning: \n", + "A value is trying to be set on a copy of a slice from a DataFrame\n", + "\n", + "See the caveats in the documentation: https://pandas.pydata.org/pandas-docs/stable/user_guide/indexing.html#returning-a-view-versus-a-copy\n", + " ttt[col][ttt[col]=='b'] = 0.5\n", + ":6: SettingWithCopyWarning: \n", + "A value is trying to be set on a copy of a slice from a DataFrame\n", + "\n", + "See the caveats in the documentation: https://pandas.pydata.org/pandas-docs/stable/user_guide/indexing.html#returning-a-view-versus-a-copy\n", + " ttt[col][ttt[col]=='o'] = 0\n" + ] + } + ], + "source": [ + "ttt['class'] = ttt['class'].astype(int)\n", + "columns = ttt.columns[:-1]\n", + "for col in columns:\n", + " ttt[col][ttt[col]=='x'] = 1\n", + " ttt[col][ttt[col]=='b'] = 0.5\n", + " ttt[col][ttt[col]=='o'] = 0\n", + " ttt[col] = ttt[col].astype(float)" + ] + }, + { + "cell_type": "code", + "execution_count": 3, "metadata": {}, "outputs": [], "source": [ - "# your code here" + "ttt_input = ttt.drop(columns=['class'])\n", + "# already normalized!\n", + "ttt_output = ttt['class']" ] }, { @@ -60,11 +395,137 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 4, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Epoch 1/25\n", + "WARNING:tensorflow:Layer dense is casting an input tensor from dtype float64 to the layer's dtype of float32, which is new behavior in TensorFlow 2. The layer has dtype float32 because its dtype defaults to floatx.\n", + "\n", + "If you intended to run this layer in float32, you can safely ignore this warning. If in doubt, this warning is likely only an issue if you are porting a TensorFlow 1.X model to TensorFlow 2.\n", + "\n", + "To change all layers to have dtype float64 by default, call `tf.keras.backend.set_floatx('float64')`. To change just this layer, pass dtype='float64' to the layer constructor. If you are the author of this layer, you can disable autocasting by passing autocast=False to the base Layer constructor.\n", + "\n", + "23/23 [==============================] - 0s 844us/step - loss: 0.7066 - accuracy: 0.4819\n", + "Epoch 2/25\n", + "23/23 [==============================] - 0s 746us/step - loss: 0.6445 - accuracy: 0.6560\n", + "Epoch 3/25\n", + "23/23 [==============================] - 0s 715us/step - loss: 0.6225 - accuracy: 0.6560\n", + "Epoch 4/25\n", + "23/23 [==============================] - 0s 615us/step - loss: 0.6067 - accuracy: 0.6560\n", + "Epoch 5/25\n", + "23/23 [==============================] - 0s 649us/step - loss: 0.5917 - accuracy: 0.6616\n", + "Epoch 6/25\n", + "23/23 [==============================] - 0s 802us/step - loss: 0.5759 - accuracy: 0.6866\n", + "Epoch 7/25\n", + "23/23 [==============================] - 0s 891us/step - loss: 0.5608 - accuracy: 0.7075\n", + "Epoch 8/25\n", + "23/23 [==============================] - 0s 768us/step - loss: 0.5462 - accuracy: 0.7298\n", + "Epoch 9/25\n", + "23/23 [==============================] - 0s 679us/step - loss: 0.5328 - accuracy: 0.7396\n", + "Epoch 10/25\n", + "23/23 [==============================] - 0s 720us/step - loss: 0.5197 - accuracy: 0.7479\n", + "Epoch 11/25\n", + "23/23 [==============================] - 0s 808us/step - loss: 0.5056 - accuracy: 0.7563\n", + "Epoch 12/25\n", + "23/23 [==============================] - 0s 794us/step - loss: 0.4931 - accuracy: 0.7577\n", + "Epoch 13/25\n", + "23/23 [==============================] - 0s 818us/step - loss: 0.4835 - accuracy: 0.7660\n", + "Epoch 14/25\n", + "23/23 [==============================] - 0s 819us/step - loss: 0.4643 - accuracy: 0.7772\n", + "Epoch 15/25\n", + "23/23 [==============================] - 0s 928us/step - loss: 0.4480 - accuracy: 0.7855\n", + "Epoch 16/25\n", + "23/23 [==============================] - 0s 820us/step - loss: 0.4317 - accuracy: 0.7925\n", + "Epoch 17/25\n", + "23/23 [==============================] - 0s 825us/step - loss: 0.4201 - accuracy: 0.8064\n", + "Epoch 18/25\n", + "23/23 [==============================] - 0s 767us/step - loss: 0.4000 - accuracy: 0.8245\n", + "Epoch 19/25\n", + "23/23 [==============================] - 0s 780us/step - loss: 0.3825 - accuracy: 0.8370\n", + "Epoch 20/25\n", + "23/23 [==============================] - 0s 792us/step - loss: 0.3652 - accuracy: 0.8426\n", + "Epoch 21/25\n", + "23/23 [==============================] - 0s 781us/step - loss: 0.3463 - accuracy: 0.8719\n", + "Epoch 22/25\n", + "23/23 [==============================] - 0s 692us/step - loss: 0.3280 - accuracy: 0.8886\n", + "Epoch 23/25\n", + "23/23 [==============================] - 0s 735us/step - loss: 0.3091 - accuracy: 0.9011\n", + "Epoch 24/25\n", + "23/23 [==============================] - 0s 811us/step - loss: 0.2927 - accuracy: 0.9109\n", + "Epoch 25/25\n", + "23/23 [==============================] - 0s 737us/step - loss: 0.2723 - accuracy: 0.9262\n" + ] + }, + { + "data": { + "text/plain": [ + "" + ] + }, + "execution_count": 4, + "metadata": {}, + "output_type": "execute_result" + } + ], "source": [ - "# your code here" + "# your code here\n", + "import tensorflow as tf\n", + "from sklearn.model_selection import train_test_split\n", + "\n", + "# split training and test data\n", + "X_train, X_test, y_train, y_test = train_test_split(ttt_input, ttt_output, test_size=0.25, random_state = 1)\n", + "\n", + "# create model\n", + "model = tf.keras.models.Sequential()\n", + "\n", + "# add layers to the model\n", + "model.add(tf.keras.layers.Dense(32, activation=tf.nn.relu))\n", + "model.add(tf.keras.layers.Dense(32, activation=tf.nn.relu))\n", + "model.add(tf.keras.layers.Dense(2, activation=tf.nn.softmax))\n", + "\n", + "# compile the model\n", + "model.compile(optimizer='adam',\n", + " loss='sparse_categorical_crossentropy',\n", + " metrics=['accuracy'])\n", + "\n", + "# fit the training data\n", + "model.fit(X_train, y_train, epochs=25)\n", + "\n", + "\n" + ] + }, + { + "cell_type": "code", + "execution_count": 5, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "8/8 [==============================] - 0s 796us/step - loss: 0.2768 - accuracy: 0.9375\n", + "0.27675187587738037 0.9375\n", + "WARNING:tensorflow:From /Users/uli/anaconda3/envs/ironhack/lib/python3.8/site-packages/tensorflow/python/training/tracking/tracking.py:111: Model.state_updates (from tensorflow.python.keras.engine.training) is deprecated and will be removed in a future version.\n", + "Instructions for updating:\n", + "This property should not be used in TensorFlow 2.0, as updates are applied automatically.\n", + "WARNING:tensorflow:From /Users/uli/anaconda3/envs/ironhack/lib/python3.8/site-packages/tensorflow/python/training/tracking/tracking.py:111: Layer.updates (from tensorflow.python.keras.engine.base_layer) is deprecated and will be removed in a future version.\n", + "Instructions for updating:\n", + "This property should not be used in TensorFlow 2.0, as updates are applied automatically.\n", + "INFO:tensorflow:Assets written to: tic-tac-toe.model/assets\n" + ] + } + ], + "source": [ + "# evaluate\n", + "val_loss, val_acc = model.evaluate(X_test, y_test)\n", + "print(val_loss, val_acc)\n", + "\n", + "# save the model\n", + "model.save('tic-tac-toe.model')" ] }, { @@ -78,11 +539,28 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 6, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "[1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, 1, 1, 0, 0, 1, 0, 0, 1]\n", + "[1 0 1 1 1 1 0 1 1 1 1 1 1 1 0 0 1 0 0 1]\n" + ] + } + ], "source": [ - "# your code here" + "# your code here\n", + "import numpy as np\n", + "new_model = tf.keras.models.load_model('tic-tac-toe.model')\n", + "predictions = new_model.predict([X_test])\n", + "pred_list = []\n", + "for i in range(0,20):\n", + " pred_list.append(np.argmax(predictions[i]))\n", + "print(pred_list)\n", + "print(y_test[:20].values)" ] }, { @@ -104,11 +582,79 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 7, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Epoch 1/25\n", + "23/23 [==============================] - 0s 894us/step - loss: 0.2664 - accuracy: 0.9192\n", + "Epoch 2/25\n", + "23/23 [==============================] - 0s 770us/step - loss: 0.2318 - accuracy: 0.9457\n", + "Epoch 3/25\n", + "23/23 [==============================] - 0s 1ms/step - loss: 0.2073 - accuracy: 0.9582\n", + "Epoch 4/25\n", + "23/23 [==============================] - 0s 1ms/step - loss: 0.1843 - accuracy: 0.9596\n", + "Epoch 5/25\n", + "23/23 [==============================] - 0s 774us/step - loss: 0.1681 - accuracy: 0.9680\n", + "Epoch 6/25\n", + "23/23 [==============================] - 0s 1ms/step - loss: 0.1548 - accuracy: 0.9708\n", + "Epoch 7/25\n", + "23/23 [==============================] - 0s 1ms/step - loss: 0.1330 - accuracy: 0.9847\n", + "Epoch 8/25\n", + "23/23 [==============================] - 0s 1ms/step - loss: 0.1179 - accuracy: 0.9875\n", + "Epoch 9/25\n", + "23/23 [==============================] - 0s 950us/step - loss: 0.1069 - accuracy: 0.9861\n", + "Epoch 10/25\n", + "23/23 [==============================] - 0s 1ms/step - loss: 0.0981 - accuracy: 0.9875\n", + "Epoch 11/25\n", + "23/23 [==============================] - 0s 937us/step - loss: 0.0894 - accuracy: 0.9889\n", + "Epoch 12/25\n", + "23/23 [==============================] - 0s 929us/step - loss: 0.0827 - accuracy: 0.9916\n", + "Epoch 13/25\n", + "23/23 [==============================] - 0s 959us/step - loss: 0.0738 - accuracy: 0.9930\n", + "Epoch 14/25\n", + "23/23 [==============================] - 0s 761us/step - loss: 0.0693 - accuracy: 0.9930\n", + "Epoch 15/25\n", + "23/23 [==============================] - 0s 945us/step - loss: 0.0619 - accuracy: 0.9930\n", + "Epoch 16/25\n", + "23/23 [==============================] - 0s 960us/step - loss: 0.0560 - accuracy: 0.9944\n", + "Epoch 17/25\n", + "23/23 [==============================] - 0s 949us/step - loss: 0.0522 - accuracy: 0.9958\n", + "Epoch 18/25\n", + "23/23 [==============================] - 0s 1ms/step - loss: 0.0448 - accuracy: 0.9958\n", + "Epoch 19/25\n", + "23/23 [==============================] - 0s 944us/step - loss: 0.0422 - accuracy: 0.9986\n", + "Epoch 20/25\n", + "23/23 [==============================] - 0s 1ms/step - loss: 0.0426 - accuracy: 0.9972\n", + "Epoch 21/25\n", + "23/23 [==============================] - 0s 938us/step - loss: 0.0399 - accuracy: 0.9958\n", + "Epoch 22/25\n", + "23/23 [==============================] - 0s 888us/step - loss: 0.0344 - accuracy: 0.9958\n", + "Epoch 23/25\n", + "23/23 [==============================] - 0s 665us/step - loss: 0.0317 - accuracy: 0.9972\n", + "Epoch 24/25\n", + "23/23 [==============================] - 0s 662us/step - loss: 0.0284 - accuracy: 1.0000\n", + "Epoch 25/25\n", + "23/23 [==============================] - 0s 711us/step - loss: 0.0269 - accuracy: 1.0000\n", + "8/8 [==============================] - 0s 645us/step - loss: 0.0901 - accuracy: 0.9792\n", + "0.09006092697381973 0.9791666865348816\n" + ] + } + ], "source": [ - "# your code here" + "# your code here\n", + "# compile the model - change learning rate from default 0.001 to 0.002\n", + "model.compile(optimizer=tf.keras.optimizers.Adam(learning_rate=0.002),\n", + " loss='sparse_categorical_crossentropy',\n", + " metrics=['accuracy'])\n", + "# fit the training data\n", + "model.fit(X_train, y_train, epochs=25)\n", + "# evaluate\n", + "val_loss, val_acc = model.evaluate(X_test, y_test)\n", + "print(val_loss, val_acc)" ] }, { @@ -120,11 +666,13 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 8, "metadata": {}, "outputs": [], "source": [ - "# your answer here" + "# your answer here\n", + "# just doubling the learning rate to 0.002 improved a lot\n", + "# probably I already had a lot neurons for this small problem?" ] } ], @@ -144,7 +692,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.7.3" + "version": "3.8.2" } }, "nbformat": 4, diff --git a/your-code/challenge-2-screenshot.png b/your-code/challenge-2-screenshot.png new file mode 100644 index 0000000..771d7e1 Binary files /dev/null and b/your-code/challenge-2-screenshot.png differ 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..1cdbda4 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..baa2c29 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..34c8c76 Binary files /dev/null and b/your-code/tic-tac-toe.model/variables/variables.index differ