简体   繁体   中英

Defining tensorflow operations in python with attributes

I am trying to register a python function and its gradient as a tensorflow operation. I found many useful examples eg:

Write Custom Python-Based Gradient Function for an Operation? (without C++ Implementation)

https://programtalk.com/python-examples/tensorflow.python.framework.function.Defun/

Nonetheless I would like to register attributes in the operation and use these attributes in the gradient definition by calling op.get_attr('attr_name').

Is this possible without going down to C implementation? May you give me an example?

Unfortunately I don't believe it is possible to add attributes without using a C++ implementation of the operation. One feature that may help though is that you can define 'private' attributes by prepending an underscore to the start. I'm not sure if this is well documented or what the long-term guarantees are, but you can try setting '_my_attr_name' and you should be able to retrieve it later.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM