TensorDataSet "size mismatch between tensors"

Question:

I have 4 matrices of size 64×64 that were stacked (Torch.Stack) to create a size of [4,64,64] and are meant to be the inputs for my TensorDataSet. I have 1 matrix of 64×64 that is meant to be output for my TensorDataSet. When I load these into the TensorDataSet(inputs,outputs), I get the size mismatch.

If I take 1 input and 1 output each with size 64×64 the TensorDataSet will accept this. However, I want to pass in 4 input values that correspond to 1 output value. For example the first value in the [0,0] position of each input has a relationship to the [0,0] position of the output.

I tried using squeeze methods and didn’t have any success.

Asked By: PytorchLearn

||

Answers:

If you look carefully at the stack trace of the error raised by TensorDataset, you will see:

assert all(tensors[0].size(0) == tensor.size(0) for tensor in tensors)

This means all tensors must have the same size along the first dimension (the dataset size).

In your case, one 4x64x64 tensor corresponds to one 64×64 tensor, in other words, the inputs to TensorDataset must be shaped (1,4,64,64) and (1,64,64) for the input and output, respectively. Therefore you need to add an extra dimension on both (with None indexing or unsqueeze):

x = torch.rand(4,64,64)
y = torch.rand(64,64)

dataset = TensorDataset(x[None], y[None])
Answered By: Ivan
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.