-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtrain_mnist.lua
126 lines (112 loc) · 3.81 KB
/
train_mnist.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
--
-- Created by IntelliJ IDEA.
-- User: taineleau
-- Date: 11/7/16
-- Time: 13:46
--
-- load torchnet:
local tnt = require 'torchnet'
require 'cudnn'
-- use GPU or not:
local cmd = torch.CmdLine()
cmd:option('-usegpu', false, 'use gpu for training')
cmd:option('-crit', 'sigmoid', 'use sigmoid or softmax')
local config = cmd:parse(arg)
print(config)
print(string.format('running on %s', config.usegpu and 'GPU' or 'CPU'))
-- function that sets of dataset iterator:
local function getIterator(mode)
return tnt.ParallelDatasetIterator{
nthread = 1,
init = function() require 'torchnet' end,
closure = function()
-- load MNIST dataset:
local mnist = require 'mnist'
local dataset = mnist[mode .. 'dataset']()
dataset.data = dataset.data:reshape(dataset.data:size(1),
dataset.data:size(2) * dataset.data:size(3)):double()
-- return batches of data:
return tnt.BatchDataset{
batchsize = 128,
dataset = tnt.ListDataset{ -- replace this by your own dataset
list = torch.range(1, dataset.data:size(1)):long(),
load = function(idx)
return {
input = dataset.data[idx],
target = dataset.data[idx], -- unsupervised learning
--target = torch.LongTensor{dataset.label[idx] + 1},
} -- sample contains input and target
end,
}
}
end,
}
end
-- set up logistic regressor:
local net = require('network')(config) --nn.Sequential():add(nn.Linear(784,10))
print(net)
local criterion = cudnn.VolumetricCrossEntropyCriterion() -- nn.MultiLabelMultiClassCriterion()--nn.BCECriterion()
-- set up training engine:
local engine = tnt.SGDEngine()
engine.hooks.onStartEpoch = function(state)
-- meter:reset()
-- clerr:reset()
end
engine.hooks.onSample = function(state)
-- print('here!!!', state.sample.input)
state.sample.input:div(256)
if config.crit == 'softmax' then
local len = math.floor(math.sqrt(state.sample.input:size(2)))
state.sample.target:resize(state.sample.input:size(1), 1, len, len)
else
state.sample.target:div(256)
end
end
engine.hooks.onForwardCriterion = function(state)
-- print('target:', state.sample.target:nElement())
-- print('output:', state.network.output:nElement())
-- meter:add(state.criterion.output)
-- clerr:add(state.network.output, state.sample.target)
if state.training then
print('loss:', state.criterion.output)
-- print(string.format('avg. loss: %2.4f; avg. error: %2.4f',
-- meter:value(), clerr:value{k = 1}))
end
end
-- set up GPU training:
if config.usegpu then
-- copy model to GPU:
require 'cunn'
net = net:cuda()
criterion = criterion:cuda()
-- copy sample to GPU buffer:
local igpu, tgpu = torch.CudaTensor(), torch.CudaTensor()
engine.hooks.onSample = function(state)
if config.crit == 'softmax' then
igpu:resize(state.sample.input:size()):copy(state.sample.input:div(256))
local len = math.floor(math.sqrt(state.sample.input:size(2)))
tgpu:resize(state.sample.input:size(1), 1, len, len)
:copy(state.sample.target)
else
igpu:resize(state.sample.input:size()):copy(state.sample.input:div(256))
tgpu:resize(state.sample.output:size()):copy(state.sample.target:div(256))
end
state.sample.input = igpu
state.sample.target = tgpu
-- print(igpu)
end -- alternatively, this logic can be implemented via a TransformDataset
end
-- train the model:
engine:train{
network = net,
iterator = getIterator('train'),
criterion = criterion,
lr = 0.2,
maxepoch = 5,
}
-- generate net:
engine:test{
network = net,
iterator = getIterator('test'),
criterion = criterion,
}