напиши реализацию функции активации GeLU с методами call(x) и grad(x, gradOutput),…
- User
напиши реализацию функции активации GeLU с методами call(x) и grad(x, gradOutput), используя только numpy и scipy
Assistant
import numpy as np from scipy.special import erf class GeLU: def __call__(self, x): # Вычисление функции активации GeLU: x * 0.5 * (1 + erf(x / sqrt(2))) return x * 0.5 * (1 + erf(x / np.sqrt(2))) def grad(self, x, grad_output): # Вычисление градиента функции GeLU # Производная GeLU: phi(x) + x * pdf(x) # где phi(x) = 0.5*(1 + erf(x/sqrt(2))), pdf(x) = (1/sqrt(2*pi)) * exp(-x²/2) sqrt_2 = np.sqrt(2) x_div_sqrt2 = x / sqrt_2 phi = 0.5 * (1 + erf(x_div_sqrt2)) # Вычисление плотности вероятности (pdf) для x pdf = np.exp(-0.5 * x**2) / np.sqrt(2 * np.pi) # Производная GeLU gelu_derivative = phi + x * pdf # Возвращаем градиент с учетом входного grad_output return gelu_derivative * grad_output
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.