Quantization lowers the numerical precision used for inference. A trained model normally has float32 weights; converting them to int8 can make an inference-only model four times smaller while keeping accuracy close to the original. That matters when memory, power, or latency is limited. section 6.3.2 section 18.3.6
Quantize a trained Keras model
With an existing model, use `model |> quantize_weights("int8")`, then obtain predictions with `model |> predict(...)`. This is the practical route; implementing the conversion by hand is usually unnecessary. section 18.3.6 section 6.3.2
Why not simply cast every value to int8? Small values could round to zero. Instead, an abs-max scheme scales each input tensor to use the available −127 to 127 range, rounds and casts it, and performs the matrix multiplication in int8. Casting the result back to float32 and dividing by the input scales approximately restores the original result. The remaining difference comes from rounding. section 18.3.6
abs_max_quantize <- function(value) {
abs_max <- op_max(op_abs(value), keepdims = TRUE) #1
scale <- op_divide(127, abs_max + 1e-7) #2
scaled_value <- value * scale #3
scaled_value <- op_clip(op_round(scaled_value), -127, 127) #4
scaled_value <- op_cast(scaled_value, dtype = "int8") #5
list(scaled_value, scale)
}
.[int_x, x_scale] <- abs_max_quantize(x)
.[int_kernel, kernel_scale] <- abs_max_quantize(kernel)
The R function calculates an absolute maximum, scales and clips values, then casts them to int8. section 18.3.6
int_y <- op_matmul(int_x, int_kernel) y <- op_cast(int_y, dtype = "float32") / (x_scale * kernel_scale)
The quantized matrix multiplication is cast back to float32 and divided by the two scales. section 18.3.6
Check the numerical difference
For a small matrix multiplication, the quantized output begins `[0.9843737, 0.39332393]`; the float32 output begins `[0.98, 0.40999997]`. The results are close, not identical. section 18.3.6












