datatools

package module
v1.1.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: May 7, 2026 License: MIT Imports: 4 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
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

func BellmanEquation(
	reward float64,
	gamma float64,
	nextMaxQ float64,
) float64

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

func DotProduct(vec1, vec2 []float64) float64

DotProduct computes the dot product of two slices.

func FindMostRelevant

func FindMostRelevant(q, e [][]float64) ([]int, error)

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

func LinearTransformation(
	input []float64,
	weights [][]float64,
	bias []float64,
) []float64

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 Normalize

func Normalize(vec []float64) []float64

Normalize computes the Euclidean norm of a slice and returns the normalized slice.

func ReLU added in v1.1.0

func ReLU(input []float64) []float64

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

func RoundSlice(vec []float64, precision int) []float64

RoundSlice helper function to round results (comparison with floats).

func ScaledDotProductAttention added in v1.1.0

func ScaledDotProductAttention(
	query, key, value [][]float64,
) [][]float64

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

func SoftmaxMatrix(matrix [][]float64) [][]float64

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.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL