Create custom dataset from tensors

username: Sayyed_Ali_Mousavi
cooked: <p>Hello everyone. I am new to pytorch. I have a program that produce tensors and labels of them. I want to create a dataset (perhaps a .pt file) and use it for training. But how should i do it? How every tensor match its label? Thanks a lot.</p>

username: ptrblck
cooked: <p>In case you have already created the data and target tensors, you could use <code>torch.utils.data.TensorDataset</code> to create the dataset.</p>
<aside class="quote no-group" data-username="Sayyed_Ali_Mousavi" data-post="1" data-topic="122566">
<div class="title">
<div class="quote-controls"></div>
<img loading="lazy" alt="" width="24" height="24" src="https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/sayyed_ali_mousavi/48/38837_2.png" class="avatar"> Sayyed_Ali_Mousavi:</div>
<blockquote>
<p>But how should i do it? How every tensor match its label?</p>
</blockquote>
</aside>
<p>You, as the creator of the dataset, would have to make sure that each create data sample matches its target.</p>

username: Sayyed_Ali_Mousavi
cooked: <p>Thank you very much. I create two empty list and in an iterative procedure append every data to first list and its target to second. Say, features=[tensor_1, tensor_2, …] and targets=[label_of_tensor_1, label_of_tensor_2, …]. Then I use TensorDataset(features, targets). Is this a correct way? I give an Error: “valueerror: only one element tensors can be converted to python scalars”! Can you help me more?</p>

username: ptrblck
cooked: <p>Yes, this is generally the right approach.<br>
Could you check, if all the data and target tensors have the same shape, and thus a tensor creation via:</p>
<pre><code class="lang-python">features = [tensor1, tensor2, ...]
features = torch.stack(features)
</code></pre>
<p>would work?<br>
I guess the new error is raised because of unexpected shapes, but am unsure which operation raises it.</p>

username: Sayyed_Ali_Mousavi
cooked: <p>I’m very sorry (In previous reply I writed error that arised after some additional code) Error is This: “AttributeError: ‘list’ object has no attribute 'size”. And simple code that write for examination:<br>
My code:<br>
my_x = [torch.rand(2,2),torch.rand(2,2)] # list of tensors<br>
my_y = [torch.rand(1), torch.rand(1)] # list of targets<br>
my_dataset = TensorDataset(my_x,my_y).</p>
<p>I apologize if my questions are childish. Thanks a lot.</p>

username: ptrblck
cooked: <p>Ah OK, that would fit my expectation of the error and this would work:</p>
<pre><code class="lang-python">my_x = torch.stack([torch.rand(2,2),torch.rand(2,2)])
my_y = torch.stack([torch.rand(1), torch.rand(1)])
my_dataset = torch.utils.data.TensorDataset(my_x,my_y)
</code></pre>
<p>The error is raised, because tensors are expected, while you are passing <code>lists</code>.</p>

username: Sayyed_Ali_Mousavi
cooked: <p>Thank you very much. It works. And for final question, I would to save my_x and my_y for further use. (or perhaps save my list of tensors and targets that was created). How to save and load them after? (If I do not save them, I will have to create two large of lists with append and it takes a long time).</p>

username: ptrblck
cooked: <p>You can save and load tensors via <code>torch.save(tensor, path)</code> and <code>torch.load(tensor, path)</code>, respectively. This would allow you to load these tensors afterwards and create the <code>TensorDataset</code> directly without creating the lists.</p>