MCPcopy Create free account
hub / github.com/tensorflow/tfjs-examples / quantize

Function quantize

quantization/draw_quantization.py:23–51  ·  view source on GitHub ↗

Simulate weight quantization. Args: w: (a numpy.ndarray) The weight to be quantized. bits: (int) number of bits used for the quantization: 8 or 16. Returns: A tuple with three elements: w_quantized: the quantized version of w, represented as an uint8- or uint16-typ

(w, bits)

Source from the content-addressed store, hash-verified

21
22
23def quantize(w, bits):
24 """
25 Simulate weight quantization.
26
27 Args:
28 w: (a numpy.ndarray) The weight to be quantized.
29 bits: (int) number of bits used for the quantization: 8 or 16.
30
31 Returns:
32 A tuple with three elements:
33 w_quantized: the quantized version of w, represented as an uint8-
34 or uint16-type numpy.ndarray.
35 w_min: Minimum value of w, required for dequantization.
36 w_max: Maximum value of w, required for dequantization.
37 """
38 if bits == 8:
39 dtype = np.uint8
40 elif bits == 16:
41 dtype = np.uint16
42 else:
43 raise ValueError('Unsupported bits of quantization: %s' % bits)
44
45 w_min = np.min(w)
46 w_max = np.max(w)
47 if w_max == w_min:
48 raise ValueError('Cannot perform quantization because w has a range of 0')
49 w_quantized = np.array(
50 np.floor((w - w_min) / (w_max - w_min) * np.power(2, bits)), dtype)
51 return w_quantized, w_min, w_max
52
53
54def dequantize(w_quantized, w_min, w_max):

Callers 1

mainFunction · 0.85

Calls

no outgoing calls

Tested by

no test coverage detected