bert

package
v0.2.1 Latest Latest
Warning

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

Go to latest
Published: Nov 8, 2023 License: BSD-2-Clause Imports: 15 Imported by: 20

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func ConfigFromFile

func ConfigFromFile[T ConfigFile](file string) (config T, _ error)

ConfigFromFile loads a Bart model Config from file.

Types

type Config

type Config struct {
	Architectures             []string          `json:"architectures"`
	AttentionProbsDropoutProb float64           `json:"attention_probs_dropout_prob"`
	GradientCheckpointing     bool              `json:"gradient_checkpointing"`
	HiddenAct                 string            `json:"hidden_act"`
	HiddenDropoutProb         float64           `json:"hidden_dropout_prob"`
	HiddenSize                int               `json:"hidden_size"`
	EmbeddingsSize            int               `json:"embeddings_size"`
	InitializerRange          float64           `json:"initializer_range"`
	IntermediateSize          int               `json:"intermediate_size"`
	LayerNormEps              float64           `json:"layer_norm_eps"`
	MaxPositionEmbeddings     int               `json:"max_position_embeddings"`
	ModelType                 string            `json:"model_type"`
	NumAttentionHeads         int               `json:"num_attention_heads"`
	NumHiddenLayers           int               `json:"num_hidden_layers"`
	PadTokenId                int               `json:"pad_token_id"`
	PositionEmbeddingType     string            `json:"position_embedding_type"`
	TransformersVersion       string            `json:"transformers_version"`
	TypeVocabSize             int               `json:"type_vocab_size"`
	UseCache                  bool              `json:"use_cache"`
	VocabSize                 int               `json:"vocab_size"`
	ID2Label                  map[string]string `json:"id2label"`
	Cybertron                 struct {
		Training            bool   `json:"training"`
		TokensStoreName     string `json:"tokens_store_name"`
		PositionsStoreName  string `json:"positions_store_name"`
		TokenTypesStoreName string `json:"token_types_store_name"`
	}
}

Config contains the global configuration of the Bert model and the heads of fine-tuning tasks. The configuration coincides with that of Hugging Face to facilitate compatibility between the two architectures.

type ConfigFile

type ConfigFile interface {
	Config | TokenizerConfig
}

ConfigFile is the union of the configuration structures.

type Embeddings

type Embeddings struct {
	nn.Module
	Vocab      *vocabulary.Vocabulary
	Tokens     *emb.Model // string
	Positions  *emb.Model
	TokenTypes *emb.Model
	Norm       *layernorm.Model
	Projector  *linear.Model
	Config     Config
}

Embeddings implements a Bert input embedding module.

func NewEmbeddings

func NewEmbeddings[T float.DType](c Config) *Embeddings

NewEmbeddings returns a new Bert input embedding module.

func (*Embeddings) EncodeTokens added in v0.2.0

func (m *Embeddings) EncodeTokens(tokens []string) []mat.Tensor

EncodeTokens performs the Bert input encoding.

type Encoder

type Encoder struct {
	nn.Module
	Layers nn.ModuleList[*EncoderLayer]
	Config Config
}

Encoder implements a Bert encoder.

func NewEncoder

func NewEncoder[T float.DType](c Config) *Encoder

NewEncoder returns a new Encoder.

func (*Encoder) Encode

func (e *Encoder) Encode(xs []mat.Tensor) []mat.Tensor

Encode performs the Bert encoding.

type EncoderLayer

type EncoderLayer struct {
	nn.Module
	SelfAttention *SelfAttentionBlock
	FF            *FeedForwardBlock
	Config        Config
}

EncoderLayer implements a Bert encoder layer.

func NewEncoderLayer

func NewEncoderLayer[T float.DType](c Config) *EncoderLayer

NewEncoderLayer returns a new EncoderLayer.

func (*EncoderLayer) Forward

func (m *EncoderLayer) Forward(xs ...mat.Tensor) []mat.Tensor

Forward performs the forward step for each input node and returns the result.

type FeedForwardBlock

type FeedForwardBlock struct {
	nn.Module
	MLP  nn.ModuleList[nn.StandardModel]
	Norm *layernorm.Model
}

FeedForwardBlock implements a Bert feed forward block.

func NewFeedForwardBlock

func NewFeedForwardBlock[T float.DType](c FeedForwardBlockConfig) *FeedForwardBlock

NewFeedForwardBlock returns a new FeedForwardBlock.

func (FeedForwardBlock) Forward

func (m FeedForwardBlock) Forward(xs []mat.Tensor) []mat.Tensor

Forward performs the forward step for each input node and returns the result.

type FeedForwardBlockConfig

type FeedForwardBlockConfig struct {
	Dim        int
	HiddenDim  int
	Activation activation.Activation
}

FeedForwardBlockConfig is the configuration for the FeedForwardBlock.

type Model

type Model struct {
	nn.Module
	Embeddings *Embeddings
	Encoder    *Encoder
	Pooler     *Pooler
	Config     Config
}

Model implements a base Bert encoder model without any head on top.

func New

func New[T float.DType](c Config) *Model

New returns a new Bert model.

func (*Model) EncodeTokens added in v0.2.0

func (m *Model) EncodeTokens(tokens []string) []mat.Tensor

EncodeTokens produce the encoded representation for the input tokens

type ModelForMaskedLM added in v0.1.2

type ModelForMaskedLM struct {
	nn.Module
	// Bart is the fine-tuned BERT model.
	Bert *Model
	// Layers contains the feedforward layers for masked language modeling.
	Layers nn.ModuleList[nn.StandardModel]
}

ModelForMaskedLM implements a Bert model for masked language modeling.

func NewModelForMaskedLM added in v0.1.2

func NewModelForMaskedLM[T float.DType](bert *Model) *ModelForMaskedLM

NewModelForMaskedLM returns a new model for masked language model.

func (*ModelForMaskedLM) Predict added in v0.1.2

func (m *ModelForMaskedLM) Predict(tokens []string) map[int]mat.Tensor

Predict returns the predictions for the token associated to the masked nodes.

type ModelForQuestionAnswering

type ModelForQuestionAnswering struct {
	nn.Module
	// Bart is the fine-tuned BERT model.
	Bert *Model
	// Classifier is the linear layer for span classification.
	Classifier *linear.Model
}

ModelForQuestionAnswering implements span classification for extractive question-answering tasks. It uses a linear layers to compute "span start logits" and "span end logits".

func NewModelForQuestionAnswering

func NewModelForQuestionAnswering[T float.DType](bert *Model) *ModelForQuestionAnswering

NewModelForQuestionAnswering returns a new model for question-answering.

func (*ModelForQuestionAnswering) Answer

func (m *ModelForQuestionAnswering) Answer(tokens []string) (starts, ends []mat.Tensor)

Answer returns the "span start logits" and "span end logits".

type ModelForSequenceClassification

type ModelForSequenceClassification struct {
	nn.Module
	// Bart is the fine-tuned BERT model.
	Bert *Model
	// Classifier is the linear layer for sequence classification.
	Classifier *linear.Model
}

ModelForSequenceClassification implements a Bert model for sequence classification.

func NewModelForSequenceClassification

func NewModelForSequenceClassification[T float.DType](bert *Model) *ModelForSequenceClassification

NewModelForSequenceClassification returns a new model for sequence classification.

func (*ModelForSequenceClassification) Classify

func (m *ModelForSequenceClassification) Classify(tokens []string) mat.Tensor

Classify returns the logits for the sequence classification.

type ModelForSequenceEncoding

type ModelForSequenceEncoding struct {
	nn.Module
	// Bart is the fine-tuned BERT model.
	Bert *Model
}

ModelForSequenceEncoding implements a Bert model for sequence encoding.

func NewModelForSequenceEncoding

func NewModelForSequenceEncoding(bert *Model) *ModelForSequenceEncoding

NewModelForSequenceEncoding returns a new model for sequence encoding.

func (*ModelForSequenceEncoding) Encode

func (m *ModelForSequenceEncoding) Encode(tokens []string, poolingStrategy PoolingStrategyType) (mat.Tensor, error)

Encode returns the vector representation for the input sequence.

type ModelForTokenClassification

type ModelForTokenClassification struct {
	nn.Module
	// Bart is the fine-tuned BERT model.
	Bert *Model
	// Classifier is the linear layer for sequence classification.
	Classifier *linear.Model
}

ModelForTokenClassification implements a Bert model for token classification.

func NewModelForTokenClassification

func NewModelForTokenClassification[T float.DType](bert *Model) *ModelForTokenClassification

NewModelForTokenClassification returns a new model for token classification.

func (*ModelForTokenClassification) Classify

func (m *ModelForTokenClassification) Classify(tokens []string) []mat.Tensor

Classify returns the logits for each token.

type Pooler

type Pooler struct {
	nn.Module
	Model nn.ModuleList[nn.StandardModel]
}

Pooler implements a Bert pooler.

func NewPooler

func NewPooler[T float.DType](c Config) *Pooler

NewPooler returns a new Pooler.

func (*Pooler) Forward

func (m *Pooler) Forward(encoded mat.Tensor) mat.Tensor

Forward applies a linear transformation followed by a Tanh activation to the first `[CLS]` encoded token.

type PoolingStrategyType

type PoolingStrategyType int

PoolingStrategyType defines the strategy to obtain the dense sequence representation

const (
	// ClsTokenPooling gets the last encoding state corresponding to [CLS], i.e. the first token (default)
	ClsTokenPooling PoolingStrategyType = iota
	// MeanPooling takes the average of the last encoding states
	MeanPooling
	// MaxPooling takes the maximum of the last encoding states
	MaxPooling
	// MeanMaxPooling does MeanPooling and MaxPooling separately and then concat them together
	MeanMaxPooling
)

type SelfAttentionBlock

type SelfAttentionBlock struct {
	nn.Module
	// Attention is the multi-head attention module.
	Attention *multiheadattention.Model
	// Norm is the layer normalization module.
	Norm *layernorm.Model
}

SelfAttentionBlock implements the self-attention block of a BERT model.

func NewSelfAttentionBlock

func NewSelfAttentionBlock[T float.DType](c SelfAttentionBlockConfig) *SelfAttentionBlock

NewSelfAttentionBlock creates a new SelfAttentionBlock.

func (SelfAttentionBlock) Forward

func (m SelfAttentionBlock) Forward(xs []mat.Tensor) []mat.Tensor

Forward returns the output of the model.

type SelfAttentionBlockConfig

type SelfAttentionBlockConfig struct {
	Dim        int
	NumOfHeads int
}

SelfAttentionBlockConfig is the configuration of a SelfAttentionBlock.

type TokenizerConfig

type TokenizerConfig struct {
	DoLowerCase          bool        `json:"do_lower_case"`
	UnkToken             string      `json:"unk_token"`
	SepToken             string      `json:"sep_token"`
	PadToken             string      `json:"pad_token"`
	ClsToken             string      `json:"cls_token"`
	MaskToken            string      `json:"mask_token"`
	TokenizeChineseChars bool        `json:"tokenize_chinese_chars"`
	StripAccents         interface{} `json:"strip_accents"`
	ModelMaxLength       int         `json:"model_max_length"`
}

TokenizerConfig contains the configuration of the tokenizer. The configuration coincides with that of Hugging Face to facilitate compatibility between the two architectures.

Jump to

Keyboard shortcuts

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