-
Notifications
You must be signed in to change notification settings - Fork 10
/
Conv.m
60 lines (53 loc) · 1.62 KB
/
Conv.m
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
classdef Conv < dagnn.Filter
properties
size = [0 0 0 0]
hasBias = true
opts = {'cuDNN'}
end
methods
function outputs = forward(obj, inputs, params)
if ~obj.hasBias, params{2} = [] ; end
outputs{1} = vl_nnconv(...
inputs{1}, params{1}, params{2}, ...
'pad', obj.pad, ...
'stride', obj.stride, ...
obj.opts{:}) ;
end
function [derInputs, derParams] = backward(obj, inputs, params, derOutputs)
if ~obj.hasBias, params{2} = [] ; end
[derInputs{1}, derParams{1}, derParams{2}] = vl_nnconv(...
inputs{1}, params{1}, params{2}, derOutputs{1}, ...
'pad', obj.pad, ...
'stride', obj.stride, ...
obj.opts{:}) ;
end
function kernelSize = getKernelSize(obj)
kernelSize = obj.size(1:2) ;
end
function outputSizes = getOutputSizes(obj, inputSizes)
outputSizes = getOutputSizes@dagnn.Filter(obj, inputSizes) ;
outputSizes{1}(3) = obj.size(4) ;
end
function params = initParams(obj)
%sc = sqrt(2 / prod(obj.size(1:3))) ;
sc = 0.04;
params{1} = randn(obj.size,'single') * sc ;
if obj.hasBias
params{2} = zeros(obj.size(4),1,'single') * sc ;
end
end
function set.size(obj, ksize)
% make sure that ksize has 4 dimensions
ksize = [ksize(:)' 1 1 1 1] ;
obj.size = ksize(1:4) ;
end
function obj = Conv(varargin)
obj.load(varargin) ;
% normalize field by implicitly calling setters defined in
% dagnn.Filter and here
obj.size = obj.size ;
obj.stride = obj.stride ;
obj.pad = obj.pad ;
end
end
end