(x)
| 47 | @tf.custom_gradient |
| 48 | def _identity(input): |
| 49 | def grad_fg(x): |
| 50 | rank = x.get_shape().ndims |
| 51 | assert rank is not None |
| 52 | maxx = tf.reduce_max(tf.abs(x), list(range(1, rank)), keep_dims=True) |
| 53 | x = x / maxx |
| 54 | n = float(2**bitG - 1) |
| 55 | x = x * 0.5 + 0.5 + tf.random_uniform( |
| 56 | tf.shape(x), minval=-0.5 / n, maxval=0.5 / n) |
| 57 | x = tf.clip_by_value(x, 0.0, 1.0) |
| 58 | x = quantize(x, bitG) - 0.5 |
| 59 | return x * maxx * 2 |
| 60 | |
| 61 | return input, grad_fg |
| 62 |