| 1 | package Peta::NN::Chain; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2 | # ABSTRACT: models put together into a model | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 4 | # Models put together into a model. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 5 | # | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 6 | # my $plural = chain(umlaut => $umlaut, ending => $ending); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 7 | # print $plural->predict('apfel', gender => 'masculine'); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 8 | # | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 9 | # A chain is what one writes; Peta::NN::Pipeline and Peta::NN::Fused are what | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 10 | # runs it. Its parts are named and come in order, and each part is a model, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 11 | # a model file, or another chain, whose parts then become parts of this one: | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 12 | # what is put together is again something to put together. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 13 | # | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 14 | # What a part does follows from what it is: | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 15 | # | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 16 | # a model that rewrites (edit, rewrite) its answer is the string the next | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 17 | # part reads | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 18 | # a model that classifies (class) its answer is kept under the | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 19 | # part's name; the string passes on | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 20 | # pooled($model) the same for a whole text at once: | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 21 | # every string of the text is | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 22 | # evidence, all get the one answer | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 23 | # chosen($part => { answer => model }) one of several models, chosen for | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 24 | # each string by what the part | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 25 | # named $part answered; '*' stands | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 26 | # for every answer not listed | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 27 | # | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 28 | # Parameters go by name. A part's model is given `gender`: if an earlier part | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 29 | # of the chain is called gender, its answer is the value; if not, gender is | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 30 | # an argument of the chain, and whoever asks the chain names it. Two parts | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 31 | # that are given the same name are given the same value. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 32 | # | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 33 | # A chain answers through the pipeline of its parts, or through the same | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 34 | # fused when it is asked to (on 'gpu'), which is the same function. It is | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 35 | # saved as one file, its parts in it as they are. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 36 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 37 | 3 | ▶ | use v5.36; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 38 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 39 | 3 | ▶ | use Exporter qw(import); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 40 | 3 | ▶ | use Storable (); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 41 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 42 | 3 | ▶ | use Peta::NN::Inference; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 43 | 3 | ▶ | use Peta::NN::Pipeline; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 44 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 45 | 3 | our $VERSION = '0.2610090'; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 46 | 3 | our @EXPORT_OK = qw(chain pooled chosen fixed train_together); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 47 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 48 | 3 | our $FORMAT = 'Peta::NN chain'; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 49 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 50 | # The layout of a chain's file. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 51 | # 1 the parts as they were given (models' data under their names), and | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 52 | # the arguments' names | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 53 | 3 | our $LAYOUT = 1; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 54 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 55 | 3 | my $ANY = '*'; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 56 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 57 | # --- how parts are written -------------------------------------------------------- | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 58 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 59 | # chain(name => part, ...): the same as Peta::NN::Chain->new. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 60 | 26 | ▶ | sub chain (@parts) { return __PACKAGE__->new(@parts) } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
chain: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 61 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 62 | # A classifying model that judges a whole text at once. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 63 | 2 | ▶ | sub pooled ($model) { return bless { model => $model }, 'Peta::NN::Chain::Pooled' } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
pooled: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 64 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 65 | # A model with some of what it is given settled once and for all: | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 66 | # fixed($model, genus => 'masculine'). The chain then does not ask for it. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 67 | 3 | ▶ | sub fixed ($model, %values) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
fixed: called CC=2 SCAR=7.2
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 68 | 3 | ▶ | die "fixed(\$model, name => value, ...)\n" if !%values; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 69 | 3 | return bless { model => $model, values => \%values }, 'Peta::NN::Chain::Fixed'; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 70 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 71 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 72 | # One of several models, chosen by the answer of an earlier part: | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 73 | # chosen(language => { ces => $czech, deu => $german, '*' => $other }). | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 74 | 6 | ▶ | sub chosen ($by, $models) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
chosen: called CC=4 SCAR=17.9
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 75 | 6 | ▶ | die "chosen(part => { answer => model, ... })\n" if ref $by || ref $models ne 'HASH' || !%$models; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%2/6
ref $by or ref $models ne "HASH" or not %$models
ref $by or ref $models ne "HASH"
MC/DC0%0/3
ref $by or ref $models ne "HASH" or not %$models
ref $by
ref $models ne "HASH"
not %$models
Truth table25%1/4
A or B or not(C)
A= ref $byB= ref $models ne "HASH"C= not %$models
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 76 | 6 | return bless { by => $by, models => $models }, 'Peta::NN::Chain::Chosen'; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 77 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 78 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 79 | # Train models that have nothing to do with each other side by side: | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 80 | # train_together({ name => model, ... }, $data, %options), the data and the | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 81 | # options being those of a chain's train(). The names are what the data is | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 82 | # given by where the models learn from different data. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 83 | 0 | ▶ | sub train_together ($models, $data, %how) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
train_together: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 84 | 0 | __PACKAGE__->new(map { $_ => $models->{$_} } sort keys %$models)->train($data, %how); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 85 | 0 | return; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 86 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 87 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 88 | # --- putting it together ------------------------------------------------------------- | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 89 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 90 | # A model as a part: what it is called, and the model data it answers from. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 91 | 54 | ▶ | sub _model ($name, $model) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_model: called CC=20 SCAR=30.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 92 | 54 | ▶ | if (ref $model eq 'Peta::NN::Chain::Fixed') { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 93 | 3 | my $of = _model($name, $model->{model}); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 94 | 3 | ▶ | defined { map { $_ => 1 } @{ $of->{given} } }->{$_} or die "the part '$name' fixes '$_', which its model is not given\n" for keys %{ $model->{values} }; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 95 | 2 | return { %$of, fixed => $model->{values} }; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 96 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 97 | # A model that is yet to be trained: it is a part, and the chain cannot answer until train(). | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 98 | 51 | ▶ | return { name => $name, pending => $model, kind => $model->{kind}, given => [ @{ $model->{given} // [] } ] } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%11/11
ref $model and ref $model ne "HASH" and $model->isa("Peta::NN::Model") and not $model->net
ref $model and ref $model ne "HASH" and $model->isa("Peta::NN::Model")
ref $model and ref $model ne "HASH"
$model->{"given"} // []
MC/DC100%4/4
ref $model and ref $model ne "HASH" and $model->isa("Peta::NN::Model") and not $model->net
ref $model
ref $model ne "HASH"
$model->isa("Peta::NN::Model")
not $model->net
MC/DC100%1/1
$model->{"given"} // []
$model->{"given"}
Truth table100%5/5
A and B and C and not(D)
A= ref $modelB= ref $model ne "HASH"C= $model->isa("Peta::NN::Model")D= not $model->net
Truth table100%2/2
A
A= $model->{"given"}
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 99 | if ref $model && ref $model ne 'HASH' && $model->isa('Peta::NN::Model') && !$model->net; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 100 | 47 | ▶ | my $data = !ref $model ? Peta::NN::Inference::read_file($model, $Peta::NN::Inference::FORMAT) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 3 branches
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 101 | : ref $model eq 'HASH' ? $model | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 102 | : $model->can('data') ? $model->data | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 103 | : die "the part '$name' is not a model, a model file or a chain\n"; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 104 | 47 | ▶ | my $read = eval { Peta::NN::Inference->new($data) } or die "the part '$name': $@"; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 105 | die "the part '$name' has parameters without names; a model in a chain is given names for them (given => [...])\n" | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 106 | 47 | ▶ | if @{ $data->{params} // [] } && !$data->{given}; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition80%4/5
@{$data->{"params"} // [];} and not $data->{"given"}
$data->{"params"} // []
MC/DC100%2/2
@{$data->{"params"} // [];} and not $data->{"given"}
@{$data->{"params"} // [];}
not $data->{"given"}
MC/DC0%0/1
$data->{"params"} // []
$data->{"params"}
Truth table100%3/3
A and not(B)
A= @{$data->{"params"} // [];}B= not $data->{"given"}
Truth table50%1/2
A
A= $data->{"params"}
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 107 | 46 | ▶ | return { name => $name, data => $data, model => $read, kind => $data->{kind}, given => [ @{ $data->{given} // [] } ], | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%8/8
$data->{"given"} // []
ref $model and ref $model ne "HASH" and $model->isa("Peta::NN::Model")
ref $model and ref $model ne "HASH"
MC/DC100%1/1
$data->{"given"} // []
$data->{"given"}
MC/DC100%3/3
ref $model and ref $model ne "HASH" and $model->isa("Peta::NN::Model")
ref $model
ref $model ne "HASH"
$model->isa("Peta::NN::Model")
Truth table100%2/2
A
A= $data->{"given"}
Truth table100%4/4
A and B and C
A= ref $modelB= ref $model ne "HASH"C= $model->isa("Peta::NN::Model")
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 108 | (ref $model && ref $model ne 'HASH' && $model->isa('Peta::NN::Model') ? (trained => $model) : ()) }; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 109 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 110 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 111 | # name => part, in order. A part is a model (Peta::NN::Model, trained; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 112 | # Peta::NN::Inference; a model file's path), pooled(...), chosen(...), or a | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 113 | # chain. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 114 | 27 | ▶ | sub new ($class, @parts) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
new: called CC=13 SCAR=25.7
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 115 | 27 | ▶ | die "a chain is made of name => part, name => part, ...\n" if !@parts || @parts % 2; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
not @parts or @parts % 2
MC/DC100%2/2
not @parts or @parts % 2
not @parts
@parts % 2
Truth table100%3/3
not(A) or B
A= not @partsB= @parts % 2
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 116 | 25 | my $self = bless { parts => [], model => {} }, $class; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 117 | 25 | ▶ | while (my ($name, $part) = splice @parts, 0, 2) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 118 | 42 | ▶ | die "a part's name is plain text\n" if ref $name || !length $name; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
ref $name or not length $name
MC/DC0%0/2
ref $name or not length $name
ref $name
not length $name
Truth table33%1/3
A or not(B)
A= ref $nameB= not length $name
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 119 | 42 | ▶ | if (ref $part && ref $part ne 'HASH' && $part->isa(__PACKAGE__)) { # a chain: its parts are parts of this one | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 3 branches
Condition100%6/6
ref $part and ref $part ne "HASH" and $part->isa(__PACKAGE__)
ref $part and ref $part ne "HASH"
MC/DC100%3/3
ref $part and ref $part ne "HASH" and $part->isa(__PACKAGE__)
ref $part
ref $part ne "HASH"
$part->isa(__PACKAGE__)
Truth table100%4/4
A and B and C
A= ref $partB= ref $part ne "HASH"C= $part->isa(__PACKAGE__)
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 120 | 5 | $self->_add(%$_) for @{ $part->{parts} }; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 121 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 122 | elsif (ref $part eq 'Peta::NN::Chain::Chosen') { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 123 | 6 | ▶ | my %models = map { $_ => _model("$name-" . ($_ eq $ANY ? 'other' : $_), $part->{models}{$_}) } keys %{ $part->{models} }; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 124 | 6 | $self->_add(name => $name, by => $part->{by}, models => \%models); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 125 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 126 | 2 | elsif (ref $part eq 'Peta::NN::Chain::Pooled') { $self->_add(name => $name, pooled => 1, models => { $ANY => _model($name, $part->{model}) }) } | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 127 | 28 | else { $self->_add(name => $name, models => { $ANY => _model($name, $part) }) } | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 128 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 129 | 19 | return $self->_wire; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 130 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 131 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 132 | # One more part: { name, models => { answer or '*' => model }, by, pooled }. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 133 | 53 | ▶ | sub _add ($self, %part) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_add: called CC=8 SCAR=20.8
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 134 | 53 | ▶ | die "the chain has two parts called '$part{name}'\n" if grep { $_->{name} eq $part{name} } @{ $self->{parts} }; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 135 | 52 | for my $model (values %{ $part{models} }) { | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 136 | 57 | ▶ | die "the chain has two models called '$model->{name}'\n" if $self->{model}{ $model->{name} }; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 137 | 57 | $self->{model}{ $model->{name} } = $model; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 138 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 139 | 52 | my ($any) = values %{ $part{models} }; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 140 | 52 | my $class = $any->{kind} eq 'class'; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 141 | die "the part '$part{name}' chooses between models that classify and models that rewrite\n" | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 142 | 52 | ▶ | if grep { ($_->{kind} eq 'class') != $class } values %{ $part{models} }; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 143 | 51 | ▶ | die "the part '$part{name}' is pooled, which is for a model that classifies\n" if $part{pooled} && !$class; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
$part{"pooled"} and not $class
MC/DC100%2/2
$part{"pooled"} and not $class
$part{"pooled"}
not $class
Truth table100%3/3
A and not(B)
A= $part{"pooled"}B= not $class
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 144 | 50 | ▶ | push @{ $self->{parts} }, { %part, classifies => $class ? 1 : 0 }; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 145 | 50 | return; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 146 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 147 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 148 | # The pipeline of the parts: each parameter is an earlier part's answer if a | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 149 | # part that classifies has its name, and an argument of the chain if not. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 150 | 27 | ▶ | sub _wire ($self) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_wire: called CC=13 SCAR=25.6
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 151 | 27 | delete @$self{qw(pipeline fused)}; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 152 | 27 | my (%answers, @arguments, %position, @steps); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 153 | 27 | for my $part (@{ $self->{parts} }) { | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 154 | 52 | my %step; # only a part that classifies has an answer to keep under its name | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 155 | 52 | ▶ | @step{qw(name classify pool)} = ($part->{name}, 1, $part->{pooled} ? 1 : 0) if $part->{classifies}; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 156 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 157 | 52 | my @models = values %{ $part->{models} }; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 158 | 52 | my @given = @{ $models[0]{given} }; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 159 | die "the models the part '$part->{name}' chooses from are not given the same parameters\n" | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 160 | 52 | ▶ | if grep { "@{ $_->{given} }" ne "@given" } @models; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 161 | 51 | ▶ | my $fixed = $models[0]{fixed} // {}; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Condition100%2/2
$models[0]{"fixed"} // {}
MC/DC100%1/1
$models[0]{"fixed"} // {}
$models[0]{"fixed"}
Truth table100%2/2
A
A= $models[0]{"fixed"}
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 162 | $step{params} = [ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 163 | map { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 164 | 51 | my $name = $_; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 165 | defined $fixed->{$name} ? "$fixed->{$name}" | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 166 | : $answers{$name} ? { answer => $name } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 167 | 20 | ▶ | : \($position{$name} //= do { push @arguments, $name; $#arguments }) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
Condition100%3/3
$position{$name} //= do {
push @arguments, $name;
$#arguments
}
MC/DC100%2/2
$position{$name} //= do {
push @arguments, $name;
$#arguments
}
$position{$name}
do {
push @arguments, $name;
$#arguments
}
Truth table100%3/3
A //= B
A= $position{$name}B= do {
push @arguments, $name;
$#arguments
}
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 168 | } @given | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 169 | ]; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 170 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 171 | 51 | ▶ | if (defined $part->{by}) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 172 | 5 | ▶ | die "the part '$part->{name}' chooses by '$part->{by}', which is not an earlier part that classifies\n" if !$answers{ $part->{by} }; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 173 | 3 | $step{model} = { $part->{by} => { map { $_ => $part->{models}{$_}{name} } keys %{ $part->{models} } } }; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 174 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 175 | 46 | else { $step{model} = $models[0]{name} } | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 176 | 49 | push @steps, \%step; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 177 | 49 | ▶ | $answers{ $part->{name} } = 1 if $part->{classifies}; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 178 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 179 | 24 | $self->{given} = \@arguments; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 180 | 24 | $self->{steps} = \@steps; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 181 | # It answers once all its models are trained. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 182 | 45 | $self->{pipeline} = Peta::NN::Pipeline->new(models => { map { $_ => $self->{model}{$_}{model} } keys %{ $self->{model} } }, steps => \@steps) | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 183 | 24 | ▶ | if !grep { $_->{pending} } values %{ $self->{model} }; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 184 | 24 | return $self; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 185 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 186 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 187 | # --- what it is --------------------------------------------------------------------------- | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 188 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 189 | # The names of its arguments, in the order the parts first need them. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 190 | 9 | ▶ | sub given ($self) { return @{ $self->{given} } } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
given: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 191 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 192 | # The names of its parts, in order. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 193 | 7 | ▶ | sub parts ($self) { return map { $_->{name} } @{ $self->{parts} } } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
parts: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 194 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 195 | # The names of its models, in the order of the parts; a part that chooses has several. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 196 | 12 | ▶ | sub models ($self) { return map { my $part = $_; map { $part->{models}{$_}{name} } sort keys %{ $part->{models} } } @{ $self->{parts} } } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
models: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 197 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 198 | # One of its models, as the inference leg's. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 199 | 10 | ▶ | sub model ($self, $name) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
model: called CC=3 SCAR=11.1
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 200 | 10 | ▶ | my $model = $self->{model}{$name} // die "this chain has no model '$name' (it has: @{[ $self->models ]})\n"; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Condition100%2/2
$self->{"model"}{$name} // die("this chain has no model '${name}' (it has: @{[$self->models];})\n")
MC/DC100%1/1
$self->{"model"}{$name} // die("this chain has no model '${name}' (it has: @{[$self->models];})\n")
$self->{"model"}{$name}
Truth table100%2/2
A
A= $self->{"model"}{$name}
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 201 | 9 | ▶ | return $model->{model} // die "the model '$name' is not trained yet; train() the chain\n"; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Condition50%1/2
$model->{"model"} // die("the model '${name}' is not trained yet; train() the chain\n")
MC/DC0%0/1
$model->{"model"} // die("the model '${name}' is not trained yet; train() the chain\n")
$model->{"model"}
Truth table50%1/2
A
A= $model->{"model"}
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 202 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 203 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 204 | # The names of the parts that classify: what a record's answers are. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 205 | 2 | ▶ | sub answers ($self) { return map { $_->{name} } grep { $_->{classifies} } @{ $self->{parts} } } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
answers: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 206 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 207 | 2 | ▶ | sub n_params ($self) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
n_params: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 208 | 2 | my $n = 0; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 209 | 2 | $n += $self->model($_)->n_params for $self->models; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 210 | 2 | return $n; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 211 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 212 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 213 | # The pipeline that runs it. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 214 | 45 | ▶ | sub pipeline ($self) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
pipeline: called CC=5 SCAR=16.7
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 215 | # A model that was to be trained may have been, by its own train() or by another chain's. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 216 | 45 | ▶ | my @since = grep { $_->{pending} && $_->{pending}->net } values %{ $self->{model} }; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Condition66%2/3
$_->{'pending'} && $_->{'pending'}->net
MC/DC0%0/2
$_->{'pending'} && $_->{'pending'}->net
$_->{'pending'}
$_->{'pending'}->net
Truth table66%2/3
A && B
A= $_->{'pending'}B= $_->{'pending'}->net
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 217 | 45 | for my $part (@since) { | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 218 | 0 | my $model = delete $part->{pending}; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 219 | 0 | @$part{qw(data model trained)} = ($model->data, $model->inference, $model); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 220 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 221 | 45 | ▶ | $self->_wire if @since; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 222 | return $self->{pipeline} // die sprintf "this chain cannot answer yet: %s not trained; train() it\n", | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 223 | 45 | ▶ | join ', ', map { "'$_' is" } grep { $self->{model}{$_}{pending} } $self->models; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Condition100%2/2
$self->{"pipeline"} // die(sprintf("this chain cannot answer yet: %s not trained; train() it\n", join(", ", map({"'${_}' is";} grep({$self->{"model"}{$_}{"pending"};} $self->models)))))
MC/DC100%1/1
$self->{"pipeline"} // die(sprintf("this chain cannot answer yet: %s not trained; train() it\n", join(", ", map({"'${_}' is";} grep({$self->{"model"}{$_}{"pending"};} $self->models)))))
$self->{"pipeline"}
Truth table100%2/2
A
A= $self->{"pipeline"}
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 224 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 225 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 226 | # The same fused, on 'cpu' or 'gpu'; dies with the reason if its parts do not fuse. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 227 | 5 | ▶ | sub fused ($self, $engine = 'cpu') { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
fused: called CC=3 SCAR=11.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 228 | 5 | require Peta::NN::Fused; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 229 | 5 | $self->pipeline; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 230 | return $self->{fused}{$engine} //= Peta::NN::Fused->new( | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 231 | 5 | ▶ | models => { map { $_ => $self->{model}{$_}{data} } keys %{ $self->{model} } }, steps => $self->{steps}, engine => $engine); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Condition100%3/3
$self->{"fused"}{$engine} //= "Peta::NN::Fused"->new("models", {map({$_, $self->{"model"}{$_}{"data"};} keys %{$$self{"model"};})}, "steps", $self->{"steps"}, "engine", $engine)
MC/DC100%2/2
$self->{"fused"}{$engine} //= "Peta::NN::Fused"->new("models", {map({$_, $self->{"model"}{$_}{"data"};} keys %{$$self{"model"};})}, "steps", $self->{"steps"}, "engine", $engine)
$self->{"fused"}{$engine}
"Peta::NN::Fused"->new("models", {map({$_, $self->{"model"}{$_}{"data"};} keys %{$$self{"model"};})}, "steps", $self->{"steps"}, "engine", $engine)
Truth table100%3/3
A //= B
A= $self->{"fused"}{$engine}B= "Peta::NN::Fused"->new("models", {map({$_, $self->{"model"}{$_}{"data"};} keys %{$$self{"model"};})}, "steps", $self->{"steps"}, "engine", $engine)
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 232 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 233 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 234 | # Where it answers from now on: 'pipeline' (as it starts), or fused on 'cpu' | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 235 | # or 'gpu'. Returns the chain. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 236 | 5 | ▶ | sub on ($self, $where) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
on: called CC=3 SCAR=11.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 237 | 5 | ▶ | die "a chain answers through its 'pipeline', or fused on 'cpu' or 'gpu', not on '$where'\n" if $where !~ /\A(?:pipeline|cpu|gpu)\z/; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 238 | 4 | ▶ | $self->fused($where) if $where ne 'pipeline'; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 239 | 3 | $self->{on} = $where; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 240 | 3 | return $self; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 241 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 242 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 243 | 34 | ▶ | sub _runner ($self) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_runner: called CC=3 SCAR=11.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 244 | 34 | ▶ | my $on = $self->{on} // 'pipeline'; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Condition100%2/2
$self->{"on"} // "pipeline"
MC/DC100%1/1
$self->{"on"} // "pipeline"
$self->{"on"}
Truth table100%2/2
A
A= $self->{"on"}
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 245 | 34 | ▶ | return $on eq 'pipeline' ? $self->pipeline : $self->fused($on); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 246 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 247 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 248 | # --- training ----------------------------------------------------------------------------------- | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 249 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 250 | # Train the models of the chain that are not trained yet, on data | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 251 | # (Peta::NN::Data), each as Peta::NN::Model's train() does it: on the fields | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 252 | # it reads, answers and is given, to its goal if it has one. They have | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 253 | # nothing to do with each other, so they are trained side by side, as many | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 254 | # at a time as PETA_NN_WORKERS says. The options are those of a model's | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 255 | # train(). Returns the chain, which can answer now. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 256 | 3 | ▶ | sub train ($self, $data, %how) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
train: called CC=2 SCAR=7.2
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 257 | 3 | require Peta::NN::Model; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 258 | 3 | require Peta::NN::Parallel; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 259 | 3 | my @pending = grep { $_->{pending} } map { $self->{model}{$_} } $self->models; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 260 | # One data for all, or for each model its own: { model => data, ..., '*' => data for the others }. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 261 | 4 | ▶ | my $data_of = sub ($name) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=4 SCAR=24.4
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 262 | 4 | ▶ | return $data if ref $data ne 'HASH'; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 263 | 0 | ▶ | return $data->{$name} // $data->{$ANY} // die "there is no data to train the model '$name' on\n"; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Condition0%0/5
($data->{$name} // $data->{$ANY}) // die("there is no data to train the model '${name}' on\n")
$data->{$name} // $data->{$ANY}
MC/DC0%0/2
($data->{$name} // $data->{$ANY}) // die("there is no data to train the model '${name}' on\n")
$data->{$name}
$data->{$ANY}
Truth table0%0/3
A // B
A= $data->{$name}B= $data->{$ANY}
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 264 | 3 | }; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 265 | my @done = Peta::NN::Parallel::in_parallel(Peta::NN::Parallel::workers(), map { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 266 | 3 | my ($model, $on) = ($_->{pending}, $data_of->($_->{name})); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 267 | 4 | ▶ | sub { $model->train($on, %how); return { state => $model->state, report => $model->report, reached => $model->reached } } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 268 | 4 | } @pending); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 269 | 3 | for my $part (@pending) { | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 270 | 4 | my ($model, $done) = (delete $part->{pending}, shift @done); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 271 | # What a child process trained comes back as a training state, into the model the caller holds. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 272 | 4 | my $trained = Peta::NN::Model->from_state($done->{state}, backend => $model->{backend}); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 273 | 4 | @$model{qw(layers side vocab labels params net)} = @$trained{qw(layers side vocab labels params net)}; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 274 | 4 | delete @$model{qw(inference card)}; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 275 | 4 | @$model{qw(report reached)} = @$done{qw(report reached)}; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 276 | 4 | @$part{qw(data model trained)} = ($model->data, $model->inference, $model); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 277 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 278 | 3 | return $self->_wire; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 279 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 280 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 281 | # What the jobs that trained the chain's models have to say, model by model; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 282 | # for the models that were trained to a goal. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 283 | 1 | ▶ | sub report ($self) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
report: called CC=3 SCAR=12.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 284 | 1 | $self->pipeline; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 285 | 1 | ▶ | return join '', map { my $of = $self->{model}{$_}{trained}; $of && $of->report ? "== $_ ==\n" . $of->report . "\n" : () } $self->models; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
$of and $of->report
MC/DC0%0/2
$of and $of->report
$of
$of->report
Truth table33%1/3
A and B
A= $ofB= $of->report
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 286 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 287 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 288 | # How the chain does on data: the share of records for which it turns the | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 289 | # field `from` into the field `to`, of those that are held out (`unseen`) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 290 | # and of each mark of the data. Its arguments are the records' fields of the | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 291 | # same names. With answer => a part that classifies, it is that part's | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 292 | # answer that is held against `to`. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 293 | 3 | ▶ | sub score ($self, $data, %arg) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
score: called CC=8 SCAR=20.8
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 294 | 3 | ▶ | defined $arg{$_} or die "score(\$data, from => the field the chain reads, to => the field it is to give)\n" for qw(from to); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 295 | 2 | my @names = $self->given; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 296 | 2 | my %of = (unseen => $data->held, map { $_ => $data->shown->marked($_) } $data->marks); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 297 | 2 | my %score; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 298 | 2 | for my $name (keys %of) { | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 299 | 3 | ▶ | my @records = $of{$name}->records or next; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 300 | 3 | my %together; # the records that are asked with the same arguments | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 301 | 3 | push @{ $together{ join "\x{1f}", @$_{@names} } }, $_ for @records; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 302 | 3 | my $right = 0; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 303 | 3 | for my $group (values %together) { | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 304 | 5 | my $answers = $self->run([ map { $_->{ $arg{from} } } @$group ], map { $_ => $group->[0]{$_} } @names); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 305 | 5 | ▶ | $right += grep { (defined $arg{answer} ? $answers->[$_]{answers}{ $arg{answer} } : $answers->[$_]{text}) eq $group->[$_]{ $arg{to} } } 0 .. $#$group; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 306 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 307 | 3 | $score{$name} = $right / @records; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 308 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 309 | 2 | return \%score; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 310 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 311 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 312 | # --- answering -------------------------------------------------------------------------------- | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 313 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 314 | # What a caller named, as the arguments in their order. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 315 | 37 | ▶ | sub _arguments ($self, @named) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_arguments: called CC=4 SCAR=13.9
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 316 | 37 | my @names = @{ $self->{given} }; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 317 | 37 | ▶ | my $takes = @names ? 'takes ' . join(', ', map { "$_ => ..." } @names) : 'takes no arguments'; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 318 | 37 | ▶ | die "this chain $takes\n" if @named != 2 * @names; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 319 | 35 | my %named = @named; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 320 | 35 | ▶ | return map { delete $named{$_} // die "this chain $takes; '$_' is missing\n" } @names; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Condition100%2/2
delete $named{$_} // die("this chain $takes; '${_}' is missing\n")
MC/DC100%1/1
delete $named{$_} // die("this chain $takes; '${_}' is missing\n")
delete $named{$_}
Truth table100%2/2
A
A= delete $named{$_}
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 321 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 322 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 323 | # One record per string: { text, confidence, answers => { part => answer } }. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 324 | 32 | ▶ | sub run ($self, $texts, @named) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
run: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 325 | 32 | my @arguments = $self->_arguments(@named); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 326 | 29 | return [ map { { text => $_->{text}, confidence => $_->{confidence}, answers => $_->{answers} } } @{ $self->_runner->run($texts, @arguments) } ]; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 327 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 328 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 329 | # The same for texts, each a list of strings; returns the records per text. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 330 | # A pooled part answers once per text. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 331 | 5 | ▶ | sub run_texts ($self, $texts, @named) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
run_texts: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 332 | 5 | my @arguments = $self->_arguments(@named); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 333 | 5 | return [ map { [ map { { text => $_->{text}, confidence => $_->{confidence}, answers => $_->{answers} } } @$_ ] } @{ $self->_runner->run_texts($texts, @arguments) } ]; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 334 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 335 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 336 | # The chain's answer for a string: the string as its parts have rewritten | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 337 | # it; in list context also its confidence, the product of the parts'. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 338 | 21 | ▶ | sub predict ($self, $text, @named) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
predict: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 339 | 21 | my ($record) = @{ $self->run([$text], @named) }; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 340 | 17 | ▶ | return wantarray ? @$record{qw(text confidence)} : $record->{text}; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 341 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 342 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 343 | # The answers for many strings at once, in their order. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 344 | 5 | ▶ | sub predict_all ($self, $texts, @named) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
predict_all: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 345 | 5 | return map { $_->{text} } @{ $self->run($texts, @named) }; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 346 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 347 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 348 | # --- a file ----------------------------------------------------------------------------------- | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 349 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 350 | # A model's data as it goes into a file: as it came from its own file, or | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 351 | # with its weights packed in full if it came as a trained model. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 352 | 14 | ▶ | sub _stored ($data, $bits) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_stored: called CC=3 SCAR=11.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 353 | 14 | ▶ | return $data if !defined $bits || !grep { ref eq 'ARRAY' } map { @{ $_->{weights} } } @{ $data->{layers} }; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
defined $bits and grep {ref $_ eq "ARRAY";} map({@{$_->{"weights"};};} @{$$data{"layers"};})
MC/DC50%1/2
defined $bits and grep {ref $_ eq "ARRAY";} map({@{$_->{"weights"};};} @{$$data{"layers"};})
defined $bits
grep {ref $_ eq "ARRAY";} map({@{$_->{"weights"};};} @{$$data{"layers"};})
Truth table66%2/3
A and B
A= defined $bitsB= grep {ref $_ eq "ARRAY";} map({@{$_->{"weights"};};} @{$$data{"layers"};})
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 354 | return { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 355 | %$data, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 356 | format => $Peta::NN::Inference::FORMAT, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 357 | layout => $Peta::NN::Inference::LAYOUT, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 358 | bits => $bits, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 359 | 12 | layers => [ map { +{ %$_, weights => [ map { Peta::NN::Inference::pack_weights($_, $bits) } @{ $_->{weights} } ] } } @{ $data->{layers} } ], | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 360 | }; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 361 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 362 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 363 | # With how many bits each model's weights went into the file at the last | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 364 | # save(): { model => 8 or 32 }; nothing for a model that came from a file. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 365 | 4 | ▶ | sub stored ($self) { return $self->{stored} } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
stored: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 366 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 367 | # How many bits a model's weights are stored with. A model that came from a | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 368 | # file stays as it is. One that was trained here is stored with 32 bits; or, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 369 | # when the caller says by which data to judge, with 8 if the model as stored | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 370 | # then still answers every record of that data the way the trained one does, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 371 | # and with 32 if not. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 372 | 13 | ▶ | sub _bits_for ($part, $judge) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_bits_for: called CC=12 SCAR=25.4
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 373 | 13 | my $data = $part->{data}; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 374 | 13 | ▶ | return undef if !grep { ref eq 'ARRAY' } map { @{ $_->{weights} } } @{ $data->{layers} }; # as it came from its file | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 375 | 11 | my $trained = $part->{trained}; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 376 | 11 | ▶ | return 32 if !$judge || !$trained || !defined $trained->{from}; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition83%5/6
$judge and $trained and defined $trained->{"from"}
$judge and $trained
MC/DC66%2/3
$judge and $trained and defined $trained->{"from"}
$judge
$trained
defined $trained->{"from"}
Truth table75%3/4
A and B and C
A= $judgeB= $trainedC= defined $trained->{"from"}
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 377 | 1 | ▶ | my $records = ref $judge eq 'HASH' ? $judge->{ $part->{name} } // $judge->{$ANY} : $judge; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
$$judge{$part->{"name"}} // $judge->{$ANY}
MC/DC0%0/2
$$judge{$part->{"name"}} // $judge->{$ANY}
$$judge{$part->{"name"}}
$judge->{$ANY}
Truth table0%0/3
A // B
A= $$judge{$part->{"name"}}B= $judge->{$ANY}
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 378 | 1 | ▶ | return 32 if !$records; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 379 | 1 | ▶ | my $pairs = $records->pairs(from => $trained->{from}, to => $trained->{to}, given => $trained->{given} // []); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Condition50%1/2
$trained->{"given"} // []
MC/DC0%0/1
$trained->{"given"} // []
$trained->{"given"}
Truth table50%1/2
A
A= $trained->{"given"}
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 380 | 1 | my %with; # the values given => the strings asked with them | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 381 | 1 | push @{ $with{ join "\x{1f}", @$_[ 2 .. $#$_ ] } }, $_ for @$pairs; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 382 | 1 | my $small = Peta::NN::Inference->new(_stored($data, 8)); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 383 | 1 | for my $group (values %with) { | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 384 | 2 | my @values = @{ $group->[0] }[ 2 .. $#{ $group->[0] } ]; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 385 | 2 | my @texts = map { $_->[0] } @$group; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 386 | 2 | my ($a, $b) = map { join "\x{1f}", map { $_->[0] } @{ $_->answers(\@texts, @values) } } $small, $part->{model}; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 387 | 2 | ▶ | return 32 if $a ne $b; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 388 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 389 | 1 | return 8; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 390 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 391 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 392 | # Write the chain to one file: its parts, each model as it is. Models that | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 393 | # were trained here are stored with 32-bit weights; with small => data (or | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 394 | # { model => data, ... }) each of them with 8 bits if it then still answers | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 395 | # every record of that data as before. A table of what the file is to say of | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 396 | # itself (name, description, ...) may follow. stored() says what was done. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 397 | 7 | ▶ | sub save ($self, $file, %meta) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
save: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 398 | 7 | my $judge = delete $meta{small}; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 399 | 7 | $self->pipeline; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 400 | 6 | $self->{stored} = { map { $_ => _bits_for($self->{model}{$_}, $judge) } keys %{ $self->{model} } }; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 401 | Storable::nstore({ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 402 | format => $FORMAT, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 403 | layout => $LAYOUT, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 404 | version => $VERSION, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 405 | 12 | parts => [ map { my $part = $_; my ($any) = values %{ $part->{models} }; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 406 | 12 | +{ %$part{qw(name by pooled)}, fixed => $any->{fixed}, models => { map { $_ => $part->{models}{$_}{name} } keys %{ $part->{models} } } } } @{ $self->{parts} } ], | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 407 | 6 | models => { map { $_ => _stored($self->{model}{$_}{data}, $self->{stored}{$_}) } keys %{ $self->{model} } }, | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 408 | meta => { created => time, %meta }, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 409 | }, $file); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 410 | 6 | return $self; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 411 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 412 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 413 | # A chain from a file: a chain's file, or a single model's file, which is a | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 414 | # chain of that one part (called by the file's name without its ending). | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 415 | 8 | ▶ | sub load ($class, $file) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
load: called CC=16 SCAR=30.1
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 416 | 8 | my $data = eval { Storable::retrieve($file, 0) }; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 417 | 8 | ▶ | die "$file cannot be read as a chain or a model: " . ($@ =~ s/ at \S+ line \d+.*//sr || "$!") . "\n" if ref $data ne 'HASH'; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
$@ =~ s/ at \S+ line \d+.*//rsu || "$!"
MC/DC0%0/2
$@ =~ s/ at \S+ line \d+.*//rsu || "$!"
$@ =~ s/ at \S+ line \d+.*//rsu
"$!"
Truth table33%1/3
A || B
A= $@ =~ s/ at \S+ line \d+.*//rsuB= "$!"
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 418 | 6 | ▶ | if (($data->{format} // '') eq $Peta::NN::Inference::FORMAT) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition50%1/2
$data->{"format"} // ""
MC/DC0%0/1
$data->{"format"} // ""
$data->{"format"}
Truth table50%1/2
A
A= $data->{"format"}
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 419 | 1 | my ($name) = $file =~ m{([^/]+?)(?:\.[^./]*)?\z}; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 420 | 1 | return $class->new($name => $data); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 421 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 422 | 5 | ▶ | die "$file is neither a chain's file nor a model's\n" if ($data->{format} // '') ne $FORMAT; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition50%1/2
$data->{"format"} // ""
MC/DC0%0/1
$data->{"format"} // ""
$data->{"format"}
Truth table50%1/2
A
A= $data->{"format"}
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 423 | 5 | ▶ | die sprintf "$file is in layout %s, and this version reads layout %d; save it again\n", $data->{layout} // '?', $LAYOUT if ($data->{layout} // '') ne $LAYOUT; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition25%1/4
$data->{"layout"} // ""
$data->{"layout"} // "?"
MC/DC0%0/1
$data->{"layout"} // ""
$data->{"layout"}
MC/DC0%0/1
$data->{"layout"} // "?"
$data->{"layout"}
Truth table50%1/2
A
A= $data->{"layout"}
Truth table0%0/2
A
A= $data->{"layout"}
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 424 | 5 | ▶ | die "$file is a malformed chain: no parts or no models\n" if ref $data->{parts} ne 'ARRAY' || ref $data->{models} ne 'HASH'; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
ref $data->{"parts"} ne "ARRAY" or ref $data->{"models"} ne "HASH"
MC/DC0%0/2
ref $data->{"parts"} ne "ARRAY" or ref $data->{"models"} ne "HASH"
ref $data->{"parts"} ne "ARRAY"
ref $data->{"models"} ne "HASH"
Truth table33%1/3
A or B
A= ref $data->{"parts"} ne "ARRAY"B= ref $data->{"models"} ne "HASH"
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 425 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 426 | 5 | my $self = bless { parts => [], model => {}, meta => $data->{meta} }, $class; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 427 | 5 | ▶ | eval { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 428 | 5 | for my $part (@{ $data->{parts} }) { | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 429 | my %models = map { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 430 | 11 | my $name = $part->{models}{$_}; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 431 | 11 | ▶ | my $model = $data->{models}{$name} // die "the part '$part->{name}' uses the model '$name', which is not in the file\n"; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Condition50%1/2
$data->{"models"}{$name} // die(qq[the part '$part->{"name"}' uses the model '${name}', which is not in the file\n])
MC/DC0%0/1
$data->{"models"}{$name} // die(qq[the part '$part->{"name"}' uses the model '${name}', which is not in the file\n])
$data->{"models"}{$name}
Truth table50%1/2
A
A= $data->{"models"}{$name}
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 432 | 11 | ▶ | $_ => _model($name, $part->{fixed} ? fixed($model, %{ $part->{fixed} }) : $model) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 433 | 10 | } keys %{ $part->{models} }; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 434 | 10 | $self->_add(name => $part->{name}, models => \%models, map { $_ => $part->{$_} } grep { defined $part->{$_} } qw(by pooled)); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 435 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 436 | 5 | $self->_wire; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 437 | 5 | 1; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 438 | } or die "$file is a malformed chain: $@"; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 439 | 5 | return $self; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 440 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 441 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 442 | # What there is to know about the chain, for a human. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 443 | 1 | ▶ | sub info ($self) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
info: called CC=4 SCAR=14.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 444 | return { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 445 | 1 | ▶ | %{ $self->{meta} // {} }, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Condition50%1/2
$self->{"meta"} // {}
MC/DC0%0/1
$self->{"meta"} // {}
$self->{"meta"}
Truth table50%1/2
A
A= $self->{"meta"}
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 446 | format => $FORMAT, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 447 | version => $VERSION, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 448 | kind => 'chain', | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 449 | 2 | ▶ | parts => [ map { my $part = $_; defined $part->{by} ? "$part->{name}, chosen by $part->{by}" : $part->{pooled} ? "$part->{name}, pooled" : $part->{name} } @{ $self->{parts} } ], | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 450 | given => [ $self->given ], | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 451 | answers => [ $self->answers ], | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 452 | weights => $self->n_params, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 453 | 1 | models => { map { $_ => $self->model($_)->info } $self->models }, | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 454 | }; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 455 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 456 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 457 | 3 | 1; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 458 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 459 | __END__ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||