-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathexperiment1.lua
More file actions
110 lines (88 loc) · 2.87 KB
/
experiment1.lua
File metadata and controls
110 lines (88 loc) · 2.87 KB
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
require 'torch'
require 'nn'
require 'optim'
mnist = require 'mnist'
trainset = mnist.traindataset()
testset = mnist.testdataset()
model = nn.Sequential()
model:add(nn.Reshape(28*28))
model:add(nn.Linear(28*28, 500))
model:add(nn.Sigmoid())
model:add(nn.Linear(500, 250))
model:add(nn.Sigmoid())
model:add(nn.Linear(250, 10))
sgd_params = {
learningRate = 0.1,
learningRateDecay = 0.0,
weightDecay = 0.0,
momentum = 0.9
}
model:add(nn.LogSoftMax())
criterion = nn.ClassNLLCriterion()
x, dl_dx = model:getParameters()
print('<mnist> using model:')
print(model)
step = function(batch_size)
local current_loss = 0
local count = 0
local shuffle = torch.randperm(trainset.size)
batch_size = batch_size or 100
for t = 1,trainset.size,batch_size do
-- setup inputs and targets for this mini-batch
local size = math.min(t + batch_size - 1, trainset.size) - t
local inputs = torch.Tensor(size, 28, 28)
local targets = torch.Tensor(size)
for i = 1,size do
local input = trainset.data[shuffle[i+t]]
local target = trainset.label[shuffle[i+t]]
-- if target == 0 then target = 10 end
inputs[i] = input
targets[i] = target
end
targets:add(1)
local feval = function(x_new)
-- reset data
if x ~= x_new then x:copy(x_new) end
dl_dx:zero()
-- perform mini-batch gradient descent
local loss = criterion:forward(model:forward(inputs), targets)
model:backward(inputs, criterion:backward(model.output, targets))
return loss, dl_dx
end
_, fs = optim.sgd(feval, x, sgd_params)
-- fs is a table containing value of the loss function
-- (just 1 value for the SGD optimization)
count = count + 1
current_loss = current_loss + fs[1]
end
-- normalize loss
return current_loss / count
end
eval = function(dataset, batch_size)
local count = 0
batch_size = batch_size or 100
for i = 1,dataset.size,batch_size do
local size = math.min(i + batch_size - 1, dataset.size) - i
local inputs = dataset.data[{{i,i+size-1}}]
local targets = dataset.label[{{i,i+size-1}}]:long()
local outputs = model:forward(inputs)
local _, indices = torch.max(outputs, 2)
indices:add(-1)
local guessed_right = indices:eq(targets):sum()
count = count + guessed_right
end
return count / dataset.size
end
max_iters = 50
trainset.data = trainset.data:double()
testset.data = testset.data:double()
print("Start training")
do
for i = 1,max_iters do
local loss = step()
local accuracy = eval(trainset)
print(string.format('Epoch: %d loss: %4f train acc: %5f', i, loss, accuracy))
end
end
local accuracy = eval(testset)
print(string.format('Test acc: %5f', accuracy))