AI-MXNet
view release on metacpan or search on metacpan
lib/AI/MXNet/Visualization.pm view on Meta::CPAN
package AI::MXNet::Visualization;
use strict;
use warnings;
use AI::MXNet::Base;
use AI::MXNet::Function::Parameters;
use JSON::PP;
=encoding UTF-8
=head1 NAME
AI::MXNet::Vizualization - Vizualization support for Perl interface to MXNet machine learning library
=head1 SYNOPSIS
use strict;
use warnings;
use AI::MXNet qw(mx);
### model
my $data = mx->symbol->Variable('data');
my $conv1= mx->symbol->Convolution(data => $data, name => 'conv1', num_filter => 32, kernel => [3,3], stride => [2,2]);
my $bn1 = mx->symbol->BatchNorm(data => $conv1, name => "bn1");
my $act1 = mx->symbol->Activation(data => $bn1, name => 'relu1', act_type => "relu");
my $mp1 = mx->symbol->Pooling(data => $act1, name => 'mp1', kernel => [2,2], stride =>[2,2], pool_type=>'max');
my $conv2= mx->symbol->Convolution(data => $mp1, name => 'conv2', num_filter => 32, kernel=>[3,3], stride=>[2,2]);
my $bn2 = mx->symbol->BatchNorm(data => $conv2, name=>"bn2");
my $act2 = mx->symbol->Activation(data => $bn2, name=>'relu2', act_type=>"relu");
my $mp2 = mx->symbol->Pooling(data => $act2, name => 'mp2', kernel=>[2,2], stride=>[2,2], pool_type=>'max');
my $fl = mx->symbol->Flatten(data => $mp2, name=>"flatten");
my $fc1 = mx->symbol->FullyConnected(data => $fl, name=>"fc1", num_hidden=>30);
my $act3 = mx->symbol->Activation(data => $fc1, name=>'relu3', act_type=>"relu");
my $fc2 = mx->symbol->FullyConnected(data => $act3, name=>'fc2', num_hidden=>10);
my $softmax = mx->symbol->SoftmaxOutput(data => $fc2, name => 'softmax');
## creates the image file working directory
mx->viz->plot_network($softmax, save_format => 'png')->render("network.png");
=head1 DESCRIPTION
Vizualization support for Perl interface to MXNet machine learning library
=head1 Class methods
=head2 print_summary
convert symbol for detail information
Parameters
----------
symbol: AI::MXNet::Symbol
symbol to be visualized
shape: hashref
hashref of shapes, str->shape (arrayref[int]), given input shapes
line_length: int
total length of printed lines
positions: arrayref[float]
relative or absolute positions of log elements in each line
Returns
------
nothing
=cut
method print_summary(
AI::MXNet::Symbol $symbol,
Maybe[HashRef[Shape]] $shape=,
Int $line_length=120,
ArrayRef[Num] $positions=[.44, .64, .74, 1]
)
{
my $show_shape;
my %shape_dict;
if(defined $shape)
{
$show_shape = 1;
my $interals = $symbol->get_internals;
my (undef, $out_shapes, undef) = $interals->infer_shape(%{ $shape });
Carp::confess("Input shape is incomplete")
unless defined $out_shapes;
@shape_dict{ @{ $interals->list_outputs } } = @{ $out_shapes };
}
my $conf = decode_json($symbol->tojson);
my $nodes = $conf->{nodes};
my %heads = map { $_ => 1 } @{ $conf->{heads}[0] };
if($positions->[-1] <= 1)
{
$positions = [map { int($line_length * $_) } @{ $positions }];
}
# header names for the different log elements
my $to_display = ['Layer (type)', 'Output Shape', 'Param #', 'Previous Layer'];
my $print_row = sub { my ($fields, $positions) = @_;
my $line = '';
enumerate(sub {
my ($i, $field) = @_;
$line .= $field//'';
$line = substr($line, 0, $positions->[$i]);
$line .= ' ' x ($positions->[$i] - length($line));
lib/AI/MXNet/Visualization.pm view on Meta::CPAN
}
else
{
$attr{fillcolor} = $cm[7];
if($op eq 'Custom')
{
$label = $node->{attr}{op_type};
}
}
$dot->graph->add_node($name, label => $label, %attr);
};
# add edges
for my $node (@{ $nodes })
{
my $op = $node->{op};
my $name = $node->{name};
if($op eq 'null')
{
next;
}
else
{
my $inputs = $node->{inputs};
for my $item (@{ $inputs })
{
my $input_node = $nodes->[$item->[0]];
my $input_name = $input_node->{name};
if(not exists $hidden_nodes{ $input_name })
{
my %attr = qw/dir back arrowtail normal/;
# add shapes
if($draw_shape)
{
my $key = $input_name;
$key .= '_output' if $input_node->{op} ne 'null';
if($input_node->{op} ne 'null' and exists $input_node->{attr})
{
if(ref $input_node->{attr} eq 'HASH' and exists $input_node->{attr}{num_outputs})
{
$key .= ($input_node->{attr}{num_outputs} - 1);
}
}
my $end = @{ $shape_dict{$key} };
$attr{label} = join('x', @{ $shape_dict{$key} }[1..$end-1]);
}
$dot->graph->add_edge($name => $input_name, %attr);
}
}
}
}
return $dot;
}
package AI::MXNet::Visualization::PythonGraphviz;
use Mouse;
use AI::MXNet::Types;
has 'format' => (
is => 'ro',
isa => enum([qw/debug canon text ps hpgl pcl mif
pic gd gd2 gif jpeg png wbmp cmapx
imap vdx vrml vtx mp fig svg svgz
plain/]
)
);
has 'graph' => (is => 'ro', isa => 'GraphViz');
method render($output=)
{
my $method = 'as_' . $self->format;
return $self->graph->$method($output);
}
1;
( run in 1.336 second using v1.01-cache-2.11-cpan-df04353d9ac )