Skip to content

Instantly share code, notes, and snippets.

@obenjiro
Last active July 8, 2020 18:12
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save obenjiro/7f1499520458fdb06fdf48086c9be50a to your computer and use it in GitHub Desktop.
Save obenjiro/7f1499520458fdb06fdf48086c9be50a to your computer and use it in GitHub Desktop.
TFJS-collab-save-tensor.ipynb
Display the source blob
Display the rendered blob
Raw
{
"nbformat": 4,
"nbformat_minor": 0,
"metadata": {
"colab": {
"name": "TensorflowJS Template.ipynb",
"provenance": [],
"collapsed_sections": [],
"machine_shape": "hm",
"include_colab_link": true
},
"accelerator": "GPU",
"kernelspec": {
"name": "javascript",
"display_name": "Javascript"
}
},
"cells": [
{
"cell_type": "markdown",
"metadata": {
"id": "view-in-github",
"colab_type": "text"
},
"source": [
"<a href=\"https://tinyurl.com/tf-js-colab\" target=\"_parent\"><img src=\"https://colab.research.google.com/assets/colab-badge.svg\" alt=\"Open In Colab\"/></a>" ]
},
{
"cell_type": "markdown",
"metadata": {
"id": "view-in-github",
"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",
"outputId": "80d31427-6be6-4d80-c483-db2c7c51a9be"
},
"source": [
"!npm install -g --unsafe-perm ijavascript\n",
"!ijsinstall --install=global\n",
"!jupyter-kernelspec list"
]
},
{
"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",
"outputId": "f519b6eb-f76d-4966-d3dd-ff28b03db0c0"
},
"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');"
]
},
{
"cell_type": "code",
"metadata": {
"id": "1oqifUNtdID-",
"colab_type": "code",
"outputId": "a6c49360-ab8a-4997-d786-2526c21ae28e"
},
"source": [
"sh('node -v; npm -v');"
]
},
{
"cell_type": "code",
"metadata": {
"id": "28tLRTVq8xms",
"colab_type": "code",
"outputId": "8c728808-e473-41ec-c1b5-d1322df84b6a"
},
"source": [
"sh('npm install @tensorflow/tfjs-node-gpu')"
]
},
{
"cell_type": "code",
"metadata": {
"id": "psJPTwp79NXv",
"colab_type": "code",
"outputId": "15ec3d58-351f-45cd-cd76-9ef22b13e4d5"
},
"source": [
"var tf = require('@tensorflow/tfjs-node-gpu');\n",
"tf.backend().isUsingGpuDevice;"
]
}
]
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment