lamp.nn
Provides building blocks for neural networks
Notable types:
- nn.GenericModule is an abstraction on parametric functions
- nn.Optimizer is an abstraction of gradient based optimizers
- nn.LossFunction is an abstraction of loss functions, see the companion object for the implemented losses
- nn.SupervisedModel combines a module with a loss
Optimizers:
Modules facilitating composing other modules:
- nn.Sequential composes a homogenous list of modules (analogous to List)
- nn.sequence composes a heterogeneous list of modules (analogous to tuples)
- nn.EitherModule composes two modules in a scala.Either
Examples of neural network building blocks, layers etc:
- nn.Linear implements
W X + b
with parametersW
andb
and inputX
- nn.BatchNorm, nn.LayerNorm implement batch and layer normalization
- nn.MLP is a factory of a multilayer perceptron architecture
Attributes
Members list
Type members
Classlikes
Attributes
- See also
-
https://arxiv.org/pdf/1711.05101.pdf Algorithm 2
- Companion
- object
- Supertypes
-
trait Serializabletrait Producttrait Equalstrait Optimizerclass Objecttrait Matchableclass AnyShow all
Attributes
- Supertypes
-
trait Serializabletrait Producttrait Equalstrait LossCalculation[Variable]class Objecttrait Matchableclass AnyShow all
Attributes
- Companion
- object
- Supertypes
-
trait Serializabletrait Producttrait Equalsclass Objecttrait Matchableclass AnyShow all
Attributes
- Companion
- object
- Supertypes
-
trait Serializabletrait Producttrait Equalsclass Objecttrait Matchableclass AnyShow all
Attributes
- Companion
- class
- Supertypes
-
trait Producttrait Mirrorclass Objecttrait Matchableclass Any
- Self type
-
BatchNorm2D.type
Attributes
- Companion
- object
- Supertypes
-
trait Serializabletrait Producttrait Equalsclass Objecttrait Matchableclass AnyShow all
Attributes
- Companion
- object
- Supertypes
-
trait Serializabletrait Producttrait Equalsclass Objecttrait Matchableclass AnyShow all
Attributes
- Companion
- object
- Supertypes
-
trait Serializabletrait Producttrait Equalsclass Objecttrait Matchableclass AnyShow all
Attributes
- Companion
- class
- Supertypes
-
trait Producttrait Mirrorclass Objecttrait Matchableclass Any
- Self type
-
Conv2DTransposed.type
Attributes
- Companion
- object
- Supertypes
-
trait Serializabletrait Producttrait Equalsclass Objecttrait Matchableclass AnyShow all
Attributes
- Supertypes
-
trait Serializabletrait Producttrait Equalstrait OptimizerHyperparameterclass Objecttrait Matchableclass AnyShow all
Attributes
- Companion
- object
- Supertypes
-
trait Serializabletrait Producttrait Equalsclass Objecttrait Matchableclass AnyShow all
Attributes
- Companion
- object
- Supertypes
-
trait Serializabletrait Producttrait Equalsclass Objecttrait Matchableclass AnyShow all
Attributes
- Companion
- class
- Supertypes
-
trait Producttrait Mirrorclass Objecttrait Matchableclass Any
- Self type
-
EitherModule.type
Learnable mapping from classes to dense vectors. Equivalent to L * W where L is the n x C one-hot encoded matrix of the classes * is matrix multiplication W is the C x dim dense matrix. W is learnable. L is never computed directly. C is the number of classes. n is the size of the batch.
Learnable mapping from classes to dense vectors. Equivalent to L * W where L is the n x C one-hot encoded matrix of the classes * is matrix multiplication W is the C x dim dense matrix. W is learnable. L is never computed directly. C is the number of classes. n is the size of the batch.
Input is a long tensor with values in [0,C-1]. Input shape is arbitrary, (). Output shape is ( x D) where D is the embedding dimension.
Attributes
- Companion
- object
- Supertypes
-
trait Serializabletrait Producttrait Equalsclass Objecttrait Matchableclass AnyShow all
Wraps a (sequence x batch) long -> (sequence x batch x dim) double stateful module and runs in it greedy (argmax) generation mode over timeSteps
steps.
Wraps a (sequence x batch) long -> (sequence x batch x dim) double stateful module and runs in it greedy (argmax) generation mode over timeSteps
steps.
Attributes
- Companion
- object
- Supertypes
-
trait Serializabletrait Producttrait Equalsclass Objecttrait Matchableclass AnyShow all
Attributes
- Companion
- class
- Supertypes
-
trait Producttrait Mirrorclass Objecttrait Matchableclass Any
- Self type
-
FreeRunningRNN.type
Attributes
- Companion
- object
- Supertypes
-
trait Serializabletrait Producttrait Equalsclass Objecttrait Matchableclass AnyShow all
Inputs of size (sequence length * batch * in dim) Outputs of size (sequence length * batch * hidden dim)
Inputs of size (sequence length * batch * in dim) Outputs of size (sequence length * batch * hidden dim)
Attributes
- Companion
- object
- Supertypes
-
trait Serializabletrait Producttrait Equalsclass Objecttrait Matchableclass AnyShow all
Attributes
- Companion
- object
- Supertypes
-
trait Serializabletrait Producttrait Equalsclass Objecttrait Matchableclass AnyShow all
Attributes
- Companion
- class
- Supertypes
-
trait Producttrait Mirrorclass Objecttrait Matchableclass Any
- Self type
-
GenericFun.type
Attributes
- Companion
- trait
- Supertypes
-
class Objecttrait Matchableclass Any
- Self type
-
GenericModule.type
Base type of modules
Base type of modules
Modules are functions of type (Seq[lamp.autograd.Constant],A) => B
, where the Seq[lamp.autograd.Constant]
arguments are optimizable parameters and A
is a non-optimizable input.
Modules provide a way to build composite functions while also keep track of the parameter list of the composite function.
===Example===
case object Weights extends LeafTag
case object Bias extends LeafTag
case class Linear(weights: Constant, bias: Option[Constant]) extends Module {
override val state = List(
weights -> Weights
) ++ bias.toList.map(b => (b, Bias))
def forward[S: Sc](x: Variable): Variable = {
val v = x.mm(weights)
bias.map(_ + v).getOrElse(v)
}
}
Some other attributes of modules are attached by type classes e.g. with the nn.TrainingMode, nn.Load type classes.
Type parameters
- A
-
the argument type of the module
- B
-
the value type of the module
Attributes
- See also
-
nn.Module is an alias for simple
Variable => Variable
modules - Companion
- object
- Supertypes
-
class Objecttrait Matchableclass Any
- Known subtypes
-
class BertEncoderclass BertLossclass BertPretrainModuleclass GraphAttentionclass LanguageModelLossclass LanguageModelModuleclass MultiheadAttentionclass Transformerclass TransformerDecoderclass TransformerDecoderBlockclass TransformerEmbeddingclass TransformerEncoderclass TransformerEncoderBlockShow all
Type class about how to initialize recurrent neural networks
Type class about how to initialize recurrent neural networks
Attributes
- Companion
- object
- Supertypes
-
class Objecttrait Matchableclass Any
Attributes
- Supertypes
-
class Objecttrait Matchableclass Any
Inputs of size (sequence length * batch * vocab) Outputs of size (sequence length * batch * output dim)
Inputs of size (sequence length * batch * vocab) Outputs of size (sequence length * batch * output dim)
Attributes
- Companion
- object
- Supertypes
Attributes
- Companion
- object
- Supertypes
-
trait Serializabletrait Producttrait Equalsclass Objecttrait Matchableclass AnyShow all
Attributes
- Supertypes
- Known subtypes
-
object PositionalEmbeddingWeight.typeobject Weights.typeobject Bias.typeobject RunningMean.typeobject RunningVar.typeobject Weights.typeobject Bias.typeobject Weights.typeobject Bias.typeobject Weights.typeobject Bias.typeobject Weights.typeobject Bias.typeobject Weights.typeobject Weights.typeobject BiasH.typeobject BiasR.typeobject BiasZ.typeobject WeightHh.typeobject WeightHr.typeobject WeightHz.typeobject WeightXh.typeobject WeightXr.typeobject WeightXz.typeobject BiasC.typeobject BiasF.typeobject BiasI.typeobject BiasO.typeobject WeightHc.typeobject WeightHf.typeobject WeightHi.typeobject WeightHo.typeobject WeightXc.typeobject WeightXf.typeobject WeightXi.typeobject WeightXo.typeobject Bias.typeobject Scale.typeobject Bias.typeobject Weights.typeobject WeightsK.typeobject WeightsO.typeobject WeightsQ.typeobject WeightsV.typeobject NoTag.typeobject BiasH.typeobject WeightHh.typeobject WeightXh.typeobject Bias.typeobject Weight.typeobject Bias1.typeobject Bias2.typeobject Weights1.typeobject Weights2.typeobject Embedding.typeobject Bias1.typeobject Bias2.typeobject Scale1.typeobject Scale2.typeobject Weights1.typeobject Weights2.typeobject Bias.typeobject WeightsG.typeobject WeightsV.typeShow all
Attributes
- Companion
- object
- Supertypes
-
class Objecttrait Matchableclass Any
Attributes
- Companion
- trait
- Supertypes
-
class Objecttrait Matchableclass Any
- Self type
-
LearningRateSchedule.type
Attributes
- Companion
- object
- Supertypes
-
trait Serializabletrait Producttrait Equalsclass Objecttrait Matchableclass AnyShow all
Attributes
- Companion
- class
- Supertypes
-
trait Producttrait Mirrorclass Objecttrait Matchableclass Any
- Self type
-
LiftedModule.type
Attributes
- Companion
- object
- Supertypes
-
trait Serializabletrait Producttrait Equalsclass Objecttrait Matchableclass AnyShow all
Type class about how to load the contents of the state of modules from external tensors
Type class about how to load the contents of the state of modules from external tensors
Attributes
- Companion
- object
- Supertypes
-
class Objecttrait Matchableclass Any
Attributes
- Supertypes
-
class Objecttrait Matchableclass Any
Loss and Gradient calculation
Loss and Gradient calculation
Takes samples, target, module, loss function and computes the loss and the gradients
Attributes
- Supertypes
-
class Objecttrait Matchableclass Any
- Known subtypes
Attributes
- Supertypes
-
class Objecttrait Matchableclass Any
- Known subtypes
-
class BCEWithLogitsobject Identity.typeobject MSE.typeclass NLLclass SequenceNLLclass SmoothL1LossShow all
Attributes
- Supertypes
-
class Objecttrait Matchableclass Any
- Self type
-
LossFunctions.type
Factory for multilayer fully connected feed forward networks
Factory for multilayer fully connected feed forward networks
Returned network has the following repeated structure: [linear -> batchnorm -> nonlinearity -> dropout]*
The last block does not include the nonlinearity and the dropout.
Value parameters
- dropout
-
dropout applied to each block
- hidden
-
list of hidden dimensions
- in
-
input dimensions
- out
-
output dimensions
Attributes
- Supertypes
-
class Objecttrait Matchableclass Any
- Self type
-
MLP.type
Attributes
- Companion
- object
- Supertypes
-
trait Serializabletrait Producttrait Equalsclass Objecttrait Matchableclass AnyShow all
Attributes
- Companion
- class
- Supertypes
-
trait Producttrait Mirrorclass Objecttrait Matchableclass Any
- Self type
-
MappedState.type
Attributes
- Supertypes
-
trait Serializabletrait Producttrait Equalsclass Objecttrait Matchableclass AnyShow all
Multi-head scaled dot product attention module
Multi-head scaled dot product attention module
Input: (query,key,value,maxLength) where
- query: batch x num queries x query dim
- key: batch x num k-v x key dim
- value: batch x num k-v x key value
- maxLength: 1D or 2D long tensor for attention masking
Attributes
- Companion
- object
- Supertypes
-
trait Serializabletrait Producttrait Equalsclass Objecttrait Matchableclass AnyShow all
Attributes
- Companion
- class
- Supertypes
-
trait Producttrait Mirrorclass Objecttrait Matchableclass Any
- Self type
-
MultiheadAttention.type
Attributes
- Supertypes
-
class Objecttrait Matchableclass Any
- Known subtypes
-
class DependentHyperparameterclass simple
A small trait to mark paramters for unique identification
A small trait to mark paramters for unique identification
Attributes
- Companion
- object
- Supertypes
-
class Objecttrait Matchableclass Any
- Known subtypes
-
class Tag[T]trait LeafTagobject PositionalEmbeddingWeight.typeobject Weights.typeobject Bias.typeobject RunningMean.typeobject RunningVar.typeobject Weights.typeobject Bias.typeobject Weights.typeobject Bias.typeobject Weights.typeobject Bias.typeobject Weights.typeobject Bias.typeobject Weights.typeobject Weights.typeobject BiasH.typeobject BiasR.typeobject BiasZ.typeobject WeightHh.typeobject WeightHr.typeobject WeightHz.typeobject WeightXh.typeobject WeightXr.typeobject WeightXz.typeobject BiasC.typeobject BiasF.typeobject BiasI.typeobject BiasO.typeobject WeightHc.typeobject WeightHf.typeobject WeightHi.typeobject WeightHo.typeobject WeightXc.typeobject WeightXf.typeobject WeightXi.typeobject WeightXo.typeobject Bias.typeobject Scale.typeobject Bias.typeobject Weights.typeobject WeightsK.typeobject WeightsO.typeobject WeightsQ.typeobject WeightsV.typeobject NoTag.typeobject BiasH.typeobject WeightHh.typeobject WeightXh.typeobject Bias.typeobject Weight.typeobject Bias1.typeobject Bias2.typeobject Weights1.typeobject Weights2.typeobject Embedding.typeobject Bias1.typeobject Bias2.typeobject Scale1.typeobject Scale2.typeobject Weights1.typeobject Weights2.typeobject Bias.typeobject WeightsG.typeobject WeightsV.typeclass Tag[T]Show all
Evaluates the gradient at current point + eps where eps is I * N(0,noiseLevel)
Evaluates the gradient at current point + eps where eps is I * N(0,noiseLevel)
Attributes
- Supertypes
Attributes
- Supertypes
-
class Objecttrait Matchableclass Any
- Self type
-
PositionalEmbedding.type
Inputs of size (sequence length * batch * in dim) Outputs of size (sequence length * batch * hidden dim)
Inputs of size (sequence length * batch * in dim) Outputs of size (sequence length * batch * hidden dim)
Attributes
- Companion
- object
- Supertypes
-
trait Serializabletrait Producttrait Equalsclass Objecttrait Matchableclass AnyShow all
Attributes
- Companion
- object
- Supertypes
-
trait Serializabletrait Producttrait Equalsclass Objecttrait Matchableclass AnyShow all
Attributes
- Companion
- object
- Supertypes
-
trait Serializabletrait Producttrait Equalsclass Objecttrait Matchableclass AnyShow all
Attributes
- Companion
- class
- Supertypes
-
trait Producttrait Mirrorclass Objecttrait Matchableclass Any
- Self type
-
ResidualModule.type
Attributes
- Companion
- object
- Supertypes
-
trait Serializabletrait Producttrait Equalsclass Objecttrait Matchableclass AnyShow all
Attributes
- Companion
- object
- Supertypes
-
trait Serializabletrait Producttrait Equalsclass Objecttrait Matchableclass AnyShow all
Attributes
- Companion
- object
- Supertypes
-
trait Serializabletrait Producttrait Equalsclass Objecttrait Matchableclass AnyShow all
Attributes
- Companion
- object
- Supertypes
-
trait Serializabletrait Producttrait Equalsclass Objecttrait Matchableclass AnyShow all
Attributes
- Companion
- object
- Supertypes
-
trait Serializabletrait Producttrait Equalsclass Objecttrait Matchableclass AnyShow all
Attributes
- Companion
- object
- Supertypes
-
trait Serializabletrait Producttrait Equalsclass Objecttrait Matchableclass AnyShow all
Inputs of size (sequence length * batch * in dim) Outputs of size (sequence length * batch * output dim) Applies a linear function to each time step
Inputs of size (sequence length * batch * in dim) Outputs of size (sequence length * batch * output dim) Applies a linear function to each time step
Attributes
- Companion
- object
- Supertypes
-
trait Serializabletrait Producttrait Equalsclass Objecttrait Matchableclass AnyShow all
Attributes
- Companion
- object
- Supertypes
-
trait Serializabletrait Producttrait Equalsclass Objecttrait Matchableclass AnyShow all
Attributes
- Companion
- class
- Supertypes
-
trait Producttrait Mirrorclass Objecttrait Matchableclass Any
- Self type
-
Sequential.type
Attributes
- See also
-
https://arxiv.org/pdf/1802.09568.pdf Algorithm 1
- Companion
- object
- Supertypes
-
trait Serializabletrait Producttrait Equalstrait Optimizerclass Objecttrait Matchableclass AnyShow all
Attributes
- Supertypes
Attributes
- Companion
- object
- Supertypes
-
trait Serializabletrait Producttrait Equalsclass Objecttrait Matchableclass AnyShow all
Attributes
- Companion
- class
- Supertypes
-
trait Producttrait Mirrorclass Objecttrait Matchableclass Any
- Self type
-
StatefulSeq2.type
Attributes
- Companion
- object
- Supertypes
-
trait Serializabletrait Producttrait Equalsclass Objecttrait Matchableclass AnyShow all
Attributes
- Companion
- class
- Supertypes
-
trait Producttrait Mirrorclass Objecttrait Matchableclass Any
- Self type
-
StatefulSeq3.type
Attributes
- Companion
- object
- Supertypes
-
trait Serializabletrait Producttrait Equalsclass Objecttrait Matchableclass AnyShow all
Attributes
- Companion
- class
- Supertypes
-
trait Producttrait Mirrorclass Objecttrait Matchableclass Any
- Self type
-
StatefulSeq4.type
Attributes
- Companion
- object
- Supertypes
-
trait Serializabletrait Producttrait Equalsclass Objecttrait Matchableclass AnyShow all
Attributes
- Companion
- class
- Supertypes
-
trait Producttrait Mirrorclass Objecttrait Matchableclass Any
- Self type
-
StatefulSeq5.type
Attributes
- Supertypes
-
trait Serializabletrait Producttrait Equalsclass Objecttrait Matchableclass AnyShow all
Attributes
- Supertypes
-
class Objecttrait Matchableclass Any
Attributes
- Supertypes
-
class Objecttrait Matchableclass Any
Attributes
- Supertypes
-
class Objecttrait Matchableclass Any
Attributes
- Supertypes
-
class Objecttrait Matchableclass Any
Type class about how to switch a module into training or evaluation mode
Type class about how to switch a module into training or evaluation mode
Attributes
- Companion
- object
- Supertypes
-
class Objecttrait Matchableclass Any
Attributes
- Companion
- trait
- Supertypes
-
class Objecttrait Matchableclass Any
- Self type
-
TrainingMode.type
Attributes
- Supertypes
-
class Objecttrait Matchableclass Any
Attributes
- Companion
- object
- Supertypes
-
trait Serializabletrait Producttrait Equalsclass Objecttrait Matchableclass AnyShow all
Attributes
- Companion
- class
- Supertypes
-
trait Producttrait Mirrorclass Objecttrait Matchableclass Any
- Self type
-
Transformer.type
Attributes
- Companion
- object
- Supertypes
-
trait Serializabletrait Producttrait Equalsclass Objecttrait Matchableclass AnyShow all
Attributes
- Companion
- class
- Supertypes
-
trait Producttrait Mirrorclass Objecttrait Matchableclass Any
- Self type
-
TransformerDecoder.type
Attributes
- Companion
- object
- Supertypes
-
trait Serializabletrait Producttrait Equalsclass Objecttrait Matchableclass AnyShow all
Attributes
- Companion
- class
- Supertypes
-
trait Producttrait Mirrorclass Objecttrait Matchableclass Any
- Self type
A module with positional and token embeddings
A module with positional and token embeddings
Token embeddings are lookup embeddings. Positional embeddings are supplied as a constant. They are supposed to come from a fixed unlearned derivation of the positions.
Token and positional embeddings are summed.
Gradients are not computed for positionalEmbedding
Attributes
- Companion
- object
- Supertypes
-
trait Serializabletrait Producttrait Equalsclass Objecttrait Matchableclass AnyShow all
Attributes
- Companion
- class
- Supertypes
-
trait Producttrait Mirrorclass Objecttrait Matchableclass Any
- Self type
-
TransformerEmbedding.type
TransformerEncoder module
TransformerEncoder module
Does not include initial embedding or position encoding.
Input is (data, maxLength)
where data
is (batch, sequence, input dimension), double tensor maxLength
is a 1D or 2D long tensor used for attention masking.
Attention masking is implemented similarly to chapter 11.3.2.1 in d2l.ai v1.0.0-beta0. It supports unmasked attention, attention on variable length input, and left-to-right attention.
Output is (bach, sequence, output dimension)
Attributes
- Companion
- object
- Supertypes
-
trait Serializabletrait Producttrait Equalsclass Objecttrait Matchableclass AnyShow all
Attributes
- Companion
- class
- Supertypes
-
trait Producttrait Mirrorclass Objecttrait Matchableclass Any
- Self type
-
TransformerEncoder.type
A single block of the transformer self attention encoder using GELU
A single block of the transformer self attention encoder using GELU
Input is (data, maxLength)
where data
is (batch, sequence, input dimension), double tensor maxLength
is a 1D or 2D long tensor used for attention masking.
The order of operations depends on gptOrder param. If gptOrder
is true then:
- y = attention(norm(input))+input
- result = mlp(norm(y))+y
- Note that in this case there is no normalization at the end of the transformer. One may wants to add one separately. This is how GPT2 is defined in hugging face or nanoGPT.
- Note that the residual connection has a path which does not flow through the normalization.
-
- dimension wise learnable scale parameter in each residual path
If gptOrder
is false then:
- y = norm(attention(input)+input )
- result = norm(mlp(y)+y)
- This follows chapter 11.7 in d2l.ai v1.0.0-beta0. (Same as in https://arxiv.org/pdf/1706.03762.pdf)
- Note that the residual connection has a path which flows through the normalization.
Output is (bach, sequence, output dimension)
Attributes
- Companion
- object
- Supertypes
-
trait Serializabletrait Producttrait Equalsclass Objecttrait Matchableclass AnyShow all
Attributes
- Companion
- class
- Supertypes
-
trait Producttrait Mirrorclass Objecttrait Matchableclass Any
- Self type
Attributes
- Companion
- object
- Supertypes
-
trait Serializabletrait Producttrait Equalsclass Objecttrait Matchableclass AnyShow all
Attributes
- Companion
- class
- Supertypes
-
class Objecttrait Matchableclass Any
- Self type
-
UnliftedModule.type
Attributes
- Companion
- object
- Supertypes
-
trait Serializabletrait Producttrait Equalsclass Objecttrait Matchableclass AnyShow all
Attributes
- Companion
- class
- Supertypes
-
trait Producttrait Mirrorclass Objecttrait Matchableclass Any
- Self type
-
WeightNormLinear.type
Attributes
- Companion
- object
- Supertypes
-
trait Serializabletrait Producttrait Equalsclass Objecttrait Matchableclass AnyShow all
Attributes
- Companion
- object
- Supertypes
-
trait Serializabletrait Producttrait Equalsclass Objecttrait Matchableclass AnyShow all
The Yogi optimizer algorithm I added the decoupled weight decay term following https://arxiv.org/pdf/1711.05101.pdf
The Yogi optimizer algorithm I added the decoupled weight decay term following https://arxiv.org/pdf/1711.05101.pdf
Attributes
Attributes
- Supertypes
-
class Objecttrait Matchableclass Any
- Self type
-
sequence.type
Attributes
- Supertypes
-
trait Serializabletrait Producttrait Equalstrait OptimizerHyperparameterclass Objecttrait Matchableclass AnyShow all
Attributes
- Supertypes
-
class Objecttrait Matchableclass Any
- Self type
-
statefulSequence.type