Documentation
¶
Index ¶
- Variables
- func BellmanEquation(reward float64, gamma float64, nextMaxQ float64) float64
- func DotProduct(vec1, vec2 []float64) float64
- func FindMostRelevant(q, e [][]float64) ([]int, error)
- func LinearTransformation(input []float64, weights [][]float64, bias []float64) []float64
- func Normalize(vec []float64) []float64
- func ReLU(input []float64) []float64
- func RoundSlice(vec []float64, precision int) []float64
- func ScaledDotProductAttention(query, key, value [][]float64) [][]float64
- func SoftmaxMatrix(matrix [][]float64) [][]float64
Constants ¶
This section is empty.
Variables ¶
var ErrInputMatricesCannotBeEmpty = errors.New("input matrices cannot be empty")
ErrInputMatricesCannotBeEmpty is returned when either the query matrix (q) or the embeddings matrix (e) is empty, or when their inner vectors have zero length.
Functions ¶
func BellmanEquation ¶ added in v1.1.0
BellmanEquation calculates the target Q-learning value using the Bellman Equation.
Formula:
Q(s, a) = r + γ * max(Q(s', a'))
Where:
- r = immediate reward
- γ = discount factor (gamma)
- max(Q(s', a')) = highest Q value of the next state
Parameters:
- reward: immediate reward received
- gamma: discount factor between 0 and 1
- nextMaxQ: highest estimated Q value for the next state
Return:
- updated value of the Bellman equation
func DotProduct ¶
DotProduct computes the dot product of two slices.
func FindMostRelevant ¶
FindMostRelevant returns a slice of indices ordered by relevance, or an error if the input matrices are invalid.
- q is the query embedding
- e is the matrix of embeddings to search in.
func LinearTransformation ¶ added in v1.1.0
LinearTransformation performs a linear transformation on an input vector. It computes the result using the formula:
y = xWᵀ + b
Where:
- x is the input vector (1xn)
- W is the weight matrix (mxn)
- b is the bias vector (1xm)
- y is the output vector (1xm)
Each element y[i] is calculated as:
y[i] = Σ (input[j] * weights[i][j]) + bias[i]
func ReLU ¶ added in v1.1.0
ReLU applies the Rectified Linear Unit activation function to a slice of float64.
It uses the formula and processes elements concurrently: f(x) = max(0, x)
For more information on ReLU, see: https://en.wikipedia.org/wiki/Rectifier_(neural_networks)
func RoundSlice ¶
RoundSlice helper function to round results (comparison with floats).
func ScaledDotProductAttention ¶ added in v1.1.0
ScaledDotProductAttention computes the attention mechanism used in Transformer models. It maps a query and a set of key-value pairs to an output.
The attention score is calculated in three steps:
1. Compute the similarity between queries and keys:
scores = QK^T
2. Scale the scores by the square root of the key dimension:
scaled_scores = scores / √d_k
3. Apply softmax to obtain attention weights and multiply by V:
Attention(Q, K, V) = softmax(scaled_scores) · V
Full formula:
:contentReference[oaicite:0]{index=0}
Where:
- Q: Query matrix
- K: Key matrix
- V: Value matrix
- d_k: Dimension of the key vectors
Parameters:
- query: A matrix representing the queries of shape [n_queries, d_k].
- key: A matrix representing the keys of shape [n_keys, d_k].
- value: A matrix representing the values of shape [n_keys, d_v].
Returns:
- A matrix representing the attention output of shape [n_queries, d_v].
func SoftmaxMatrix ¶ added in v1.1.0
SoftmaxMatrix applies the Softmax function to each row of a 2D matrix.
It uses a stable implementation by subtracting the maximum value of each row to prevent numerical overflow during exponentiation. The computation is performed concurrently for each row.
Types ¶
This section is empty.