samsambakster
samsambakster

Reputation: 163

Covnert a List of Tensors to a Tensor

I have a list of tensors like this:

[tensor(-2.9222, grad_fn=<SqueezeBackward1>), tensor(-2.8192, grad_fn=<SqueezeBackward1>), tensor(-3.1894, grad_fn=<SqueezeBackward1>), tensor(-2.9048, grad_fn=<SqueezeBackward1>)]

I want it to be in this form:

tensor([-0.5575, -0.9004, -0.8491,  ..., -0.7345, -0.6729, -0.7553],
   grad_fn=<SqueezeBackward1>)

How can I do this? I appreciate the help.

Upvotes: 0

Views: 828

Answers (1)

trialNerror
trialNerror

Reputation: 3573

Since these tensor are 0-dimensional, torch.catwill not work but you can use torch.stack (which creates a new dimension along which to concatenate):

a = torch.tensor(1.0, requires_grad=True)
b = torch.tensor(2.0, requires_grad=True)
torch.stack([a,b], dim=0)
>>>tensor([1.,2.], grad_fn=<StackBackward>)

Upvotes: 1

Related Questions