ラベル

AI Forecast 8306MUFG (214) Social (75) life (70) Financial (62) IT (57) Proposal (56) idea (50) Fund Management (48) Trading (43) economics (42) Covid-19 (40) AI (38) Hedge Fund (36) Risk (36) Culture (31) BOJ (29) Science (26) hobby (24) accounting (17) Apps (16) career (11) job (7) Travel (6) Hiking (5) emotion (5) music (3) Statarb (2) piano (2)

2020年6月16日火曜日

NodejsへのTensorflowセットアップ方法 How to setup Tensorflow onto Nodejs

NodejsへのTensorflowセットアップ方法

How to setup Tensorflow onto Nodejs


NodejsはJavascriptをサーバー側で走らせるための環境。Javascriptは基本、ブラウザ内で走るが、Nodejsとすると、C++モジュールを実行するため高速化される。Nodejs is a Javascript environment to run on server-side. Javascript normally runs in browser but in Nodejs, it can run faster due to C++ module called direct.

ここでは、新値三本株価予測AI(日足)をNodejs上で実行。Here, I run three line break forecast AI(daily) on Nodejs.

結果、C++モジュールによる高速化は最近のCPUでないと動作しないということが分かり手持ちのi5-650デスクトップではテストできなかったが、C++高速化なしで、かつ、古いCPUでも2倍程度(12分が6分に短縮)の高速化を達成。ただ、Chromeブラウザのハードウエアアクセラレーション(WebGL)をONにすると、Nodejsより若干早い4分で完了したため、新しいCPUでないと、Nodejsを用いる意味がないことが分かった。As a result, I found out that C++ module acceleration works only when recent CPU is used but not on my legacy CPU, i5-650 Desktop. Nevertheless, I achieved acceleration for 2 times(execution time reduced from 12 min down to 6 min). However, if I turn hardware acceleration of Chrome browser as ON(WebGL), program completed in 4 minutes that is faster than Nodejs. Hence, unless CPU is new, there is no point of using Nodejs if WebGL is available.

環境はwin10。ユーティリティーライブラリ管理などの環境維持のために、npmという環境がnodejs標準インストール時に自動的に設定される。Environment is Windows10. For the purpose of utility and library maintenance, environment called npm is also installed by default when nodejs is installed.

ブラウザーでJavascriptを実行する場合、ライブラリはSRCでスクリプトをインターネット上で参照。一方、NodejsではライブラリをPCにnpm経由でインストール。When run Javascript on browser, libraries are called through SRC from internet but in nodejs, library is installed onto PC explicit through npm.
  • Nodejs、npmインストール Nodejs, npm installation
    1. https://nodejs.org/en/download/から、LTS(安定板)をインストール。Install stable version LTS from link https://nodejs.org/en/download/.
    2. コマンドnode --versionでバージョン確認。Check version by command node --version.
    3. npm --versionでnpmの確認。Check npm version by command npm --version. 
  • ライブラリインストール Install libraries. 
    1. 自分のJavascriptソースコードファイルが置いてあるディレクトリに移動。ここで、nodejsがインストールされているディレクトリではなく、ソースコードが置いてあるディレクトリである事がとても重要。Nodejsではプロジェクトごとにライブラリを設定。Move to directory where your own Javascript source code is saved. It is very important to move not to the nodejs installation directory but to the directory where we have source code. It looks nodejs needs to setup library for each projects. 
    2. 個別インストール Individual installation
      • 統計処理ライブラリインストール Statistical library installation
        • コマンドnpm install simple-statisticsで統計処理パッケージのインストール。Use command 'npm install simple-statistics' to install.
        • var ss = require('simple-statistics')をプログラム中で宣言。In the program, define var ss = require('simple-statistics').
      • 日付ライブラリーインストール Date library installation
        • コマンドnpm install date-utilsでインストール。Run command 'npm install date-utils' to install.
        • require('date-utils')をプログラム中で宣言。Declare 'require('date-utils')' in the program.
      • Tensorflowインストール Tensorflow installation
        • コマンドnpm install @tensorflow/tfjs-nodeでTensorflow.jsの高速版nodejsをインストール(新しいCPUの場合)。CPUが古い場合、コマンドはnpm install @tensorflow/tfjs。Use comand 'npm install @tensorflow/tfjs-node' for fast Tensorflow.js on nodejs(Only for new CPU). When CPU is old, command is 'npm install @tensorflow/tfjs'.
        • var tf = require('@tensorflow/tfjs-node')をプログラム中で宣言(新しいCPUの場合)。CPUが古い場合、プログラムはvar tf = require('@tensorflow/tfjs')。In program, define var tf = require('@tensorflow/tfjs-node') when CPU is new.  If CPU is old, define 'var tf = require('@tensorflow/tfjs')'.
      • インストールディレクトリにディレクトリnode_modulesが生成され、その下にインストールしたsimple-statistics、date-utils、Tensorflowを確認。After installation, you can see directory node_modules created in the installation directory under which you can confirm simple-statistics、date-utils、Tensorflow.
    3. プログラムをWeb版からNodejs版に移植 Transplant program from Web version to Nodejs version
      • プログラムの最初の方でライブラリ読込。Load library at the beginning of the program. 
        • var ss = require('simple-statistics')
        • var tf = require('@tensorflow/tfjs-node')。低速CPUはFor slow CPU, var tf = require('@tensorflow/tfjs')
        • require('date-utils');
      • Web画面が無いため、GUI関連部分はすべて消去、書き換え。Since there is no Web screen, scratch out or rewrite for whole GUI part. 
なお、古いPCでそのままtfjsのまま走らせていると、次のメッセージがコンソールに表示されるのはかわいい。Furthermore, if you run on tfjs basis on old PC, below messages are shown up on console, that is cute. 
============================
Hi there �. Looks like you are running TensorFlow.js in Node.js. To speed things up dramatically, install our node backend, which binds to TensorFlow C++, by running npm i @tensorflow/tfjs-node, or npm i @tensorflow/tfjs-node-gpu if you have CUDA. Then call require('@tensorflow/tfjs-node'); (-gpu suffix for CUDA) at the start of your program. Visit https://github.com/tensorflow/tfjs-node for more details.
============================