| 1 | # Licensed to the Apache Software Foundation (ASF) under one | ||
| 2 | # or more contributor license agreements. See the NOTICE file | ||
| 3 | # distributed with this work for additional information | ||
| 4 | # regarding copyright ownership. The ASF licenses this file | ||
| 5 | # to you under the Apache License, Version 2.0 (the | ||
| 6 | # "License"); you may not use this file except in compliance | ||
| 7 | # with the License. You may obtain a copy of the License at | ||
| 8 | # | ||
| 9 | # http://www.apache.org/licenses/LICENSE-2.0 | ||
| 10 | # | ||
| 11 | # Unless required by applicable law or agreed to in writing, | ||
| 12 | # software distributed under the License is distributed on an | ||
| 13 | # "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
| 14 | # KIND, either express or implied. See the License for the | ||
| 15 | # specific language governing permissions and limitations | ||
| 16 | # under the License. | ||
| 17 | |||
| 18 | package AI::MXNet::RNN; | ||
| 19 | use strict; | ||
| 20 | use warnings; | ||
| 21 | use AI::MXNet::NS; | ||
| 22 | use AI::MXNet::Function::Parameters; | ||
| 23 | use AI::MXNet::RNN::IO; | ||
| 24 | use AI::MXNet::RNN::Cell; | ||
| 25 | use List::Util qw(max); | ||
| 26 | |||
| 27 | =encoding UTF-8 | ||
| 28 | |||
| 29 | =head1 NAME | ||
| 30 | |||
| 31 | AI::MXNet::RNN - Functions for constructing recurrent neural networks. | ||
| 32 | =cut | ||
| 33 | |||
| 34 | =head1 SYNOPSIS | ||
| 35 | |||
| 36 | |||
| 37 | =head1 DESCRIPTION | ||
| 38 | |||
| 39 | Functions for constructing recurrent neural networks. | ||
| 40 | =cut | ||
| 41 | |||
| 42 | =head2 save_rnn_checkpoint | ||
| 43 | |||
| 44 | Save checkpoint for model using RNN cells. | ||
| 45 | Unpacks weight before saving. | ||
| 46 | |||
| 47 | Parameters | ||
| 48 | ---------- | ||
| 49 | cells : AI::MXNet::RNN::Cell or array ref of AI::MXNet::RNN::Cell | ||
| 50 | The RNN cells used by this symbol. | ||
| 51 | prefix : str | ||
| 52 | Prefix of model name. | ||
| 53 | epoch : int | ||
| 54 | The epoch number of the model. | ||
| 55 | symbol : Symbol | ||
| 56 | The input symbol | ||
| 57 | arg_params : hash ref of str to AI::MXNet::NDArray | ||
| 58 | Model parameter, hash ref of name to NDArray of net's weights. | ||
| 59 | aux_params : hash ref of str to AI::MXNet::NDArray | ||
| 60 | Model parameter, hash ref of name to NDArray of net's auxiliary states. | ||
| 61 | |||
| 62 | Notes | ||
| 63 | ----- | ||
| 64 | - prefix-symbol.json will be saved for symbol. | ||
| 65 | - prefix-epoch.params will be saved for parameters. | ||
| 66 | =cut | ||
| 67 | |||
| 68 | method save_rnn_checkpoint( | ||
| 69 | AI::MXNet::RNN::Cell::Base|ArrayRef[AI::MXNet::RNN::Cell::Base] $cells, | ||
| 70 | Str $prefix, | ||
| 71 | Int $epoch, | ||
| 72 | AI::MXNet::Symbol $symbol, | ||
| 73 | HashRef[AI::MXNet::NDArray] $arg_params, | ||
| 74 | HashRef[AI::MXNet::NDArray] $aux_params | ||
| 75 | ) | ||
| 76 | { | ||
| 77 | $cells = [$cells] unless ref $cells eq 'ARRAY'; | ||
| 78 | my %arg_params = %{ $arg_params }; | ||
| 79 | %arg_params = %{ $_->unpack_weights(\%arg_params) } for @{ $cells }; | ||
| 80 | AI::MXNet::Module->model_save_checkpoint($prefix, $epoch, $symbol, \%arg_params, $aux_params); | ||
| 81 | } | ||
| 82 | |||
| 83 | |||
| 84 | =head2 load_rnn_checkpoint | ||
| 85 | |||
| 86 | Load model checkpoint from file. | ||
| 87 | Pack weights after loading. | ||
| 88 | |||
| 89 | Parameters | ||
| 90 | ---------- | ||
| 91 | cells : AI::MXNet::RNN::Cell or ir array ref of AI::MXNet::RNN::Cell | ||
| 92 | The RNN cells used by this symbol. | ||
| 93 | prefix : str | ||
| 94 | Prefix of model name. | ||
| 95 | epoch : int | ||
| 96 | Epoch number of model we would like to load. | ||
| 97 | |||
| 98 | Returns | ||
| 99 | ------- | ||
| 100 | symbol : Symbol | ||
| 101 | The symbol configuration of computation network. | ||
| 102 | arg_params : hash ref of str to NDArray | ||
| 103 | Model parameter, dict of name to NDArray of net's weights. | ||
| 104 | aux_params : hash ref of str to NDArray | ||
| 105 | Model parameter, dict of name to NDArray of net's auxiliary states. | ||
| 106 | |||
| 107 | Notes | ||
| 108 | ----- | ||
| 109 | - symbol will be loaded from prefix-symbol.json. | ||
| 110 | - parameters will be loaded from prefix-epoch.params. | ||
| 111 | =cut | ||
| 112 | |||
| 113 | method load_rnn_checkpoint( | ||
| 114 | AI::MXNet::RNN::Cell::Base|ArrayRef[AI::MXNet::RNN::Cell::Base] $cells, | ||
| 115 | Str $prefix, | ||
| 116 | Int $epoch | ||
| 117 | ) | ||
| 118 | { | ||
| 119 | my ($sym, $arg, $aux) = AI::MXNet::Module->load_checkpoint($prefix, $epoch); | ||
| 120 | $cells = [$cells] unless ref $cells eq 'ARRAY'; | ||
| 121 | $arg = $_->pack_weights($arg) for @{ $cells }; | ||
| 122 | return ($sym, $arg, $aux); | ||
| 123 | } | ||
| 124 | |||
| 125 | =head2 do_rnn_checkpoint | ||
| 126 | |||
| 127 | Make a callback to checkpoint Module to prefix every epoch. | ||
| 128 | unpacks weights used by cells before saving. | ||
| 129 | |||
| 130 | Parameters | ||
| 131 | ---------- | ||
| 132 | cells : subclass of RNN::Cell | ||
| 133 | RNN cells used by this module. | ||
| 134 | prefix : str | ||
| 135 | The file prefix to checkpoint to | ||
| 136 | period : int | ||
| 137 | How many epochs to wait before checkpointing. Default is 1. | ||
| 138 | |||
| 139 | Returns | ||
| 140 | ------- | ||
| 141 | callback : function | ||
| 142 | The callback function that can be passed as iter_end_callback to fit. | ||
| 143 | =cut | ||
| 144 | |||
| 145 | method do_rnn_checkpoint( | ||
| 146 | AI::MXNet::RNN::Cell::Base|ArrayRef[AI::MXNet::RNN::Cell::Base] $cells, | ||
| 147 | Str $prefix, | ||
| 148 | Int $period | ||
| 149 | ) | ||
| 150 | { | ||
| 151 | $period = max(1, $period); | ||
| 152 | return sub { | ||
| 153 | my ($iter_no, $sym, $arg, $aux) = @_; | ||
| 154 | if (($iter_no + 1) % $period == 0) | ||
| 155 | { | ||
| 156 | __PACKAGE__->save_rnn_checkpoint($cells, $prefix, $iter_no+1, $sym, $arg, $aux); | ||
| 157 | } | ||
| 158 | }; | ||
| 159 | } | ||
| 160 | |||
| 161 | ## In order to closely resemble the Python's usage | ||
| 162 | method RNNCell(@args) { AI::MXNet::RNN::Cell->new(@args % 2 ? ('num_hidden', @args) : @args) } | ||
| 163 | method LSTMCell(@args) { AI::MXNet::RNN::LSTMCell->new(@args % 2 ? ('num_hidden', @args) : @args) } | ||
| 164 | method GRUCell(@args) { AI::MXNet::RNN::GRUCell->new(@args % 2 ? ('num_hidden', @args) : @args) } | ||
| 165 | method FusedRNNCell(@args) { AI::MXNet::RNN::FusedCell->new(@args % 2 ? ('num_hidden', @args) : @args) } | ||
| 166 | method SequentialRNNCell(@args) { AI::MXNet::RNN::SequentialCell->new(@args) } | ||
| 167 | method BidirectionalCell(@args) { AI::MXNet::RNN::BidirectionalCell->new(@args) } | ||
| 168 | method DropoutCell(@args) { AI::MXNet::RNN::DropoutCell->new(@args) } | ||
| 169 | method ZoneoutCell(@args) { AI::MXNet::RNN::ZoneoutCell->new(@args) } | ||
| 170 | method ConvRNNCell(@args) { AI::MXNet::RNN::ConvCell->new(@args) } | ||
| 171 | method ConvLSTMCell(@args) { AI::MXNet::RNN::ConvLSTMCell->new(@args) } | ||
| 172 | method ConvGRUCell(@args) { AI::MXNet::RNN::ConvGRUCell->new(@args) } | ||
| 173 | method ResidualCell(@args) { AI::MXNet::RNN::ResidualCell->new(@args) } | ||
| 174 | method encode_sentences(@args) { AI::MXNet::RNN::IO->encode_sentences(@args) } | ||
| 175 | method BucketSentenceIter(@args) | ||
| 176 | { | ||
| 177 | my $sentences = shift(@args); | ||
| 178 | my $batch_size = shift(@args); | ||
| 179 | AI::MXNet::BucketSentenceIter->new(sentences => $sentences, batch_size => $batch_size, @args); | ||
| 180 | } | ||
| 181 | |||
| 182 | 1; |