{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Linear regression in keras\n", "\n", "***" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# Neural networks.\n", "import tensorflow.keras as kr\n", "\n", "# Numerical arrays\n", "import numpy as np\n", "\n", "# Data frames.\n", "import pandas as pd\n", "\n", "# Plotting\n", "import matplotlib.pyplot as plt" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# Plot style.\n", "plt.style.use(\"ggplot\")\n", "\n", "# Plot size.\n", "plt.rcParams['figure.figsize'] = [14, 8]" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "
\n", "\n", "#### Fake data\n", "\n", "***" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# Simple linear equation.\n", "f = lambda x: 3.0 * x + 1.0" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# Create a training data frame with x and y values.\n", "# The x values are randomly selected between 0 and 100.\n", "# y_i is f(x_i)\n", "train = pd.DataFrame()\n", "train['x'] = np.random.uniform(0.0, 100.0, 1000)\n", "train['y'] = f(train['x'])\n", "train" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# Create a corresponding test data frame.\n", "# It might be better to create one big data frame and randomly select test cases.\n", "test = pd.DataFrame()\n", "test['x'] = np.random.uniform(0.0, 100.0, 100)\n", "test['y'] = f(test['x'])\n", "test" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# Create a neural network with one neuron.\n", "model = kr.models.Sequential()\n", "model.add(kr.layers.Dense(1, input_shape=(1,), activation=\"linear\", kernel_initializer='ones', bias_initializer='zeros'))\n", "model.compile('adam', loss='mean_squared_error')" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# Train the neural network on our training data.\n", "model.fit(train['x'], train['y'], epochs=500)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# Take four hand-picked values and see their predictions.\n", "model.predict([1.0,2.0,3.0,100.0])" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# See what f says they are.\n", "np.array([[f(i)] for i in [1.0, 2.0, 3.0, 100.0]])" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# Let's plot our predictions of the x values we trained on\n", "plt.plot(test['x'], test['y'], label='actual')\n", "plt.plot(test['x'], model.predict(test['x']), label='prediction')\n", "plt.legend();" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# Evaluate the neural network on the test data.\n", "model.evaluate(test['x'], test['y'])" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "
\n", "\n", "#### Polynomial\n", "\n", "***\n", "\n", "$$ ax^2 + bx + c $$\n", "\n", "
" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# Let's set f to a polynomial instead.\n", "f = lambda x: 2.0 * x**2 + 3.0 * x + 4.0\n", "\n", "poly = pd.DataFrame()\n", "poly['x'] = np.linspace(-10.0, 10.0, 1000)\n", "poly['y'] = poly['x'].apply(f)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# Have a look.\n", "plt.plot(poly['x'], poly['y']);" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# Re-build our model.\n", "model = kr.models.Sequential()\n", "model.add(kr.layers.Dense(1, input_shape=(1,), activation='linear', kernel_initializer=\"ones\", bias_initializer=\"zeros\"))\n", "model.compile('adam', loss='mean_squared_error')" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# Fit the data.\n", "model.fit(poly['x'], poly['y'], epochs=500)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# Plot the predictions (on the training set itself).\n", "plt.plot(poly['x'], poly['y'], label='actual')\n", "plt.plot(poly['x'], model.predict(poly['x']), label='prediction')\n", "plt.legend();" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "
\n", "\n", "#### Sigmoids\n", "\n", "***" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# Change the activation function.\n", "model = kr.models.Sequential()\n", "model.add(kr.layers.Dense(1, input_shape=(1,), activation='sigmoid', kernel_initializer=\"ones\", bias_initializer=\"zeros\"))\n", "model.compile('adam', loss='mean_squared_error')" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# Without training, let's have a look at the output.\n", "sigdata = pd.DataFrame()\n", "sigdata['x'] = np.linspace(-10.0, 10.0, 1000)\n", "sigdata['y'] = model.predict(sigdata['x'])" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# Let's see what that looks like.\n", "plt.plot(sigdata['x'], sigdata['y']);" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "
\n", "\n", "#### Back to the polynomial\n", "\n", "***" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# Same polynomial.\n", "f = lambda x: 2.0 * x**2 + 3.0 * x + 4.0\n", "\n", "poly = pd.DataFrame()\n", "poly['x'] = np.linspace(-10.0, 10.0, 1000)\n", "poly['y'] = poly['x'].apply(f)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# Train a different model.\n", "model = kr.models.Sequential()\n", "model.add(kr.layers.Dense(50, input_shape=(1,), activation='sigmoid', kernel_initializer=\"glorot_uniform\", bias_initializer=\"glorot_uniform\"))\n", "model.add(kr.layers.Dense(1, activation='linear', kernel_initializer=\"glorot_uniform\", bias_initializer=\"glorot_uniform\"))\n", "model.compile(kr.optimizers.Adam(lr=0.001), loss='mean_squared_error')" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# Fit the data.\n", "model.fit(poly['x'], poly['y'], epochs=500, batch_size=10)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# Now let's see.\n", "plt.plot(poly['x'], poly['y'], label='actual')\n", "plt.plot(poly['x'], model.predict(poly['x']), label='prediction')\n", "plt.legend();" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Note that re-running the fitting code will generally give different results." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "***\n", "\n", "#### End" ] } ], "metadata": { "kernelspec": { "display_name": "Python 3 (ipykernel)", "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.9.7" } }, "nbformat": 4, "nbformat_minor": 4 }