Last active
February 23, 2020 09:27
-
-
Save obenjiro/cd4b095c39af7ed78b12572560fbe0ac to your computer and use it in GitHub Desktop.
TFJS-collab-save-tensor.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": "TFJS-collab-save-tensor.ipynb", | |
| "provenance": [], | |
| "include_colab_link": true | |
| }, | |
| "kernelspec": { | |
| "name": "javascript", | |
| "display_name": "Javascript" | |
| }, | |
| "accelerator": "GPU" | |
| }, | |
| "cells": [ | |
| { | |
| "cell_type": "markdown", | |
| "metadata": { | |
| "id": "view-in-github", | |
| "colab_type": "text" | |
| }, | |
| "source": [ | |
| "<a href=\"https://colab.research.google.com/gist/obenjiro/cd4b095c39af7ed78b12572560fbe0ac/notebook.ipynb\" target=\"_parent\"><img src=\"https://colab.research.google.com/assets/colab-badge.svg\" alt=\"Open In Colab\"/></a>" | |
| ] | |
| }, | |
| { | |
| "cell_type": "markdown", | |
| "metadata": { | |
| "id": "fsqcND6Klagn", | |
| "colab_type": "text" | |
| }, | |
| "source": [ | |
| "# Prep JS runtime for Google Colab" | |
| ] | |
| }, | |
| { | |
| "cell_type": "markdown", | |
| "metadata": { | |
| "id": "bjNF3VjPjJHV", | |
| "colab_type": "text" | |
| }, | |
| "source": [ | |
| "Run cell (commands) below and then hit F5 (refresh the page) right after that" | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "metadata": { | |
| "id": "ZVkF16NwcMsZ", | |
| "colab_type": "code", | |
| "colab": {} | |
| }, | |
| "source": [ | |
| "!npm install -g npm@latest\n", | |
| "!npm cache verify\n", | |
| "!npm install -g --unsafe-perm ijavascript\n", | |
| "!ijsinstall --install=global\n", | |
| "!jupyter-kernelspec list" | |
| ], | |
| "execution_count": 0, | |
| "outputs": [] | |
| }, | |
| { | |
| "cell_type": "markdown", | |
| "metadata": { | |
| "id": "r2UzIt5Gi6Em", | |
| "colab_type": "text" | |
| }, | |
| "source": [ | |
| "That's it! :) Now in the next cells you can write JavaScript" | |
| ] | |
| }, | |
| { | |
| "cell_type": "markdown", | |
| "metadata": { | |
| "id": "MMyXtMptltc9", | |
| "colab_type": "text" | |
| }, | |
| "source": [ | |
| "# Example of usage" | |
| ] | |
| }, | |
| { | |
| "cell_type": "markdown", | |
| "metadata": { | |
| "id": "qbVfsd0emCWY", | |
| "colab_type": "text" | |
| }, | |
| "source": [ | |
| "**Remember not to use `const` or `let`! Use `var` instead**\n", | |
| "\n", | |
| "This is how you can execute shell commands:" | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "metadata": { | |
| "id": "skqAs_UHc8_P", | |
| "colab_type": "code", | |
| "colab": {} | |
| }, | |
| "source": [ | |
| "var { spawn } = require('child_process');\n", | |
| "var sh = (cmd) => { \n", | |
| " $$.async();\n", | |
| " var sp = spawn(cmd, { cwd: process.cwd(), stdio: 'pipe', shell: true, encoding: 'utf-8' });\n", | |
| " sp.stdout.on('data', data => console.log(data.toString()));\n", | |
| " sp.stderr.on('data', data => console.error(data.toString()));\n", | |
| " sp.on('close', () => $$.done());\n", | |
| "};\n", | |
| "var run_async = async (pf) => {\n", | |
| " $$.async();\n", | |
| " await pf();\n", | |
| " $$.done();\n", | |
| "};\n", | |
| "sh('npm init -y');" | |
| ], | |
| "execution_count": 0, | |
| "outputs": [] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "metadata": { | |
| "id": "1oqifUNtdID-", | |
| "colab_type": "code", | |
| "outputId": "7a8d3f4b-7875-4d6b-fa63-332ffbb2364f", | |
| "colab": { | |
| "base_uri": "https://localhost:8080/", | |
| "height": 102 | |
| } | |
| }, | |
| "source": [ | |
| "sh('node -v; npm -v');" | |
| ], | |
| "execution_count": 5, | |
| "outputs": [ | |
| { | |
| "output_type": "stream", | |
| "text": [ | |
| "v8.11.3\n", | |
| "\n", | |
| "\n", | |
| "6.13.7\n", | |
| "\n" | |
| ], | |
| "name": "stdout" | |
| } | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "metadata": { | |
| "id": "28tLRTVq8xms", | |
| "colab_type": "code", | |
| "outputId": "9c323758-8a9c-45ce-b959-6017abc21544", | |
| "colab": { | |
| "base_uri": "https://localhost:8080/", | |
| "height": 680 | |
| } | |
| }, | |
| "source": [ | |
| "sh('npm install @tensorflow/tfjs-node-gpu')" | |
| ], | |
| "execution_count": 7, | |
| "outputs": [ | |
| { | |
| "output_type": "stream", | |
| "text": [ | |
| "\n", | |
| "> @tensorflow/tfjs-node-gpu@1.5.2 install /content/node_modules/@tensorflow/tfjs-node-gpu\n", | |
| "> node scripts/install.js gpu download\n", | |
| "\n", | |
| "\n", | |
| "GPU-linux-1.5.2.tar.gz\n", | |
| "\n" | |
| ], | |
| "name": "stdout" | |
| }, | |
| { | |
| "output_type": "stream", | |
| "text": [ | |
| "* Downloading libtensorflow\n", | |
| "\n", | |
| "\n", | |
| "\n", | |
| "* Building TensorFlow Node.js bindings\n", | |
| "\n", | |
| "npm\n", | |
| " notice created a lockfile as package-lock.json. You should commit this file.\n", | |
| "\n", | |
| "npm\n", | |
| " \n", | |
| "WARN\n", | |
| " content@1.0.0 No description\n", | |
| "\n", | |
| "npm\n", | |
| " \n", | |
| "WARN\n", | |
| " content@1.0.0 No repository field.\n", | |
| "\n", | |
| "\n", | |
| "\n" | |
| ], | |
| "name": "stderr" | |
| }, | |
| { | |
| "output_type": "stream", | |
| "text": [ | |
| "+ @tensorflow/tfjs-node-gpu@1.5.2\n", | |
| "added 88 packages from 91 contributors and audited 153 packages in 32.336s\n", | |
| "\n", | |
| "\n", | |
| "1 package is looking for funding\n", | |
| " run `npm fund` for details\n", | |
| "\n", | |
| "\n", | |
| "found 0 vulnerabilities\n", | |
| "\n", | |
| "\n" | |
| ], | |
| "name": "stdout" | |
| } | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "metadata": { | |
| "id": "psJPTwp79NXv", | |
| "colab_type": "code", | |
| "outputId": "d76be7bc-d6a2-41cd-c7f8-b0478e25515b", | |
| "colab": { | |
| "base_uri": "https://localhost:8080/", | |
| "height": 119 | |
| } | |
| }, | |
| "source": [ | |
| "var tf = require('@tensorflow/tfjs-node-gpu');\n", | |
| "console.log(tf.backend().isUsingGpuDevice);" | |
| ], | |
| "execution_count": 10, | |
| "outputs": [ | |
| { | |
| "output_type": "stream", | |
| "text": [ | |
| "node-pre-gyp info This Node instance does not support builds for N-API version 4\n", | |
| "node-pre-gyp info This Node instance does not support builds for N-API version 5\n", | |
| "node-pre-gyp info This Node instance does not support builds for N-API version 4\n", | |
| "node-pre-gyp info This Node instance does not support builds for N-API version 5\n" | |
| ], | |
| "name": "stderr" | |
| }, | |
| { | |
| "output_type": "stream", | |
| "text": [ | |
| "true\n" | |
| ], | |
| "name": "stdout" | |
| }, | |
| { | |
| "output_type": "stream", | |
| "text": [ | |
| "(node:292) Warning: N-API is an experimental feature and could change at any time.\n" | |
| ], | |
| "name": "stderr" | |
| } | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "metadata": { | |
| "id": "GcQiKW6sJFZX", | |
| "colab_type": "code", | |
| "colab": { | |
| "base_uri": "https://localhost:8080/", | |
| "height": 68 | |
| }, | |
| "outputId": "3fb3c483-2e05-4263-d01f-3630162d253c" | |
| }, | |
| "source": [ | |
| "var tensorToSave = tf.tensor([[1, 2], [3, 4]]);\n", | |
| "tensorToSave.print()" | |
| ], | |
| "execution_count": 13, | |
| "outputs": [ | |
| { | |
| "output_type": "stream", | |
| "text": [ | |
| "Tensor\n", | |
| " [[1, 2],\n", | |
| " [3, 4]]\n" | |
| ], | |
| "name": "stdout" | |
| } | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "metadata": { | |
| "id": "uEB7uGBFUeUY", | |
| "colab_type": "code", | |
| "colab": { | |
| "base_uri": "https://localhost:8080/", | |
| "height": 51 | |
| }, | |
| "outputId": "01fe7880-b15b-42ff-9f1f-dc9e395b8d7a" | |
| }, | |
| "source": [ | |
| "// saving tensor\n", | |
| "var fs = require('fs');\n", | |
| "fs.writeFileSync('saved-tensor.json', JSON.stringify(tensorToSave.arraySync()), 'utf-8');\n", | |
| "sh('ls -la saved-tensor.json');" | |
| ], | |
| "execution_count": 27, | |
| "outputs": [ | |
| { | |
| "output_type": "stream", | |
| "text": [ | |
| "-rw-r--r-- 1 root root 13 Feb 23 09:16 saved-tensor.json\n", | |
| "\n" | |
| ], | |
| "name": "stdout" | |
| } | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "metadata": { | |
| "id": "FX9NhyWsU1BM", | |
| "colab_type": "code", | |
| "colab": { | |
| "base_uri": "https://localhost:8080/", | |
| "height": 68 | |
| }, | |
| "outputId": "9904bc71-ae57-465a-daf2-eab3542910d5" | |
| }, | |
| "source": [ | |
| "// loading tensor\n", | |
| "var fs = require('fs');\n", | |
| "var str = fs.readFileSync('saved-tensor.json', 'utf-8');\n", | |
| "var tensorRestored = tf.tensor(JSON.parse(str));\n", | |
| "tensorRestored.print()" | |
| ], | |
| "execution_count": 31, | |
| "outputs": [ | |
| { | |
| "output_type": "stream", | |
| "text": [ | |
| "Tensor\n", | |
| " [[1, 2],\n", | |
| " [3, 4]]\n" | |
| ], | |
| "name": "stdout" | |
| } | |
| ] | |
| } | |
| ] | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment