aboutsummaryrefslogtreecommitdiff
path: root/nerv/nn/layer_repo.lua
blob: 2f8de0817a26786ef79adabf256c0bb072315759 (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
39
40
41
local LayerRepo = nerv.class("nerv.LayerRepo")

function LayerRepo:__init(layer_spec, param_repo, global_conf)
    self.layers = {}
    self:add_layers(layer_spec, param_repo, global_conf)
end

function LayerRepo:add_layers(layer_spec, param_repo, global_conf)
    local layers = self.layers
    for ltype, llist in pairs(layer_spec) do
        local layer_type = nerv.get_type(ltype)
        for id, spec in pairs(llist) do
            if layers[id] ~= nil then
                nerv.error("a layer with id %s already exists", id)
            end
            nerv.info("create layer: %s", id)
            if type(spec[2]) ~= "table" then
                nerv.error("layer config table is need")
            end
            layer_config = spec[2]
            if type(spec[1]) ~= "table" then
                nerv.error("parameter description table is needed")
            end
            for pname, pid in pairs(spec[1]) do
                layer_config[pname] = param_repo:get_param(pid)
            end
            if layer_config.pr == nil then
                layer_config.pr = param_repo
            end
            layers[id] = layer_type(id, global_conf, layer_config)
        end
    end
end

function LayerRepo:get_layer(lid)
    local layer = self.layers[lid]
    if layer == nil then
        nerv.error("layer with id %s not found", lid)
    end
    return layer
end