AI-MXNet

 view release on metacpan or  search on metacpan

t/test_symbol.t  view on Meta::CPAN

}

test_symbol_children();

sub test_symbol_storable
{
    my $mlist = [mlp2(), conv()];
    my $data = freeze($mlist);
    my $mlist2 = thaw($data);
    zip(sub {
        my ($x, $y) = @_;
        is($x->tojson, $y->tojson);
    }, $mlist, $mlist2);
}

test_symbol_storable();

sub test_symbol_saveload
{
    my $sym = mlp2();
    my $fname = 'tmp_sym.json';
    $sym->save($fname);
    my $data2 = mx->symbol->load($fname);
    # save because of order
    is($sym->tojson, $data2->tojson);
    unlink $fname;
}

test_symbol_saveload();

sub test_symbol_infer_type
{
    my $data = mx->symbol->Variable('data');
    my $f32data = mx->symbol->Cast(data=>$data, dtype=>'float32');
    my $fc1 = mx->symbol->FullyConnected(data => $f32data, name=>'fc1', num_hidden=>128);
    my $mlp = mx->symbol->SoftmaxOutput(data => $fc1, name => 'softmax');

    my ($arg, $out, $aux) = $mlp->infer_type(data=>'float16');
    is_deeply($arg, [qw/float16 float32 float32 float32/]);
    is_deeply($out, ['float32']);
    is_deeply($aux, []);
}

test_symbol_infer_type();

sub test_symbol_infer_shape
{
    my $num_hidden = 128;
    my $num_dim    = 64;
    my $num_sample = 10;

    my $data = mx->symbol->Variable('data');
    my $prev = mx->symbol->Variable('prevstate');
    my $x2h  = mx->symbol->FullyConnected(data=>$data, name=>'x2h', num_hidden=>$num_hidden);
    my $h2h  = mx->symbol->FullyConnected(data=>$prev, name=>'h2h', num_hidden=>$num_hidden);

    my $out  = mx->symbol->Activation(data=>mx->sym->elemwise_add($x2h, $h2h), name=>'out', act_type=>'relu');

    # shape inference will fail because information is not available for h2h
    my @ret  = $out->infer_shape(data=>[$num_sample, $num_dim]);
    is_deeply(\@ret, [undef, undef, undef]);

    my ($arg_shapes, $out_shapes, $aux_shapes) = $out->infer_shape_partial(data=>[$num_sample, $num_dim]);
    my %arg_shapes;
    @arg_shapes{ @{ $out->list_arguments } } = @{ $arg_shapes };
    is_deeply($arg_shapes{data}, [$num_sample, $num_dim]);
    is_deeply($arg_shapes{x2h_weight}, [$num_hidden, $num_dim]);
    is_deeply($arg_shapes{h2h_weight}, []);

    # now we can do full shape inference
    my $state_shape = $out_shapes->[0];
    ($arg_shapes, $out_shapes, $aux_shapes) = $out->infer_shape(data=>[$num_sample, $num_dim], prevstate=>$state_shape);
    @arg_shapes{ @{ $out->list_arguments } } = @{ $arg_shapes };
    is_deeply($arg_shapes{data}, [$num_sample, $num_dim]);
    is_deeply($arg_shapes{x2h_weight}, [$num_hidden, $num_dim]);
    is_deeply($arg_shapes{h2h_weight}, [$num_hidden, $num_hidden]);
}

test_symbol_infer_shape();

sub test_symbol_infer_shape_var
{
    #Test specifying shape information when constructing a variable
    my $shape = [2, 3];
    my $a = mx->symbol->Variable('a', shape=>$shape);
    my $b = mx->symbol->Variable('b');
    my $c = mx->symbol->elemwise_add($a, $b);
    my ($arg_shapes, $out_shapes, $aux_shapes) = $c->infer_shape();
    is_deeply($arg_shapes->[0], $shape);
    is_deeply($arg_shapes->[1], $shape);
    is_deeply($out_shapes->[0], $shape);

    $shape = [5, 6];
    ($arg_shapes, $out_shapes, $aux_shapes) = $c->infer_shape(a=>$shape);
    is_deeply($arg_shapes->[0], $shape);
    is_deeply($arg_shapes->[1], $shape);
    is_deeply($out_shapes->[0], $shape);
}

test_symbol_infer_shape_var();

sub check_symbol_consistency
{
    my ($sym1, $sym2, $ctx) = @_;
    is_deeply($sym1->list_arguments(), $sym2->list_arguments());
    is_deeply($sym1->list_auxiliary_states(), $sym2->list_auxiliary_states());
    is_deeply($sym1->list_outputs(), $sym2->list_outputs());
    check_consistency(sym => [$sym1, $sym2], ctx_list => [$ctx, $ctx]);
}

sub test_load_000800
{
    my ($data, $weight, $fc1, $act1);
    {
        local($mx::AttrScope) = mx->AttrScope(ctx_group=>'stage1');
        $data = mx->symbol->Variable('data', lr_mult=>0.2);
        $weight = mx->sym->Variable('fc1_weight', lr_mult=>1.2);
        $fc1  = mx->symbol->FullyConnected(data => $data, weight=>$weight, name=>'fc1', num_hidden=>128, wd_mult=>0.3);
        $act1 = mx->symbol->Activation(data => $fc1, name=>'relu1', act_type=>"relu");
    }
    my ($fc2, $act2, $fc3, $sym1);
    {
        local($mx::AttrScope) = mx->AttrScope(ctx_group=>'stage2');
        $fc2  = mx->symbol->FullyConnected(data => $act1, name => 'fc2', num_hidden => 64, lr_mult=>0.01);
        $act2 = mx->symbol->Activation(data => $fc2, name=>'relu2', act_type=>"relu");
        $fc3  = mx->symbol->FullyConnected(data => $act2, name=>'fc3', num_hidden=>10);
        $fc3  = mx->symbol->BatchNorm($fc3, name=>'batchnorm0');
        $sym1 = mx->symbol->SoftmaxOutput(data => $fc3, name => 'softmax')
    }
    { local $/ = undef; my $json = <DATA>; open(F, ">save_000800.json"); print F $json; close(F); };
    my $sym2 = mx->sym->load('save_000800.json');
    unlink 'save_000800.json';

    my %attr1 = %{ $sym1->attr_dict };
    my %attr2 = %{ $sym2->attr_dict };
    while(my ($k, $v1) = each %attr1)
    {
        ok(exists $attr2{ $k });
        my $v2 = $attr2{$k};
        while(my ($kk, $vv1) = each %{ $v1 })
        {
            if($kk =~ /^__/ and $kk =~ /__$/)
            {
                ok(exists $v2->{$kk} and $v2->{$kk} eq $vv1);
            }
        }
    }

    check_symbol_consistency($sym1, $sym2,
        {ctx => mx->cpu(0), group2ctx =>{stage1 => mx->cpu(1), stage2 => mx->cpu(2) }, shapes => { data => [1,200] }}
    );
}

test_load_000800();

__DATA__
{
  "nodes": [
    {
      "op": "null", 
      "param": {}, 
      "name": "data", 
      "inputs": [], 
      "backward_source_id": -1, 
      "attr": {
        "ctx_group": "stage1", 
        "lr_mult": "0.2"
      }
    }, 
    {
      "op": "null", 
      "param": {}, 
      "name": "fc1_weight", 
      "inputs": [], 
      "backward_source_id": -1, 
      "attr": {
        "ctx_group": "stage1", 
        "wd_mult": "0.3", 
        "weight_lr_mult": "1.2"
      }
    }, 
    {
      "op": "null", 
      "param": {}, 
      "name": "fc1_bias", 
      "inputs": [], 
      "backward_source_id": -1, 
      "attr": {
        "ctx_group": "stage1", 
        "wd_mult": "0.3", 



( run in 1.944 second using v1.01-cache-2.11-cpan-d80b1682f3f )