Toy example for pack_padded_sequence

I was trying to run the working example on how to use packing for variable-length sequence inputs for rnn taken from this link (Simple working example how to use packing for variable-length sequence inputs for rnn).

Here is the code:

import torch
import torch.nn as nn
from torch.autograd import Variable
from torch.nn import functional as F

batch_size=4
max_length = 3
hidden_size = 2
n_layers = 1

vec_1 = torch.FloatTensor([[1, 2, 3]])
vec_2 = torch.FloatTensor([[1, 2, 0]])
vec_3 = torch.FloatTensor([[1, 0, 0]])
vec_3 = torch.FloatTensor([[4, 0, 0]])

print(vec_1)

batch_in[0] = vec_1
batch_in[1] = vec_2
batch_in[2] = vec_3
batch_in[3] = vec_4

batch_in = Variable(batch_in)

print(batch_in)

seq_lengths = [3,2,1, 1] # list of integers holding information about the batch size at each sequence step

pack = torch.nn.utils.rnn.pack_padded_sequence(batch_in, seq_lengths, batch_first=True)
print(pack)

rnn = nn.RNN(max_length, hidden_size, n_layers, batch_first=True)
h0 = Variable(torch.randn(n_layers, batch_size, hidden_size))

out, _ = rnn(pack, h0)

batch_in = torch.zeros((batch_size, max_length, 1))

However, When I execute the code. It throws me the following error.

RuntimeError: The expanded size of the tensor (1) must match the existing size (3) at non-singleton dimension 1. Target sizes: [3, 1]. Tensor sizes: [3]

But, If I change the vec_1, vec_2, vec_3, vec_4 to the following way:

vec_1 = torch.FloatTensor([[1], [2], [3]])
vec_2 = torch.FloatTensor([[1], [2], [0]])
vec_3 = torch.FloatTensor([[1], [0], [0]])
vec_3 = torch.FloatTensor([[4], [0], [0]])

I don’t get the above error but a different error. Here it is:

RuntimeError: input.size(-1) must be equal to input_size. Expected 3, got 1

Can someone explain what is wrong in here? In the discussion form, it seems for everyone else it is working.