3 ms·
I use "something like this" (GPT4) all the time. Use a good model. Can't wait till proper open source models catch up, but they're not there yet. Here's GPT4's
by afro88 3y ago
I use "something like this" (GPT4) all the time. Use a good model. Can't wait till proper open source models catch up, but they're not there yet.
Here's GPT4's response:
```
import math
def clamped_tanh(x, n_terms=10):
"""
Approximate the hyperbolic tangent (tanh) function using a Maclaurin series expansion.
Args:
x (float): The input value for which to compute the tanh.
n_terms (int, optional): The number of terms to use in the Maclaurin series. Default is 10.
Returns:
float: The approximated tanh value.
"""
tanh_approx = 0
for n in range(n_terms):
coef = ((-1) ** n) * (2 * n + 1)
term = coef * (x ** (2 * n + 1)) / math.factorial(2 * n + 1)
tanh_approx += term
# Clamping the tanh approximation to the range [-1, 1]
tanh_approx = max(-1, min(tanh_approx, 1))
return tanh_approx
# Example usage
x = 0.5
result = clamped_tanh(x)
print(f"clamped_tanh({x}) = {result}")
```
- dvt 3y agoI don't mean to get in the weeds here, but this is still bad, as the Padé approximation[1] (which is what you're actually really looking for) is magnitudes better than the Maclaurin series (which is just a special case of the Taylor expansion). Keep in mind that I'm not even an expert (I merely earned a minor in math). In fact, I only know this because I did some research years ago on rational approximations of hyperbolic functions. The right answer gets clipped to -1 or 1 beyond (-3, 3), but it should look like: `x * ( 27 + x * x ) / ( 27 + 9 * x * x )`—though the coefficients (and clip range) can vary. [1] https://en.wikipedia.org/wiki/Pad%C3%A9_approximant https://en.wikipedia.org/wiki/Pad%C3%A9_approximant
- afro88 3y agoAh. It weakens my point, but I'll say it anyway: you do need to be able to tell the difference between good and bad responses to use it to make you more efficient. I definitely wouldn't use it blindly for things I don't know much about at this point. Pretending I know my Pade from my Maclaurian, I'd follow up with: "use the better Pade approximation".