Created
October 9, 2019 08:24
-
-
Save jkrukowski/59ee41d8522daa7d248ea50daa1886f3 to your computer and use it in GitHub Desktop.
liniear-regression.ipynb
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
{ | |
"nbformat": 4, | |
"nbformat_minor": 0, | |
"metadata": { | |
"colab": { | |
"name": "liniear-regression.ipynb", | |
"provenance": [], | |
"collapsed_sections": [], | |
"include_colab_link": true | |
}, | |
"kernelspec": { | |
"name": "swift", | |
"display_name": "Swift" | |
}, | |
"accelerator": "GPU" | |
}, | |
"cells": [ | |
{ | |
"cell_type": "markdown", | |
"metadata": { | |
"id": "view-in-github", | |
"colab_type": "text" | |
}, | |
"source": [ | |
"<a href=\"https://colab.research.google.com/gist/jkrukowski/59ee41d8522daa7d248ea50daa1886f3/liniear-regression.ipynb\" target=\"_parent\"><img src=\"https://colab.research.google.com/assets/colab-badge.svg\" alt=\"Open In Colab\"/></a>" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": { | |
"id": "CoEfOhVWQI9T", | |
"colab_type": "text" | |
}, | |
"source": [ | |
"# Linear Regression with Swift for TensorFlow" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": { | |
"id": "w9oJuFnoUKuS", | |
"colab_type": "text" | |
}, | |
"source": [ | |
"## Imports\n", | |
"### Import necessary libraries" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"metadata": { | |
"id": "glNzWvo49gIl", | |
"colab_type": "code", | |
"colab": {} | |
}, | |
"source": [ | |
"import TensorFlow\n", | |
"import Python\n", | |
"%include \"EnableIPythonDisplay.swift\"\n", | |
"IPythonDisplay.shell.enable_matplotlib(\"inline\")\n", | |
"let plt = Python.import(\"matplotlib.pyplot\")\n", | |
"let np = Python.import(\"numpy\")" | |
], | |
"execution_count": 0, | |
"outputs": [] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": { | |
"id": "S9aaSkrQVw0u", | |
"colab_type": "text" | |
}, | |
"source": [ | |
"## Data\n", | |
"### Data for linear regression" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"metadata": { | |
"id": "VEHLo6jPCw1i", | |
"colab_type": "code", | |
"colab": {} | |
}, | |
"source": [ | |
"let n = 100\n", | |
"let a: Float = 1.5\n", | |
"let b: Float = 4.0\n", | |
"let x = Tensor<Float>(randomNormal: [n])\n", | |
"let y = x * a + b + Tensor<Float>(randomNormal: [n])" | |
], | |
"execution_count": 0, | |
"outputs": [] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": { | |
"id": "tdNVW4siUNX5", | |
"colab_type": "text" | |
}, | |
"source": [ | |
"### Plotting function\n" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"metadata": { | |
"id": "DS-OLs3aTjvF", | |
"colab_type": "code", | |
"colab": {} | |
}, | |
"source": [ | |
"func plotData(\n", | |
" x: PythonObject, \n", | |
" y: PythonObject, \n", | |
" fitLine: PythonObject\n", | |
") {\n", | |
" plt.plot(x, y, \"yo\", x, fitLine, \"--k\")\n", | |
" plt.show()\n", | |
"}" | |
], | |
"execution_count": 0, | |
"outputs": [] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": { | |
"id": "NFk9Lyd2UyMb", | |
"colab_type": "text" | |
}, | |
"source": [ | |
"## NumPy Linear Regression\n", | |
"### [Docs](https://docs.scipy.org/doc/numpy/reference/generated/numpy.polyfit.html)" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"metadata": { | |
"id": "3cHkRZ0jA8JK", | |
"colab_type": "code", | |
"colab": {} | |
}, | |
"source": [ | |
"func linearRegression(\n", | |
" x: PythonObject, \n", | |
" y: PythonObject\n", | |
") -> PythonObject {\n", | |
" let fit = np.polyfit(x, y, 1)\n", | |
" let poly = np.poly1d(fit)\n", | |
" print(poly)\n", | |
" return poly(x)\n", | |
"}\n", | |
"\n", | |
"let regressionFunction = linearRegression(\n", | |
" x: x.makeNumpyArray(), \n", | |
" y: y.makeNumpyArray()\n", | |
")\n", | |
"\n", | |
"plotData(\n", | |
" x: x.makeNumpyArray(), \n", | |
" y: y.makeNumpyArray(), \n", | |
" fitLine: regressionFunction\n", | |
")" | |
], | |
"execution_count": 0, | |
"outputs": [] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": { | |
"id": "IcSlAmqpXFQ4", | |
"colab_type": "text" | |
}, | |
"source": [ | |
"## Swift for TensorFlow Linear Regression" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": { | |
"id": "hxfk4CYxv39O", | |
"colab_type": "text" | |
}, | |
"source": [ | |
"### Model and Train Function" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"metadata": { | |
"id": "gq2SD-H1v21s", | |
"colab_type": "code", | |
"colab": {} | |
}, | |
"source": [ | |
"struct LiniearModel: Differentiable {\n", | |
" var w: Float\n", | |
" var b: Float\n", | |
"\n", | |
" func applied(to input: Tensor<Float>) -> Tensor<Float> {\n", | |
" return w * input + b\n", | |
" }\n", | |
"}\n", | |
"\n", | |
"func train(\n", | |
" x: Tensor<Float>, \n", | |
" y: Tensor<Float>, \n", | |
" model: inout LiniearModel,\n", | |
" epoch: Int,\n", | |
" lr: Float\n", | |
") {\n", | |
" for _ in 0..<epoch { \n", | |
" let grad = model.gradient { m -> Tensor<Float> in\n", | |
" let predictedY = m.applied(to: x)\n", | |
" return (y - predictedY).squared().mean()\n", | |
" }\n", | |
" model.w -= lr * grad.w\n", | |
" model.b -= lr * grad.b\n", | |
" } \n", | |
"}" | |
], | |
"execution_count": 0, | |
"outputs": [] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": { | |
"id": "7Ob1Qu_jvZ_P", | |
"colab_type": "text" | |
}, | |
"source": [ | |
"### Training Loop" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"metadata": { | |
"id": "yuNumQaFGFIf", | |
"colab_type": "code", | |
"colab": {} | |
}, | |
"source": [ | |
"var model = LiniearModel(w: 0.0, b: 0.0)\n", | |
"train(x: x, y: y, model: &model, epoch: 100, lr: 0.1)\n", | |
"print(model)" | |
], | |
"execution_count": 0, | |
"outputs": [] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": { | |
"id": "c_ap6q_i-Gp0", | |
"colab_type": "text" | |
}, | |
"source": [ | |
"### Plot the result" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"metadata": { | |
"id": "Qqq1qHCvIjHX", | |
"colab_type": "code", | |
"colab": {} | |
}, | |
"source": [ | |
"func swiftLinearRegression(\n", | |
" x: Tensor<Float>, \n", | |
" model: LiniearModel\n", | |
") -> PythonObject {\n", | |
" let result = model.applied(to: x)\n", | |
" return result.makeNumpyArray()\n", | |
"}\n", | |
"\n", | |
"let swiftRegressionFunction = swiftLinearRegression(\n", | |
" x: x, \n", | |
" model: model\n", | |
")\n", | |
"\n", | |
"plotData(\n", | |
" x: x.makeNumpyArray(), \n", | |
" y: y.makeNumpyArray(), \n", | |
" fitLine: swiftRegressionFunction\n", | |
")" | |
], | |
"execution_count": 0, | |
"outputs": [] | |
}, | |
{ | |
"cell_type": "code", | |
"metadata": { | |
"id": "2OEPxOmlC5X4", | |
"colab_type": "code", | |
"colab": {} | |
}, | |
"source": [ | |
"" | |
], | |
"execution_count": 0, | |
"outputs": [] | |
} | |
] | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment