Last active
December 16, 2015 01:36
-
-
Save nagadomi/1e639ba3d79183c0b0f9 to your computer and use it in GitHub Desktop.
This file contains 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 'cutorch' | |
require 'cunn' | |
local function build_model() | |
-- a CNN | |
local model = nn.Sequential() | |
model:add(nn.SpatialConvolutionMM(3, 128, 5, 5, 1, 1)) | |
model:add(nn.ReLU()) | |
model:add(nn.SpatialMaxPooling(2, 2, 2, 2)) | |
model:add(nn.View(10 * 10 * 128)) | |
model:add(nn.Linear(10 * 10 * 128, 1024)) | |
model:add(nn.ReLU()) | |
model:add(nn.Dropout(0.5)) | |
model:add(nn.Linear(1024, 10)) | |
model:add(nn.LogSoftMax()) -- use nn.LogSoftMax(), LogSoftMax() == SoftMax():log(), LogSoftMax():exp() = SoftMax() | |
return model | |
end | |
local function cuda_test() | |
local model = build_model():cuda() | |
local criterion = nn.ClassNLLCriterion():cuda() | |
local x = torch.Tensor(64, 3, 24, 24):uniform():cuda() -- input tensor (batch, depth, height, width) | |
local y = torch.Tensor(64):random(1, 10):cuda() -- labels (array of class id) | |
-- batch forward | |
local z = model:forward(x) | |
local err = criterion:forward(z, y) | |
-- batch backward | |
model:backward(x, criterion:backward(z, y)) | |
print("CUDA Test Successful!") | |
end | |
torch.setdefaulttensortype('torch.FloatTensor') | |
cuda_test() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment