最新版 |
編集中の文章 |
1行目: |
1行目: |
− | '''TensorFlowSharp'''<ref>https://github.com/migueldeicaza/TensorFlowSharp</ref>とは、[[TensorFlow]]を[[Mono]]から叩けるようにしたバインディングライブラリである。 | + | '''TensorFlowSharp'''<ref>https://github.com/migueldeicaza/TensorFlowSharp</ref>とは、TensorFlowを[[Mono]]から叩けるようにしたバインディングライブラリである。 |
| | | |
| == 概要 == | | == 概要 == |
− | [[Mono]]の神様である[[Miguel de Icaza]]氏のツイッターから流れてきた。
| + | Miguel de Icaza氏のツイッターから流れてきた。 |
− | [[ソースコード]]レベルでの[[移植]]ではなくバインディングなので事前の環境構築が必要となる。
| + | ソースコードレベルでの移植ではなくバインディングなので事前の環境構築が必要となる。 |
− | 事前に構築した[[データベース]]をもとに[[Xamarin]]と[[スマホ]]単体で[[人工知能]]が使えるようになると色々おもしろいかもしれない。
| |
| | | |
| == 環境構築 == | | == 環境構築 == |
14行目: |
13行目: |
| ln -s /usr/local/lib/libtensorflow.so /usr/local/lib/libtensorflow.dylib | | ln -s /usr/local/lib/libtensorflow.so /usr/local/lib/libtensorflow.dylib |
| | | |
− | == 公式サンプルを動かしてみる ==
| |
| TensorFlowSharpの[[ソースコード]](サンプル含む)を落としてくる。 | | TensorFlowSharpの[[ソースコード]](サンプル含む)を落としてくる。 |
| git clone https://github.com/migueldeicaza/TensorFlowSharp.git | | git clone https://github.com/migueldeicaza/TensorFlowSharp.git |
28行目: |
26行目: |
| TFWhileParams result = TF_NewWhile (handle, inputs, inputs.Length, cstatus.handle); | | TFWhileParams result = TF_NewWhile (handle, inputs, inputs.Length, cstatus.handle); |
| | | |
− | == プロジェクトを作ってみる == | + | == プロジェクトを作る == |
| + | * フレームワーク:.NET Framework 4.6.1 |
| * プラットフォーム:x64 ←これ重要 | | * プラットフォーム:x64 ←これ重要 |
− | *: Xamarin Studioで「コンソールアプリケーション」を作るとデフォルトで「x86」となっているので変更する。libtensorflowが64ビット必須であるため「x86」にしたままビルドすると実行時に落ちる。必ずx64でビルドする必要がある。
| + | |
− | * フレームワーク:.NET Framework 4.6.1以上
| + | libtensorflowが64ビット必須であるため、プラットフォームターゲットを「x86」にしたままビルドすると実行時に落ちる。 |
− | *: Xamarin Studioで「コンソールアプリケーション」を作るとデフォルトで「.NET Framework 4.5」となっているので変更する。これをやらないとTensorFlowSharpを参照に追加する際にグレーアウトしている。
| + | 必ずx64でビルドする必要がある。 |
− | *: [[Visual Studio for Mac]]でプロジェクトを新規作成すると.NET Framework 4.7が選ばれるはずなので変更する必要はない。
| |
− | * TensorFlowSharpへの「参照」を追加する。
| |
− | *: <s>NuGetにもTensorFlowSharpCoreというライブラリがあるようだが何故かパッケージ追加に失敗する。TensorFlowSharpのソースコードを落としてきてプロジェクトごと参照に追加する方法だと問題ない。</s>
| |
− | *: NuGet版も正常に動くようになっている。「TensorFlowSharp」で検索していれる。
| |
| | | |
| == サンプル == | | == サンプル == |
| + | とりあえず「c = a + b」という式をTensorFlowに投げて実行してみる。 |
| | | |
− | === 足し算 === | + | <source lang="csharp"> |
− | とりあえず「c = a + b」という式をTensorFlowに投げて実行してみる。<syntaxhighlight>
| |
| using System; | | using System; |
| using System.Collections.Generic; | | using System.Collections.Generic; |
68行目: |
63行目: |
| } | | } |
| } | | } |
− | </syntaxhighlight> | + | </source> |
− | | |
− | === 保存と読込 ===
| |
− | 1年前はできなかった保存と読込ができるようになったぞ。トレーニングも捗るな。<syntaxhighlight>
| |
− | using System;
| |
− | using System.Collections.Generic;
| |
− | using System.Linq;
| |
− | using System.Text;
| |
− | using System.Threading.Tasks;
| |
− | using TensorFlow;
| |
− | | |
− | namespace tensor1
| |
− | {
| |
− | class MainClass
| |
− | {
| |
− | public static void Main(string[] args)
| |
− | {
| |
− | using (var graph = new TFGraph())
| |
− | {
| |
− | Console.WriteLine("-- save --");
| |
− | using (var session = new TFSession(graph))
| |
− | {
| |
− | // 数値を作る
| |
− | var a = session.Graph.Const(30, "a");
| |
− | var b = session.Graph.Const(12, "b");
| |
− | | |
− | // 足し算
| |
− | var multiplyResults = session.GetRunner().Run(session.Graph.Add(a, b));
| |
− | var multiplyResultValue = multiplyResults.GetValue();
| |
− | Console.WriteLine("a + b = {0}", multiplyResultValue);
| |
− | | |
− | // 保存
| |
− | session.SaveTensors("saved.tsf", ("a", a), ("b", b));
| |
− | }
| |
− | | |
− | Console.WriteLine("-- load --");
| |
− | using (var session = new TFSession())
| |
− | {
| |
− | // 読込
| |
− | var a = session.RestoreTensor("saved.tsf", "a", TFDataType.Int32);
| |
− | var b = session.RestoreTensor("saved.tsf", "b", TFDataType.Int32);
| |
− | // 足し算
| |
− | var multiplyResults = session.GetRunner().Run(session.Graph.Add(a, b));
| |
− | var multiplyResultValue = multiplyResults.GetValue();
| |
− | Console.WriteLine("a + b = {0}", multiplyResultValue);
| |
− | }
| |
− | }
| |
− | }
| |
− | }
| |
− | }
| |
− | </syntaxhighlight>
| |
| | | |
| == 関連項目 == | | == 関連項目 == |
127行目: |
72行目: |
| | | |
| {{stub}} | | {{stub}} |
− | <references />
| |