(x)
| 19 | return _quantize(x) |
| 20 | |
| 21 | def fw(x): |
| 22 | if bitW == 32: |
| 23 | return x |
| 24 | |
| 25 | if bitW == 1: # BWN |
| 26 | E = tf.stop_gradient(tf.reduce_mean(tf.abs(x))) |
| 27 | |
| 28 | @tf.custom_gradient |
| 29 | def _sign(x): |
| 30 | return tf.where(tf.equal(x, 0), tf.ones_like(x), tf.sign(x / E)) * E, lambda dy: dy |
| 31 | |
| 32 | return _sign(x) |
| 33 | |
| 34 | x = tf.tanh(x) |
| 35 | x = x / tf.reduce_max(tf.abs(x)) * 0.5 + 0.5 |
| 36 | return 2 * quantize(x, bitW) - 1 |
| 37 | |
| 38 | def fa(x): |
| 39 | if bitA == 32: |
no test coverage detected