{"action":"created","issue":{"url":"https://api.github.com/repos/tensorflow/tensorflow/issues/9162","repository_url":"https://api.github.com/repos/tensorflow/tensorflow","labels_url":"https://api.github.com/repos/tensorflow/tensorflow/issues/9162/labels{/name}","comments_url":"https://api.github.com/repos/tensorflow/tensorflow/issues/9162/comments","events_url":"https://api.github.com/repos/tensorflow/tensorflow/issues/9162/events","html_url":"https://github.com/tensorflow/tensorflow/issues/9162","id":221241314,"number":9162,"title":"Stop gradient for some entry of a tensor","user":{"login":"JerrikEph","id":17830427,"avatar_url":"https://avatars1.githubusercontent.com/u/17830427?v=4","gravatar_id":"","url":"https://api.github.com/users/JerrikEph","html_url":"https://github.com/JerrikEph","followers_url":"https://api.github.com/users/JerrikEph/followers","following_url":"https://api.github.com/users/JerrikEph/following{/other_user}","gists_url":"https://api.github.com/users/JerrikEph/gists{/gist_id}","starred_url":"https://api.github.com/users/JerrikEph/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/JerrikEph/subscriptions","organizations_url":"https://api.github.com/users/JerrikEph/orgs","repos_url":"https://api.github.com/users/JerrikEph/repos","events_url":"https://api.github.com/users/JerrikEph/events{/privacy}","received_events_url":"https://api.github.com/users/JerrikEph/received_events","type":"User","site_admin":false},"labels":[],"state":"closed","locked":false,"assignee":null,"assignees":[],"milestone":null,"comments":5,"created_at":"2017-04-12T12:09:08Z","updated_at":"2018-03-28T10:09:48Z","closed_at":"2017-04-12T15:06:28Z","author_association":"CONTRIBUTOR","body":"As far as I know, the tf.stop_gradient function can only stop the gradient of a whole tensor. I recently wanted to implement a model that requires the stop of gradient for some entry of a tensor (not the whole tensor). and I have come up with an workaround,\r\n```\r\ndef entry_stop_gradients(target, mask):\r\n    mask_h = tf.logical_not(mask)\r\n    \r\n    mask = tf.cast(mask, dtype=target.dtype)\r\n    mask_h = tf.cast(mask_h, dtype=target.dtype)\r\n    \r\n    return tf.stop_gradient(mask_h * target) + mask * target\r\n```\r\n \r\nhope somebody could implement a low level version of this feature."},"comment":{"url":"https://api.github.com/repos/tensorflow/tensorflow/issues/comments/376833798","html_url":"https://github.com/tensorflow/tensorflow/issues/9162#issuecomment-376833798","issue_url":"https://api.github.com/repos/tensorflow/tensorflow/issues/9162","id":376833798,"user":{"login":"asimshankar","id":16018,"avatar_url":"https://avatars2.githubusercontent.com/u/16018?v=4","gravatar_id":"","url":"https://api.github.com/users/asimshankar","html_url":"https://github.com/asimshankar","followers_url":"https://api.github.com/users/asimshankar/followers","following_url":"https://api.github.com/users/asimshankar/following{/other_user}","gists_url":"https://api.github.com/users/asimshankar/gists{/gist_id}","starred_url":"https://api.github.com/users/asimshankar/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/asimshankar/subscriptions","organizations_url":"https://api.github.com/users/asimshankar/orgs","repos_url":"https://api.github.com/users/asimshankar/repos","events_url":"https://api.github.com/users/asimshankar/events{/privacy}","received_events_url":"https://api.github.com/users/asimshankar/received_events","type":"User","site_admin":false},"created_at":"2018-03-28T10:09:48Z","updated_at":"2018-03-28T10:09:48Z","author_association":"MEMBER","body":"Does [`tf.custom_gradient`](https://github.com/tensorflow/tensorflow/blob/r1.7/tensorflow/python/ops/custom_gradient.py#L33) (in 1.7, `tf.contrib.eager.custom_gradient` in 1.6) help?\r\n\r\nYou can use that to define the custom gradient that appropriately masks out portions that are of interest.\r\n\r\nIf not, the workaround suggested in the original posting seems reasonable. \r\nI'm perhaps misunderstanding what you'd want as a feature. "}}
{"actor":{"display_login":"asimshankar"}}