Help with dimensions for LSTM

First time on the forum, glad to be here! I intend on making torch my go-to for deep learning.

So I am trying to run a tensor with dim:
[# texts, # sequential words, len of 1-hot for each word]
[2500,13,100]

target is a single string per text: [2500,1]

so I’m trying to map a sequence of 13 one-hot vectors of len 100 to 1 string for 2500 instances.

heres my code:

``````D_in, D_out = ([torch_x.shape[1], torch_x.shape[2]]), torch_y.shape[1]
print(D_in, D_out)
[13, 100] 1

H1, H2, H3 = 500, 800, 300

class RNN(nn.Module):
def __init__(self, D_in, H1, H2, H3, D_out):
super(RNN, self).__init__()

self.lstm = nn.LSTM(D_in, H1)
self.linear1 = nn.Linear(H1, H2)
self.linear2 = nn.Linear(H2, D_out)

``````
``````def forward(self, x):
y_pred, states = self.lstm(x)
y_pred         = self.linear1(y_pred).clamp(min=0)
y_pred         = self.linear2(y_pred)
return y_pred
model = RNN(D_in, H1, H2, H3, D_out)
optimizer = torch.optim.Adam(model.parameters(), lr=1e-4 * 2)
criterion = nn.MSELoss(reduction='sum')
``````
``````
losses = []
for t in range(50):
y_pred = model(torch_c)
``````
``````
loss = criterion(y_pred, torch_y)
print(t, loss.item())
losses.append(loss.item())

if torch.isnan(loss):
break

loss.backward()
optimizer.step()
``````

and the error for this is:

—> 14 y_pred, states = self.lstm(x)
RuntimeError: input.size(-1) must be equal to input_size. Expected 13, got 100

if I change D_in to a 1-D of 100 , it’s:

----> 5 loss = criterion(y_pred, torch_y)
RuntimeError: The size of tensor a (13) must match the size of tensor b (2500) at non-singleton dimension 1

any thoughts on how I can do this?
thanks so much!

@jekasm
I think the dimension of [# texts, # sequential words, len of 1-hot for each word] is okay, BUT make sure the `batch_first argument` of the lstm is True just as the documentation suggested .LSTM — PyTorch 1.9.1 documentation