| 1 | package Peta::NN::Layer::Embed; | ||||||||||
| 2 | # ABSTRACT: learned vectors for token indices | ||||||||||
| 3 | |||||||||||
| 4 | # Embedding layer: the input is a fixed number of token indices per sample | ||||||||||
| 5 | # (the characters of a window, say), each looked up in a learned table and | ||||||||||
| 6 | # the rows concatenated. It can only be the first layer, so it never | ||||||||||
| 7 | # produces an input gradient. | ||||||||||
| 8 | |||||||||||
| 9 | 27 | ▶ | use v5.36; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 10 | |||||||||||
| 11 | 27 | our $VERSION = '0.2610090'; | |||||||||
| 12 | |||||||||||
| 13 | 27 | my $INIT_SCALE = 0.1; | |||||||||
| 14 | |||||||||||
| 15 | 116 | ▶ | sub new ($class, %arg) { | ||||||||
|
Subroutine
new: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||
| 16 | 116 | return bless { dim => $arg{dim} }, $class; | |||||||||
| 17 | } | ||||||||||
| 18 | |||||||||||
| 19 | 1691 | ▶ | sub type ($self) { return 'embed' } | ||||||||
|
Subroutine
type: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||
| 20 | |||||||||||
| 21 | # $in is { tokens => count per sample, vocab => table rows }. | ||||||||||
| 22 | 116 | ▶ | sub init ($self, $in, $rng, $backend) { | ||||||||
|
Subroutine
init: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||
| 23 | 116 | ▶ | die "embed must be the first layer, on a token input\n" if ref $in ne 'HASH'; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 24 | 115 | my $dim = $self->{dim}; | |||||||||
| 25 | 115 | $self->{backend} = $backend; | |||||||||
| 26 | 115 | $self->{E} = $backend->tensor([ map { $rng->normal * $INIT_SCALE } 1 .. $in->{vocab} * $dim ], $dim); | |||||||||
| 27 | 115 | return $in->{tokens} * $dim; | |||||||||
| 28 | } | ||||||||||
| 29 | |||||||||||
| 30 | 14817 | ▶ | sub param_names ($self) { return qw(E) } | ||||||||
|
Subroutine
param_names: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||
| 31 | |||||||||||
| 32 | # Numbers per row of the table: one row per token. | ||||||||||
| 33 | 794 | ▶ | sub param_cols ($self, $name) { return $self->{dim} } | ||||||||
|
Subroutine
param_cols: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||
| 34 | |||||||||||
| 35 | 70245 | ▶ | sub forward ($self, $tokens) { | ||||||||
|
Subroutine
forward: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||
| 36 | 70245 | $self->{tokens} = $tokens; | |||||||||
| 37 | 70245 | return $self->{backend}->embed($self->{E}, $tokens); | |||||||||
| 38 | } | ||||||||||
| 39 | |||||||||||
| 40 | 69142 | ▶ | sub backward ($self, $dY) { | ||||||||
|
Subroutine
backward: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||
| 41 | 69142 | $self->{gE} = $self->{backend}->embed_grad($self->{E}, $self->{tokens}, $dY); | |||||||||
| 42 | 69142 | return; | |||||||||
| 43 | } | ||||||||||
| 44 | |||||||||||
| 45 | 29 | ▶ | sub spec ($self) { return [ embed => $self->{dim} ] } | ||||||||
|
Subroutine
spec: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||
| 46 | |||||||||||
| 47 | 27 | 1; | |||||||||
| 48 | |||||||||||
| 49 | __END__ | ||||||||||