イントロダクション
前回まではPythonでのTensorFlowのチュートリアルを一通り(Text Classificationまで)やりました。
- Python Tensorflow 〜初めての人工知能〜
- Tensorflow Keras〜初めのトレーニング_1〜
- Tensorflow Keras〜初めのトレーニング_2:前処理〜
- TensorFlow Keras〜テキストの分類〜
- TensorFlow Keras〜回帰、準備から予測まで〜
今回はJavaで、もう少し深掘りしてみようと思います。参考サイトはこちら
Javaでの環境構築
eclipseでMavenプロジェクトを作成します。(シンプルプロジェクトでもWebProjectでも大丈夫)。そして、POMファイルを編集します。参考サイトの記載を追加してMavenインストールを行うだけです。
サンプルとして、既存のプロジェクトですが以下のようになっています、POMファイルがある状態でMavenプロジェクトになっていればOKです。
<元のPOMファイル>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>zenryokuservice</groupId> <artifactId>gui.mtm</artifactId> <version>0.0.1-SNAPSHOT</version> <packaging>jar</packaging> <name>gui.mtm</name> <url>http://maven.apache.org</url> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> </properties> <dependencies> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>3.8.1</version> <scope>test</scope> </dependency> <dependency> <groupId>org.joml</groupId> <artifactId>joml</artifactId > <version>1.9.6</version > </dependency> <dependency> <groupId>org.l33tlabs.twl</groupId> <artifactId>pngdecoder</artifactId> <version>1.0</version> </dependency> </dependencies> </project>
<変更後POMファイル>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>zenryokuservice</groupId> <artifactId>gui.mtm</artifactId> <version>0.0.1-SNAPSHOT</version> <packaging>jar</packaging> <name>gui.mtm</name> <url>http://maven.apache.org</url> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> </properties> <dependencies> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>3.8.1</version> <scope>test</scope> </dependency> <dependency> <groupId>org.joml</groupId> <artifactId>joml</artifactId > <version>1.9.6</version > </dependency> <dependency> <groupId>org.l33tlabs.twl</groupId> <artifactId>pngdecoder</artifactId> <version>1.0</version> </dependency> <!-- TensorFlow --> <dependency> <groupId>org.tensorflow</groupId> <artifactId>tensorflow</artifactId> <version>1.12.0</version> </dependency> </dependencies> </project>
太字の部分を追記しました。
一番しから3行文がTensorFlowのライブラリです。
この状態でサンプルコードを実行するとエラリます。。。
Exception in thread "main" java.lang.UnsatisfiedLinkError: Cannot find TensorFlow native library for OS: darwin, architecture: x86_64. See https://github.com/tensorflow/tensorflow/tree/master/tensorflow/java/README.md for possible solutions (such as building the library from source). Additional information on attempts to find the native library can be obtained by adding org.tensorflow.NativeLibrary.DEBUG=1 to the system properties of the JVM. at org.tensorflow.NativeLibrary.load(NativeLibrary.java:77) at org.tensorflow.TensorFlow.init(TensorFlow.java:66) at org.tensorflow.TensorFlow.<clinit>(TensorFlow.java:70) at org.tensorflow.Graph.<clinit>(Graph.java:361) at zenryoku.servise.app.autoblog.AutoBlogPoster.helloTensorflow(AutoBlogPoster.java:225) at zenryoku.servise.app.autoblog.AutoBlogPoster.main(AutoBlogPoster.java:258)
ネイティブライブラリが足りないようです。参考サイトよりネイティブライブラリをダウンロードします。自分の端末はMacなのでmacのものを選択します。
そして解凍します。ここのフォルダをネイティブライブラリのパスとして登録します。
そして、プロジェクトのプロパティを開き以下のようにネイティブライブラリのパスを設定します。Java Build Path -> Native Library Location -> Edit
そして実行すると以下のようにバージョン番号が出力されます。
とりあえずはこれでTensorFlow in Java が起動できます。
[rakuten ids="onetrust:10012931"]