Skip to content

Instantly share code, notes, and snippets.

@rozeappletree
Created November 21, 2019 13:15
Show Gist options
  • Save rozeappletree/1d8238ac665c491a233c8a822f9970d6 to your computer and use it in GitHub Desktop.
Save rozeappletree/1d8238ac665c491a233c8a822f9970d6 to your computer and use it in GitHub Desktop.
io19-demo-1-swift-in-colab.ipynb
Display the source blob
Display the rendered blob
Raw
{
"nbformat": 4,
"nbformat_minor": 0,
"metadata": {
"colab": {
"name": "io19-demo-1-swift-in-colab.ipynb",
"provenance": [],
"collapsed_sections": [],
"include_colab_link": true
},
"kernelspec": {
"name": "swift",
"display_name": "Swift"
}
},
"cells": [
{
"cell_type": "markdown",
"metadata": {
"id": "view-in-github",
"colab_type": "text"
},
"source": [
"<a href=\"https://colab.research.google.com/gist/rakesh4real/1d8238ac665c491a233c8a822f9970d6/io19-demo-1-swift-in-colab.ipynb\" target=\"_parent\"><img src=\"https://colab.research.google.com/assets/colab-badge.svg\" alt=\"Open In Colab\"/></a>"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "xSYmKIt59Zt3",
"colab_type": "text"
},
"source": [
"**Credits:** Google I/O 19 "
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "g8sVjqcc-ej8",
"colab_type": "text"
},
"source": [
"# Hello, Colab!"
]
},
{
"cell_type": "code",
"metadata": {
"id": "kZRlD4utdPuX",
"colab_type": "code",
"outputId": "8a0b2b2f-85a9-4208-b450-25c939121e34",
"colab": {
"base_uri": "https://localhost:8080/",
"height": 34
}
},
"source": [
"let pi = 3.14\n",
"let y = [1, 2, 3]\n",
"print(\"Hello\")"
],
"execution_count": 0,
"outputs": [
{
"output_type": "stream",
"text": [
"Hello\r\n"
],
"name": "stdout"
}
]
},
{
"cell_type": "code",
"metadata": {
"id": "ym3klNgB9iA4",
"colab_type": "code",
"outputId": "b41187af-e83b-4064-cb98-33c8e2802706",
"colab": {
"base_uri": "https://localhost:8080/",
"height": 68
}
},
"source": [
"let someNumbers = [\n",
" \"Prime\": [2, 3, 5, 7, 11, 13],\n",
" \"Fibonacci\": [1, 1, 2, 3, 5, 8],\n",
" \"Square\": [1, 4, 9, 16, 25],\n",
"]\n",
"\n",
"for (kind, numbers) in someNumbers {\n",
" var largest = 0\n",
" for x in numbers where x > largest && x.isEven {\n",
" largest = x\n",
" }\n",
" print(\"The largest even \\(kind) is \\(largest)\")\n",
"}"
],
"execution_count": 0,
"outputs": [
{
"output_type": "stream",
"text": [
"The largest even Fibonacci is 8\r\n",
"The largest even Square is 16\r\n",
"The largest even Prime is 2\r\n"
],
"name": "stdout"
}
]
},
{
"cell_type": "code",
"metadata": {
"id": "igwEhJod9qla",
"colab_type": "code",
"colab": {}
},
"source": [
"extension Int{\n",
" var isEven: Bool{\n",
" return isMultiple(of: 2)\n",
" }\n",
"}"
],
"execution_count": 0,
"outputs": []
},
{
"cell_type": "markdown",
"metadata": {
"id": "LVbNpcWY-gea",
"colab_type": "text"
},
"source": [
"# The Tensor Library"
]
},
{
"cell_type": "code",
"metadata": {
"id": "CYKFHSzl9yUS",
"colab_type": "code",
"outputId": "9d44ed40-6ec8-4f20-e3f3-a09997652975",
"colab": {
"base_uri": "https://localhost:8080/",
"height": 119
}
},
"source": [
"\n",
"import TensorFlow\n",
"\n",
"let x: Tensor<Float> = [[[1, 2], [4, 5]]]\n",
"\n",
"print(\"x + x =\\n\\(x + x)\\n\")\n",
"print(\"sum = \\(x.sum())\")\n",
"print(\"stdev = \\(x.standardDeviation(squeezingAxes: 0, 1))\")"
],
"execution_count": 0,
"outputs": [
{
"output_type": "stream",
"text": [
"x + x =\r\n",
"[[[ 2.0, 4.0],\r\n",
" [ 8.0, 10.0]]]\r\n",
"\r\n",
"sum = 12.0\r\n",
"stdev = [1.5, 1.5]\r\n"
],
"name": "stdout"
}
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "xo-oB7g1-vct",
"colab_type": "text"
},
"source": [
"# Deep Learning"
]
},
{
"cell_type": "code",
"metadata": {
"id": "vV_giCKJ-noW",
"colab_type": "code",
"colab": {}
},
"source": [
"struct Model: Layer {\n",
" var conv = Conv2D<Float>(filterShape: (5, 5, 3, 6))\n",
" var maxpool = MaxPool2D<Float>(poolSize: (2, 2), strides: (2, 2))\n",
" var flatten = Flatten<Float>()\n",
" var dense = Dense<Float>(inputSize: 36 * 6, outputSize: 10)\n",
"\n",
" @differentiable\n",
" func callAsFunction(_ input: Tensor<Float>) -> Tensor<Float> {\n",
" return input.sequenced(through: conv, maxpool, flatten, dense )\n",
" }\n",
"}"
],
"execution_count": 0,
"outputs": []
},
{
"cell_type": "code",
"metadata": {
"id": "DAi4uvSV-1lq",
"colab_type": "code",
"colab": {}
},
"source": [
"// Use random training data.\n",
"let x = Tensor<Float>(randomNormal: [10, 16, 16, 3])\n",
"let y = Tensor<Int32>(rangeFrom: 0, to: 10, stride: 1)"
],
"execution_count": 0,
"outputs": []
},
{
"cell_type": "code",
"metadata": {
"id": "GfMUZTNN_oV6",
"colab_type": "code",
"colab": {}
},
"source": [
"var model = Model()\n",
"let optimizer = SGD(for: model)"
],
"execution_count": 0,
"outputs": []
},
{
"cell_type": "code",
"metadata": {
"id": "j5gVGbNQ_tda",
"colab_type": "code",
"outputId": "ebc0133b-09f7-4916-a2e7-0dc7cfaa9f37",
"colab": {
"base_uri": "https://localhost:8080/",
"height": 187
}
},
"source": [
"for i in 1...10 {\n",
" let (loss, grads) = valueWithGradient(at: model) { model -> Tensor<Float> in\n",
" let logits = model(x)\n",
" return softmaxCrossEntropy(logits: logits, labels: y)\n",
" }\n",
" print(\"Step \\(i), loss is: \\(loss)\")\n",
" optimizer.update(&model.allDifferentiableVariables, along: grads)\n",
"}"
],
"execution_count": 0,
"outputs": [
{
"output_type": "stream",
"text": [
"Step 1, loss is: 3.16299\r\n",
"Step 2, loss is: 2.758711\r\n",
"Step 3, loss is: 2.4353342\r\n",
"Step 4, loss is: 2.1696808\r\n",
"Step 5, loss is: 1.9441502\r\n",
"Step 6, loss is: 1.7495903\r\n",
"Step 7, loss is: 1.5788343\r\n",
"Step 8, loss is: 1.4273479\r\n",
"Step 9, loss is: 1.2923636\r\n",
"Step 10, loss is: 1.1733923\r\n"
],
"name": "stdout"
}
]
}
]
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment