-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathadd_layer.pl
37 lines (31 loc) · 941 Bytes
/
add_layer.pl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
#!/usr/bin/perl
# very simple weight averaging script
# does not do much error checking, so use with caution
use strict;
use IO::File;
#use JSON qw( decode_json );
use JSON::XS;
use Data::Dumper;
if ($#ARGV < 4) {
print "Usage: $0 <in_net> <out_net> <name> <type> <size>\n";
exit 1;
}
my ($in, $out, $name, $type, $size) = @ARGV;
open(F, $in) or die "$in: $!";
my @l = <F>;
close(F);
my $json = join("", @l);
my $dj = decode_json($json);
my %aj = %$dj;
my $nl = $#{$dj->{'layers'}};
%{$aj{'layers'}[$nl+1]} = %{$dj->{'layers'}->[$nl]};
%{$aj{'layers'}[$nl]} = %{$dj->{'layers'}->[$nl-1]};
$aj{'layers'}[$nl-1]{'name'} = $name;
$aj{'layers'}[$nl-1]{'type'} = $type;
$aj{'layers'}[$nl-1]{'size'} = $size + 0;
$aj{'layers'}[$nl-1]{'bias'} = 1.0;
delete $aj{'weights'}{$dj->{'layers'}->[$nl]->{'name'}};
open(OUT, '>', $out) or die "$out: $!";
print OUT JSON::XS->new->utf8->pretty(1)->encode(\%aj);
#print Dumper %aj;
close(OUT);