MCPcopy Create free account
hub / github.com/tensorpack/tensorpack / fw

Function fw

examples/DoReFa-Net/dorefa.py:21–36  ·  view source on GitHub ↗
(x)

Source from the content-addressed store, hash-verified

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:

Callers 3

binarize_weightMethod · 0.85
new_get_variableMethod · 0.85
new_get_variableMethod · 0.85

Calls 2

_signFunction · 0.85
quantizeFunction · 0.85

Tested by

no test coverage detected