I have a code I wrote in Torch which I am migrating to PyTorch. Its a regression-based learning problem. In Torch, after defining my network, I define concatenate my inputs along the 2nd axis as follows
inputs = torch.cat({inputs[1], inputs[2], inputs[3], inputs[4],
inputs[5], inputs[6], inputs[7], inputs[8],
inputs[9]}, 2)
And the network is as defined:
(lstm1): LSTM(9, 9)
(lstm2): LSTM(9, 6)
(lstm3): LSTM(6, 6)
(drop): Dropout (p = 0.3)
(fc1): Linear (6 -> 3)
(fc2): Linear (3 -> 3)
In torch, I can forward call and backward call okay as the inputs tensor id of batch size [100, 9] torch.*Tensor.
I construct the network the same way in PyTorch and concatenate my nine inputs along the 1st axis as
inputs = torch.cat((base_in, base_out, left_in,
left_out, right_in, right_out,
z, pitch, yaw), 1)
I define my model in PyTorch as
class LSTMModel(nn.Module):
def __init__(self, nFeatures, nCls, nHidden, nineq=12, neq=0, eps=1e-4,
noutputs=3,numLayers=1):
super(LSTMModel, self).__init__()
self.nFeatures = nFeatures
self.nHidden = nHidden
self.nCls = nCls
self.nineq = nineq
self.neq = neq
self.eps = eps
self.cost = nn.MSELoss(size_average=False)
self.noutputs = noutputs
# self.neunet = nn.Sequential()
self.lstm1 = nn.LSTM(nHidden[0],nHidden[0],num_layers=numLayers)
self.lstm2 = nn.LSTM(nHidden[0],nHidden[1],num_layers=numLayers)
self.lstm3 = nn.LSTM(nHidden[1],nHidden[2],num_layers=numLayers)
self.drop = nn.Dropout(0.3)
self.fc1 = nn.Linear(nHidden[2], noutputs)
self.fc2 = nn.Linear(noutputs, noutputs)
self.M = Variable(torch.tril(torch.ones(nCls, nCls)))
self.L = Parameter(torch.tril(torch.rand(nCls, nCls)))
self.G = Parameter(torch.Tensor(nineq/2, nCls).uniform_(-1,1))
"""
define constraints, z_i, and slack variables, s_i,
for six valves. z_i and c_i are learnable parameters
"""
self.z0 = Parameter(torch.zeros(nCls))
self.s0 = Parameter(torch.ones(nineq/2))
self.z0p = Parameter(torch.zeros(nCls))
self.s0p = Parameter(torch.ones(nineq/2))
def forward(self, x):
nBatch = x.size(0)
# LSTM-dropout-LSTM-dropout-lstm-dropout-FC-QP-FC
x = x.view(nBatch, -1)
x = self.lstm1(x)
x = self.drop(x)
x = self.lstm2(x)
x = self.drop(x)
x = self.lstm3(x)
x = self.drop(x)
x = self.fc1(x)
But my calls to forward gives me runtime errors like so
RuntimeError: matrices expected, got 1D, 2D tensors at /home/robotec/Documents/NNs/locuclab-pytorch/torch/lib/TH/generic/THTensorMath.c:1224
I wonder what I must be doing wrong.