-
Notifications
You must be signed in to change notification settings - Fork 3
/
model.lua
159 lines (136 loc) · 3.94 KB
/
model.lua
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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
--[[ Model file for Galaxy Zoo
By Xiang Zhang @ New York University
--]]
-- Prerequisite
require("nn")
-- Create the class
local Model = torch.class("Model")
-- Constructor
-- config: the configuration table
-- [1],[2],...: the layered specification of model
-- .p: dropout probability
function Model:__init(config)
-- Create a sequential for self
if config.file then
self.sequential = torch.load(config.file)
else
self.sequential = Model:createSequential(config)
end
self.p = config.p or 0.5
self.tensortype = torch.getdefaulttensortype()
end
-- Get the parameters of the model
function Model:getParameters()
return self.sequential:getParameters()
end
-- Forward propagation
function Model:forward(input)
self.output = self.sequential:forward(input)
return self.output
end
-- Backward propagation
function Model:backward(input, gradOutput)
self.gradInput = self.sequential:backward(input, gradOutput)
return self.gradInput
end
-- Randomize the model to random parameters
function Model:randomize(sigma)
local w,dw = self:getParameters()
w:normal():mul(sigma or 1)
end
-- Enable Dropouts
function Model:enableDropouts()
self.sequential = self:changeSequentialDropouts(self.sequential, self.p)
end
-- Disable Dropouts
function Model:disableDropouts()
self.sequential = self:changeSequentialDropouts(self.sequential,0)
end
-- Switch to a different data mode
function Model:type(tensortype)
if tensortype == "torch.CudaTensor" then
self.sequential:cuda()
self.tensortype = tensortype
elseif tensortype ~= nil then
self.sequential:type(tensortype)
self.tensortype = tensortype
end
return self.tensortype
end
-- Switch to cuda
function Model:cuda()
self:type("torch.CudaTensor")
end
-- Switch to double
function Model:double()
self:type("torch.DoubleTensor")
end
-- Switch to float
function Model:float()
self:type("torch.FloatTensor")
end
-- Change dropouts
function Model:changeSequentialDropouts(model,p)
for i,m in ipairs(model.modules) do
if m.module_name == "nn.Dropout" or torch.typename(m) == "nn.Dropout" then
m.p = p
end
end
return model
end
-- Create a sequential model using configurations
function Model:createSequential(model)
local new = nn.Sequential()
for i,m in ipairs(model) do
new:add(Model:createModule(m))
end
return new
end
-- Create a module using configurations
function Model:createModule(m)
if m.module == "nn.Reshape" then
return Model:createReshape(m)
elseif m.module == "nn.Linear" then
return Model:createLinear(m)
elseif m.module == "nn.Threshold" then
return Model:createThreshold(m)
elseif m.module == "nn.SpatialConvolution" then
return Model:createSpatialConvolution(m)
elseif m.module == "nn.SpatialMaxPooling" then
return Model:createSpatialMaxPooling(m)
elseif m.module == "nn.SpatialZeroPadding" then
return Model:createSpatialZeroPadding(m)
elseif m.module == "nn.Dropout" then
return Model:createDropout(m)
else
error("Unrecognized module for creation: "..tostring(m.module))
end
end
-- Create a new reshape model
function Model:createReshape(m)
return nn.Reshape(m.size)
end
-- Create a new linear model
function Model:createLinear(m)
return nn.Linear(m.inputSize, m.outputSize)
end
-- Create a new threshold model
function Model:createThreshold(m)
return nn.Threshold()
end
-- Create a new Spatial Convolution model
function Model:createSpatialConvolution(m)
return nn.SpatialConvolution(m.nInputPlane, m.nOutputPlane, m.kW, m.kH, m.dW, m.dH)
end
-- Create a new spatial max pooling model
function Model:createSpatialMaxPooling(m)
return nn.SpatialMaxPooling(m.kW, m.kH, m.dW, m.dH)
end
-- Create a new Spatial Zeo Padding module
function Model:createSpatialZeroPadding(m)
return nn.SpatialZeroPadding(m.pad_l,m.pad_r,m.pad_t,m.pad_b)
end
-- Create a new dropout module
function Model:createDropout(m)
return nn.Dropout(m.p)
end