embeddings

package
v1.0.1 Latest Latest
Warning

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

Go to latest
Published: Sep 16, 2022 License: BSD-2-Clause Imports: 10 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Config

type Config struct {
	// Size of the embedding vectors.
	Size int
	// Whether to return the `ZeroEmbedding` in case the key doesn't exist in
	// the embeddings store.
	// If it is false, nil is returned instead, so the caller has more
	// responsibility but also more control.
	UseZeroEmbedding bool
	// The name of the store.Store get from a store.Repository for the
	// data handled by the embeddings model.
	StoreName string
	// A trainable model allows its Embedding parameters to have gradient
	// values that can be propagated. When set to false, gradients handling is
	// disabled.
	Trainable bool
}

Config provides configuration settings for an embeddings Model.

type Embedding

type Embedding[K Key] struct {
	// contains filtered or unexported fields
}

Embedding is an implementation of nn.Param representing embedding values.

func (*Embedding[_]) AccGrad

func (e *Embedding[_]) AccGrad(gx mat.Matrix)

AccGrad satisfies the interfaces nn.Param and ag.Node.

func (*Embedding[_]) ApplyDelta

func (e *Embedding[_]) ApplyDelta(delta mat.Matrix)

ApplyDelta satisfies the interface nn.Param.

func (*Embedding[_]) ClearPayload

func (e *Embedding[_]) ClearPayload()

ClearPayload satisfies the interface nn.Param.

func (*Embedding[_]) Grad

func (e *Embedding[_]) Grad() mat.Matrix

Grad satisfies the interfaces nn.Param and ag.Node.

func (*Embedding[_]) HasGrad

func (e *Embedding[_]) HasGrad() bool

HasGrad satisfies the interfaces nn.Param and ag.Node.

func (*Embedding[_]) Name

func (e *Embedding[_]) Name() string

Name satisfies the interface nn.Param.

func (*Embedding[_]) Payload

func (e *Embedding[_]) Payload() *nn.Payload

Payload satisfies the interface nn.Param.

func (*Embedding[_]) ReplaceValue

func (e *Embedding[_]) ReplaceValue(value mat.Matrix)

ReplaceValue satisfies the interface nn.Param.

func (*Embedding[_]) RequiresGrad

func (e *Embedding[_]) RequiresGrad() bool

RequiresGrad satisfies the interfaces nn.Param and ag.Node. It returns the same value of Config.Trainable of the Model tied to this Embedding.

func (*Embedding[_]) ScalarValue

func (e *Embedding[_]) ScalarValue() float.Float

ScalarValue satisfies the interfaces nn.Param and ag.Node.

func (*Embedding[_]) SetPayload

func (e *Embedding[_]) SetPayload(payload *nn.Payload)

SetPayload satisfies the interface nn.Param.

func (*Embedding[_]) SetRequiresGrad

func (e *Embedding[_]) SetRequiresGrad(bool)

SetRequiresGrad satisfies the interface nn.Param. It always panics: it's not possible to assign a custom grad requirement to an Embedding parameter.

func (*Embedding[_]) Type

func (e *Embedding[_]) Type() nn.ParamsType

Type satisfies the interface nn.Param. Embedding params are always considered nn.Weights.

func (*Embedding[_]) Value

func (e *Embedding[_]) Value() mat.Matrix

Value satisfies the interfaces nn.Param and ag.Node.

func (*Embedding[_]) ZeroGrad

func (e *Embedding[_]) ZeroGrad()

ZeroGrad satisfies the interfaces nn.Param and ag.Node.

type Key

type Key interface {
	[]byte | string | int
}

Key is a type constraint for the embeddings' storage keys.

type Model

type Model[K Key] struct {
	nn.Module
	Config
	// ZeroEmbedding is used as a fallback value for missing embeddings.
	//
	// If Config.UseZeroEmbedding is true, ZeroEmbedding is initialized
	// as a zero-vector of size Config.Size, otherwise it is set to nil.
	ZeroEmbedding nn.Param `spago:"type:weights"`
	// Database where embeddings are stored.
	Store store.Store
	// contains filtered or unexported fields
}

A Model for handling embeddings.

func New

func New[T float.DType, K Key](conf Config, repo store.Repository) *Model[K]

New returns a new embeddings Model.

It panics in case of errors getting the Store from the Repository.

func (*Model[_]) ClearEmbeddingsWithGrad

func (m *Model[_]) ClearEmbeddingsWithGrad()

ClearEmbeddingsWithGrad empties the memory of visited embeddings with non-null gradient value.

func (*Model[_]) Count

func (m *Model[_]) Count() int

Count counts how many embedding key/value pairs are currently stored. It panics in case of reading errors.

func (*Model[_]) CountEmbeddingsWithGrad

func (m *Model[_]) CountEmbeddingsWithGrad() int

CountEmbeddingsWithGrad counts how many embedding key/value pairs are currently active. It panics in case of reading errors.

func (*Model[K]) Embedding

func (m *Model[K]) Embedding(key K) (*Embedding[K], bool)

Embedding returns the Embedding parameter associated with the given key, also reporting whether the key was found in the store.

Even if an embedding parameter is not found in the store, a usable value is still returned; it's sufficient to set some data on it (value, payload) to trigger its creation on the store.

It panics in case of errors reading from the underlying store.

func (*Model[K]) EmbeddingFast added in v1.0.1

func (m *Model[K]) EmbeddingFast(key K) *Embedding[K]

EmbeddingFast is like Embedding, but skips the existence checks. Depending on the store implementation, this might save a considerable amount of time.

func (*Model[K]) Encode

func (m *Model[K]) Encode(keys []K) []ag.Node

Encode returns the embedding values associated with the input keys.

The value are returned as Node(s) already inserted in the graph.

Missing embedding values can be either nil or ZeroEmbedding, according to the Model's Config.

func (*Model[K]) TraverseParams

func (m *Model[K]) TraverseParams(callback nn.ParamsTraversalFunc)

TraverseParams allows embeddings with gradients to be traversed for optimization.

func (*Model[_]) UseRepository

func (m *Model[_]) UseRepository(repo store.Repository) error

UseRepository allows the Model to use a Store from the given Repository.

It only works if a store is not yet present. This can only happen in special situations, for example upon an Embedding model being deserialized, or when manually instantiating and handling a Model (i.e. bypassing New).

type Shared

type Shared[K Key] struct {
	*Model[K]
}

Shared wraps Model, overriding binary marshaling methods in order to produce (and expect) empty data. This is useful e.g. to share embeddings between encoder and decoder models.

func (Shared[_]) MarshalBinary

func (Shared[_]) MarshalBinary() ([]byte, error)

MarshalBinary satisfies encoding.BinaryMarshaler interface. It always produces empty data (nil) and no error.

func (Shared[_]) UnmarshalBinary

func (Shared[_]) UnmarshalBinary(data []byte) error

UnmarshalBinary satisfies encoding.BinaryUnmarshaler interface. It only accepts empty data (nil or zero-length slice), producing no side effects at all. If data is not blank, it returns an error.

Directories

Path Synopsis
diskstore Module

Jump to

Keyboard shortcuts

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