AI-MXNet

 view release on metacpan or  search on metacpan

t/test_symbol.t  view on Meta::CPAN

    # 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 0.752 second using v1.01-cache-2.11-cpan-5837b0d9d2c )