I’ve changed the sine wave generator to use float32 to try to help compatibility:
import math
import numpy as np
import torch
T = 20
L = 1000
N = 100
np.random.seed(2)
x = np.empty((N, L), 'int64')
x[:] = np.array(range(L)) + np.random.randint(-4*T, 4*T, N).reshape(N, 1)
data = np.sin(x / 1.0 / T).astype('float32')
torch.save(data, open('traindata.pt', 'wb'))
I’ve tried .cuda()-ing everything I can think of but always get a type error. Here’s one of the tries.
import torch
import torch.nn as nn
from torch import autograd
import torch.optim as optim
import numpy as np
import matplotlib
matplotlib.use('Agg')
class Variable(autograd.Variable):
def __init__(self, data, *args, **kwargs):
data = data.cuda()
super(Variable, self).__init__(data, *args, **kwargs)
class Sequence(nn.Module):
def __init__(self):
super(Sequence, self).__init__()
self.lstm1 = nn.LSTMCell(1, 51).cuda()
self.lstm2 = nn.LSTMCell(51, 1).cuda()
def forward(self, input, future = 0):
input = input.cuda()
outputs = []
h_t = Variable(torch.zeros(input.size(0), 51), requires_grad=False)
c_t = Variable(torch.zeros(input.size(0), 51), requires_grad=False)
h_t2 = Variable(torch.zeros(input.size(0), 1), requires_grad=False)
c_t2 = Variable(torch.zeros(input.size(0), 1), requires_grad=False)
for i, input_t in enumerate(input.chunk(input.size(1), dim=1)):
h_t, c_t = self.lstm1(input_t, (h_t, c_t))
h_t2, c_t2 = self.lstm2(c_t, (h_t2, c_t2))
outputs += [c_t2]
for i in range(future):# if we should predict the future
h_t, c_t = self.lstm1(c_t2, (h_t, c_t))
h_t2, c_t2 = self.lstm2(c_t, (h_t2, c_t2))
outputs += [c_t2]
outputs = torch.stack(outputs, 1).squeeze(2)
return outputs
if __name__ == '__main__':
# set ramdom seed to 0
np.random.seed(0)
torch.manual_seed(0)
# load data and make training set
data = torch.load('traindata.pt')
input = Variable(torch.from_numpy(data[3:, :-1]), requires_grad=False)
input.cuda()
print(type(input))
target = Variable(torch.from_numpy(data[3:, 1:]), requires_grad=False).cuda()
# build the model
seq = Sequence()
#seq.double()
seq.cuda()
criterion = nn.MSELoss()
# use LBFGS as optimizer since we can load the whole data to train
optimizer = optim.LBFGS(seq.parameters())
#begin to train
for i in range(10):
print('STEP: ', i)
def closure():
optimizer.zero_grad()
out = seq(input).cuda()
loss = criterion(out, target)
print('loss:', loss.data.numpy()[0])
loss.backward()
return loss
optimizer.step(closure)
# begin to predict
future = 1000
pred = seq(input[:3], future = future)
y = pred.data.numpy()
The error is at optimizer.step(closure)
What’s missing?