INNER CODE UNIT · Go

Softmax

takara-ai/go-attention · attention/attention.go:81

func Softmax(x Vector) Vector {
	if len(x) == 0 {
		return Vector{}
	}

	maxVal := x[0]
	// Find max value for numerical stability
	for _, v := range x[1:] {
		if v > maxVal {
			maxVal = v
		}
	}

	exps := make(Vector, len(x))
	sumExp := 0.0
	for i, v := range x {
		// Subtracting maxVal before exponentiation
		exps[i] = math.Exp(v - maxVal)

View source record →

📰 Research Paper
Loading…
⏳ Fetching content…