<?xml version="1.0"?>
<feed xmlns="http://www.w3.org/2005/Atom" xml:lang="en">
	<id>https://mscneuro.neuro.uni-bremen.de/index.php?action=history&amp;feed=atom&amp;title=Interfacing_Data</id>
	<title>Interfacing Data - Revision history</title>
	<link rel="self" type="application/atom+xml" href="https://mscneuro.neuro.uni-bremen.de/index.php?action=history&amp;feed=atom&amp;title=Interfacing_Data"/>
	<link rel="alternate" type="text/html" href="https://mscneuro.neuro.uni-bremen.de/index.php?title=Interfacing_Data&amp;action=history"/>
	<updated>2026-06-03T06:33:45Z</updated>
	<subtitle>Revision history for this page on the wiki</subtitle>
	<generator>MediaWiki 1.43.5</generator>
	<entry>
		<id>https://mscneuro.neuro.uni-bremen.de/index.php?title=Interfacing_Data&amp;diff=471&amp;oldid=prev</id>
		<title>Davrot: Created page with &quot;We need to handle our data and make it accessible for PyTorch.  Questions to [mailto:davrot@uni-bremen.de David Rotermund]  There are options to interface your data.  == [https://pytorch.org/docs/stable/data.html#torch.utils.data.TensorDataset torch.utils.data.TensorDataset] == &lt;syntaxhighlight lang=&quot;python&quot;&gt;CLASS torch.utils.data.TensorDataset(*tensors)&lt;/syntaxhighlight&gt;&lt;blockquote&gt;Dataset wrapping tensors.  Each sample will be retrieved by indexing tensors along the fi...&quot;</title>
		<link rel="alternate" type="text/html" href="https://mscneuro.neuro.uni-bremen.de/index.php?title=Interfacing_Data&amp;diff=471&amp;oldid=prev"/>
		<updated>2025-10-21T09:48:12Z</updated>

		<summary type="html">&lt;p&gt;Created page with &amp;quot;We need to handle our data and make it accessible for PyTorch.  Questions to [mailto:davrot@uni-bremen.de David Rotermund]  There are options to interface your data.  == [https://pytorch.org/docs/stable/data.html#torch.utils.data.TensorDataset torch.utils.data.TensorDataset] == &amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;CLASS torch.utils.data.TensorDataset(*tensors)&amp;lt;/syntaxhighlight&amp;gt;&amp;lt;blockquote&amp;gt;Dataset wrapping tensors.  Each sample will be retrieved by indexing tensors along the fi...&amp;quot;&lt;/p&gt;
&lt;p&gt;&lt;b&gt;New page&lt;/b&gt;&lt;/p&gt;&lt;div&gt;We need to handle our data and make it accessible for PyTorch.&lt;br /&gt;
&lt;br /&gt;
Questions to [mailto:davrot@uni-bremen.de David Rotermund]&lt;br /&gt;
&lt;br /&gt;
There are options to interface your data.&lt;br /&gt;
&lt;br /&gt;
== [https://pytorch.org/docs/stable/data.html#torch.utils.data.TensorDataset torch.utils.data.TensorDataset] ==&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;CLASS torch.utils.data.TensorDataset(*tensors)&amp;lt;/syntaxhighlight&amp;gt;&amp;lt;blockquote&amp;gt;Dataset wrapping tensors.&lt;br /&gt;
&lt;br /&gt;
Each sample will be retrieved by indexing tensors along the first dimension.&lt;br /&gt;
&lt;br /&gt;
&amp;#039;&amp;#039;&amp;#039;*tensors&amp;#039;&amp;#039;&amp;#039; : (Tensor) – tensors that have the same size of the first dimension.&amp;lt;/blockquote&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== [https://pytorch.org/docs/stable/data.html#torch.utils.data.Dataset torch.utils.data.Dataset] ==&lt;br /&gt;
In the case we might not be able to load the fully dataset into memory, the &amp;#039;&amp;#039;&amp;#039;torch.utils.data.Dataset&amp;#039;&amp;#039;&amp;#039; is very helpful.&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;CLASS torch.utils.data.Dataset(*args, **kwds)&amp;lt;/syntaxhighlight&amp;gt;&amp;lt;blockquote&amp;gt;An abstract class representing a Dataset.&lt;br /&gt;
&lt;br /&gt;
All datasets that represent a map from keys to data samples should subclass it. All subclasses should overwrite &amp;#039;&amp;#039;&amp;#039;__getitem__()&amp;#039;&amp;#039;&amp;#039;, supporting fetching a data sample for a given key. Subclasses could also optionally overwrite &amp;#039;&amp;#039;&amp;#039;__len__()&amp;#039;&amp;#039;&amp;#039;, which is expected to return the size of the dataset by many Sampler implementations and the default options of DataLoader. Subclasses could also optionally implement &amp;#039;&amp;#039;&amp;#039;__getitems__()&amp;#039;&amp;#039;&amp;#039;, for speedup batched samples loading. This method accepts list of indices of samples of batch and returns list of samples.&amp;lt;/blockquote&amp;gt;We need to create a new class which is derived from &amp;#039;&amp;#039;&amp;#039;torch.utils.data.Dataset&amp;#039;&amp;#039;&amp;#039;. We can do what every we want in this class as long as we service the functions * &amp;#039;&amp;#039;&amp;#039;__len__()&amp;#039;&amp;#039;&amp;#039; : gives us the number of pattern in the dataset * &amp;#039;&amp;#039;&amp;#039;__getitem__(index)&amp;#039;&amp;#039;&amp;#039; : gives us the information about ONE pattern at position index in the data set. In the following example, I return the image as 3d torch.Tensor and the corresponding class for that pattern (for which I use int).&lt;br /&gt;
&lt;br /&gt;
We have a lot of freedom for our own design. e.g.: * The argument &amp;#039;&amp;#039;&amp;#039;train:bool&amp;#039;&amp;#039;&amp;#039; of the contructor was introduced by me. * The &amp;#039;&amp;#039;&amp;#039;__getitem__(index)&amp;#039;&amp;#039;&amp;#039; doesn’t need to give back the data for that pattern in exactly this way (means: order of variables, types of variables, number of variables).&lt;br /&gt;
&lt;br /&gt;
We assume that the data is in the four following files: * train_pattern_storage.npy * train_label_storage.npy * test_pattern_storage.npy * test_label_storage.npy&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;import numpy as np&lt;br /&gt;
import torch&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
class MyDataset(torch.utils.data.Dataset):&lt;br /&gt;
&lt;br /&gt;
    # Initialize&lt;br /&gt;
    def __init__(self, train: bool = False) -&amp;gt; None:&lt;br /&gt;
        super(MyDataset, self).__init__()&lt;br /&gt;
&lt;br /&gt;
        if train is True:&lt;br /&gt;
            self.pattern_storage: np.ndarray = np.load(&amp;quot;train_pattern_storage.npy&amp;quot;)&lt;br /&gt;
            self.label_storage: np.ndarray = np.load(&amp;quot;train_label_storage.npy&amp;quot;)&lt;br /&gt;
        else:&lt;br /&gt;
            self.pattern_storage = np.load(&amp;quot;test_pattern_storage.npy&amp;quot;)&lt;br /&gt;
            self.label_storage = np.load(&amp;quot;test_label_storage.npy&amp;quot;)&lt;br /&gt;
&lt;br /&gt;
        self.pattern_storage = self.pattern_storage.astype(np.float32)&lt;br /&gt;
        self.pattern_storage /= np.max(self.pattern_storage)&lt;br /&gt;
&lt;br /&gt;
        # How many pattern are there?&lt;br /&gt;
        self.number_of_pattern: int = self.label_storage.shape[0]&lt;br /&gt;
&lt;br /&gt;
    def __len__(self) -&amp;gt; int:&lt;br /&gt;
        return self.number_of_pattern&lt;br /&gt;
&lt;br /&gt;
    # Get one pattern at position index&lt;br /&gt;
    def __getitem__(self, index: int) -&amp;gt; tuple[torch.Tensor, int]:&lt;br /&gt;
&lt;br /&gt;
        image = torch.tensor(self.pattern_storage[index, np.newaxis, :, :])&lt;br /&gt;
        target = int(self.label_storage[index])&lt;br /&gt;
&lt;br /&gt;
        return image, target&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
if __name__ == &amp;quot;__main__&amp;quot;:&lt;br /&gt;
    pass&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;/div&gt;</summary>
		<author><name>Davrot</name></author>
	</entry>
</feed>