transformer

package
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: Aug 4, 2025 License: Apache-2.0 Imports: 10 Imported by: 1

Documentation

Overview

Package transformer provides transformer-related neural network layers.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Block

type Block[T tensor.Numeric] struct {
	// contains filtered or unexported fields
}

Block implements a single Transformer encoder block.

func NewTransformerBlock

func NewTransformerBlock[T tensor.Numeric](engine compute.Engine[T], ops numeric.Arithmetic[T], modelDim, numQueryHeads, numKeyValueHeads, ffnDim int, epsilon T) (*Block[T], error)

NewTransformerBlock creates a new Transformer encoder block. modelDim: The input and output dimension of the block (d_model). numQueryHeads: The number of query heads. numKeyValueHeads: The number of key/value heads. ffnDim: The inner dimension of the feed-forward network. epsilon: Epsilon for Layer Normalization.

func (*Block[T]) Backward

func (tb *Block[T]) Backward(ctx context.Context, dOut *tensor.Tensor[T], inputs ...*tensor.Tensor[T]) ([]*tensor.Tensor[T], error)

Backward computes the gradients for the Transformer Block.

func (*Block[T]) Forward

func (tb *Block[T]) Forward(ctx context.Context, inputs ...*tensor.Tensor[T]) (*tensor.Tensor[T], error)

Forward computes the Transformer Block's forward pass.

func (*Block[T]) OutputShape

func (tb *Block[T]) OutputShape(inputShapes ...[]int) ([]int, error)

OutputShape returns the output shape, which is the same as the input shape.

func (*Block[T]) Parameters

func (tb *Block[T]) Parameters() []graph.Parameter[T]

Parameters returns all trainable parameters from its sub-layers.

Jump to

Keyboard shortcuts

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