| 1 | package Peta::NN::Model; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2 | # ABSTRACT: train a micro model from string pairs, export it as a model file | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 4 | # The training leg's view of a micro model: a small network with the string | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 5 | # handling around it, trained from pairs of strings. What it produces is a | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 6 | # model file for the inference leg (Peta::NN::Inference). | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 7 | # | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 8 | # Three kinds, differing only in what one decision of the network means: | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 9 | # class the string as a whole gets a label | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 10 | # edit one end of the string, or both, is rewritten; the label is the edit | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 11 | # rewrite every character gets a replacement of its own | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 12 | # | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 13 | # A training pair is [input, output] followed by any number of parameters: | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 14 | # [input, output, 'dative'], [input, output, 'feminine', 'comparative']. A | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 15 | # parameter is an opaque value. The model learns what to do for each value | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 16 | # it is shown; it knows nothing of what a value means, and every pair must | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 17 | # have the same number of them. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 18 | # | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 19 | # Answers always come from Peta::NN::Inference, here as after shipping, so | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 20 | # what is measured during training is what a user of the model file gets. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 21 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 22 | 25 | ▶ | use v5.36; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 23 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 24 | 25 | ▶ | use Storable (); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 25 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 26 | 25 | ▶ | use Peta::NN; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 27 | 25 | ▶ | use Peta::NN::Codec qw(build_vocab edit_label edit2_label); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 28 | 25 | ▶ | use Peta::NN::Inference; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 29 | 25 | ▶ | use Peta::NN::RNG; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 30 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 31 | 25 | our $VERSION = '0.2610090'; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 32 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 33 | 25 | my %KIND = map { $_ => 1 } qw(class edit rewrite); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 34 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 35 | 25 | my @DEFAULT_LAYERS = ([ embed => 8 ], [ dense => 32 ], 'relu'); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 36 | 25 | my @CONFIG_KEYS = qw(kind window side radius layers seed given from to goal); # not `how`: a state is plain data, and that may hold subs | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 37 | 25 | my @LEARNED_KEYS = qw(vocab labels params); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 38 | 25 | my @READING_KEYS = qw(kind window side radius vocab labels params given); # what inference needs besides the weights | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 39 | 25 | my @META_KEYS = qw(name description source fidelity); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 40 | 25 | my $STATE_FORMAT = 'Peta::NN training state'; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 41 | 25 | my $EXPORT_BITS = 32; # model files carry 32-bit floats unless asked for 8; training state keeps doubles | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 42 | 25 | my $EPOCHS = 30; # train() without a goal: at most so many epochs, | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 43 | 25 | my $PATIENCE = 3; # and no more than so many without the held-out records being answered better | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 44 | 25 | my %SEARCH = (scale => [ 16, 512 ], start => 64, depth => [1]); # train() to a goal: the widths a job tries | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 45 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 46 | # What a model reads of a string, said in one table: { end => 8 }, { front => 2 }, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 47 | # { both => 5 } (so many characters of each end), or for a rewrite model | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 48 | # { around => 2 } (so many either side of each character). | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 49 | 25 | my %READS = (end => [ side => 'right' ], front => [ side => 'left' ], both => [ side => 'both' ], around => []); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 50 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 51 | 104 | ▶ | sub new ($class, %arg) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
new: called CC=18 SCAR=29.4
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 52 | 104 | ▶ | my $kind = $arg{kind} // die "kind => class, edit or rewrite is required\n"; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Condition50%1/2
$arg{"kind"} // die("kind => class, edit or rewrite is required\n")
MC/DC0%0/1
$arg{"kind"} // die("kind => class, edit or rewrite is required\n")
$arg{"kind"}
Truth table50%1/2
A
A= $arg{"kind"}
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 53 | 104 | ▶ | die "unknown kind '$kind'\n" if !$KIND{$kind}; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 54 | 103 | ▶ | if (my $reads = delete $arg{reads}) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 55 | 9 | ▶ | my ($where, @more) = ref $reads eq 'HASH' ? keys %$reads : (); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 56 | 9 | ▶ | die "reads => { end => N }, { front => N }, { both => N } or { around => N }\n" if !defined $where || @more || !$READS{$where}; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition50%3/6
not defined $where or @more or not $READS{$where}
not defined $where or @more
MC/DC33%1/3
not defined $where or @more or not $READS{$where}
not defined $where
@more
not $READS{$where}
Truth table50%2/4
not(A) or B or not(C)
A= not defined $whereB= @moreC= not $READS{$where}
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 57 | 8 | ▶ | %arg = (%arg, @{ $READS{$where} }, ($where eq 'around' ? 'radius' : 'window') => $reads->{$where}); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 58 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 59 | 102 | ▶ | my $side = $arg{side} // 'right'; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Condition100%2/2
$arg{"side"} // "right"
MC/DC100%1/1
$arg{"side"} // "right"
$arg{"side"}
Truth table100%2/2
A
A= $arg{"side"}
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 60 | 102 | ▶ | die "side is 'left', 'right', 'both' or 'auto'\n" if $side !~ /\A(?:left|right|both|auto)\z/; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 61 | 101 | ▶ | die "side 'auto' is for edit models\n" if $kind ne 'edit' && $side eq 'auto'; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
$kind ne "edit" and $side eq "auto"
MC/DC100%2/2
$kind ne "edit" and $side eq "auto"
$kind ne "edit"
$side eq "auto"
Truth table100%3/3
A and B
A= $kind ne "edit"B= $side eq "auto"
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 62 | return bless { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 63 | kind => $kind, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 64 | window => $arg{window} // 6, # class, edit: characters read from an end | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 65 | side => $side, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 66 | radius => $arg{radius} // 2, # rewrite: characters read either side | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 67 | layers => $arg{layers} // [@DEFAULT_LAYERS], | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 68 | seed => $arg{seed} // 1, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 69 | backend => $arg{backend}, # where training runs; see Peta::NN::Backend | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 70 | given => $arg{given}, # the names of the parameters, in the order the pairs have them | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 71 | from => $arg{from}, # the field of the data it reads, and the one it answers; train() | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 72 | to => $arg{to}, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 73 | goal => $arg{goal}, # what it has to reach: { unseen => share, a mark of the data => share } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 74 | 0 | ▶ | how => { map { $_ => $arg{$_} } grep { defined $arg{$_} } qw(train search budget) }, # how train() goes about it | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 75 | 100 | ▶ | sized => defined $arg{layers} ? 1 : 0, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Condition100%8/8
$arg{"window"} // 6
$arg{"radius"} // 2
$arg{"layers"} // [@DEFAULT_LAYERS]
$arg{"seed"} // 1
MC/DC100%1/1
$arg{"window"} // 6
$arg{"window"}
MC/DC100%1/1
$arg{"radius"} // 2
$arg{"radius"}
MC/DC100%1/1
$arg{"layers"} // [@DEFAULT_LAYERS]
$arg{"layers"}
MC/DC100%1/1
$arg{"seed"} // 1
$arg{"seed"}
Truth table100%2/2
A
A= $arg{"window"}
Truth table100%2/2
A
A= $arg{"radius"}
Truth table100%2/2
A
A= $arg{"layers"}
Truth table100%2/2
A
A= $arg{"seed"}
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 76 | }, $class; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 77 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 78 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 79 | 336 | ▶ | sub net ($self) { return $self->{net} } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
net: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 80 | 12 | ▶ | sub labels ($self) { return @{ $self->{labels} } } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
labels: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 81 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 82 | # The values known for each parameter: one sorted list per position. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 83 | 2 | ▶ | sub parameters ($self) { return map { [ sort keys %$_ ] } @{ $self->{params} // [] } } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
parameters: called CC=2 SCAR=7.1
Pod
documented
Condition50%1/2
$self->{"params"} // []
MC/DC0%0/1
$self->{"params"} // []
$self->{"params"}
Truth table50%1/2
A
A= $self->{"params"}
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 84 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 85 | # The names of the parameters, in their order, if they were given any. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 86 | 0 | ▶ | sub given ($self) { return @{ $self->{given} // [] } } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
given: not called CC=2 SCAR=17.9
Pod
documented
Condition0%0/2
$self->{"given"} // []
MC/DC0%0/1
$self->{"given"} // []
$self->{"given"}
Truth table0%0/2
A
A= $self->{"given"}
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 87 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 88 | # How many token positions the network reads per decision. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 89 | 99 | ▶ | sub _tokens ($self) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_tokens: called CC=4 SCAR=13.9
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 90 | 99 | ▶ | my $params = @{ $self->{params} // [] }; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Condition50%1/2
$self->{"params"} // []
MC/DC0%0/1
$self->{"params"} // []
$self->{"params"}
Truth table50%1/2
A
A= $self->{"params"}
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 91 | 99 | ▶ | return $params + 2 * $self->{radius} + 1 if $self->{kind} eq 'rewrite'; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 92 | 96 | ▶ | return $params + ($self->{side} eq 'both' ? 2 : 1) * $self->{window}; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 93 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 94 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 95 | # How the output differs from the input, as the model's side sees it: at the | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 96 | # end, at the front (an edit at the end of the reversed string), or at both. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 97 | 192733 | ▶ | sub _edit_label ($self, $in, $out) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_edit_label: called CC=3 SCAR=11.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 98 | 192733 | my $side = $self->{side}; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 99 | 192733 | ▶ | return edit2_label($in, $out) if $side eq 'both'; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 100 | 186733 | ▶ | return $side eq 'left' ? edit_label(scalar reverse($in), scalar reverse($out)) : edit_label($in, $out); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 101 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 102 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 103 | # One training pair becomes one or more (tokens, label) decisions. The | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 104 | # windows are the inference leg's: both legs read a string the same way. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 105 | 222997 | ▶ | sub _decisions ($self, $in, $out, @values) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_decisions: called CC=4 SCAR=13.9
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 106 | 222997 | my @chars = split //, $in; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 107 | 222997 | my @tokens = Peta::NN::Inference::param_tokens($self->{params}, @values); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 108 | 222997 | my $kind = $self->{kind}; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 109 | 222997 | ▶ | return [ Peta::NN::Inference::end_window($self, \@chars, @tokens), $out ] if $kind eq 'class'; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 110 | 195134 | ▶ | return [ Peta::NN::Inference::end_window($self, \@chars, @tokens), $self->_edit_label($in, $out) ] if $kind eq 'edit'; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 111 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 112 | 4401 | my @target = split //, $out; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 113 | 4401 | ▶ | die "rewrite needs pairs of equal length: '$in' / '$out'\n" if @chars != @target; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 114 | 4400 | return map { [ Peta::NN::Inference::char_window($self, \@chars, $_, @tokens), $target[$_] ] } 0 .. $#chars; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 115 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 116 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 117 | # Learn from [input, output, parameters...] string pairs, starting from | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 118 | # nothing: the vocabulary, the parameter values, the labels and a fresh | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 119 | # network all come from these pairs. Two of the remaining arguments are the | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 120 | # model's own: | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 121 | # validate string pairs that are not trained on; their loss is measured | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 122 | # every epoch, which is what `patience` then watches | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 123 | # weight sub ($in, $out, parameters...) giving how much a pair counts; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 124 | # 1 is plain | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 125 | # Everything else goes to Peta::NN's train(). | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 126 | 79 | ▶ | sub fit ($self, $pairs, %train) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
fit: called CC=13 SCAR=26.5
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 127 | # An edit model told to choose its side takes the end at which the pairs | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 128 | # differ in fewer ways: a prefix shows as a handful of edits from the | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 129 | # left and as one edit per word from the right. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 130 | 79 | ▶ | if ($self->{side} eq 'auto') { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 131 | 1 | my %count; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 132 | 1 | for my $side (qw(right left)) { | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 133 | 2 | $self->{side} = $side; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 134 | 2 | my %seen = map { $self->_edit_label(@$_[ 0, 1 ]) => 1 } @$pairs; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 135 | 2 | $count{$side} = keys %seen; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 136 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 137 | 1 | ▶ | $self->{side} = $count{left} < $count{right} ? 'left' : 'right'; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 138 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 139 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 140 | 79 | ▶ | die "there are no pairs to learn from\n" if !@$pairs; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 141 | 78 | $self->{vocab} = build_vocab([ map { $_->[0] } @$pairs ]); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 142 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 143 | # Parameter values are tokens after the characters, one embedding row | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 144 | # each. A value has its own token per position: 'a' as first parameter | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 145 | # and 'a' as second are different things to the model. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 146 | 78 | my $positions = @{ $pairs->[0] } - 2; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 147 | 78 | ▶ | die "every pair must have the same number of parameters\n" if grep { @$_ - 2 != $positions } @$pairs; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 148 | die sprintf "the pairs have %d parameter%s, and the model is given %d name%s for them (%s)\n", $positions, $positions == 1 ? '' : 's', | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 149 | 0 | ▶ | scalar @{ $self->{given} }, @{ $self->{given} } == 1 ? '' : 's', join ', ', @{ $self->{given} } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 150 | 77 | ▶ | if $self->{given} && @{ $self->{given} } != $positions; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
Condition66%2/3
$self->{"given"} and @{$self->{"given"};} != $positions
MC/DC0%0/2
$self->{"given"} and @{$self->{"given"};} != $positions
$self->{"given"}
@{$self->{"given"};} != $positions
Truth table66%2/3
A and B
A= $self->{"given"}B= @{$self->{"given"};} != $positions
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 151 | 77 | my $next = 2 + keys %{ $self->{vocab} }; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 152 | $self->{params} = [ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 153 | map { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 154 | 77 | my $position = $_; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 155 | 16 | ▶ | my %seen = map { ($_->[ 2 + $position ] // die "a parameter is undefined\n") => 1 } @$pairs; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Condition50%1/2
$$_[2 + $position] // die("a parameter is undefined\n")
MC/DC0%0/1
$$_[2 + $position] // die("a parameter is undefined\n")
$$_[2 + $position]
Truth table50%1/2
A
A= $$_[2 + $position]
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 156 | 16 | +{ map { $_ => $next++ } sort keys %seen } | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 157 | } 0 .. $positions - 1 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 158 | ]; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 159 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 160 | # Labels are numbered in order of first appearance. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 161 | 77 | my (%seen, @labels); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 162 | 77 | for my $label (map { $_->[1] } map { $self->_decisions(@$_) } @$pairs) { | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 163 | 83802 | ▶ | push @labels, $label if !$seen{$label}++; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 164 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 165 | 76 | $self->{labels} = \@labels; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 166 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 167 | 76 | $self->{net} = $self->_network($self->{layers}); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 168 | 76 | return $self->_train($pairs, %train); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 169 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 170 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 171 | # Go on training the network the model has, on new pairs. What the model can | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 172 | # read and answer stays as it is: a character it has never seen reads as | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 173 | # unknown, and a pair whose answer is not one of its labels, or whose | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 174 | # parameter value it does not know, cannot be learned this way and is | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 175 | # refused. The arguments are fit()'s. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 176 | 54 | ▶ | sub tune ($self, $pairs, %train) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
tune: called CC=4 SCAR=13.9
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 177 | 54 | ▶ | die "the model has not been trained; use fit\n" if !$self->{net}; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 178 | 53 | my %known = map { $_ => 1 } @{ $self->{labels} }; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 179 | 53 | my @foreign = grep { !$known{ $_->[1] } } map { $self->_decisions(@$_) } @$pairs; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 180 | die sprintf "%d of the pairs need an answer this model does not have (%s); that takes a fit from scratch\n", | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 181 | 53 | ▶ | scalar @foreign, join ', ', map { "'$_->[1]'" } @foreign[ 0 .. ($#foreign < 2 ? $#foreign : 2) ] if @foreign; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 182 | 52 | return $self->_train($pairs, %train); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 183 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 184 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 185 | # Make the hidden layers wider, to $width units each, without changing a | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 186 | # single answer: a new unit reads with fresh random weights and is read with | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 187 | # zeros, so the model computes what it did until training gives the new | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 188 | # units something to say. A layer that is as wide already stays as it is. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 189 | # This is how a model that has learned all its size allows gets more room | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 190 | # and goes on learning, instead of a larger one starting from nothing. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 191 | 22 | ▶ | sub widen ($self, $width) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
widen: called CC=9 SCAR=22.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 192 | 22 | ▶ | die "the model has not been trained; a model is widened after fit\n" if !$self->{net}; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 193 | 21 | ▶ | die "the width is a positive whole number, not '$width'\n" if $width !~ /\A[1-9][0-9]*\z/; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 194 | 21 | my @layers = $self->{net}->layers; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 195 | 21 | my @weights = @{ $self->{net}->weights }; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 196 | 21 | my $rng = Peta::NN::RNG->new($self->{seed} * 7919 + $width); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 197 | 21 | my ($last) = grep { $layers[$_]->type eq 'dense' } reverse 0 .. $#layers; # the output layer keeps its size | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 198 | 21 | my $added = 0; # how many inputs the layer in hand has gained, at the end of each row | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 199 | 21 | my (@new, @spec); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 200 | 21 | for my $at (0 .. $#layers) { | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 201 | 88 | my $layer = $layers[$at]; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 202 | 88 | my @own = splice @weights, 0, scalar(() = $layer->param_names); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 203 | 88 | ▶ | if ($layer->type ne 'dense') { push @new, @own; push @spec, $layer->spec if $at < $last; next } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 204 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 205 | 44 | my ($W, $b) = @own; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 206 | 44 | my $out = @$b; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 207 | 44 | my $in = @$W / $out; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 208 | 44 | ▶ | my $more = $at < $last && $width > $out ? $width - $out : 0; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
$at < $last and $width > $out
MC/DC100%2/2
$at < $last and $width > $out
$at < $last
$width > $out
Truth table100%3/3
A and B
A= $at < $lastB= $width > $out
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 209 | 44 | my $limit = sqrt(6 / ($in + $added + $out + $more)); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 210 | 44 | my @W = map { (@$W[ $_ * $in .. ($_ + 1) * $in - 1 ], (0.0) x $added) } 0 .. $out - 1; # what was there reads nothing new | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 211 | 44 | push @W, map { (2 * $rng->uniform - 1) * $limit } 1 .. $more * ($in + $added); # a new unit reads everything | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 212 | 44 | push @new, \@W, [ @$b, (0.0) x $more ]; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 213 | 44 | ▶ | push @spec, [ dense => $out + $more ] if $at < $last; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 214 | 44 | $added = $more; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 215 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 216 | 21 | $self->{layers} = \@spec; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 217 | 21 | $self->{net} = $self->_network(\@spec)->set_weights(\@new); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 218 | 21 | delete @$self{qw(inference inference_of)}; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 219 | 21 | return $self; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 220 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 221 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 222 | # How many rows the embedding table needs: the two reserved tokens, the | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 223 | # characters, and every value of every parameter. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 224 | 99 | ▶ | sub _token_rows ($self) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_token_rows: called CC=3 SCAR=11.1
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 225 | 99 | my $rows = 2 + keys %{ $self->{vocab} }; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 226 | 99 | ▶ | $rows += keys %$_ for @{ $self->{params} // [] }; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Condition50%1/2
$self->{"params"} // []
MC/DC0%0/1
$self->{"params"} // []
$self->{"params"}
Truth table50%1/2
A
A= $self->{"params"}
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 227 | 99 | return $rows; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 228 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 229 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 230 | # A network for this model's reading and labels: the given hidden layers and | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 231 | # an output layer of one unit per label. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 232 | 99 | ▶ | sub _network ($self, $hidden) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_network: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 233 | return Peta::NN->new( | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 234 | input => { tokens => $self->_tokens, vocab => $self->_token_rows }, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 235 | 99 | layers => [ @$hidden, [ dense => scalar @{ $self->{labels} } ] ], | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 236 | loss => 'softmax', | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 237 | seed => $self->{seed}, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 238 | backend => $self->{backend}, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 239 | 99 | ); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 240 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 241 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 242 | # Train the network on the pairs, with the labels the model has. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 243 | 128 | ▶ | sub _train ($self, $pairs, %train) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_train: called CC=4 SCAR=13.9
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 244 | 128 | my $validate = delete $train{validate}; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 245 | 128 | my $weight = delete $train{weight}; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 246 | 128 | my %index; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 247 | 128 | @index{ @{ $self->{labels} } } = 0 .. $#{ $self->{labels} }; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 248 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 249 | # [tokens, class, weight]; one pair may be several decisions, all of its weight. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 250 | my @data = map { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 251 | 128 | ▶ | my $counts = $weight ? $weight->(@$_) : undef; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 252 | 102876 | map { [ $_->[0], $index{ $_->[1] }, $counts ] } $self->_decisions(@$_) | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 253 | } @$pairs; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 254 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 255 | # A validation pair whose answer is a label the model does not have cannot | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 256 | # be scored by the loss; accuracy() will count it as the miss it is. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 257 | 9756 | my @held = map { [ $_->[0], $index{ $_->[1] } ] } grep { exists $index{ $_->[1] } } | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 258 | 128 | ▶ | map { $self->_decisions(@$_) } @{ $validate // [] }; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Condition100%2/2
$validate // []
MC/DC100%1/1
$validate // []
$validate
Truth table100%2/2
A
A= $validate
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 259 | 128 | ▶ | $self->{net}->train(data => \@data, (@held ? (validate => \@held) : ()), %train); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 260 | 128 | return $self; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 261 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 262 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 263 | # A trainable model from a model file: what export() wrote is enough to grow | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 264 | # the training leg's side back. The architecture follows from the layers in | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 265 | # the file; the weights are the file's, so as coarse as it stored them. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 266 | # tune() goes on from there. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 267 | 4 | ▶ | sub from_model ($class, $file, %arg) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
from_model: called CC=7 SCAR=19.6
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 268 | 4 | my $data = Peta::NN::Inference::read_file($file, $Peta::NN::Inference::FORMAT); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 269 | 3 | ▶ | my $read = eval { Peta::NN::Inference->new($data) } or die $@ =~ s/\Amalformed model:/$file is a malformed model:/r; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 270 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 271 | 2 | my @layers = @{ $read->{layers} }; # checked, and with their weights unpacked | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 272 | 2 | my @hidden = map { $_->{type} eq 'embed' ? [ embed => $_->{dim} ] | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 273 | 4 | : $_->{type} eq 'dense' ? [ dense => scalar @{ $_->{weights}[1] } ] | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 274 | 8 | ▶ | : $_->{type} } @layers; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 275 | 2 | ▶ | die "$file does not end in a dense layer\n" if !ref $hidden[-1] || $hidden[-1][0] ne 'dense'; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
not ref $hidden[-1] or $hidden[-1][0] ne "dense"
MC/DC0%0/2
not ref $hidden[-1] or $hidden[-1][0] ne "dense"
not ref $hidden[-1]
$hidden[-1][0] ne "dense"
Truth table33%1/3
not(A) or B
A= not ref $hidden[-1]B= $hidden[-1][0] ne "dense"
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 276 | 2 | pop @hidden; # the output layer: _network adds it, one unit per label | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 277 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 278 | 2 | my $self = $class->new(%$data{qw(kind window side radius given)}, layers => \@hidden, %arg); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 279 | 2 | @$self{qw(vocab labels)} = @$data{qw(vocab labels)}; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 280 | 2 | ▶ | $self->{params} = $data->{params} // []; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Condition50%1/2
$data->{"params"} // []
MC/DC0%0/1
$data->{"params"} // []
$data->{"params"}
Truth table50%1/2
A
A= $data->{"params"}
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 281 | 2 | $self->{net} = $self->_network(\@hidden)->set_weights([ map { @{ $_->{weights} } } @layers ]); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 282 | 2 | return $self; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 283 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 284 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 285 | # --- trained on data, to a goal ---------------------------------------------------- | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 286 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 287 | # The pairs this model learns from, of some data: what it reads, what it | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 288 | # answers and what it is given are fields of the records. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 289 | 20 | ▶ | sub _pairs_of ($self, $data) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_pairs_of: called CC=4 SCAR=13.9
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 290 | 20 | ▶ | defined $self->{$_} or die "a model that is trained on data says which field it reads and which it answers: from => ..., to => ...\n" for qw(from to); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 291 | 19 | ▶ | return $data->pairs(from => $self->{from}, to => $self->{to}, given => $self->{given} // []); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Condition100%2/2
$self->{"given"} // []
MC/DC100%1/1
$self->{"given"} // []
$self->{"given"}
Truth table100%2/2
A
A= $self->{"given"}
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 292 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 293 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 294 | # Train the model on data (Peta::NN::Data): on the records that are not held | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 295 | # out, reading the field `from`, answering the field `to`, given the fields | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 296 | # `given`. A model with a goal is trained until it gets there, by a job | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 297 | # (Peta::NN::Job): `unseen` is the share of records it was not shown that it | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 298 | # has to answer right, and every other name is a mark of the data, the share | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 299 | # of those records it has to. Without a goal it is fitted once, with the | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 300 | # held-out records to say when to stop. Options: | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 301 | # train what goes to the training itself (batch, lr, epochs, ...) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 302 | # search for a goal: the widths to try, { scale => [min, max], start => N } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 303 | # budget for a goal: { seconds => N, ms => N per answer } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 304 | # backend where; the fastest there is unless named | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 305 | 9 | ▶ | sub train ($self, $data, %how) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
train: called CC=14 SCAR=26.9
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 306 | # What the model was told when it was made, and over that what this call says. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 307 | 9 | ▶ | %how = (%how, map { $_ => { %{ $self->{how}{$_} // {} }, %{ $how{$_} // {} } } } grep { $self->{how}{$_} || $how{$_} } qw(train search budget)); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Condition57%4/7
$self->{"how"}{$_} // {}
$how{$_} // {}
$self->{'how'}{$_} || $how{$_}
MC/DC0%0/1
$self->{"how"}{$_} // {}
$self->{"how"}{$_}
MC/DC0%0/1
$how{$_} // {}
$how{$_}
MC/DC50%1/2
$self->{'how'}{$_} || $how{$_}
$self->{'how'}{$_}
$how{$_}
Truth table50%1/2
A
A= $self->{"how"}{$_}
Truth table50%1/2
A
A= $how{$_}
Truth table66%2/3
A || B
A= $self->{'how'}{$_}B= $how{$_}
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 308 | 9 | my $pairs = $self->_pairs_of($data->shown); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 309 | 7 | ▶ | die "there are no records to train on\n" if !@$pairs; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 310 | 7 | my $goal = $self->{goal}; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 311 | 7 | ▶ | $self->{backend} = $how{backend} if defined $how{backend}; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 312 | 7 | ▶ | $self->{backend} //= $ENV{PETA_NN_BACKEND} // 'auto'; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Condition40%2/5
$self->{"backend"} //= $ENV{"PETA_NN_BACKEND"} // "auto"
$ENV{"PETA_NN_BACKEND"} // "auto"
MC/DC0%0/2
$self->{"backend"} //= $ENV{"PETA_NN_BACKEND"} // "auto"
$self->{"backend"}
$ENV{"PETA_NN_BACKEND"}
Truth table0%0/3
A //= B
A= $self->{"backend"}B= $ENV{"PETA_NN_BACKEND"}
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 313 | 7 | ▶ | if (!$goal) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 314 | 4 | my $held = $self->_pairs_of($data->held); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 315 | 4 | ▶ | $self->fit($pairs, (@$held ? (validate => $held, patience => $PATIENCE) : ()), epochs => $EPOCHS, %{ $how{train} // {} }); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition50%1/2
$how{"train"} // {}
MC/DC0%0/1
$how{"train"} // {}
$how{"train"}
Truth table50%1/2
A
A= $how{"train"}
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 316 | 4 | return $self; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 317 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 318 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 319 | 3 | require Peta::NN::Job; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 320 | 3 | ▶ | my $key = sub ($pair) { join "\x{1f}", @$pair[ 0, 2 .. $#$pair ] }; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 321 | 3 | my %marked = map { $_ => { map { $key->($_) => 1 } @{ $self->_pairs_of($data->shown->marked($_)) } } } grep { $_ ne 'unseen' } keys %$goal; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 322 | my $job = Peta::NN::Job->new( | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 323 | 14 | ▶ | model => { map { $_ => $self->{$_} } grep { defined $self->{$_} } qw(kind window side radius given) }, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 324 | pairs => $pairs, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 325 | 1 | fidelity => { (defined $goal->{unseen} ? (all => $goal->{unseen}) : ()), map { $_ => $goal->{$_} } keys %marked }, | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 326 | 1 | ▶ | subsets => { map { my $set = $marked{$_}; ($_ => { of => 'train', where => sub ($pair) { $set->{ $key->($pair) } } }) } keys %marked }, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 327 | 2256 | ▶ | (%marked ? (always_train => sub ($pair) { my $k = $key->($pair); scalar grep { $_->{$k} } values %marked }) : ()), | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 328 | backend => $self->{backend}, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 329 | seed => $self->{seed}, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 330 | 3 | ▶ | search => { %SEARCH, %{ $how{search} // {} } }, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Condition50%1/2
$how{"search"} // {}
MC/DC0%0/1
$how{"search"} // {}
$how{"search"}
Truth table50%1/2
A
A= $how{"search"}
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 331 | 3 | (map { $_ => $how{$_} } grep { defined $how{$_} } qw(budget train)), | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 332 | ); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 333 | 3 | my $trained = $job->run; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 334 | 3 | @$self{qw(layers side vocab labels params net)} = @$trained{qw(layers side vocab labels params net)}; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 335 | 3 | delete @$self{qw(inference card)}; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 336 | 3 | $self->{report} = $job->report; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 337 | 3 | $self->{reached} = $job->result; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 338 | 3 | return $self; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 339 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 340 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 341 | # What the job that trained the model has to say, and what it measured; for | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 342 | # a model trained to a goal. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 343 | 10 | ▶ | sub report ($self) { return $self->{report} } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
report: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 344 | 5 | ▶ | sub reached ($self) { return $self->{reached} } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
reached: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 345 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 346 | # How the model does on data: the share of records it answers exactly, of | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 347 | # those that are held out (`unseen`), and of each mark of the data, of its | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 348 | # records that are not held out: those are the ones a goal is about. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 349 | 3 | ▶ | sub score ($self, $data) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
score: called CC=2 SCAR=7.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 350 | 3 | my %of = (unseen => $data->held, map { $_ => $data->shown->marked($_) } $data->marks); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 351 | 3 | ▶ | return { map { my $pairs = $self->_pairs_of($of{$_}); @$pairs ? ($_ => $self->accuracy($pairs)) : () } keys %of }; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 352 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 353 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 354 | # The model as the inference leg's data: how it reads a string, its labels, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 355 | # and its layers with their weights as lists. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 356 | 767 | ▶ | sub data ($self) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
data: called CC=3 SCAR=11.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 357 | 767 | ▶ | die "the model has not been trained\n" if !$self->{net}; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 358 | 767 | my @weights = @{ $self->{net}->weights }; # in layer order, as many lists as each layer has parameters | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 359 | my @layers = map { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 360 | { type => $_->type, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 361 | 3072 | ▶ | ($_->type eq 'embed' ? (dim => $_->{dim}) : ()), | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 362 | weights => [ splice @weights, 0, scalar(() = $_->param_names) ], | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 363 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 364 | 767 | } $self->{net}->layers; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 365 | 767 | return { %$self{@READING_KEYS}, layers => \@layers }; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 366 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 367 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 368 | # The inference object for the weights as they are now. It is rebuilt when | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 369 | # the network has been updated since, so it is safe to ask during training. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 370 | 6424 | ▶ | sub inference ($self) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
inference: called CC=4 SCAR=13.9
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 371 | 6424 | ▶ | my $generation = $self->{net} ? $self->{net}->generation : die "the model has not been trained\n"; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 372 | 6423 | ▶ | if (!$self->{inference} || $self->{inference_of} != $generation) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
not $self->{"inference"} or $self->{"inference_of"} != $generation
MC/DC100%2/2
not $self->{"inference"} or $self->{"inference_of"} != $generation
not $self->{"inference"}
$self->{"inference_of"} != $generation
Truth table100%3/3
not(A) or B
A= not $self->{"inference"}B= $self->{"inference_of"} != $generation
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 373 | 645 | $self->{inference} = Peta::NN::Inference->new($self->data); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 374 | 645 | $self->{inference_of} = $generation; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 375 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 376 | 6423 | return $self->{inference}; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 377 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 378 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 379 | # The model's answer for a string and its parameters; in list context also | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 380 | # its confidence (for rewrite: that of its least certain character). | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 381 | 4278 | ▶ | sub predict ($self, $text, @values) { return $self->inference->predict($text, @values) } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
predict: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 382 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 383 | 6 | ▶ | sub predict_all ($self, $texts, @values) { return $self->inference->predict_all($texts, @values) } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
predict_all: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 384 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 385 | # Every answer the model considers, with its probability; see Peta::NN::Inference. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 386 | 7 | ▶ | sub distribution ($self, $text, @values) { return $self->inference->distribution($text, @values) } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
distribution: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 387 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 388 | # One distribution for several strings together; class models only. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 389 | 2 | ▶ | sub pooled ($self, $texts, @values) { return $self->inference->pooled($texts, @values) } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
pooled: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 390 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 391 | # This model as one that answers on the graphics card, when that is where it | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 392 | # is trained and it is of a kind the card answers for (class models, and edit | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 393 | # models that rewrite the end): the weights as they are now, in single | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 394 | # precision. Nothing otherwise. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 395 | 1428 | ▶ | sub _on_card ($self) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_on_card: called CC=10 SCAR=41.5
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 396 | 1428 | ▶ | my $net = $self->{net} or die "the model has not been trained\n"; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 397 | 1428 | ▶ | return if $net->backend ne 'gpu' || $self->{kind} eq 'rewrite' || $self->{kind} eq 'edit' && $self->{side} ne 'right'; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition22%2/9
$net->backend ne "gpu" or $self->{"kind"} eq "rewrite" or $self->{"kind"} eq "edit" and $self->{"side"} ne "right"
$net->backend ne "gpu" or $self->{"kind"} eq "rewrite"
$self->{"kind"} eq "edit" and $self->{"side"} ne "right"
MC/DC0%0/4
$net->backend ne "gpu" or $self->{"kind"} eq "rewrite" or $self->{"kind"} eq "edit" and $self->{"side"} ne "right"
$net->backend ne "gpu"
$self->{"kind"} eq "rewrite"
$self->{"kind"} eq "edit"
$self->{"side"} ne "right"
Truth table20%1/5
A or B or C and D
A= $net->backend ne "gpu"B= $self->{"kind"} eq "rewrite"C= $self->{"kind"} eq "edit"D= $self->{"side"} ne "right"
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 398 | 0 | ▶ | if (!$self->{card} || $self->{card_of} != $net->generation) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
not $self->{"card"} or $self->{"card_of"} != $net->generation
MC/DC0%0/2
not $self->{"card"} or $self->{"card_of"} != $net->generation
not $self->{"card"}
$self->{"card_of"} != $net->generation
Truth table0%0/3
not(A) or B
A= not $self->{"card"}B= $self->{"card_of"} != $net->generation
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 399 | 0 | require Peta::NN::Fused; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 400 | 0 | ▶ | my @params = map { \(my $n = $_) } 0 .. $#{ $self->{params} // [] }; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Condition0%0/2
$self->{"params"} // []
MC/DC0%0/1
$self->{"params"} // []
$self->{"params"}
Truth table0%0/2
A
A= $self->{"params"}
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 401 | $self->{card} = Peta::NN::Fused->new( | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 402 | models => { model => $self->data }, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 403 | 0 | ▶ | steps => [ { model => 'model', params => \@params, ($self->{kind} eq 'class' ? (name => 'answer', classify => 1) : ()) } ], | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 404 | engine => 'gpu', | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 405 | ); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 406 | 0 | $self->{card_of} = $net->generation; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 407 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 408 | 0 | return $self->{card}; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 409 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 410 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 411 | # The pairs this model cannot get right together with all the others: those | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 412 | # it reads exactly as it reads some other pair that has a different answer. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 413 | # The model sees a window of a string, not the string, so two strings that | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 414 | # differ outside it are one and the same to it (with nine characters from | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 415 | # the end, frühschichten and geschichten are both "schichten"). No training | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 416 | # changes that; a wider window does. Returned as [pair, another answer, a | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 417 | # pair that has it], one for each such pair. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 418 | 13 | ▶ | sub indistinct ($self, $pairs) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
indistinct: called CC=8 SCAR=20.8
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 419 | 13 | ▶ | die "the model has not been trained\n" if !$self->{net}; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 420 | 13 | my (%answers, @read); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 421 | 13 | for my $pair (@$pairs) { | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 422 | 7487 | my @keys; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 423 | 7487 | for my $decision ($self->_decisions(@$pair)) { | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 424 | 7487 | my $key = join ' ', @{ $decision->[0] }; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 425 | 7487 | ▶ | $answers{$key}{ $decision->[1] } //= $pair; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Condition66%2/3
$answers{$key}{$decision->[1]} //= $pair
MC/DC0%0/2
$answers{$key}{$decision->[1]} //= $pair
$answers{$key}{$decision->[1]}
$pair
Truth table66%2/3
A //= B
A= $answers{$key}{$decision->[1]}B= $pair
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 426 | 7487 | push @keys, [ $key, $decision->[1] ]; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 427 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 428 | 7487 | push @read, [ $pair, \@keys ]; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 429 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 430 | 13 | my @found; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 431 | 13 | for my $read (@read) { | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 432 | 7487 | my ($pair, $keys) = @$read; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 433 | 7487 | for my $key (@$keys) { | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 434 | 7487 | ▶ | my ($other) = grep { $_ ne $key->[1] } sort keys %{ $answers{ $key->[0] } } or next; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 435 | 294 | push @found, [ $pair, $other, $answers{ $key->[0] }{$other} ]; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 436 | 294 | last; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 437 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 438 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 439 | 13 | return @found; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 440 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 441 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 442 | # Share of pairs reproduced exactly. Pairs with the same parameters are | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 443 | # answered in one pass. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 444 | 1741 | ▶ | sub accuracy ($self, $pairs, %how) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
accuracy: called CC=6 SCAR=18.9
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 445 | 1741 | my %same; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 446 | 1741 | push @{ $same{ join "\x{1f}", @$_[ 2 .. $#$_ ] } }, $_ for @$pairs; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 447 | 1741 | ▶ | my $card = $how{where_trained} ? $self->_on_card : undef; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 448 | 1741 | my $right = 0; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 449 | 1741 | for my $group (values %same) { | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 450 | 1762 | my @values = @{ $group->[0] }[ 2 .. $#{ $group->[0] } ]; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 451 | 1762 | my $inputs = [ map { $_->[0] } @$group ]; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 452 | 104290 | ▶ | my @answers = !$card ? map { $_->[0] } @{ $self->inference->answers($inputs, @values) } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 453 | 0 | : $self->{kind} eq 'class' ? map { $_->{answers}{answer} } @{ $card->run($inputs, @values) } | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 454 | 1762 | ▶ | : map { $_->{text} } @{ $card->run($inputs, @values) }; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 455 | 1762 | $right += grep { $answers[$_] eq $group->[$_][1] } 0 .. $#$group; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 456 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 457 | 1741 | return $right / @$pairs; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 458 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 459 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 460 | # The training state as plain data: the model's definition and its weights | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 461 | # at full precision. It is what a state file holds, and what carries a model | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 462 | # from one process to another. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 463 | 8 | ▶ | sub state ($self) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
state: called CC=2 SCAR=7.2
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 464 | 8 | ▶ | die "the model has not been trained\n" if !$self->{net}; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 465 | return { %$self{ @CONFIG_KEYS, @LEARNED_KEYS }, format => $STATE_FORMAT, version => $VERSION, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 466 | 8 | net => Peta::NN::freeze_state($self->{net}->state) }; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 467 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 468 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 469 | # The model a training state describes. The network inside refuses weights | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 470 | # that do not fit its definition. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 471 | 11 | ▶ | sub from_state ($class, $state, %arg) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
from_state: called CC=7 SCAR=20.4
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 472 | die "this is not a training state\n" | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 473 | 11 | ▶ | if ref $state ne 'HASH' || ($state->{format} // '') ne $STATE_FORMAT || ref $state->{net} ne 'HASH' || ref $state->{layers} ne 'ARRAY'; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition63%7/11
ref $state ne "HASH" or ($state->{"format"} // "") ne $STATE_FORMAT or ref $state->{"net"} ne "HASH" or ref $state->{"layers"} ne "ARRAY"
ref $state ne "HASH" or ($state->{"format"} // "") ne $STATE_FORMAT or ref $state->{"net"} ne "HASH"
ref $state ne "HASH" or ($state->{"format"} // "") ne $STATE_FORMAT
$state->{"format"} // ""
MC/DC0%0/4
ref $state ne "HASH" or ($state->{"format"} // "") ne $STATE_FORMAT or ref $state->{"net"} ne "HASH" or ref $state->{"layers"} ne "ARRAY"
ref $state ne "HASH"
$state->{"format"}
ref $state->{"net"} ne "HASH"
ref $state->{"layers"} ne "ARRAY"
Truth table20%1/5
A or B or C or D
A= ref $state ne "HASH"B= $state->{"format"}C= ref $state->{"net"} ne "HASH"D= ref $state->{"layers"} ne "ARRAY"
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 474 | 10 | my $self = $class->new(%$state{@CONFIG_KEYS}, %arg); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 475 | 10 | @$self{@LEARNED_KEYS} = @$state{@LEARNED_KEYS}; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 476 | 10 | ▶ | $self->{params} //= []; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Condition50%1/2
$self->{"params"} //= []
MC/DC0%0/1
$self->{"params"} //= []
$self->{"params"}
Truth table50%1/2
A
A= $self->{"params"}
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 477 | 10 | $self->{net} = Peta::NN->from_state(Peta::NN::thaw_state($state->{net}), backend => $self->{backend}); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 478 | 10 | return $self; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 479 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 480 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 481 | # The training state as a file (by convention *.state), to be loaded again by | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 482 | # this class. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 483 | 2 | ▶ | sub save ($self, $file) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
save: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 484 | 2 | Storable::nstore($self->state, $file); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 485 | 2 | return $self; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 486 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 487 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 488 | # The file is read as plain data. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 489 | 5 | ▶ | sub load ($class, $file, %arg) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
load: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 490 | 5 | my $model = eval { $class->from_state(Peta::NN::Inference::read_file($file, $STATE_FORMAT), %arg) }; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 491 | 5 | ▶ | die $@ =~ s/\Athis is not a training state/$file is a malformed training state/r if !$model; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 492 | 4 | return $model; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 493 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 494 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 495 | # Write the model file that ships (by convention *.model): the inference | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 496 | # leg's data with the weights packed, and what the file says of itself. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 497 | # file where to write it | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 498 | # bits 32 (the default) for 32-bit floats, 8 for signed bytes with | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 499 | # a scale per weight array: a quarter of the size, at some | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 500 | # cost in accuracy that is the caller's to measure | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 501 | # name, description, source, fidelity | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 502 | # free-form: what the model is, what it was trained from, and | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 503 | # the fidelity measured for it ({ name => share }) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 504 | # Peta::NN::Inference->load reads the file. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 505 | 19 | ▶ | sub export ($self, %arg) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
export: called CC=6 SCAR=18.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 506 | 19 | ▶ | my $file = $arg{file} // die "file => path is required\n"; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Condition50%1/2
$arg{"file"} // die("file => path is required\n")
MC/DC0%0/1
$arg{"file"} // die("file => path is required\n")
$arg{"file"}
Truth table50%1/2
A
A= $arg{"file"}
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 507 | 19 | ▶ | my $bits = $arg{bits} // $EXPORT_BITS; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Condition66%2/3
$arg{"bits"} // $EXPORT_BITS
MC/DC0%0/2
$arg{"bits"} // $EXPORT_BITS
$arg{"bits"}
$EXPORT_BITS
Truth table66%2/3
A // B
A= $arg{"bits"}B= $EXPORT_BITS
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 508 | 19 | ▶ | die "a model file stores weights with 8 or 32 bits, not '$bits'\n" if $bits != 8 && $bits != 32; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
$bits != 8 and $bits != 32
MC/DC100%2/2
$bits != 8 and $bits != 32
$bits != 8
$bits != 32
Truth table100%3/3
A and B
A= $bits != 8B= $bits != 32
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 509 | 18 | my $data = $self->data; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 510 | 18 | for my $layer (@{ $data->{layers} }) { | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 511 | 72 | $layer->{weights} = [ map { Peta::NN::Inference::pack_weights($_, $bits) } @{ $layer->{weights} } ]; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 512 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 513 | Storable::nstore({ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 514 | %$data, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 515 | format => $Peta::NN::Inference::FORMAT, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 516 | layout => $Peta::NN::Inference::LAYOUT, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 517 | version => $VERSION, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 518 | bits => $bits, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 519 | 18 | meta => { created => time, map { $_ => $arg{$_} } grep { defined $arg{$_} } @META_KEYS }, | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 520 | }, $file); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 521 | 18 | return $self; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 522 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 523 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 524 | 25 | 1; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 525 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 526 | __END__ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||