三日坊主のプログラミング日誌

なんか色々書いておくところ。

Swift For TensFlow - Array input is not a constant array of tensors とエラーが出たら...

 最近、macOS上で使えるMarkdownエディタのQuiverを使って大学の研究室仲間や秋以降に研究室入りする3年生達のためにディープラーニング 用の教科書を書いてる(って言っても、ゼロから作るDeepLearningをベースに更に噛み砕いてるような物だけど...)今日この頃。

 今回は昨日に引き続き、Swift for TensorFlowのネタ。Playground上ではなく、Xcodeのプロジェクト上で走らせようと格闘していた時にで出来たエラーの解決法のお話。

 ちなみに、今回のこのページはMarkdownで書くってのが裏テーマ。

そもそも、Playgroundが重いのが全部悪い。

 ご存知の通り、Xcode内蔵のPlaygroundは手軽にSwiftの開発を行える代わりに、Swiftの特徴である強力な型推定の所為(だと思う...?)で実行結果を返すのに非常に時間がかかるので、あまり使い物になりません

 そこで、

「Playgroundじゃなくて、みんな大好き大正義Xcode先輩に頼もうぜ!」

ってなったは良いものの、以下のような単純なコードを書いて実行してみたら...

 出るわ出るわ、エラーの嵐です。Google先生に「Array input is not a constant array of tensors」って検索かけても良い感じの記事が出てこない...

f:id:Hiro2201:20180429194449p:plain
第一の関門: Array input is not a constant array of tensors

 コード自体は問題はないはずです。実際にPlaygroundでは、ついさっきまで元気(?)に動いてくれました。

英語を読む。

 では、困った時の開発元のGitHubのFAQ。とりあえず見てみましょう↓

Why do I get "error: array input is not a constant array of tensors"?

If you ran into this error, you likely wrote some code using Tensor without running Swift with optimizations (-O).

The -O flag enables optimizations and is currently required for the graph program extraction algorithm to work correctly. We're working on >making -O not required, but in the meantime you need to specify it.

Here's how to enable optimizations in different environments:

REPL: No need to add extra flags. Optimizations are on by default. Interpreter: swift -O main.swift Compiler: swiftc -O main.swift swift build: swift build -Xswiftc -O Xcode: Go to Build Settings > Swift Compiler > Code Generation > Optimization Level and select Optimize for Speed [-O]. You may also need to add libtensorflow.so and libtensorflow_framework.so to Linked Frameworks and Libraries and change Runtime Search >Paths. See this comment for specific instructions with screenshots.

   

 英語が致命的に苦手な自分ですが、Google先生の力を借りつつ簡単に今回必要そうな所だけを訳して見ると、

If you ran into this error, you likely wrote some code using Tensor without running Swift with optimizations (-O).

意訳

Swiftを最適化してないのに、Tensorを使ってコードを書いたせい。

(なるほど申し訳ない...)

Xcodeでの設定方法

Xcode: Go to Build Settings > Swift Compiler > Code Generation > Optimization Level and select Optimize for Speed [-O].

意訳

Xcodeでは、次のように設定を書きかえろよ。

Build Settings → Swift Compiler → Code Generation → Optimization Level and select Optimize for Speed [-O].

 OK、OK、毎度プロジェクトごとに設定するのは面倒くさいが、一々Xcodeを終了させてあげないと碌に動かないPlaygroundなんかに比べれば可愛良いもんだ。どれどれ...

f:id:Hiro2201:20180429201013p:plain
増えた...英語のエラーが...一杯... (O_O)

 「増えた...待てよ...バカじゃないの...コレはアレか、Googleが我々リンゴに取り憑かれてる人間に対する拷問かなにかか(白目)」と思っていると、何だかXcodeの項目になにか続きがあるじゃないか...

You may also need to add libtensorflow.so and libtensorflow_framework.so to Linked Frameworks and Libraries and change Runtime Search >Paths. See this comment for specific instructions with screenshots.

意訳

libtensorflow.solibtensorflow_framework.soフレームワークとライブラリに追加してランタイム検索パスを弄ってね。詳しくはリンク先のページにスクショ付きで書いてあるから、あとヨロ(^_−)−☆

神か

何事も手抜きは良くないですね。

早速、先人の知恵を借りに行きましょう。

github.com

 このページをここまで読んでる方が実際に弄るのはStep 2以降です。

 上のリンク先の記事にはパスしか書いてないので、以下のショートカットを実行すれば楽に目的のファイルを選択する事が出来ます。

⌘ + shift + G

  動いた...

f:id:Hiro2201:20180429204320p:plain
動いた...

 無事、XcodeでTensorFlowが動いてくれました。一回一回この設定をしてあげないといけないのは面倒ですが、まだ、Early Releaseだからしょうがないね。今後、この最適化設定をする必要がなくなるらしいので、それまでの我慢って事で。今日はここまで。良いGWを!

 

 最近、ゆるキャン△にハマって、斉藤恵那 可愛くない?ショートカット女子どストライクだし、りえりー の声最高だし、最高だわ。高木さんと言い、斉藤恵那といい、りえりー最高だな。  

 あと、FAQの読み方って、やっぱりファ◯クで合ってるのね。 このFAQの解説、全然FAQになってねえじゃんF◯CKって言ってる外国人絶対いるよな(笑)    

Quiver - take better notes

Quiver - take better notes

  • HappenApps, Inc.
  • 仕事効率化
  • ¥1,200

プライバシーポリシー お問い合わせ