{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# First Deep Learning Model" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "import numpy as np\n", "%matplotlib inline\n", "import matplotlib.pyplot as plt" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "from sklearn.datasets import make_circles" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "X, y = make_circles(n_samples=1000,\n", " noise=0.1,\n", " factor=0.2,\n", " random_state=0)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "X" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "X.shape" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "plt.figure(figsize=(5, 5))\n", "plt.plot(X[y==0, 0], X[y==0, 1], 'ob', alpha=0.5)\n", "plt.plot(X[y==1, 0], X[y==1, 1], 'xr', alpha=0.5)\n", "plt.xlim(-1.5, 1.5)\n", "plt.ylim(-1.5, 1.5)\n", "plt.legend(['0', '1'])\n", "plt.title(\"Blue circles and Red crosses\")" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "from tensorflow.keras.models import Sequential\n", "from tensorflow.keras.layers import Dense\n", "from tensorflow.keras.optimizers import SGD" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "model = Sequential()" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "model.add(Dense(4, input_shape=(2,), activation='tanh'))" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "model.add(Dense(1, activation='sigmoid'))" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "model.compile(SGD(learning_rate=0.5), 'binary_crossentropy', metrics=['accuracy'])" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "model.fit(X, y, epochs=20)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "hticks = np.linspace(-1.5, 1.5, 101)\n", "vticks = np.linspace(-1.5, 1.5, 101)\n", "aa, bb = np.meshgrid(hticks, vticks)\n", "ab = np.c_[aa.ravel(), bb.ravel()]\n", "c = model.predict(ab)\n", "cc = c.reshape(aa.shape)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "plt.figure(figsize=(5, 5))\n", "plt.contourf(aa, bb, cc, cmap='bwr', alpha=0.2)\n", "plt.plot(X[y==0, 0], X[y==0, 1], 'ob', alpha=0.5)\n", "plt.plot(X[y==1, 0], X[y==1, 1], 'xr', alpha=0.5)\n", "plt.xlim(-1.5, 1.5)\n", "plt.ylim(-1.5, 1.5)\n", "plt.legend(['0', '1'])\n", "plt.title(\"Blue circles and Red crosses\")" ] }, { "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.10" } }, "nbformat": 4, "nbformat_minor": 2 }