LoginSignup
0
3

More than 3 years have passed since last update.

TensorFlow.jsチュートリアルのnpm install @tensorflow/tfjs-nodeでつまづいた話 on Mac

Last updated at Posted at 2020-04-24

背景

この記事みて、TensorFlow.jsめっちゃいいじゃんと思ってNode.jsも最近触ってるしチュートリアルやってみようとしたら初手でつまづいてキレそうになったので、メモ。
ちなみにWindows10とMacでやってWindowsはいまだに解決できないので、誰か教えてくらはい。

環境

PC: MacBook Air (Retina, 13-inch, 2018)
OS: macOS Catalina (ver 10.15.4)
プロセッサ: 1.6 GHz Intel Core i5
メモリ: 16 GB 2133 MHz LPDDR3

$ node -v

v10.16.0

$ nmp -v

6.9.0

つまづいたこと

Node.jsを使っていてnpmの人は下記をすればいいと書いてあったので、実行したらERR!がクソほどでた。
具体的にはこんなの↓

$ npm install @tensorflow/tfjs-node

de-pre-gyp install failed with error: Error: Command failed: node-pre-gyp install --fallback-to-build
node-pre-gyp WARN Using needle for node-pre-gyp https download
node-pre-gyp WARN Tried to download(404): https://storage.googleapis.com/tf-builds/pre-built-binary/napi-v5/1.7.3/
...
...
gyp: No Xcode or CLT version detected!
gyp ERR! configure error
gyp ERR! stack Error: gyp failed with exit code: 1
...
...
node-pre-gyp ERR! build error
node-pre-gyp ERR! stack Error: Failed to execute
...
...

解決した方法

ぐぐるとnode-gypのインストールでエラーが起きているらしく、同じく困っている人がたくさんいた。TensorFlow.js以外にも。

インストールは公式(手順はここを参照)に載っている方法でする。

$ npm install -g node-gyp

ただし、これでもうまくいかず。
よくみるとCatalinaの人は

If your Mac has been upgraded to macOS Catalina (10.15), please read macOS_Catalina.md

と書かれていたので、公式(手順はここを参照)に載っている対処法を実施してXcode Command Line Toolsというのをインストールする。

Acidテストとかいうのをしてテストに合格したら無事に先ほどのnpm install -g node-gypができるということらしい。

僕の場合は一度削除するコマンドを実行して再度Xcode Command Line Toolsをインストール(30分ほどでインストールが終わる)

やっとターミナルに戻って再度

$ npm install @tensorflow/tfjs-node

を実行すればエラー無くインストールできた。

チュートリアルにある下記コードをコピペして

sample.js

const tf = require('@tensorflow/tfjs');

// オプションとしてバインディングを読み込み
// GPU上で動作する場合は'@tensorflow/tfjs-node-gpu'を使用してください
require('@tensorflow/tfjs-node');

// 単純なモデルを訓練
const model = tf.sequential();
model.add(tf.layers.dense({units: 100, activation: 'relu', inputShape: [10]}));
model.add(tf.layers.dense({units: 1, activation: 'linear'}));
model.compile({optimizer: 'sgd', loss: 'meanSquaredError'});

const xs = tf.randomNormal([100, 10]);
const ys = tf.randomNormal([100, 1]);

model.fit(xs, ys, {
  epochs: 100,
  callbacks: {
    onEpochEnd: (epoch, log) => console.log(`Epoch ${epoch}: loss = ${log.loss}`)
  }
});


実行する

$ node sample.js

Epoch 1 / 100
eta=0.0 ========================================================>
76ms 764us/step - loss=1.33
Epoch 0: loss = 1.332172155380249
...
...
Epoch 99 / 100
eta=0.0 ========================================================>
40ms 404us/step - loss=0.632
Epoch 98: loss = 0.6319155097007751
Epoch 100 / 100
eta=0.0 ========================================================>
26ms 255us/step - loss=0.628
Epoch 99: loss = 0.6275108456611633

なんか機械学習ぽい単語が出てるので多分ヨシッ!

その他

どうやら、nodejsのバージョンとかでも影響するぽいので、nodebrewでバージョンを変えてみるのもあり

今入っているバージョン羅列

$ nodebrew list

v10.16.0
v11.1.0
v12.16.1

current: v12.16.1

バージョンを変える

$ nodebrew use v10.16.0

use v10.16.0

確認

$ node -v

v10.16.0

Windows

ちなみにWindowsでもいろいろ探したがうまく実行できていない。
とりあえず、よくあるのはPowersherllを管理者権限で起動して下記を実行するやつ。

$ npm install --global --production windows-build-tools

やったけど結局同じエラー。

あとは一応、ERRという表記がなくなったのはこれ。

$ npm install @tensorflow/tfjs-node@1.2.7

ただし、sample.js を実行させたところ下記のように出てしまったので、思考停止してMacに浮気。

internal/modules/cjs/loader.js:1206
  return process.dlopen(module, path.toNamespacedPath(filename));
                 ^

Error: The specified module could not be found.
....
0
3
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
3