Last active
November 27, 2020 23:56
-
-
Save shadaj/72148f7dfe74ae7a898e5d511cff294d to your computer and use it in GitHub Desktop.
ScalaPy TensorFlow.ipynb
This file contains hidden or 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": "ScalaPy TensorFlow.ipynb", | |
"provenance": [], | |
"collapsed_sections": [], | |
"include_colab_link": true | |
}, | |
"kernelspec": { | |
"display_name": "Scala", | |
"language": "scala", | |
"name": "scala" | |
} | |
}, | |
"cells": [ | |
{ | |
"cell_type": "markdown", | |
"metadata": { | |
"id": "view-in-github", | |
"colab_type": "text" | |
}, | |
"source": [ | |
"<a href=\"https://colab.research.google.com/gist/shadaj/72148f7dfe74ae7a898e5d511cff294d/scalapy-tensorflow.ipynb\" target=\"_parent\"><img src=\"https://colab.research.google.com/assets/colab-badge.svg\" alt=\"Open In Colab\"/></a>" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": { | |
"id": "up2G8zDbhsEN" | |
}, | |
"source": [ | |
"# ScalaPy and TensorFlow in Colab - Linear Regression\n", | |
"This Colab notebook demonstrates how to use ScalaPy to do some simple TensorFlow machine learning." | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": { | |
"id": "sL1Vq6r6J6dA" | |
}, | |
"source": [ | |
"## Install the Scala Kernel\n", | |
"If you get a \"scala\" kernel not recognized warning when loading up the notebook for the first time, start by running the two cells below. Once you are done **reload the page** to load the notebook in the installed Scala kernel." | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"metadata": { | |
"id": "RoCcdvTCAqeR" | |
}, | |
"source": [ | |
"%%shell\n", | |
"SCALA_VERSION=2.12.8 ALMOND_VERSION=0.3.0+16-548dc10f-SNAPSHOT\n", | |
"curl -Lo coursier https://git.io/coursier-cli\n", | |
"chmod +x coursier\n", | |
"./coursier bootstrap \\\n", | |
" -r jitpack -r sonatype:snapshots \\\n", | |
" -i user -I user:sh.almond:scala-kernel-api_$SCALA_VERSION:$ALMOND_VERSION \\\n", | |
" sh.almond:scala-kernel_$SCALA_VERSION:$ALMOND_VERSION \\\n", | |
" --sources --default=true \\\n", | |
" -o almond-snapshot --embed-files=false\n", | |
"rm coursier\n", | |
"./almond-snapshot --install --global --force\n", | |
"rm almond-snapshot" | |
], | |
"execution_count": null, | |
"outputs": [] | |
}, | |
{ | |
"cell_type": "code", | |
"metadata": { | |
"id": "BZchIo9sArF4" | |
}, | |
"source": [ | |
"%%shell\n", | |
"echo \"{\n", | |
" \\\"language\\\" : \\\"scala\\\",\n", | |
" \\\"display_name\\\" : \\\"Scala\\\",\n", | |
" \\\"argv\\\" : [\n", | |
" \\\"bash\\\",\n", | |
" \\\"-c\\\",\n", | |
" \\\"env SCALAPY_PYTHON_LIBRARY=python3.6m java -Djna.library.path=/usr/lib/x86_64-linux-gnu/ -jar /usr/local/share/jupyter/kernels/scala/launcher.jar --connection-file {connection_file}\\\"\n", | |
" ]\n", | |
"}\" > /usr/local/share/jupyter/kernels/scala/kernel.json" | |
], | |
"execution_count": null, | |
"outputs": [] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": { | |
"id": "hx0zqmeBLWfe" | |
}, | |
"source": [ | |
"## Run Linear Regression" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": { | |
"id": "cZINatPHEdTM" | |
}, | |
"source": [ | |
"First, we load ScalaPy" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"metadata": { | |
"attributes": { | |
"classes": [ | |
"tut" | |
], | |
"id": "" | |
}, | |
"id": "Jo2-BxdFhsEO", | |
"outputId": "e36dbbce-83d7-4d21-b97c-9a99b840661f", | |
"colab": { | |
"base_uri": "https://localhost:8080/" | |
} | |
}, | |
"source": [ | |
"import $ivy.`me.shadaj::scalapy-core:0.4.0`\n", | |
"\n", | |
"import me.shadaj.scalapy.py\n", | |
"import me.shadaj.scalapy.py.SeqConverters" | |
], | |
"execution_count": 1, | |
"outputs": [ | |
{ | |
"output_type": "execute_result", | |
"data": { | |
"text/plain": [ | |
"\u001b[32mimport \u001b[39m\u001b[36m$ivy.$ \n", | |
"\n", | |
"\u001b[39m\n", | |
"\u001b[32mimport \u001b[39m\u001b[36mme.shadaj.scalapy.py\n", | |
"\u001b[39m\n", | |
"\u001b[32mimport \u001b[39m\u001b[36mme.shadaj.scalapy.py.SeqConverters\u001b[39m" | |
] | |
}, | |
"metadata": { | |
"tags": [] | |
}, | |
"execution_count": 1 | |
} | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": { | |
"id": "4TQ7mSpOErbB" | |
}, | |
"source": [ | |
"Then we generate training data for a simple machine learning example: fitting a line. Here we set the slope to 0.1 and the y-intercept to 0.3." | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"metadata": { | |
"id": "i4WWK8cMXAaM", | |
"outputId": "a681fc30-da20-417c-ff4b-c1d3d5c9aa99", | |
"colab": { | |
"base_uri": "https://localhost:8080/" | |
} | |
}, | |
"source": [ | |
"val np = py.module(\"numpy\")\n", | |
"val tf = py.module(\"tensorflow.compat.v1\")\n", | |
"\n", | |
"tf.disable_v2_behavior()\n", | |
"\n", | |
"val xData = np.random.rand(100).astype(np.float32)\n", | |
"val yData = (xData * 0.1) + 0.3" | |
], | |
"execution_count": 2, | |
"outputs": [ | |
{ | |
"output_type": "execute_result", | |
"data": { | |
"text/plain": [ | |
"\u001b[36mnp\u001b[39m: \u001b[32mpy\u001b[39m.\u001b[32mModule\u001b[39m = <module 'numpy' from '/usr/local/lib/python3.6/dist-packages/numpy/__init__.py'>\n", | |
"\u001b[36mtf\u001b[39m: \u001b[32mpy\u001b[39m.\u001b[32mModule\u001b[39m = <module 'tensorflow.compat.v1' from '/usr/local/lib/python3.6/dist-packages/tensorflow/_api/v2/compat/v1/__init__.py'>\n", | |
"\u001b[36mres1_2\u001b[39m: \u001b[32mpy\u001b[39m.\u001b[32mDynamic\u001b[39m = None\n", | |
"\u001b[36mxData\u001b[39m: \u001b[32mpy\u001b[39m.\u001b[32mDynamic\u001b[39m = [0.38615048 0.83877236 0.21226473 0.8100247 0.49199915 0.9301891\n", | |
" 0.4787747 0.14933264 0.71685404 0.9649488 0.09848763 0.07564199\n", | |
" 0.07801221 0.36855558 0.29295823 0.84548897 0.86105126 0.18807457\n", | |
" 0.5605356 0.58221984 0.00275893 0.8188113 0.05965184 0.9146461\n", | |
" 0.8298311 0.9100036 0.08679805 0.00649861 0.8297746 0.5610333\n", | |
" 0.30910683 0.7216868 0.94857 0.6980366 0.16643395 0.7098128\n", | |
" 0.10955168 0.04587598 0.03221069 0.3445784 0.09798189 0.8815386\n", | |
" 0.04849149 0.25952098 0.21104197 0.0789226 0.85318434 0.10705896\n", | |
" 0.20929255 0.11514268 0.59209245 0.02176601 0.4008569 0.34937045\n", | |
" 0.7699836 0.6604024 0.01293949 0.88260424 0.4824916 0.4994289\n", | |
" 0.0143864 0.7308849 0.3905802 0.00954154 0.90413505 0.6399921\n", | |
" 0.58967865 0.66122895 0.70527065 0.8807726 0.8511866 0.35269243\n", | |
" 0.6219342 0.6520254 0.838608 0.7430698 0.9049002 0.23632616\n", | |
" 0.7980827 0.32016534 0.97926974 0.94999105 0.5597961 0.21901529\n", | |
" 0.7336192 0.47523713 0.29402828 0.07242049 0.9614453 0.38583645\n", | |
" 0.8409937 0.8230996 0.5776055 0.8110992 0.11923148 0.7241995\n", | |
" 0.44522977 0.39794257 0.25918305 0.3515687 ]\n", | |
"\u001b[36myData\u001b[39m: \u001b[32mpy\u001b[39m.\u001b[32mDynamic\u001b[39m = [0.33861506 0.38387725 0.32122648 0.3810025 0.34919992 0.39301893\n", | |
" 0.34787747 0.31493327 0.37168542 0.3964949 0.3098488 0.3075642\n", | |
" 0.30780125 0.33685556 0.32929584 0.3845489 0.38610515 0.31880748\n", | |
" 0.35605356 0.358222 0.3002759 0.38188115 0.3059652 0.39146462\n", | |
" 0.38298312 0.3910004 0.30867982 0.30064988 0.3829775 0.35610333\n", | |
" 0.33091068 0.3721687 0.39485702 0.36980367 0.31664342 0.3709813\n", | |
" 0.31095517 0.3045876 0.30322108 0.33445784 0.3097982 0.38815388\n", | |
" 0.30484915 0.3259521 0.3211042 0.30789226 0.38531846 0.3107059\n", | |
" 0.32092926 0.3115143 0.35920927 0.30217662 0.34008572 0.33493707\n", | |
" 0.37699836 0.36604026 0.30129397 0.38826042 0.34824917 0.3499429\n", | |
" 0.30143866 0.3730885 0.33905804 0.30095416 0.39041352 0.36399922\n", | |
" 0.35896787 0.3661229 0.3705271 0.38807726 0.38511866 0.33526924\n", | |
" 0.36219344 0.36520255 0.38386083 0.374307 0.39049003 0.32363263\n", | |
" 0.37980828 0.33201656 0.397927 0.39499912 0.35597962 0.32190153\n", | |
" 0.37336195 0.34752372 0.32940283 0.30724207 0.39614454 0.33858365\n", | |
" 0.3840994 0.38230997 0.35776055 0.38110992 0.31192315 0.37241995\n", | |
" 0.34452298 0.33979428 0.32591832 0.3351569 ]" | |
] | |
}, | |
"metadata": { | |
"tags": [] | |
}, | |
"execution_count": 2 | |
} | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": { | |
"id": "-hOe5VbdE01J" | |
}, | |
"source": [ | |
"Now that we have training data, we set up a model to learn what the parameters of the line are from just the generated data. We create a variable `W` that represents the slope (initialized to a random value between -1 and 1) and `b` that represents the y-intercept." | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"metadata": { | |
"id": "uYz44oHBEnVN", | |
"outputId": "075eab20-1550-4044-dd41-162a4c157e8e", | |
"colab": { | |
"base_uri": "https://localhost:8080/" | |
} | |
}, | |
"source": [ | |
"val W = tf.Variable(tf.random_uniform(py.Dynamic.global.list(Seq(1).toPythonProxy), -1, 1)) // slope\n", | |
"val b = tf.Variable(tf.zeros(Seq(1).toPythonProxy)) // intercept\n", | |
"val y = (W * xData) + b // predicted" | |
], | |
"execution_count": 3, | |
"outputs": [ | |
{ | |
"output_type": "execute_result", | |
"data": { | |
"text/plain": [ | |
"\u001b[36mW\u001b[39m: \u001b[32mpy\u001b[39m.\u001b[32mDynamic\u001b[39m = <tf.Variable 'Variable:0' shape=(1,) dtype=float32_ref>\n", | |
"\u001b[36mb\u001b[39m: \u001b[32mpy\u001b[39m.\u001b[32mDynamic\u001b[39m = <tf.Variable 'Variable_1:0' shape=(1,) dtype=float32_ref>\n", | |
"\u001b[36my\u001b[39m: \u001b[32mpy\u001b[39m.\u001b[32mDynamic\u001b[39m = Tensor(\"add:0\", shape=(100,), dtype=float32)" | |
] | |
}, | |
"metadata": { | |
"tags": [] | |
}, | |
"execution_count": 3 | |
} | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": { | |
"id": "B4uY2xBoFDJb" | |
}, | |
"source": [ | |
"With the model defined, we now define a loss that TensorFlow will minimize. We also define an optimizer (here just simple gradient descent with a step size of 0.5) that will be used to minimize the loss. Finally, we tie the optimizer and loss togetehr into an operation that will execute one step of gradient descent." | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"metadata": { | |
"id": "xaLrLVsmFCeP", | |
"outputId": "9b2711c3-b3f4-4b19-b92c-d6a6057f047d", | |
"colab": { | |
"base_uri": "https://localhost:8080/" | |
} | |
}, | |
"source": [ | |
"val loss = tf.reduce_mean(tf.square(y - yData))\n", | |
"val optimizer = tf.train.GradientDescentOptimizer(0.5)\n", | |
"val train = optimizer.minimize(loss)" | |
], | |
"execution_count": 4, | |
"outputs": [ | |
{ | |
"output_type": "execute_result", | |
"data": { | |
"text/plain": [ | |
"\u001b[36mloss\u001b[39m: \u001b[32mpy\u001b[39m.\u001b[32mDynamic\u001b[39m = Tensor(\"Mean:0\", shape=(), dtype=float32)\n", | |
"\u001b[36moptimizer\u001b[39m: \u001b[32mpy\u001b[39m.\u001b[32mDynamic\u001b[39m = <tensorflow.python.training.gradient_descent.GradientDescentOptimizer object at 0x7fc71ff34c50>\n", | |
"\u001b[36mtrain\u001b[39m: \u001b[32mpy\u001b[39m.\u001b[32mDynamic\u001b[39m = name: \"GradientDescent\"\n", | |
"op: \"NoOp\"\n", | |
"input: \"^GradientDescent/update_Variable/ApplyGradientDescent\"\n", | |
"input: \"^GradientDescent/update_Variable_1/ApplyGradientDescent\"\n" | |
] | |
}, | |
"metadata": { | |
"tags": [] | |
}, | |
"execution_count": 4 | |
} | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": { | |
"id": "oexvFvf8Fccv" | |
}, | |
"source": [ | |
"With the training approach defined, all that's left is to actually perform the training steps. Here we run 200 steps of gradient descent, printing out the current variable values every 20 steps." | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"metadata": { | |
"id": "IWJg5KO7Ep7f", | |
"outputId": "cf8c1de1-8529-4029-a26a-a4b3c6f300d8", | |
"colab": { | |
"base_uri": "https://localhost:8080/" | |
} | |
}, | |
"source": [ | |
"val sess = tf.Session()\n", | |
"sess.run(tf.global_variables_initializer())\n", | |
"\n", | |
"(0 to 200).foreach { step =>\n", | |
" sess.run(train)\n", | |
"\n", | |
" if (step % 20 == 0) {\n", | |
" println(s\"Step $step: W = ${sess.run(W).bracketAccess(0)}, b = ${sess.run(b).bracketAccess(0)}\")\n", | |
" }\n", | |
"}" | |
], | |
"execution_count": 5, | |
"outputs": [ | |
{ | |
"output_type": "stream", | |
"text": [ | |
"Step 0: W = 0.7076286, b = -0.044888854\n", | |
"Step 20: W = 0.22272547, b = 0.23428665\n", | |
"Step 40: W = 0.12446017, b = 0.28690282\n", | |
"Step 60: W = 0.1048751, b = 0.29738963\n", | |
"Step 80: W = 0.10097166, b = 0.29947975\n", | |
"Step 100: W = 0.10019367, b = 0.2998963\n", | |
"Step 120: W = 0.1000386, b = 0.29997933\n", | |
"Step 140: W = 0.100007705, b = 0.2999959\n", | |
"Step 160: W = 0.100001544, b = 0.29999918\n", | |
"Step 180: W = 0.10000032, b = 0.29999983\n", | |
"Step 200: W = 0.1000001, b = 0.29999995\n" | |
], | |
"name": "stdout" | |
}, | |
{ | |
"output_type": "execute_result", | |
"data": { | |
"text/plain": [ | |
"\u001b[36msess\u001b[39m: \u001b[32mpy\u001b[39m.\u001b[32mDynamic\u001b[39m = <tensorflow.python.client.session.Session object at 0x7fc71ff51f98>\n", | |
"\u001b[36mres4_1\u001b[39m: \u001b[32mpy\u001b[39m.\u001b[32mDynamic\u001b[39m = None" | |
] | |
}, | |
"metadata": { | |
"tags": [] | |
}, | |
"execution_count": 5 | |
} | |
] | |
} | |
] | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment