Hello I try implement this into video classification. Let say I have 3 feature per frame then I have 10 frames to consider. I am using this example, I think sequence_length=10 and input_shape=3 am I right? this is my code for the architecture.
# Hyper Parameters
sequence_length = 10
input_size = 3
hidden_size = 128
num_layers = 2
num_classes = 2
batch_size = 100
num_epochs = 2
learning_rate = 0.003
train_loader = torch.utils.data.DataLoader(dataset=spatio_dataset,
batch_size=batch_size,
shuffle=True)
print(train_loader)
# BiRNN Model (Many-to-One)
class BiRNN(nn.Module):
def __init__(self, input_size, hidden_size, num_layers, num_classes):
super(BiRNN, self).__init__()
self.hidden_size = hidden_size
self.num_layers = num_layers
self.lstm = nn.LSTM(input_size, hidden_size, num_layers,
batch_first=True, bidirectional=True)
self.fc = nn.Linear(hidden_size*2, num_classes) # 2 for bidirection
def forward(self, x):
# Set initial states
h0 = Variable(torch.zeros(self.num_layers*2, x.size(0), self.hidden_size)).cuda() # 2 for bidirection
c0 = Variable(torch.zeros(self.num_layers*2, x.size(0), self.hidden_size)).cuda()
# Forward propagate RNN
out, _ = self.lstm(x, (h0, c0))
# Decode hidden state of last time step
out = self.fc(out[:, -1, :])
return out
rnn = BiRNN(input_size, hidden_size, num_layers, num_classes)
rnn.cuda()
# Loss and Optimizer
criterion = nn.CrossEntropyLoss()
optimizer = torch.optim.Adam(rnn.parameters(), lr=learning_rate)
Here is my train code:
# Train the Model
for epoch in range(num_epochs):
for i, (spatio, label) in enumerate(train_loader):
#print(spatio)
#print(label)
spatio = Variable(spatio.view(-1, sequence_length, input_size)).cuda()
label = Variable(label).cuda()
print(spatio.size())
print(label.size())
# Forward + Backward + Optimize
optimizer.zero_grad()
outputs = rnn(spatio)
loss = criterion(outputs, label)
loss.backward()
optimizer.step()
if (i+1) % 100 == 0:
print ('Epoch [%d/%d], Step [%d/%d], Loss: %.4f'
%(epoch+1, num_epochs, i+1, len(train_dataset)//batch_size, loss.data[0]))
However I got This error:
---------------------------------------------------------------------------
AssertionError Traceback (most recent call last)
<ipython-input-54-5beb6b9da8ce> in <module>()
11 # Forward + Backward + Optimize
12 optimizer.zero_grad()
---> 13 outputs = rnn(spatio)
14 loss = criterion(outputs, label)
15 loss.backward()
/usr/local/lib/python2.7/dist-packages/torch/nn/modules/module.pyc in __call__(self, *input, **kwargs)
204
205 def __call__(self, *input, **kwargs):
--> 206 result = self.forward(*input, **kwargs)
207 for hook in self._forward_hooks.values():
208 hook_result = hook(self, input, result)
<ipython-input-51-474be96e77da> in forward(self, x)
29
30 # Forward propagate RNN
---> 31 out, _ = self.lstm(x, (h0, c0))
32
33 # Decode hidden state of last time step
/usr/local/lib/python2.7/dist-packages/torch/nn/modules/module.pyc in __call__(self, *input, **kwargs)
204
205 def __call__(self, *input, **kwargs):
--> 206 result = self.forward(*input, **kwargs)
207 for hook in self._forward_hooks.values():
208 hook_result = hook(self, input, result)
/usr/local/lib/python2.7/dist-packages/torch/nn/modules/rnn.pyc in forward(self, input, hx)
89 dropout_state=self.dropout_state
90 )
---> 91 output, hidden = func(input, self.all_weights, hx)
92 if is_packed:
93 output = PackedSequence(output, batch_sizes)
/usr/local/lib/python2.7/dist-packages/torch/nn/_functions/rnn.pyc in forward(input, *fargs, **fkwargs)
341 else:
342 func = AutogradRNN(*args, **kwargs)
--> 343 return func(input, *fargs, **fkwargs)
344
345 return forward
/usr/local/lib/python2.7/dist-packages/torch/autograd/function.pyc in _do_forward(self, *input)
200 self._nested_input = input
201 flat_input = tuple(_iter_variables(input))
--> 202 flat_output = super(NestedIOFunction, self)._do_forward(*flat_input)
203 nested_output = self._nested_output
204 nested_variables = _unflatten(flat_output, self._nested_output)
/usr/local/lib/python2.7/dist-packages/torch/autograd/function.pyc in forward(self, *args)
222 def forward(self, *args):
223 nested_tensors = _map_variable_tensor(self._nested_input)
--> 224 result = self.forward_extended(*nested_tensors)
225 del self._nested_input
226 self._nested_output = result
/usr/local/lib/python2.7/dist-packages/torch/nn/_functions/rnn.pyc in forward_extended(self, input, weight, hx)
283 hy = tuple(h.new() for h in hx)
284
--> 285 cudnn.rnn.forward(self, input, hx, weight, output, hy)
286
287 self.save_for_backward(input, hx, weight, output)
/usr/local/lib/python2.7/dist-packages/torch/backends/cudnn/rnn.pyc in forward(fn, input, hx, weight, output, hy)
253 w.zero_()
254 params = get_parameters(fn, handle, w)
--> 255 _copyParams(weight, params)
256
257 if tuple(hx.size()) != hidden_size:
/usr/local/lib/python2.7/dist-packages/torch/backends/cudnn/rnn.pyc in _copyParams(params_from, params_to)
181 for layer_params_from, layer_params_to in zip(params_from, params_to):
182 for param_from, param_to in zip(layer_params_from, layer_params_to):
--> 183 assert param_from.type() == param_to.type()
184 param_to.copy_(param_from)
185
AssertionError:
After I check the input shape, the example and my code have same input shape but it has different output shape.
Example MNIST shape:
torch.Size([100, 28, 28])
torch.Size([100])
While my shape is:
torch.Size([100, 10, 3])
torch.Size([100, 1])
Anybody know how to solved this error?, if my assumpion is right because of shape, do you know how to shape from torch.Size([100, 1]) into torch.Size([100]) ?
-Thank You-
Hello I try implement this into video classification. Let say I have 3 feature per frame then I have 10 frames to consider. I am using this example, I think
sequence_length=10
andinput_shape=3
am I right? this is my code for the architecture.Here is my train code:
However I got This error:
After I check the input shape, the example and my code have same input shape but it has different output shape. Example MNIST shape:
While my shape is:
Anybody know how to solved this error?, if my assumpion is right because of shape, do you know how to shape from
torch.Size([100, 1])
intotorch.Size([100])
? -Thank You-