aboutsummaryrefslogtreecommitdiff
path: root/nerv/layer/elem_mul.lua
blob: fe80a3ff308544d328567d018e0255d078dba72b (plain) (blame)
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
local ElemMulLayer = nerv.class('nerv.ElemMulLayer', 'nerv.Layer')

function ElemMulLayer:__init(id, global_conf, layer_conf)
    self.id = id
    self.dim_in = layer_conf.dim_in
    self.dim_out = layer_conf.dim_out
    self.gconf = global_conf
    -- element-wise multiplication of input[1] and input[2]
    self:check_dim_len(2, 1)
end

function ElemMulLayer:init(batch_size)
    if self.dim_in[1] ~= self.dim_in[2] or
        self.dim_in[1] ~= self.dim_out[1] then
        nerv.error("mismatching dimensions of input and output")
    end
end

function ElemMulLayer:batch_resize(batch_size)
    -- do nothing
end

function ElemMulLayer:propagate(input, output)
    output[1]:mul_elem(input[1], input[2])
end

function ElemMulLayer:back_propagate(bp_err, next_bp_err, input, output)
    next_bp_err[1]:mul_elem(bp_err[1], input[2])
    next_bp_err[2]:mul_elem(bp_err[1], input[1])
end

function ElemMulLayer:update(bp_err, input, output)
    -- do nothing
end

function ElemMulLayer:get_params()
    return nerv.ParamRepo({})
end