Documentation ¶
Index ¶
- type Config
- type Embedding
- func (e *Embedding[_]) AccGrad(gx mat.Matrix)
- func (e *Embedding[_]) ApplyDelta(delta mat.Matrix)
- func (e *Embedding[_]) ClearPayload()
- func (e *Embedding[_]) Grad() mat.Matrix
- func (e *Embedding[_]) HasGrad() bool
- func (e *Embedding[_]) Name() string
- func (e *Embedding[_]) Payload() *nn.Payload
- func (e *Embedding[_]) ReplaceValue(value mat.Matrix)
- func (e *Embedding[_]) RequiresGrad() bool
- func (e *Embedding[_]) ScalarValue() float.Float
- func (e *Embedding[_]) SetPayload(payload *nn.Payload)
- func (e *Embedding[_]) SetRequiresGrad(bool)
- func (e *Embedding[_]) Type() nn.ParamsType
- func (e *Embedding[_]) Value() mat.Matrix
- func (e *Embedding[_]) ZeroGrad()
- type Key
- type Model
- func (m *Model[_]) ClearEmbeddingsWithGrad()
- func (m *Model[_]) Count() int
- func (m *Model[_]) CountEmbeddingsWithGrad() int
- func (m *Model[K]) Embedding(key K) (*Embedding[K], bool)
- func (m *Model[K]) EmbeddingFast(key K) *Embedding[K]
- func (m *Model[K]) Encode(keys []K) []ag.Node
- func (m *Model[K]) TraverseParams(callback nn.ParamsTraversalFunc)
- func (m *Model[_]) UseRepository(repo store.Repository) error
- type Shared
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[_]) ApplyDelta ¶
ApplyDelta satisfies the interface nn.Param.
func (*Embedding[_]) ClearPayload ¶
func (e *Embedding[_]) ClearPayload()
ClearPayload satisfies the interface nn.Param.
func (*Embedding[_]) ReplaceValue ¶
ReplaceValue satisfies the interface nn.Param.
func (*Embedding[_]) RequiresGrad ¶
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 ¶
ScalarValue satisfies the interfaces nn.Param and ag.Node.
func (*Embedding[_]) SetPayload ¶
SetPayload satisfies the interface nn.Param.
func (*Embedding[_]) SetRequiresGrad ¶
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.
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 ¶
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 ¶
Count counts how many embedding key/value pairs are currently stored. It panics in case of reading errors.
func (*Model[_]) CountEmbeddingsWithGrad ¶
CountEmbeddingsWithGrad counts how many embedding key/value pairs are currently active. It panics in case of reading errors.
func (*Model[K]) Embedding ¶
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
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 ¶
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 ¶
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 ¶
MarshalBinary satisfies encoding.BinaryMarshaler interface. It always produces empty data (nil) and no error.
func (Shared[_]) UnmarshalBinary ¶
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.