Dividing 3D tensor into Batches

username: Adrian_Briceno_Aguil
cooked: <p>I have a tensor of shape (50,50,2). That is filled with values of a grid in (x,y). Like this.</p>
<pre><code class="lang-auto">r =0.5
#Sample 3D space
min = -10
max = 10
complexnum = 50j

#Sample 3D space
X,Y  = np.mgrid[min:max:complexnum,min:max:complexnum,] * r

T = np.sin(3*X) * np.sin(3*Y)


XY = np.concatenate((X.reshape((X.shape[0],X.shape[0],1)),Y.reshape((X.shape[0],X.shape[0],1))),2)
XY = torch.from_numpy(XY.astype(np.float32)).to(device)
T = T.reshape((T.shape[0],T.shape[0],1))
T = torch.from_numpy(T.astype(np.float32)).to(device)
</code></pre>
<p>I created a Neural Network that can take the XY as input and T as an output. In that is creating a function f(x,y). Now I need to create a way to train that NN using batches. The total amount of samples should be 2500 = 50 x 50. I want to divide the XY and T tensor into batches. I have tried DataLoader and creating a data set class with no luck. Is there a way I can achieve this? Are my Tensor shapes correct?</p>
<p>Sorry for this very basic question, I have been struggling to work with higher dimensions.</p>
<p>Are the questions badly explained? I would have guessed someone would reply by now.</p>

username: ptrblck
cooked: <p>If the total number of samples should be 2500, you should use this shape in <code>dim0</code> via:</p>
<pre><code class="lang-python">XY = XY.view(-1, 2) # should have shape [2500, 2] now
T = T.view(-1, 1) # should have shape [2500, 1] now
</code></pre>
<p>Afterwards you can pass these tensors to a <code>TensorDataset</code> and this dataset to a <code>DataLoader</code>.<br>
Let me know, if you get stuck.</p>

username: Adrian_Briceno_Aguil
cooked: <p>Another question, If I want to transform that grid into a list of the points of (x,y). Would I do the same? I found that if I do<code> XY.view(-1, 2)</code> I will have reaping (x,y) pairs.</p>

username: Adrian_Briceno_Aguil
cooked: <p>Thank you for your reply. Maybe I didn’t understand the whole problem myself before explaining. But now I do. So what I want to create batches of parts of the grid. If you think of it as an image is divided into subsets of the images, then randomly sample each of them to create new sub-images filled with random samples of those subdivisions.</p>
<p>So for example I want to make batches of shape [12,12,2] and [12,12,1] that have random samples of the data set. I think I would have to create and slice it in my own way. I tried to find something similar but I could find anything like it.</p>
<p>For now, I have done something similar to what you have told me to do.</p>
<p>Thank you for the reply, if you do know how I could start doing this it could improve my current code.</p>

username: ptrblck
cooked: <p>I’m not sure if I fully understand your use case, but would you like to create windows or patches from a larger image?<br>
E.g. given an input image of <code>[channels=3, height=224, width=224]</code>, would you like to create multiple smaller patches in e.g. <code>[channels=3, height=12, width=12]</code> using a sliding window approach?<br>
If so, then you should use <code>tensor.unfold</code> to create these patches.</p>
<p><a href="https://discuss.pytorch.org/t/fold-unfold-to-get-patches/53419/6">Here</a> is an example.</p>

username: Adrian_Briceno_Aguil
cooked: <p>Yeah, I think I haven’t mastered how to properly use grids.</p>
<p>I’m trying to train a NN that can learn f(x,y), so for each pair of x,y there is a value associated with it.</p>
<p>This specific function:</p>
<pre><code class="lang-auto">T = np.sin(3*X) * np.sin(3*Y)
</code></pre>
<p>I wanted to do this example first to try and see if I can do it before using it for more complicated inputs such as images. That is why I tried to use <code>np.mgrid</code>  to produce sample (x,y) uniformly and use it as my training set. Could you think of any way I could do it more easily?</p>

username: Adrian_Briceno_Aguil
cooked: <p>Yes, exactly to later use as mini-batches in the training. And if you are confused of why I’m trying to do this for. I’m working on a new algorithm for academic purposes .</p>

username: ptrblck
cooked: <aside class="quote no-group" data-username="Adrian_Briceno_Aguil" data-post="8" data-topic="100198">
<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/adrian_briceno_aguil/48/12377_2.png" class="avatar"> Adrian_Briceno_Aguil:</div>
<blockquote>
<p>That is why I tried to use <code>np.mgrid</code> to produce sample (x,y) uniformly and use it as my training set. Could you think of any way I could do it more easily?</p>
</blockquote>
</aside>
<p>I think you are using a valid approach.<br>
Are you concerned about the ordering of the data after the reshape?<br>
If so, you could print some data and check, if the shape is expected.</p>
<p>After my <code>view</code> operation, the <code>[50, 50]</code> numpy array would be flattened to <code>[2500, 1]</code>, so that each “point” is a sample now.</p>

username: Adrian_Briceno_Aguil
cooked: <p>Yeah, that is one of my concerns. Since I want to train a NN to a specific image I wanted to see if I could just use a grid structure to train the network and try not to flatten it.</p>
<p>Since I wanted to cut it into overlapping quadrants, and somehow is been quite difficult to do it in a 1d array. Haha, I’m just trying to avoid redoing my whole code.</p>