bert

package
v0.1.2 Latest Latest
Warning

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

Go to latest
Published: Oct 17, 2022 License: BSD-2-Clause Imports: 15 Imported by: 62 modules, 50 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

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
	Tokens     *emb.Model[string]
	Positions  *emb.Model[int]
	TokenTypes *emb.Model[int]
	Norm       *layernorm.Model
	Projector  *linear.Model
	Config     Config
}

Embeddings implements a Bert input embedding module.

func NewEmbeddings

func NewEmbeddings[T float.DType](c Config, repo store.Repository) *Embeddings

NewEmbeddings returns a new Bert input embedding module.

func (*Embeddings) Encode

func (m *Embeddings) Encode(tokens []string) []ag.Node

Encode performs the Bert input encoding.

type Encoder

type Encoder struct {
	nn.Module
	Layers []*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 []ag.Node) []ag.Node

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 ...ag.Node) []ag.Node

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

type FeedForwardBlock

type FeedForwardBlock struct {
	nn.Module
	MLP  []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 []ag.Node) []ag.Node

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

type FeedForwardBlockConfig

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

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, repo store.Repository) *Model

New returns a new Bert model.

func (*Model) Encode

func (m *Model) Encode(tokens []string) []ag.Node

Encode produce the encoded representation for the input tokens

func (*Model) SetEmbeddings

func (m *Model) SetEmbeddings(repo *diskstore.Repository) (err error)

SetEmbeddings sets the embeddings of the model.

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.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]ag.Node

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 []ag.Node)

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) ag.Node

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) (ag.Node, 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) []ag.Node

Classify returns the logits for each token.

type Pooler

type Pooler struct {
	nn.Module
	Model []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 []ag.Node) ag.Node

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.SelfAttention
	// 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 []ag.Node) []ag.Node

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