GenericModule

lamp.nn.GenericModule
See theGenericModule companion object
trait GenericModule[A, B]

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
Graph
Supertypes
class Object
trait Matchable
class Any
Known subtypes
class BertEncoder
class BertLoss
class EitherModule[A, B, M1, M2]
class GenericFun[A, B]
class Recursive[A, M]
class Seq2[T1, T2, T3, M1, M2]
class Seq3[T1, T2, T3, T4, M1, M2, M3]
class Seq4[T1, T2, T3, T4, T5, M1, M2, M3, M4]
class Seq5[T1, T2, T3, T4, T5, T6, M1, M2, M3, M4, M5]
class Seq6[T1, T2, T3, T4, T5, T6, T7, M1, M2, M3, M4, M5, M6]
class Sequential[A, M]
class Transformer
class UnliftedModule[A, B, C, D, M]
Show all

Members list

Value members

Abstract methods

def forward[S : Sc](x: A): B

The implementation of the function.

The implementation of the function.

In addition of x it can also use all the `state to compute its value.

Attributes

def state: Seq[(Constant, PTag)]

List of optimizable, or non-optimizable, but stateful parameters

List of optimizable, or non-optimizable, but stateful parameters

Stateful means that the state is carried over the repeated forward calls.

Attributes

Concrete methods

def apply[S : Sc](a: A): B

Alias of forward

Alias of forward

Attributes

final def gradients(loss: Variable, zeroGrad: Boolean): Seq[Option[STen]]

Computes the gradient of loss with respect to the parameters.

Computes the gradient of loss with respect to the parameters.

Attributes

final def learnableParameters: Long

Returns the total number of optimizable parameters.

Returns the total number of optimizable parameters.

Attributes

final def parameters: Seq[(Constant, PTag)]

Returns the state variables which need gradient computation.

Returns the state variables which need gradient computation.

Attributes

final def zeroGrad(): Unit