Hi there,
I’ve coded a model which takes a tensor (batch_size,8,26)
input of one-hot rows and yields a tensor (batch_size, 8, 189)
.
My labels are also tensors (batch_size, 8, 189)
of one-hot rows, so i wanted to make preds=model(batch)
into one-hot rows trasformin the max of the row in one and others in zeros.
So I can determine if the prediction is correct, not just the drop in loss, which I can get now.
I tried with this:
def MaxT1(self, batch_size, batch):
#self.mphonsl,self.phons = 8, 189, because inputs from a fc layer
batch=batch.view(batch_size,self.mphonsl,self.phons)
view=batch
batch=(view==batch.max(dim=2, keepdim=True)[0]).view_as(batch)
return batch
and calling it in the .forward
of the model as:
t=self.MaxT1(batch, batch_size)
However when calling loss.backward()
I get
---------------------------------------------------------------------------
RuntimeError Traceback (most recent call last)
<ipython-input-18-f2b8b1fbff2f> in <module>
22 loss=loss(labels.float(),preds.float())
23 optimizer.zero_grad() # otherwise it keeps all gradients
---> 24 loss.backward()
25 optimizer.step()
26 tb.add_scalar('Loss', loss, epoch)
~\.conda\envs\Pytorch\lib\site-packages\torch\tensor.py in backward(self, gradient, retain_graph, create_graph)
148 products. Defaults to ``False``.
149
--> 150 torch.autograd.backward(self, gradient, retain_graph, create_graph)
151
152 def register_hook(self, hook):
~\.conda\envs\Pytorch\lib\site-packages\torch\autograd\__init__.py in backward(tensors, grad_tensors, retain_graph, create_graph, grad_variables)
97 Variable._execution_engine.run_backward(
98 tensors, grad_tensors, retain_graph, create_graph,
---> 99 allow_unreachable=True) # allow_unreachable flag
100
101
RuntimeError: element 0 of tensors does not require grad and does not have a grad_fn
Any idea about how can i do it differently or solve this?
Thanks in advance