【发布时间】:2016-08-19 12:18:23
【问题描述】:
我正在学习 LSTM 网络并决定尝试综合测试。我希望由一些点 (x,y) 提供的 LSTM 网络来区分三个基本功能:
- 行:y = k*x + b
- 抛物线:y = k*x^2 + b
- sqrt: y = k*sqrt(x) + b
我正在使用 lua + 火炬。
数据集是完全虚拟的——它是在“数据集”对象上动态创建的。当训练周期要求另一个小批量样本时,函数 mt.__index 返回样本,动态创建。它从三个描述的函数中随机选择一个,并为它们挑选一些随机点。
想法是 LSTM 网络将学习一些特征来识别最后一个点属于哪种函数。
包括完整而简单的源脚本:
require "torch"
require "nn"
require "rnn"
-- hyper-parameters
batchSize = 8
rho = 5 -- sequence length
hiddenSize = 100
outputSize = 3
lr = 0.001
-- Initialize synthetic dataset
-- dataset[index] returns table of the form: {inputs, targets}
-- where inputs is a set of points (x,y) of a randomly selected function: line, parabola, sqrt
-- and targets is a set of corresponding class of a function (1=line, 2=parabola, 3=sqrt)
local dataset = {}
dataset.size = function (self)
return 1000
end
local mt = {}
mt.__index = function (self, i)
local class = math.random(3)
local t = torch.Tensor(3):zero()
t[class] = 1
local targets = {}
for i = 1,batchSize do table.insert(targets, class) end
local inputs = {}
local k = math.random()
local b = math.random()*5
-- Line
if class == 1 then
for i = 1,batchSize do
local x = math.random()*10 + 5
local y = k*x + b
input = torch.Tensor(2)
input[1] = x
input[2] = y
table.insert(inputs, input)
end
-- Parabola
elseif class == 2 then
for i = 1,batchSize do
local x = math.random()*10 + 5
local y = k*x*x + b
input = torch.Tensor(2)
input[1] = x
input[2] = y
table.insert(inputs, input)
end
-- Sqrt
else
for i = 1,batchSize do
local x = math.random()*5 + 5
local y = k*math.sqrt(x) + b
input = torch.Tensor(2)
input[1] = x
input[2] = y
table.insert(inputs, input)
end
end
return { inputs, targets }
end -- dataset.__index meta function
setmetatable(dataset, mt)
-- Initialize random number generator
math.randomseed( os.time() )
-- build simple recurrent neural network
local model = nn.Sequencer(
nn.Sequential()
:add( nn.LSTM(2, hiddenSize, rho) )
:add( nn.Linear(hiddenSize, outputSize) )
:add( nn.LogSoftMax() )
)
print(model)
-- build criterion
local criterion = nn.SequencerCriterion( nn.ClassNLLCriterion() )
-- training
model:training()
local epoch = 1
while true do
print ("Epoch "..tostring(epoch).." started")
for iteration = 1, dataset:size() do
-- 1. Load minibatch of samples
local sample = dataset[iteration] -- pick random sample (dataset always returns random set)
local inputs = sample[1]
local targets = sample[2]
-- 2. Perform forward run and calculate error
local outputs = model:forward(inputs)
local err = criterion:forward(outputs, targets)
print(string.format("Epoch %d Iteration %d Error = %f", epoch, iteration, err))
-- 3. Backward sequence through model(i.e. backprop through time)
local gradOutputs = criterion:backward(outputs, targets)
-- Sequencer handles the backwardThroughTime internally
model:backward(inputs, gradOutputs)
model:updateParameters(lr)
model:zeroGradParameters()
end -- for dataset
epoch = epoch + 1
end -- while epoch
问题是:网络不收敛。 你能分享一下我做错了什么吗?
【问题讨论】:
标签: lua neural-network torch lstm recurrent-neural-network