Last active
March 9, 2019 21:01
-
-
Save caisq/ff7d9cdd5b590cb3ddeba9002f8dca69 to your computer and use it in GitHub Desktop.
Simple example of TensorFlow.js in IJavaScript Notebook
This file contains 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
{ | |
"cells": [ | |
{ | |
"cell_type": "code", | |
"execution_count": 1, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"name": "stderr", | |
"output_type": "stream", | |
"text": [ | |
"(node:110831) Warning: N-API is an experimental feature and could change at any time.\n" | |
] | |
} | |
], | |
"source": [ | |
"const tf = require('@tensorflow/tfjs-node');" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 2, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"_________________________________________________________________\n", | |
"Layer (type) Output shape Param # \n", | |
"=================================================================\n", | |
"dense_Dense1 (Dense) [null,5] 25 \n", | |
"_________________________________________________________________\n", | |
"dense_Dense2 (Dense) [null,3] 18 \n", | |
"=================================================================\n", | |
"Total params: 43\n", | |
"Trainable params: 43\n", | |
"Non-trainable params: 0\n", | |
"_________________________________________________________________\n" | |
] | |
} | |
], | |
"source": [ | |
"model = tf.sequential();\n", | |
"model.add(tf.layers.dense({units: 5, activation: 'relu', inputShape: [4]}));\n", | |
"model.add(tf.layers.dense({units: 3, activation: 'softmax'}));\n", | |
"model.compile({loss: 'categoricalCrossentropy', optimizer: 'sgd'});\n", | |
"model.summary();" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 3, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"class_1 { size: null }" | |
] | |
}, | |
"execution_count": 3, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"SPECIES = ['setosa', 'versicolor', 'virginica'];\n", | |
"dataset = tf.data.csv(\n", | |
" 'https://raw.githubusercontent.com/uiuc-cse/data-fa14/gh-pages/data/iris.csv')\n", | |
" .map(item => [\n", | |
" [item['sepal_length'], item['sepal_width'], item['petal_length'], item['petal_width']],\n", | |
" tf.oneHot(tf.scalar(SPECIES.indexOf(item['species']), 'int32'), 3)\n", | |
" ]).shuffle(100).batch(16);" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 4, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"Epoch 1 / 10\n" | |
] | |
}, | |
{ | |
"name": "stderr", | |
"output_type": "stream", | |
"text": [ | |
"\n" | |
] | |
}, | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"173ms 17325us/step - loss=1.29 \n", | |
"Epoch 2 / 10\n" | |
] | |
}, | |
{ | |
"name": "stderr", | |
"output_type": "stream", | |
"text": [ | |
"\n" | |
] | |
}, | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"133ms 13324us/step - loss=1.10 \n", | |
"Epoch 3 / 10\n" | |
] | |
}, | |
{ | |
"name": "stderr", | |
"output_type": "stream", | |
"text": [ | |
"\n" | |
] | |
}, | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"128ms 12849us/step - loss=1.07 \n", | |
"Epoch 4 / 10\n" | |
] | |
}, | |
{ | |
"name": "stderr", | |
"output_type": "stream", | |
"text": [ | |
"\n" | |
] | |
}, | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"113ms 11344us/step - loss=1.06 \n", | |
"Epoch 5 / 10\n" | |
] | |
}, | |
{ | |
"name": "stderr", | |
"output_type": "stream", | |
"text": [ | |
"\n" | |
] | |
}, | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"106ms 10620us/step - loss=1.05 \n", | |
"Epoch 6 / 10\n" | |
] | |
}, | |
{ | |
"name": "stderr", | |
"output_type": "stream", | |
"text": [ | |
"\n" | |
] | |
}, | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"120ms 12047us/step - loss=1.04 \n", | |
"Epoch 7 / 10\n" | |
] | |
}, | |
{ | |
"name": "stderr", | |
"output_type": "stream", | |
"text": [ | |
"\n" | |
] | |
}, | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"105ms 10519us/step - loss=1.03 \n", | |
"Epoch 8 / 10\n" | |
] | |
}, | |
{ | |
"name": "stderr", | |
"output_type": "stream", | |
"text": [ | |
"\n" | |
] | |
}, | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"101ms 10133us/step - loss=1.02 \n", | |
"Epoch 9 / 10\n" | |
] | |
}, | |
{ | |
"name": "stderr", | |
"output_type": "stream", | |
"text": [ | |
"\n" | |
] | |
}, | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"115ms 11505us/step - loss=1.01 \n", | |
"Epoch 10 / 10\n" | |
] | |
}, | |
{ | |
"name": "stderr", | |
"output_type": "stream", | |
"text": [ | |
"\n" | |
] | |
}, | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"98ms 9759us/step - loss=1.00 \n" | |
] | |
}, | |
{ | |
"data": { | |
"text/plain": [ | |
"History {\n", | |
" validationData: null,\n", | |
" params: \n", | |
" { epochs: 10,\n", | |
" initialEpoch: null,\n", | |
" samples: null,\n", | |
" steps: null,\n", | |
" batchSize: null,\n", | |
" verbose: 1,\n", | |
" doValidation: false,\n", | |
" metrics: [ 'loss' ] },\n", | |
" epoch: [ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 ],\n", | |
" history: \n", | |
" { loss: \n", | |
" [ 1.2919349670410156,\n", | |
" 1.1000075340270996,\n", | |
" 1.0673496723175049,\n", | |
" 1.0560511350631714,\n", | |
" 1.0499601364135742,\n", | |
" 1.0428799390792847,\n", | |
" 1.0348219871520996,\n", | |
" 1.0247327089309692,\n", | |
" 1.0146008729934692,\n", | |
" 1.0047441720962524 ] } }" | |
] | |
}, | |
"execution_count": 4, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"model.fitDataset(dataset, {epochs: 10});" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"metadata": {}, | |
"outputs": [], | |
"source": [] | |
} | |
], | |
"metadata": { | |
"kernelspec": { | |
"display_name": "Javascript (Node.js)", | |
"language": "javascript", | |
"name": "javascript" | |
}, | |
"language_info": { | |
"file_extension": ".js", | |
"mimetype": "application/javascript", | |
"name": "javascript", | |
"version": "8.11.3" | |
} | |
}, | |
"nbformat": 4, | |
"nbformat_minor": 2 | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment