forked from ThorJonsson/DeepLearningNLP
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathautoencoder.lua
More file actions
123 lines (100 loc) · 3.42 KB
/
autoencoder.lua
File metadata and controls
123 lines (100 loc) · 3.42 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
111
112
113
114
115
116
117
118
119
120
121
-- Parameters
-- Date: 24/07/14
require 'rnn'
cmd = torch.CmdLine()
cmd:text()
cmd:text('Simple LSTM example for the RNN library')
cmd:text()
cmd:text('Options')
cmd:option('-use_saved',false,'Use previously saved inputs and trained network instead of new')
cmd:option('-cuda',true,'Run on CUDA-enabled GPU instead of CPU')
cmd:text()
-- parse input params
opt = cmd:parse(arg)
if opt.cuda then
require 'cunn'
end
-- Keep the input layer small so the model trains / converges quickly while training
local inputSize = 20
-- Larger numbers here mean more complex problems can be solved, but can also over-fit. 256 works well for now
local hiddenSize = 512
-- We want the network to classify the inputs using a one-hot representation of the outputs
--
local outputSize = 3
-- the dataset size is the total number of examples we want to present to the LSTM
local dsSize=2000
-- We present the dataset to the network in batches where batchSize << dsSize
local batchSize=5
--And seqLength is the length of each sequence, i.e. the number of "events" we want to pass to the LSTM
--to make up a single example. I'd like this to be dynamic ideally for the YOOCHOOSE dataset..
local seqLength=50
-- number of target classes or labels, needs to be the same as outputSize above
-- or we get the dreaded "ClassNLLCriterion.lua:46: Assertion `cur_target >= 0 && cur_target < n_classes' failed. "
local nClass = 3
function build_network(inputSize, hiddenSize, outputSize)
if opt.use_saved then
rnn = torch.load('trained-model.t7')
else
rnn = nn.Sequential()
:add(nn.Linear(inputSize, hiddenSize))
:add(nn.LSTM(hiddenSize, hiddenSize))
:add(nn.LSTM(hiddenSize, hiddenSize))
:add(nn.Linear(hiddenSize, outputSize))
:add(nn.LogSoftMax())
-- wrap this in a Sequencer such that we can forward/backward
-- entire sequences of length seqLength at once
rnn = nn.Sequencer(rnn)
if opt.cuda then
rnn:cuda()
end
end
return rnn
end
function build_input()
torch.randn()
end
function build_data()
local inputs = {}
local targets = {}
-- TODO in case we have an already trained model
for i = 1, dsSize do
-- populate both tables to get ready for training
local input = torch.randn(batch_size,seq_len)
local target = factors(input)
if opt.cuda then
input = input:float():cuda()
target = target:float():cuda()
end
table.insert(inputs, input)
table.insert(targets, target)
end
return inputs, targets
end
-- Synthetic data:
-- We choose to exploit prime factorization with our neural network
-- This will show us how far the LSTM is from complicated deterministic tasks
-- **Prime Factorization**
-- For a given random number this function returns the Prime factors
-- The prime factors are stored in a table
function factor(num)
local i = 2;
local factors = {};
if not num or num < 1 then
print('your input must be postive!')
end
if num and num == 1 then
factors[1] = 1
return factors
end
while num and num > 1 do
while num % i == 0 do
factors[#factors + 1] = i
num = num / i
end
i = i + 1
end
return factors
end
-- two tables to hold the *full* dataset input and target tensors
local inputs, targets = build_data()
local rnn = build_network(inputSize, hiddenSize, outputSize)