Windows上安裝Tensorflow

1,TensorFlow只支持64位平臺
2,下載python 3.5.X (必須是64位的),地址如下:
https://www.python.org/ftp/python/3.5.2/python-3.5.2-amd64.exe
https://www.python.org/ftp/python/3.5.2/python-3.5.2-embed-amd64.zip
3,安裝python,並驗證python和pip
C:\Users\Administrator>python -V
Python 3.5.2

C:\Users\Administrator>pip3 -V
pip3 8.1.1 from d:\program files\python35\lib\site-packages (python 3.5)
4,安裝TensorFlow(only cpu)
pip3 install --upgrade tensorflow
5,TensorFlow的hello world
C:\Users\Administrator>python

Python 3.5.2 (v3.5.2:4def2a2901a5, Jun 25 2016, 22:18:55) [MSC v.1900 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.

>>> import tensorflow as tf
>>> hello = tf.constant('Hello tensorflow1')
>>> session = tf.Session()
>>> print(session.run(hello))

b'Hello tensorflow1'

看到輸出b'Hello tensorflow1'即證明TensorFlow環境安裝完成.
6,如果你是java開發的話,可能想嘗試下java API,結果很讓人失望,太慢了,世界末日了,代碼如下,自己去享受一把:
public String tf() throws Exception{
    final String value = "Hello tensorflow version :  "  + TensorFlow.version();

    System.out.println(value);

    //將數據轉爲tensor
    Tensor tensor = Tensor.create(value.getBytes("UTF-8"));

    //構造一個圖對象
    Graph graph = new Graph();
    graph.opBuilder("Const","myConst").setAttr("dtype",tensor.dataType())
            .setAttr("value",tensor).build();
    //創建一個Session
    Session session = new Session(graph);
    graph.close();
    //計算輸出結果
    Tensor result = session.runner().fetch("myConst").run().get(0);
    System.out.println(result);
    return new String(result.bytesValue(),"UTF-8");
}
發佈了30 篇原創文章 · 獲贊 15 · 訪問量 8萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章