'pytorch implementation of tanh
I asked another question on why tanh in pytorch is faster than numpy, and someone told me that pytorch uses a lookup table instead of actually computing the tanh function.
I searched the code in pytorch git repo and found a tanh.c file. Unfortunately I don't understand cpp language, here's what I think how it roughly translate to python:
import numpy as np
lookup_table = np.arange(256)
scaled_min = output_min
scaled_max = output_max
for i in range(256):
x = input_scale * (i - input_zero_point)
scaled_tanh_x = 128.0 * np.tanh(x) + 128.0
if scaled_tanh_x < scaled_min:
scaled_tanh_x = scaled_min
if scaled_tanh_x > scaled_max:
scaled_tanh_x = scaled_max;
lookup_table[i] = np.rint(scaled_tanh_x)
and I don't know what are the value of those parameters... Can someone help me understand how pytorch constructs its tanh lookup table, and how it's used in computing tanh of an array, preferably using python and numpy. Thanks!
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
