Pytorch-this gives error of shape expand(torch.DoubleTensor{[100, 16, 16]}, size=[100]): the number of sizes provided (1) must be greater or equal to the number of dimensions in the tensor (3)

Urooj_99: <p>##############################################################################</p>
<h1>TODO: Implement this function using only a single loop over x_train.</h1>
<h1></h1>
<h1>You may not use torch.norm (or its instance method variant), nor any</h1>
<h1>functions from torch.nn or torch.nn.functional.</h1>
<p>##############################################################################</p>
<h1>Replace “pass” statement with your code</h1>
<p>for i in range(num_train):</p>
<pre><code>sum_=torch.sum((x_test-x_train[i,:]) **2,dim=1)

cists[i] = torch.sqrt(sum_)</code></pre>

boto: <p>Please provide more information on what your task is and show us your code so far.</p>

Urooj_99: <p>def compute_distances_no_loops(x_train, x_test):</p>
<p>“”"</p>
<p>Computes the squared Euclidean distance between each element of the training</p>
<p>set and each element of the test set. Images should be flattened and treated</p>
<p>as vectors.</p>
<p>This implementation should not use any Python loops. For memory-efficiency,</p>
<p>it also should not create any large intermediate tensors; in particular you</p>
<p>should not create any intermediate tensors with O(num_train*num_test)</p>
<p>elements.</p>
<p>Inputs:</p>
<ul>
<li>
<p>x_train: Torch tensor of shape (num_train, C, H, W)</p>
</li>
<li>
<p>x_test: Torch tensor of shape (num_test, C, H, W)</p>
</li>
</ul>
<p>Returns:</p>
<ul>
<li>
<p>dists: Torch tensor of shape (num_train, num_test) where dists[i, j] is the</p>
<p>squared Euclidean distance between the ith training point and the jth test</p>
<p>point.</p>
</li>
</ul>
<p>“”"</p>
<h1>Initialize dists to be a tensor of shape (num_train, num_test) with the</h1>
<h1>same datatype and device as x_train</h1>
<p>num_train = x_train.shape[0]</p>
<p>num_test = x_test.shape[0]</p>
<p>dists = x_train.new_zeros(num_train, num_test)</p>
<p>##############################################################################</p>
<h1>TODO: Implement this function without using any explicit loops and without</h1>
<h1>creating any intermediate tensors with O(num_train * num_test) elements.</h1>
<h1></h1>
<h1>You may not use torch.norm (or its instance method variant), nor any</h1>
<h1>functions from torch.nn or torch.nn.functional.</h1>
<h1></h1>
<h1>HINT: Try to formulate the Euclidean distance using two broadcast sums</h1>
<h1>and a matrix multiply.</h1>
<p>##############################################################################</p>
<h1>Replace “pass” statement with your code</h1>
<h1>Output: sqrt((x-y)^2)</h1>
<h1>(x-y)^2 = x^2 + y^2 - 2xy</h1>
<p>test_sum = torch.sum((x_test).pow(2), dim=1)</p>
<p>train_sum = torch.sum((x_train).pow(2), dim=1)</p>
<p>inner_product = (x_test)*(x_train)</p>
<p>dists = torch.sqrt( test_sum + train_sum-2*inner_product) # broadcast</p>
<p>##############################################################################</p>
<h1>END OF YOUR CODE</h1>
<p>##############################################################################</p>
<p>return dists<br>
So I am supposed to calculate dists vector without using any loops,basically by the help of broadcasting and matrix multiplication .I think there need to be done reshaping but i am not getting how.I have done this using two loops as well as one loop</p>

Urooj_99: <p>Basicaly x_train and x_test are two tensors here ,x_test has the testing data set where as the training data set is against which we are checking the similarity of our data set</p>