akankshakshurai/cuda-error-resolution-analysis huggingface dataset:
Number of weights on conv2d layer

afshin67: <p>I have a simple convolution network:</p>
<pre><code>import torch.nn as nn 

class model(nn.Module):
def __init__(self, ks=1):
    super(model, self).__init__()
    self.conv1 = nn.Conv2d(in_channels=4, out_channels=32, kernel_size=ks, stride=1)

    self.fc1 = nn.Linear(8*8*32*ks, 64)
    self.fc2 = nn.Linear(64, 64)

def forward(self, x):
    x = F.relu(self.conv1(x))
    x = x.view(x.size(0), -1)
    x = F.relu(self.fc1(x))
    x = self.fc2(x)
    return x

cnn = model(1)
</code></pre>
<p>Since the kernel size is <code>1</code> and the output channel is <code>32</code>, I assume that there should be <code>32*1*1</code> weights in this layer. But, when I ask <code>pytorch</code> about the shape of the weight matrix <code>cnn.conv1.weight.shape</code>, it returns <code>torch.Size([32, 4, 1, 1])</code>. Why the number of input channel should matter on the weight of a <code>conv2d</code> layer?</p>
<p>Am I missing something?</p>

Nikronic: <p>Hi,</p>
<p>I have explained this idea here, <a href="https://discuss.pytorch.org/t/why-add-an-extra-dimension-to-convolution-layer-weights/86954/2" class="inline-onebox">Why add an extra dimension to convolution layer weights?</a></p>
<p>Bests</p>

afshin67: <p>Thanks, that post cleared my question.</p>
Error in loading the model weights on colab even though no changes to the model

Aditya_Kapoor: <p>Hey guys, I have trained a model on a remote workstation and pulled its weights (.pt file) onto my local machine. I am using colab to test the model however I face the following error:</p>
<p>RuntimeError: Error(s) in loading state_dict for CentralizedActorCritic:<br>
size mismatch for value1.weight: copying a param with shape torch.Size([256, 6]) from checkpoint, the shape in current model is torch.Size([256, 18]).<br>
size mismatch for policy1.weight: copying a param with shape torch.Size([256, 6]) from checkpoint, the shape in current model is torch.Size([256, 18]).</p>
<p>The model specifications on both colab and the remote workstation are the same. Basically, I am running the exact same code but unable to load the trained weights on colab.<br>
Thanks <img src="https://discuss.pytorch.org/images/emoji/apple/smiley.png?v=9" title=":smiley:" class="emoji" alt=":smiley:"></p>

ptrblck: <p>Could you create the model locally and on Colab before loading the <code>state_dict</code> and print the shapes of these conflicting parameters?<br>
Are you using some conditions to create the model, which might differ on the platforms?</p>

Aditya_Kapoor: <p>Hey, I got it solved. It was some condition to do with the creation of the model! Thanks <img src="https://discuss.pytorch.org/images/emoji/apple/smiley.png?v=9" title=":smiley:" class="emoji" alt=":smiley:"></p>
Any reason to use .data on Tensor when converting?

bfeeny: <p>I have seen code that has like this:</p>
<p><code>  data_predict.extend(train_predict.cpu().data.numpy().tolist())</code></p>
<p>where data_predict is a List, and train_predict is a Tensor.  My understanding is that .data is used to extract the Tensor from a Variable, and that if the object is already a Tensor, there is no purpose for it in this manner.  Is that correct? Or is there some reason/difference in having .data as above vs omitting it.</p>
<p>Brian</p>

ptrblck: <p><code>Variables</code> are deprecated since PyTorch <code>0.4</code> and you can use tensors now. Also, you should not access the <code>.data</code> attribute directly, as it might yield unwanted side effects.<br>
So you are correct and should remove the <code>.data</code> usage. <img src="https://discuss.pytorch.org/images/emoji/apple/wink.png?v=9" title=":wink:" class="emoji" alt=":wink:"></p>
Should their be any difference in results when running DataParallel?

bfeeny: <p>I have ran a few models using DataParallel, just by simply passing the model to nn.DataParallel() and I have had a range of strange things that happen sometimes.  What I see commonly ,is my graphs (matplotlib, etc) will look different, like WAY different, which makes me think there is something being computed differently.  Other times, I get warnings or errors.  I see most of the problems with LSTM’s, and in searching I saw a few bugs with LSTM’s and DataParallel.  Is it / should it be pretty safe to run things using DataParallel or is it unstable?  I am running 1.5.0a0+8f84ded, which I know there are newer versions, but I am running the NVIDIA NGC image and they haven’t released a new one.  I did update pytorch via pip just to try it out and still had some of the issues so I went back to 1.5.0a0+8f84ded.  Many times models don’t seem all that much faster with DataParallel, not sure if there are any tips to make things faster.  I do use pin_memory and calibrate my dataloader num_workers to find what is fastest.</p>

ptrblck: <p>The training might differ generally, as the gradients will be reduced from all devices to the default device, which would be equivalent to training with a bigger batch size.<br>
Also, if you are using batchnorm layers, their running estimates won’t be synchronized in <code>nn.DataParallel</code>, which might also make a difference.</p>
<p>We recommend to use <code>DistributedDataParallel</code> with a single process per GPU for the best performance. DDP also supports <code>SyncBatchNorm</code>, which might be beneficial.</p>
Crash launching sequentially the same model

wolfviking0: <p>I am using a model with libtorch and everything work fine when I launch the first time.<br>
The module libtorch is inside a c++ class and I can create and delete my object.<br>
The class has a module where I load the model, when I delete I am not sure how to unload the module but I am using CUDACachingAllocator::emptyCache().</p>
<p>If I create/delete my object multiple time in a forloop I have this. behavior:</p>
<p>loop 1 - &gt; load / unload success<br>
loop 2 -&gt; load failed with error<br>
loop 3 -&gt; load / unload success<br>
loop 4 -&gt; load failed with error<br>
loop 5 -&gt; load / unload success …</p>
<p>I am sure its because I am not doing the right thing when I release the module, but I am not able to figure out, any idea why I have this error.</p>
<pre><code class="lang-auto">[ERROR] CUDA error: resource not mapped (launch_kernel at /Volumes/torch_cuda/darwin/src/aten/src/ATen/native/cuda/CUDALoops.cuh:217)
frame #0: c10::Error::Error(c10::SourceLocation, std::__1::basic_string&lt;char, std::__1::char_traits&lt;char&gt;, std::__1::allocator&lt;char&gt; &gt; const&amp;) + 68 (0x112bf2ab4 in libc10.dylib)
frame #1: void at::native::gpu_kernel_impl&lt;__nv_hdl_wrapper_t&lt;false, false, __nv_dl_tag&lt;void (*)(at::TensorIterator&amp;, c10::Scalar), &amp;(at::native::fill_kernel_cuda(at::TensorIterator&amp;, c10::Scalar)), 1u&gt;, unsigned char (), unsigned char&gt; &gt;(at::TensorIterator&amp;, __nv_hdl_wrapper_t&lt;false, false, __nv_dl_tag&lt;void (*)(at::TensorIterator&amp;, c10::Scalar), &amp;(at::native::fill_kernel_cuda(at::TensorIterator&amp;, c10::Scalar)), 1u&gt;, unsigned char (), unsigned char&gt; const&amp;) + 8119 (0x101e08ab7 in libtorch_cuda.dylib)
frame #2: at::native::fill_kernel_cuda(at::TensorIterator&amp;, c10::Scalar) + 1319 (0x101e05177 in libtorch_cuda.dylib)
frame #3: void at::native::DispatchStub&lt;void (*)
</code></pre>

ptrblck: <p>Could you post the code you are using to load and unload the model?<br>
An executable code snippet to reproduce this issue would be even better. <img src="https://discuss.pytorch.org/images/emoji/apple/slight_smile.png?v=9" title=":slight_smile:" class="emoji" alt=":slight_smile:"></p>
Replacing For Loop with indexing

chinmay5: <p>I have a numpy code which I want to convert into PyTorch. It is related to nms but I do not have enough expertise to write a CUDA kernel for it. Can someone please help me with the conversion. Specifically I was hoping to find a way to remove the for loop here. I think if the for loop is replaced, the operation might not be slow.</p>
<pre><code class="lang-auto">def nms(dets, scores, thresh):
    '''
    dets is a numpy array : num_dets, 6
    scores ia  nump array : num_dets,
    '''
    x1 = dets[:, 0]
    y1 = dets[:, 1]
    z1 = dets[:, 2]
    x2 = dets[:, 3]
    y2 = dets[:, 4]
    z2 = dets[:, 5]

    volume = (x2 - x1 + 1) * (y2 - y1 + 1) * (z2 - z1 + 1)
    order = scores.argsort()[::-1]  # get boxes with more ious first

    keep = []
    while order.size &gt; 0:
        i = order[0]  # pick maxmum iou box
        keep.append(i)
        xx1 = np.maximum(x1[i], x1[order[1:]])
        yy1 = np.maximum(y1[i], y1[order[1:]])
        zz1 = np.maximum(z1[i], z1[order[1:]])
        xx2 = np.minimum(x2[i], x2[order[1:]])
        yy2 = np.minimum(y2[i], y2[order[1:]])
        zz2 = np.minimum(z2[i], z2[order[1:]])

        w = np.maximum(0.0, xx2 - xx1 + 1)  # maximum width
        h = np.maximum(0.0, yy2 - yy1 + 1)  # maxiumum height
        l = np.maximum(0.0, zz2 - zz1 + 1)  # maxiumum length
        inter = w * h * l
        ovr = inter / (volume[i] + volume[order[1:]] - inter)

        inds = np.where(ovr &lt;= thresh)[0]
        order = order[inds + 1]

    return keep
</code></pre>

chinmay5: <p>A discussion about the same is continued <a href="https://discuss.pytorch.org/t/non-max-suppression-using-pytorch/86931">here</a>. Please refer to it for clarifications.</p>
Finding the indexing code

dfalbel: <p>It’s totally a dum question, sorry!,<br>
But I couldn’t find where the indexing code is defined for the Torch tensor.<br>
Could someone point me to that?</p>

ptrblck: <p>The CPU indexing should be found <a href="https://github.com/pytorch/pytorch/blob/f1406c43fc869211d477f853d162afc40f40653e/aten/src/ATen/TensorIndexing.cpp">here</a>, which would call into <a href="https://github.com/pytorch/pytorch/blob/09660896c0dd2bec888857300a7be9edb52dd05d/aten/src/ATen/TensorIndexing.h#L478"><code>get_item</code></a>.</p>
Why 'CUDA error: device-side assert triggered'?

oasjd7: <p>sometimes <code>CUDA error: device-side assert triggered</code> occurs. with this.<br>
<div class="lightbox-wrapper"><a class="lightbox" href="https://discuss.pytorch.org/uploads/default/original/3X/1/2/127c498f72736c16f10fed3e195e6f45eb358149.png" data-download-href="https://discuss.pytorch.org/uploads/default/127c498f72736c16f10fed3e195e6f45eb358149" title="image"><img src="https://discuss.pytorch.org/uploads/default/original/3X/1/2/127c498f72736c16f10fed3e195e6f45eb358149.png" alt="image" data-base62-sha1="2DwSuoXfdIJabmm7HPX4BXA9i4N" width="690" height="42" data-dominant-color="3F403A"><div class="meta"><svg class="fa d-icon d-icon-far-image svg-icon" aria-hidden="true"><use href="#far-image"></use></svg><span class="filename">image</span><span class="informations">963×60 4.15 KB</span><svg class="fa d-icon d-icon-discourse-expand svg-icon" aria-hidden="true"><use href="#discourse-expand"></use></svg></div></a></div></p>
<p>The error indicates <code>loss = torch.tensor(0).float().cuda()</code>.</p>
<pre><code class="lang-auto">This is my code.
loss = torch.tensor(0).float().cuda()
...get loss_A
loss += loss_A
</code></pre>
<p>What`s wrong in my code?</p>

harsha_g: <p>I don’t think <code>loss = torch.tensor(0).float().cuda()</code> is needed. <code>loss = 0</code> will suffice. Did you try just that?</p>

ptrblck: <p>The error message points to a target values, which is out of bounds:</p>
<pre><code class="lang-python">Assertion `cur_target &gt;= 0 &amp;&amp; cur_target &lt; n_classes` failed
</code></pre>
<p>Make sure the target contains values in the range <code>[0, nb_classes-1]</code> for <code>nn.CrossEntropyLoss</code> or <code>nn.NLLLoss</code>.</p>
Suppress scientific notation libtorch

jamesbut: <p>Does anybody know how to suppress scientific notation(standard form) in libtorch?</p>

ptrblck: <p>It seems that the format of the C++ API output is defined <a href="https://github.com/pytorch/pytorch/blob/2a698682e45fb67ea8b0fe0fc4c3909dbd5dbb35/aten/src/ATen/core/Formatting.cpp#L95-L124">here</a>, which seems to use a few conditions and uses the fixed formatting.<br>
I don’t see any options you could pass to change the format, but you could create a feature request and exaplain your use case on GitHub. <img src="https://discuss.pytorch.org/images/emoji/apple/slight_smile.png?v=9" title=":slight_smile:" class="emoji" alt=":slight_smile:"></p>

jamesbut: <p>Great, thank you very much for your help <img src="https://discuss.pytorch.org/images/emoji/apple/slight_smile.png?v=9" title=":slight_smile:" class="emoji" alt=":slight_smile:"></p>
Confused by torch.scatter

4158ndfkvBHJ1: <p>Hello everybody,<br>
I have been struggling for a few days now to get the scatter function working as I want. I’m really sorry, I tried to understand the documentation but I failed. My usecase is the following :</p>
<p>I have a [1,height, width] sized tensor, it’s a semantic mask for a segmentation network. Every pixel takes a value between 0 and 6.</p>
<p>If I try to understand the documentation (I’m using the out of place version of scatter) :</p>
<ul>
<li>
<p>the index tensor tells the scatter function at which <em>index</em> it’s supposed to move the values of the src tensor. In my case, I’m expected the scattered tensor to go from size [1,height, width] with values inbetween 0 and 6 to go to size [7, height, width] with only 2 values, 0 and 1. In my case, is the segmentation mask the index ? That’s what I was thinking because if a pixel takes the value 3, then It should stay in the same (x,y) place, but be moved to the third index along the 0th dimension of the new, scattered tensor.</p>
</li>
<li>
<p>The dim argument tells the function along which dimension to scatter the values. In my case, I think I’m pretty sure it’s 0 (channels first representation for torch.nn.BCELossWithLogits()).</p>
</li>
<li>
<p>I’m confused by the src argument. Should I make an additional torch.ones() tensor that contains the value that will be moved according to src ?</p>
</li>
</ul>
<p>In the end, based on my understanding, I wrote the following code :<br>
x = torch.scatter(dim = 0, index = semantic_mask, src = torch.ones(semantic_mask.size()))</p>
<p>with the size argument of torch.ones based on the fact that index and src should have the same size according to the documentation, but that doesn’t work. Overall it’s a bit stupid but I’m really stuck here.</p>

ptrblck: <p>Your workflow sounds right and this code snippet should work:</p>
<pre><code class="lang-python">h, w = 3, 3
mask = torch.randint(0, 7, (1, h, w))

res = torch.zeros(7, h, w)
res.scatter_(0, mask, 1.)
</code></pre>
<p>Could you check it locally and verify, that the result contains the expected values?</p>

4158ndfkvBHJ1: <p>Yes, it works fine, thanks a lot for your help !</p>
Create batch of size 1 to test trained model

shwe87: <p>Hi,</p>
<p>I have a trained model loaded with</p>
<pre><code class="lang-auto">model = torch.load(...)
</code></pre>
<p>I want to create like an app to take an input (which is an spectrogram of a recorded audio) but the input should be in shape like (batch, height, width)<br>
I only have (H, W) but would want something like (1, H, W).</p>
<p>How can I do this? Thanks a lot.</p>

harsha_g: <p>Assuming input is (H, W), <code>input = input.unsqueeze(0)</code> will make it (1, H, W)</p>

shwe87: <p>Thank you so much!!  <img src="https://discuss.pytorch.org/images/emoji/apple/slight_smile.png?v=9" title=":slight_smile:" class="emoji" alt=":slight_smile:"></p>
Error installing pytorch via pip

Wai_Hing_Chung: <p>Running setup.py install for torch … error<br>
Complete output from command c:\python27\python.exe -u -c “import setuptools, tokenize;<strong>file</strong>=‘c:\users\oriko\appdata\local\temp\pip-install-xkasnp\torch\setup.py’;f=getattr(tokenize, ‘open’, open)(<strong>file</strong>);code=f.read().replace(’\r\n’, ‘\n’);f.close();exec(compile(code, <strong>file</strong>, ‘exec’))” install --record c:\users\oriko\appdata\local\temp\pip-record-cqwvhb\install-record.txt --single-version-externally-managed --compile:<br>
running install<br>
running build_deps<br>
Traceback (most recent call last):<br>
File “”, line 1, in <br>
File “c:\users\oriko\appdata\local\temp\pip-install-xkasnp\torch\setup.py”, line 265, in <br>
description=“Tensors and Dynamic neural networks in Python with strong GPU acceleration”,<br>
File “c:\python27\lib\site-packages\setuptools_<em>init</em>_.py”, line 143, in setup<br>
return distutils.core.setup(**attrs)<br>
File “c:\python27\lib\distutils\core.py”, line 151, in setup<br>
dist.run_commands()<br>
File “c:\python27\lib\distutils\dist.py”, line 953, in run_commands<br>
self.run_command(cmd)<br>
File “c:\python27\lib\distutils\dist.py”, line 972, in run_command<br>
cmd_obj.run()<br>
File “c:\users\oriko\appdata\local\temp\pip-install-xkasnp\torch\setup.py”, line 99, in run<br>
self.run_command(‘build_deps’)<br>
File “c:\python27\lib\distutils\cmd.py”, line 326, in run_command<br>
self.distribution.run_command(command)<br>
File “c:\python27\lib\distutils\dist.py”, line 972, in run_command<br>
cmd_obj.run()<br>
File “c:\users\oriko\appdata\local\temp\pip-install-xkasnp\torch\setup.py”, line 51, in run<br>
from tools.nnwrap import generate_wrappers as generate_nn_wrappers<br>
ImportError: No module named nnwrap</p>
<pre><code>----------------------------------------
</code></pre>
<p>Command “c:\python27\python.exe -u -c “import setuptools, tokenize;<strong>file</strong>=‘c:\users\oriko\appdata\local\temp\pip-install-xkasnp\torch\setup.py’;f=getattr(tokenize, ‘open’, open)(<strong>file</strong>);code=f.read().replace(’\r\n’, ‘\n’);f.close();exec(compile(code, <strong>file</strong>, ‘exec’))” install --record c:\users\oriko\appdata\local\temp\pip-record-cqwvhb\install-record.txt --single-version-externally-managed --compile” failed with error code 1 in c:\users\oriko\appdata\local\temp\pip-install-xkasnp\torch\</p>
RuntimeError: CUDA out of memory. Only 1.55 MiB free

JacksonD2: <p>I am using an RTX 2060, trying to run a tacotron2 model. For reference I am using these versions of these python libraries</p>
<p>matplotlib==2.1.0<br>
tensorflow==1.15.2<br>
numpy==1.13.3<br>
inflect==0.2.5<br>
librosa==0.6.0<br>
scipy==1.0.0<br>
Unidecode==1.0.22<br>
pillow</p>
<p><code>RuntimeError: CUDA out of memory. Tried to allocate 2.00 MiB (GPU 0; 6.00 GiB total capacity; 3.77 GiB already allocated; 1.55 MiB free; 3.80 GiB reserved in total by PyTorch)</code></p>
<p>I have no idea how to fix this, other posts of this problem had a substantially larger amount of free memory.</p>

Scott_Hoang: <p>reduce your batch size.</p>
How to overfit on a single batch

Droid786: <p>I am trying to classify certain categories of images.<br>
I read from various places to overfit on a single batch to find the errors.<br>
but I don’t know how to overfit on a single batch?</p>

Nikronic: <p>Hi,</p>
<p>If you run a deep (depends on data) model for many epochs for a small proportion of data, like a batch of data, model will overfit. Overfitting means that model focused too much on that small data that just memorized everything.</p>
<p>Bests</p>
Procedure for importing .py script

shangguan91: <p>quite frustrated,  i am having problems, implementing GCN model.</p>
<p>firstly, i have download those init.commits, upload to colab and then run by !python …py<br>
then i have to install packages. After i finished all , the last script give me error of re not defined.<br>
i imported re, still do not work… help, is my procedure right?</p>
<pre><code class="lang-auto">from imports import *
from callbacks import *
from nn import *
from support_functions import *
</code></pre>

ptrblck: <p>Could you post the error message you are getting?<br>
Your currently posted code doesn’t seem to use any PyTorch-specific modules, so I assume you are struggling with Python imports in general?</p>

shangguan91: <p>i have already import re```</p>
<blockquote>
<p>/content/3D/support_functions.py in ()<br>
72     return _inner<br>
73<br>
—&gt; 74 _camel_re1 = re.compile(‘(.)([A-Z][a-z]+)’)<br>
75 _camel_re2 = re.compile(‘([a-z0-9])([A-Z])’)<br>
76 def camel2snake(name):</p>
</blockquote>
<p>NameError: name ‘re’ is not defined</p>
<pre><code class="lang-auto"></code></pre>

ptrblck: <p>It seems that the <code>import re</code> is missing in <code>/content/3D/support_functions.py</code>.<br>
Could you add it there and rerun the script?<br>
I assume that you are importing some functions from <code>support_functions</code>. Is that correct?</p>
Can quantizing models enable you to have bigger batch sizes during inference?

miken: <p>I have a model in production. I want to be able to increase my model’s throughput for speed reasons. I’ve tried quantizing the model but for some reason, if increase the batch size I still run into an OOM error. I thought that quantizing the model from fp32 to say fp16 would allow the bigger batch sizes? If that’s not the case what is the use case of quantizing models?</p>

supriyar: <p>Quantizing model will enable to run the model at lower precision (int8) so it runs faster. Also since the tensors as quantized to 8 bit they will occupy less storage space as well.</p>
MSE loss for multi-class problem

netaglazer: <p>i have a multi class problem and i want to use MSE loss<br>
i have weights, so the loss is:</p>
<pre><code class="lang-auto">def weighted_mse_loss(input,target,weights):
    out = (input-target)**2
    out = out * weights.expand_as(out)
    loss = out.sum(0)
    return loss
</code></pre>
<p>but now the loss is a tensor with 61 length (i have 61 classes)<br>
and i get error:</p>
<p>“grad can be implicitly created only for scalar outputs”</p>

KFrank: <p>Hi Neta!</p>
<aside class="quote no-group" data-username="netaglazer" data-post="1" data-topic="86853" data-full="true">
<div class="title">
<div class="quote-controls"></div>
<img loading="lazy" alt="" width="24" height="24" src="https://discuss.pytorch.org/letter_avatar_proxy/v4/letter/n/f1d935/48.png" class="avatar"> netaglazer:</div>
<blockquote>
<p>i have a multi class problem and i want to use MSE loss</p>
</blockquote>
</aside>
<p>As an aside, you probably <em>don’t</em> want to use <code>MSELoss</code> for a<br>
multi-class problem.</p>
<aside class="quote no-group">
<blockquote>
<pre><code class="lang-auto">    loss = out.sum(0)
</code></pre>
<p>but now the loss is a tensor with 61 length (i have 61 classes)<br>
and i get error:</p>
<p>“grad can be implicitly created only for scalar outputs”</p>
</blockquote>
</aside>
<p><code>out.sum(0)</code> is summing over the batch dimension of your<br>
<code>input</code> / <code>target</code> (even if your batch size is 1).</p>
<p>Given what you say, I speculate that <code>input</code> (the output of your<br>
model) is a vector of 61 values, one for each class (for a single<br>
sample in your batch), and that <code>target</code> is also a vector of 61<br>
values (perhaps your class labels one-hot encoded).</p>
<p>If you want to do this (and your probably don’t), you should be<br>
using <code>out.sum()</code> to sum over all elements of the <code>out</code> tensor,<br>
that is, over both the batch and class dimensions.</p>
<p>Best.</p>
<p>K. Frank</p>

netaglazer: <p>you mean i should return loss.sum() ?</p>

KFrank: <p>Hello Neta!</p>
<aside class="quote no-group" data-username="netaglazer" data-post="3" data-topic="86853" data-full="true">
<div class="title">
<div class="quote-controls"></div>
<img loading="lazy" alt="" width="24" height="24" src="https://discuss.pytorch.org/letter_avatar_proxy/v4/letter/n/f1d935/48.png" class="avatar"> netaglazer:</div>
<blockquote>
<p>you mean i should return loss.sum() ?</p>
</blockquote>
</aside>
<p>What I really mean is that you should reorganize your problem a<br>
little bit and use <code>BCEWithLogitsLoss</code>.</p>
<p>However, if you have a good reason to be using <code>MSELoss</code> (and I’m<br>
right that your <code>input</code> and <code>target</code> have shape <code>[nBatch, nClass]</code>),<br>
then, yes, you should return <code>out.sum()</code> (rather than <code>out.sum (0)</code>)<br>
so that you will be summing over classes as well as the samples in<br>
your batch.</p>
<p>Best.</p>
<p>K. Frank</p>
Can't find an equivelant to tensor.expand_dims in pytorch

Abdulrahman-Khalid: <p>I am implementing a pairwise distance function and I can’t find an pytorch equivalent to do the following</p>
<pre><code class="lang-auto">    # ||a - b||^2 = ||a||^2  - 2 &lt;a, b&gt; + ||b||^2
    distances = tensor.expand_dims(square_norm, 0) - 2.0 * dot_product + tensor.expand_dims(square_norm, 1)
</code></pre>

Abdulrahman-Khalid: <pre><code class="lang-auto">    distances = torch.unsqueeze(square_norm, 0) - 2.0 * dot_product + torch.unsqueeze(square_norm, 1)
</code></pre>
<p>worked for me</p>
Converting a numpy dtype to torch dtype

Valiox: <p>I’d like to know the torch dtype that will result from applying <code>torch.from_numpy(array)</code> without actually calling this function. Since torch and numpy dtypes are incompatible (e.g. doing <code>torch.zeros(some_shape, dtype=array.dtype)</code> will yield an error), how can I do that?</p>

ptrblck: <p>I’m not sure, if <a href="https://github.com/pytorch/pytorch/blob/29881c7f02ed7cb263a602f73a84089a79571c8e/torch/csrc/utils/tensor_numpy.cpp#L198" rel="nofollow noopener">this method</a> is exposed in the Python API, but you might create a custom <code>dict</code> mapping the numpy dtypes to PyTorch ones.</p>

mruberry: <p>Fun fact, we actually have such a dict in our test suite now: <a href="https://github.com/pytorch/pytorch/blob/e180ca652f8a38c479a3eff1080efe69cbc11621/torch/testing/_internal/common_utils.py#L349" rel="nofollow noopener">https://github.com/pytorch/pytorch/blob/e180ca652f8a38c479a3eff1080efe69cbc11621/torch/testing/_internal/common_utils.py#L349</a>.</p>
Is there any sort of GPU scheduler / dispatcher?

bfeeny: <p>I know I can select which cuda device to run my code on (cuda:0 cuda:1, etc).  But is it possible I can do something so that pytorch looks at what is the least used GPU and runs it there?  Sometimes I like to have multiple models running at a time and I want to avoid manually checking nvidia-smi and editing my code to update the device.</p>
Create structural tensor view based on pointers

weidler: <p>The goal is to create a doubly circulant matrix based on an original kernel tensor. This operation is costly, suffering particularly from slow rolling. Problematically, the operation in theory needs to be repeated every step because the original tensor is changing. However, the process of restructuring the tensor will be always the same.</p>
<p>Is it possible to create a tensor as only a view on the original tensor, such that as the original tensor changes using due to optimization, the values in the “view tensor” also change and thus the restructuring does not need to be repeated? (and just to clarify, by “view” I do not refer to torch.view() but to view in some sort of database sense).</p>