Convert a tuple to torch tensor

I working on capsule networks and I get this error once I go through the loss function :

AttributeError: ‘tuple’ object has no attribute ‘size’ the error comes form this line:

t = torch.zeros(lengths.size()).long()

I tired multiple ways to convert it to tuples but always has an issue with detach from the GPU

this is the loss function:

class MarginLoss(nn.Module):
def init(self, m_pos, m_neg, lambda_):
super(MarginLoss, self).init()
self.m_pos = m_pos
self.m_neg = m_neg
self.lambda_ = lambda_

def forward(self, lengths, targets, size_average=True):
    
    t = torch.zeros(lengths.size()).long()
    if targets.is_cuda:
        t = t.cuda()
    t = t.scatter_(1, targets.data.view(-1, 1), 1)
    targets = Variable(t)
    losses = targets.float() * F.relu(self.m_pos - lengths).pow(2) + \
             self.lambda_ * (1. - targets.float()) * F.relu(lengths - self.m_neg).pow(2)
    return losses.mean() if size_average else losses.sum()

Thanks in advance

You can directly transform a tuple to a tensor if the entries are suitable:

a = (1., 2.)
print(type(a))
# <class 'tuple'>

b = torch.tensor(a)
print(b)
# tensor([1., 2.])

I just tried that it throws back this error :

ValueError: only one element tensors can be converted to Python scalars

What does the tuple contain?

it contains the out put of the model : some thing like this
log_probs=model(x)
loss=margin_loss(log_probs,labels)

ones it fall in parameters log_probs will be lenghts and labels will be target