Skip to content

Instantly share code, notes, and snippets.

@dmus
Created October 19, 2016 12:47
Show Gist options
  • Save dmus/a01698ed787f4b1828ed6cfab7a1ffb6 to your computer and use it in GitHub Desktop.
Save dmus/a01698ed787f4b1828ed6cfab7a1ffb6 to your computer and use it in GitHub Desktop.
Sample
require 'nn'
require 'torch'
require 'rnn'
require 'gnuplot'
-- the input data is: x[i]=i (i=1..1000)
data = torch.Tensor(1001):range(1,1001):apply(math.rad):apply(math.sin)
lstm = torch.load('rnn_sine.t7')
local N = 50 -- number of steps to sample
local start = 900 -- start here
local warmup = 10 -- warm up the lstm by forwarding data for this amount of steps
lstm:forget() -- set hidden state to zero
local x = torch.Tensor(1,1,1):zero()
-- warm-up the lstm
for i=1,warmup do
x[1][1][1] = data[start+i-1] -- start
print(x[1][1][1])
lstm:forward(x)
end
print('output')
output = torch.Tensor(N)
for i=1,N do
local x_next = lstm:forward(x)
output[i] = x_next:squeeze()
print(output[i])
x = x_next
end
gnuplot.pngfigure("output.png")
gnuplot.plot({'target', data,'-'},{'output',torch.range(start+warmup, start+N+warmup-1),output:float(),'-'}, {'init',torch.range(start, start+warmup-1),data[{{start,start+warmup-1}}],'-'})
gnuplot.plotflush()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment