Tensorflow-Go的擴充

來源:互聯網
上載者:User
這是一個建立於 的文章,其中的資訊可能已經有所發展或是發生改變。

Google的tensorflow雖然提供了go版本,但是官方的說法是:

TensorFlow provides APIs for use in Go programs. These APIs are particularly well-suited to loading models created in Python and executing them within a Go application.

意思是go的庫只是用來裝載python建立的模型,然後執行的,而且在go版本api的godoc中也寫到:

The tensorflow package currently does not have the ability to export a model to a directory from Go. This function thus currently targets loading models exported in other languages, such as using tf.saved_model.builder in Python. See: https://www.tensorflow.org/code/tensorflow/python/saved_model/

說go不能將模型匯出,而且現階段go版本的api沒有直接建立variable的op,但是通過實驗可以發現其實是可以使用的,先看/tensorflow/core/ops/state_ops.cc中variable這個op的聲明:

REGISTER_OP("VariableV2")    .Output("ref: Ref(dtype)")    .Attr("shape: shape")    .Attr("dtype: type")    .Attr("container: string = ''")    .Attr("shared_name: string = ''")    .SetIsStateful()    .SetShapeFn(shape_inference::ExplicitShape)    .Doc(R"doc(Holds state in the form of a tensor that persists across steps.Outputs a ref to the tensor state so it may be read or modified.TODO(zhifengc/mrry): Adds a pointer to a more detail documentabout sharing states in tensorflow.ref: A reference to the variable tensor.shape: The shape of the variable tensor.dtype: The type of elements in the variable tensor.container: If non-empty, this variable is placed in the given container.        Otherwise, a default container is used.shared_name: If non-empty, this variable is named in the given bucket             with this shared_name. Otherwise, the node name is used instead.)doc");

然後觀察/tensorflow/go/op/wrappers.go中調用類似的一個op叫placeholder的方法:

// A placeholder op that passes through `input` when its output is not fed.//// Arguments://  input: The default value to produce when `output` is not fed.//  shape: The (possibly partial) shape of the tensor.//// Returns A placeholder tensor that defaults to `input` if it is not fed.func PlaceholderWithDefault(scope *Scope, input tf.Output, shape tf.Shape) (output tf.Output) {    if scope.Err() != nil {        return    }    attrs := map[string]interface{}{"shape": shape}    opspec := tf.OpSpec{        Type: "PlaceholderWithDefault",        Input: []tf.Input{            input,        },        Attrs: attrs,    }    op := scope.AddOperation(opspec)    return op.Output(0)}

可以看到使用tf.OpSpec結構體,並且對特定格式把參數裝進去就可以,經過實驗,添加一個Variable的變數op到一個Scope是成功的。以此,在go版本上面做出optimizer等訓練需要的東西,只需要自己封裝好梯度計算的op,然後對變數進行增改op,完全可以做出一個擁有tensorflow-python版完整功能的api庫。

另外,有一點是官方編譯的libtensorflow.so檔案裡面是缺少contrib的內容的,具體解決辦法是在tensorflow/BUILD檔案(r1.3)的以下小節加入依賴:

cc_binary(    name = "libtensorflow.so",    linkopts = select({        "//tensorflow:darwin": [            "-Wl,-exported_symbols_list",  # This line must be directly followed by the exported_symbols.lds file            "//tensorflow/c:exported_symbols.lds",        ],        "//tensorflow:windows": [],        "//tensorflow:windows_msvc": [],        "//conditions:default": [            "-z defs",            "-s",            "-Wl,--version-script",  #  This line must be directly followed by the version_script.lds file            "//tensorflow/c:version_script.lds",        ],    }),    linkshared = 1,    deps = [        "//tensorflow/contrib:contrib_kernels",       #Add        "//tensorflow/contrib:contrib_ops_op_lib",    #Add        "//tensorflow/c:c_api",        "//tensorflow/c:exported_symbols.lds",        "//tensorflow/c:version_script.lds",        "//tensorflow/core:tensorflow",    ],)
相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.