More Related Content Similar to TensorFlow Operation 作ってみた (20) TensorFlow Operation 作ってみた7. 今回作ったOperationの紹介
❏ Operation name “string_add”
❏ 文字列を数値として扱い、それを加算させる。(tf.addは文字列連結になります。)
❏ 特に便利という訳ではありません。勉強用にTensorFlowにないOperationを実装しました。
❏ 新規API(tf.string_add)で呼び出せる。
❏ Eigenではなく、組み込み演算子で実装。
❏ ソースコード公開します。
❏ GitHub(stakky/tensorflow)
❏ ビルド方法など環境構築に関しては”付録”スライドを参照してください。
❏ おまけも作りました。(説明は省略します。)
❏ “hello” :”Hello”と叫ぶだけです。
# tf.add の実行例
>>> a = tf.constant("3")
>>> b = tf.constant("4")
>>> c = tf.add(a,b)
>>> tf.Session().run(c)
'34' これを7にしたい!!
10. OpKernel概要
❏ OpKernelとは、Operationが持っている実行コードである。
❏ OpKernelは各device(or data_type, etc...)ごとに最適化されている。
参考:https://www.tensorflow.org/guide/extend/architecture#kernel_implementations
Kernel Implementations
The runtime contains over 200 standard operations including mathematical,
array manipulation, control flow, and state management operations. Each of
these operations can have kernel implementations optimized for a variety
of devices.
12. OpKernel::Compute()
❏ OpKernelの計算が実装されるメソッド。
❏ REGISTER_KERNEL_BUILDERマクロで指定したclassで実装する。
❏ Eigenを使用せず、c++組み込み演算子で実装しました。
❏ 文字列からint型の変換など、詳細は GitHubを参照して下さい。
参考:https://github.com/tensorflow/tensorflow/blob/master/tensorflow/core/framework/op_kernel.h#L93-L110
class StringAddOp : public OpKernel {
Public:
void Compute(OpKernelContext* context) override {
//(snipping some lines)
for (int64 i = 0; i < N; i++) {
output_data[i] = input0_data[i] + input1_data[i];
}
LOG(INFO) << "tfinternal!!!";
}
};
13. その他Topics(pythonAPI, BUILD, etc...)
❏ 新規pythonAPIを生成するには以下の手順が必要です。
❏ “tensorflow/core/api_def/update_api_def.sh”を実行する。
❏ “api_def*.pbtxt”が生成されていることを確認する。
❏ “--nouse_action_cache”のオプションを付けて buildする(clean buildが必要のようです)
[tensorflow/core/api_def/update_api_def.sh]
# Script to create tensorflow/core/api_def/base_api/api_def*.pbtxt
# files for new ops.
set -e
current_file="$(readlink -f "$0")"
current_dir="$(dirname "$current_file")"
bazel build //tensorflow/core/api_def:update_api_def
bazel-bin/tensorflow/core/api_def/update_api_def
--api_def_dir="${current_dir}/base_api"
--op_file_pattern="${current_dir}/../ops/*_ops.cc"
16. 付録:環境構築
❏ ビルド手順
❏ 基本的には公式ページ通りにすればできます。
❏ gccとbazelのversionに注意が必要です。
❏ 参考:https://www.tensorflow.org/install/source#tested_build_configurations
❏ ビルドマシン環境
❏ かなりリソース食います。 (メモリ少ないと落ちるかも )
❏ GCPなどのcloud instanceを試してみるのもありかもしれません。
❏ 参考:https://cloud.google.com/compute/