-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathpooling.sac
More file actions
109 lines (95 loc) · 2.68 KB
/
pooling.sac
File metadata and controls
109 lines (95 loc) · 2.68 KB
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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
module pooling;
use Array: all;
export {AveragePool, AveragePoolFlop,
BackAveragePool, BackAveragePoolFlop,
MaxPool, BackMaxPool};
inline
float average( float[*] array)
{
return sum( array) / tof( prod( shape( array)));
}
inline
float maximum( float[*] array)
{
return with {
(0*shape(array) <= iv < shape(array)) : array[iv];
} : fold( max, minfloat());
}
inline
float[*] AveragePool( float[*] in, int[.] filter)
//
// assert( dim(in) >= shape(filter)[0] )
// assert( shape(out) == shape(in)/filter )
//
{
ones = genarray( [dim( in)], 1);
filter = drop( shape( filter), ones) ++ filter;
shp = shape( in) / filter;
/*
* out = { iv -> average( { ov -> in[iv+ov] | ov < filter})
* | iv < shp};
*/
out = with {
(. <= iv <= .) : average( with {
(. <= ov <= .) : in[iv*filter+ov];
} : genarray( filter, 0f));
} : genarray( shp, 0f);
return out;
}
int[.], double AveragePoolFlop( int[.] in_shp, int[.] filter)
{
ones = genarray( shape( in_shp), 1);
filter = drop( shape( filter), ones) ++ filter;
shp = in_shp / filter;
return (shp, tod (prod(shp) * (prod(shape(filter))-1)) );
}
inline
float[*] BackAveragePool( float[*] d_out, int[.] filter )
{
ones = genarray( [dim( d_out)], 1);
filter = drop( shape( filter), ones) ++ filter;
shp = shape( d_out) * filter;
d_in = with {
(. <= iv <=.) : d_out[iv/filter] / tof( prod( filter));
} : genarray( shp, 0f);
return d_in;
}
int[.], double BackAveragePoolFlop( int[.] d_out_shp, int[.] filter)
{
ones = genarray( shape( d_out_shp), 1);
filter = drop( shape( filter), ones) ++ filter;
shp = d_out_shp * filter;
return (shp, tod (prod(shp) ) );
}
inline
float[*] MaxPool( float[*] in, int[.] filter)
//
// assert( dim(in) >= shape(filter)[0] )
// assert( shape(out) == shape(in)/filter )
//
{
ones = genarray( [dim( in)], 1);
filter = drop( shape( filter), ones) ++ filter;
shp = shape( in) / filter;
/*
* out = { iv -> maximum( { ov -> in[iv+ov] | ov < filter})
* | iv < shp};
*/
out = with {
(. <= iv <= .) : maximum( with {
(. <= ov <= .) : in[iv*filter+ov];
} : genarray( filter, 0f));
} : genarray( shp, 0f);
return out;
}
inline
float[*] BackMaxPool( float[*] d_out, int[.] filter )
{
ones = genarray( [dim( d_out)], 1);
filter = drop( shape( filter), ones) ++ filter;
shp = shape( d_out) * filter;
d_in = with {
(. <= iv <=.) : d_out[iv/filter] / tof( prod( filter));
} : genarray( shp, 0f);
return d_in;
}