Created
November 8, 2019 17:31
-
-
Save INF800/33fcad0f4fab7b0ccafe04f1397b2279 to your computer and use it in GitHub Desktop.
Tensorflow-Lets-Cook-in-ML.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": "Tensorflow-Lets-Cook-in-ML.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/33fcad0f4fab7b0ccafe04f1397b2279/tensorflow-lets-cook-in-ml.ipynb\" target=\"_parent\"><img src=\"https://colab.research.google.com/assets/colab-badge.svg\" alt=\"Open In Colab\"/></a>" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"metadata": { | |
"id": "kZRlD4utdPuX", | |
"colab_type": "code", | |
"colab": {} | |
}, | |
"source": [ | |
"import TensorFlow" | |
], | |
"execution_count": 0, | |
"outputs": [] | |
}, | |
{ | |
"cell_type": "code", | |
"metadata": { | |
"id": "VoWRtV0ujokr", | |
"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", | |
"}\n" | |
], | |
"execution_count": 0, | |
"outputs": [] | |
}, | |
{ | |
"cell_type": "code", | |
"metadata": { | |
"id": "5O-5kCEniSpJ", | |
"colab_type": "code", | |
"colab": {} | |
}, | |
"source": [ | |
"// Use random training data.\n", | |
"let x = Tensor<Float>(randomNormal: [10, 16, 16, 3]) //dimensions\n", | |
"let y = Tensor<Int32>(rangeFrom: 0, to: 10, stride: 1)" | |
], | |
"execution_count": 0, | |
"outputs": [] | |
}, | |
{ | |
"cell_type": "code", | |
"metadata": { | |
"id": "x44kFjA0tRFB", | |
"colab_type": "code", | |
"colab": {} | |
}, | |
"source": [ | |
"var model = Model()\n", | |
"let opt = SGD(for: model)\n", | |
"Context.local.learningPhase = .training" | |
], | |
"execution_count": 0, | |
"outputs": [] | |
}, | |
{ | |
"cell_type": "code", | |
"metadata": { | |
"id": "mMxjNQIDtxgR", | |
"colab_type": "code", | |
"outputId": "6ced9d9a-3e4e-4431-b5c0-a7f6e93175a7", | |
"colab": { | |
"base_uri": "https://localhost:8080/", | |
"height": 697 | |
} | |
}, | |
"source": [ | |
"for i in 1...20 {\n", | |
" print(\"Starting training step \\(i)\")\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(\"Loss: \\(loss)\")\n", | |
" opt.update(&model.allDifferentiableVariables, along: grads)\n", | |
"}" | |
], | |
"execution_count": 0, | |
"outputs": [ | |
{ | |
"output_type": "stream", | |
"text": [ | |
"Starting training step 1\n", | |
"Loss: 2.5361626\n", | |
"Starting training step 2\n", | |
"Loss: 2.2827177\n", | |
"Starting training step 3\n", | |
"Loss: 2.059002\n", | |
"Starting training step 4\n", | |
"Loss: 1.8636631\n", | |
"Starting training step 5\n", | |
"Loss: 1.688523\n", | |
"Starting training step 6\n", | |
"Loss: 1.5316066\n", | |
"Starting training step 7\n", | |
"Loss: 1.389894\n", | |
"Starting training step 8\n", | |
"Loss: 1.2644584\n", | |
"Starting training step 9\n", | |
"Loss: 1.1527299\n", | |
"Starting training step 10\n", | |
"Loss: 1.0534394\n", | |
"Starting training step 11\n", | |
"Loss: 0.9644995\n", | |
"Starting training step 12\n", | |
"Loss: 0.8861335\n", | |
"Starting training step 13\n", | |
"Loss: 0.81693286\n", | |
"Starting training step 14\n", | |
"Loss: 0.75583524\n", | |
"Starting training step 15\n", | |
"Loss: 0.70112455\n", | |
"Starting training step 16\n", | |
"Loss: 0.6510164\n", | |
"Starting training step 17\n", | |
"Loss: 0.6062659\n", | |
"Starting training step 18\n", | |
"Loss: 0.5659787\n", | |
"Starting training step 19\n", | |
"Loss: 0.52945524\n", | |
"Starting training step 20\n", | |
"Loss: 0.4967416\n" | |
], | |
"name": "stdout" | |
} | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"metadata": { | |
"id": "zCt9VnVuSLcS", | |
"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