gpt4 book ai didi

java - 将 Tensorflow 添加到 Java Project Netbeans

转载 作者:行者123 更新时间:2023-11-29 04:31:21 25 4
gpt4 key购买 nike

我正在尝试根据此链接中的建议为 java 安装 Tensorflow...

https://www.tensorflow.org/install/install_java#install_on_windows

说明说明下载 .jar 文件和一个单独的 .dll 文件。我已将 jar 文件包含到 netbeans 项目中,并设置上面网页上列出的代码。

package tensorflowtest;
import org.tensorflow.Graph;
import org.tensorflow.Session;
import org.tensorflow.Tensor;
import org.tensorflow.TensorFlow;


public class TensorFlowTest {

public static void main(String[] args) throws Exception {
try (Graph g = new Graph()) {
final String value = "Hello from " + TensorFlow.version();

// Construct the computation graph with a single operation, a constant
// named "MyConst" with a value "value".
try (Tensor t = Tensor.create(value.getBytes("UTF-8"))) {
// The Java API doesn't yet include convenience functions for adding operations.
g.opBuilder("Const", "MyConst").setAttr("dtype", t.dataType()).setAttr("value", t).build();
}

// Execute the "MyConst" operation in a Session.
try (Session s = new Session(g);
Tensor output = s.runner().fetch("MyConst").run().get(0)) {
System.out.println(new String(output.bytesValue(), "UTF-8"));
}
}
}

}

运行时出现错误

Exception in thread "main" java.lang.UnsatisfiedLinkError: Cannot find TensorFlow native library for OS: windows, architecture: x86. See https://github.com/tensorflow/tensorflow/tree/master/java/README.md for possible solutions (such as building the library from source).
at org.tensorflow.NativeLibrary.load(NativeLibrary.java:66)
at org.tensorflow.TensorFlow.init(TensorFlow.java:27)
at org.tensorflow.TensorFlow.<clinit>(TensorFlow.java:31)
at org.tensorflow.Graph.<clinit>(Graph.java:194)
at tensorflowtest.TensorFlowTest.main(TensorFlowTest.java:11)

我知道这是因为没有找到 .dll 文件,但我已经尝试将 .dll 放在所有根文件中,我已经尝试将 dll 添加到我的源和/或库中,并且我已经尝试添加命令-Djava.library.path=。到我在 Netbeans 中的 VM 选项并尝试添加 System.setProperty("java.library.path", ".");到我的主要功能的开始,都没有成功。我还尝试以“C:\Path\To\File”的形式提供 dll 的直接路径

如有任何关于如何解决此问题的建议,我们将不胜感激。

最佳答案

如果文件位于运行它的目录中,它应该首先工作(我错过了什么吗?)在 VM 选项下设置正确的绝对路径对我有用:

-Djava.library.path="C:\Program Files\Java\tensorflow"

这样您就不必在项目之间来回复制 dll。

另请参阅:giving 'java.library.path' in netbeans for .dll/.so files

关于java - 将 Tensorflow 添加到 Java Project Netbeans,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43711054/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com