2016-11-17 4 views
0

lineがあります:定数Tensorの作成時に "CopyFrom"が使用されるのはなぜですか?定数テンソルの作成プロセス中に

tensor_value.tensor.CopyFrom(
     tensor_util.make_tensor_proto(value, dtype=dtype, shape=shape)) 

CopyFromは、新しく作成されたテンソルプロトのコピーを作成します。しかし、文書によればmake_tensor_protoが新しいオブジェクトを作成するので、これは対処するためのリソースの無駄のように見えます。それだけで次に行うこと、より十分であろう:

tensor_value.tensor = 
     tensor_util.make_tensor_proto(value, dtype=dtype, shape=shape) 

これは、新しいオブジェクトを作成しないでください、それに加えてもoneofのいるProtobufフィールドの有効な使用です。

+0

良い点を私はCopyFromを削除してテストを行っています。 – yuefengz

答えて

4

このdocで説明したようにあなたがprotoの分野にプロトを割り当てることができません。https://developers.google.com/protocol-buffers/docs/reference/python-generated

You cannot assign a value to an embedded message field. Instead, assigning a value to any field within the child message implies setting the message field in the parent.

あなたはCopyFromを削除すると、次のエラーを取得します:

AttributeError: Assignment not allowed to field "tensor" in protocol message object. 
関連する問題