Created
September 26, 2016 07:59
-
-
Save nagadomi/25fbf503b53697befc45fc3542943e32 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'nn' | |
local N = 1000 | |
local FEAT = 64 | |
local S = 2 | |
function nn.SpatialConvolution:reset() | |
self.weight:uniform(0, 1) | |
self.bias:zero() | |
end | |
local function dropout_conv_conv_model() | |
local seq = nn.Sequential() | |
seq.name = "dropout_conv_conv" | |
seq:add(nn.Dropout()) | |
seq:add(nn.SpatialConvolution(FEAT, FEAT, S, S, S, S, 0, 0)) | |
seq:add(nn.ReLU()) | |
seq:add(nn.SpatialConvolution(FEAT, 1, S, S, 1, 1, 0, 0)) | |
seq:add(nn.ReLU()) | |
return seq | |
end | |
local function dropout_avg_conv_model() | |
local seq = nn.Sequential() | |
seq.name = "dropout_avg_conv" | |
seq:add(nn.Dropout()) | |
seq:add(nn.SpatialAveragePooling(S, S, S, S)) | |
seq:add(nn.SpatialConvolution(FEAT, 1, S, S, 1, 1, 0, 0)) | |
seq:add(nn.ReLU()) | |
return seq | |
end | |
local function dropout_max_conv_model() -- this is bad example | |
local seq = nn.Sequential() | |
seq.name = "dropout_max_conv" | |
seq:add(nn.Dropout()) | |
seq:add(nn.SpatialMaxPooling(S, S, S, S)) | |
seq:add(nn.SpatialConvolution(FEAT, 1, S, S, 1, 1, 0, 0)) | |
seq:add(nn.ReLU()) | |
return seq | |
end | |
local function dropout_conv_max_conv_model() | |
local seq = nn.Sequential() | |
seq.name = "dropout_conv_max_conv" | |
seq:add(nn.Dropout()) | |
seq:add(nn.SpatialConvolution(FEAT, FEAT, S, S, 1, 1, 0, 0)) | |
seq:add(nn.ReLU()) | |
seq:add(nn.SpatialMaxPooling(S, S, S, S)) | |
seq:add(nn.SpatialConvolution(FEAT, 1, 1, 1, 1, 1, 0, 0)) | |
seq:add(nn.ReLU()) | |
return seq | |
end | |
local function run_test() | |
local models = {dropout_conv_conv_model(), dropout_avg_conv_model(), dropout_max_conv_model(), dropout_conv_max_conv_model()} | |
local inputs = torch.Tensor(N, FEAT, S * 2, S * 2):uniform(0, 1) | |
local res = {} | |
for i = 1, #models do | |
local output | |
models[i]:training() | |
output = models[i]:forward(inputs) | |
res[models[i].name] = {train = output:mean()} | |
--print(output:size()) | |
models[i]:evaluate() | |
output = models[i]:forward(inputs) | |
res[models[i].name]["predict"] = output:mean() | |
res[models[i].name]["train/predict"] = res[models[i].name]["train"] / res[models[i].name]["predict"] | |
end | |
print(res) | |
end | |
torch.setdefaulttensortype("torch.FloatTensor") | |
run_test() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.