akankshakshurai/cuda-error-resolution-analysis huggingface dataset:
Is there a way to insert data augmentation into the training data based on the validation data?

AndreTeixeira: <p>I’m training a CNN for plant diseases recognition and the training set is composed by laboratory images. I am achieving low precision in my validation and test sets as they are composed of images taken in the field and which contain an extremely varied dynamic range. Is there a way to introduce data augmentation on my training set based on the validation set in order to get a better acurracy?</p>

Nikronic: <p>Hi,</p>
<p>I am not sure I have understood your question properly, but I think the problem is for instance, train set has normal images, but val set has rotated images or many other issues that differs from train set and you want to introduce some augmentations like rotation to make training set more similar to val.</p>
<p>If it is that case, I have to mention a few points:</p>
<ol>
<li>Introducing any new statistical changes to training based on validation set is some kind of cheating as in real world, we know that we are going to test our algorithm against samples that it has never seen. So actually, you have seen val set, tested your model got output and now you know model is not working well. So, I think your idea is not really acceptable to change train set to be similar to val set.</li>
<li>Why not shuffling lab and field images? create a new train set that has samples in val set.</li>
<li>I am not sure about this but how about adding all type of augmentations to make sure all possible transformation can be learned by network.</li>
<li>I thing there was a paper called <em>auto augment</em> which tried to find best augmentation based on data.</li>
</ol>
<p>Bests</p>
Pytorch set parameters (weight and bias) of layers

Fugurcali: <p>I have constructed a network using C++ API. I want to assign my network layers to specific weight and bias that I get from pretrained tensorflow model. How can I achieve this? One of my layer declaration is as follows:</p>
<pre><code class="lang-auto">    torch::nn::Sequential conv1_1{
        torch::nn::Conv2d(torch::nn::Conv2dOptions(3,64,3).stride(1).padding(1).bias(true)),
        torch::nn::ReLU()
    };
</code></pre>
<p>For now I am assigning parameters randomly as follows:</p>
<pre><code class="lang-auto">    torch::NoGradGuard no_grad;
    for (auto&amp; p : this-&gt;parameters()){
        p.uniform_(-1,1);
    }
</code></pre>
<p>How can I assign a tensor as weight to a layer?</p>

Fugurcali: <p>I know that <code>model-&gt;conv1_1-&gt;parameters()[0]</code> represents the weights and <code>model-&gt;conv1_1-&gt;parameters()[1]</code> represents the bias for conv1_1. But I can’t set weights with:</p>
<pre><code class="lang-auto">torch::Tensor conv_wght = torch::zeros({64, 3, 3, 3},torch::TensorOptions().dtype(torch::kFloat32).device(torch::kCUDA, 0));
model-&gt;conv1_1-&gt;parameters()[0] = conv_wght;
</code></pre>
<p>Anyone can give me a clue?</p>

Fugurcali: <pre><code class="lang-auto">for (const auto&amp; pair : this-&gt;conv1_1-&gt;parameters()) {
        pair.copy_(in); // in is torch::Tensor type
    }
</code></pre>
<p>copy_() solves my problem. Just be careful about in tensor size since this loop will also try to update bias.</p>
A question on detach() in DQN loss

Maziar: <p>Hi,</p>
<p>In Deep-Q-Networks we compute the loss as:</p>
<pre><code class="lang-auto">loss = Q(s,a) - (r + gamma * maxQ'(s', a'))
</code></pre>
<p>So in order to calculate values of Q(s,a) and maxQ’(s’,a’), we need to do to a forward pass on the model two times. If I understood correctly this would create two different computation graphs for each forward pass.</p>
<p>So my question is whether I “have to” detach() the resulting value of maxQ’(s’,a’) before doing the backward pass. Does it lead to errors if I don’t, and why?</p>

liuruiqi1107: <p>You can write your code in this way to avoid the unwanted grad calculation:</p>
<pre><code class="lang-auto">Q(s, a) = your_model(s, a)
with torch.no_grad():
    maxQ'(s',a') = your_model(s', a')
</code></pre>
<p>Then the forward calculation of maxQ will not accumulate grads on parameters of your model.</p>

Maziar: <p>Thanks Liu.</p>
<p>However, my question is mostly about if I have to do that, and whether it would produce errors if I don’t, and why. (in a situation where we have x and y, both resulting from forward passes and both have gradients and now we want to backprop loss(x, y))</p>

Maziar: <p>Any help/pointers/references on this topic is still appreciated.</p>

liuruiqi1107: <p>You are welcome, yes you have to do that either by using ‘with torch.no_grad()’ or ‘.detach()’, otherwise your gradient is not accumulated correctly with respect to the loss.</p>
Torch.load() error

111342: <p>when I use the function of torch.load(), get a error, as follow:</p>
<p>Traceback (most recent call last):<br>
File “/home/xllv/proj/Demo/load.py”, line 7, in <br>
vocab = torch.load("/home/xllv/proj/Demo/vocab.test")<br>
File “/home/xllv/bin/anaconda3/lib/python3.7/site-packages/torch/serialization.py”, line 387, in load<br>
return _load(f, map_location, pickle_module, **pickle_load_args)<br>
File “/home/xllv/bin/anaconda3/lib/python3.7/site-packages/torch/serialization.py”, line 564, in _load<br>
magic_number = pickle_module.load(f, **pickle_load_args)<br>
_pickle.UnpicklingError: unpickling stack underflow</p>
<p>my code just have one line as follow:</p>
<p>vocab = torch.load(“vocab.test”)</p>

harsha_g: <p><a href="https://discuss.pytorch.org/t/unpickling-stack-underflow/68181">https://discuss.pytorch.org/t/unpickling-stack-underflow/68181</a></p>
Getting `__half*` out of an fp16 tensor?

sharvil: <p>Is there a clean way to get a native pointer out of an fp16 tensor inside <code>AT_DISPATCH_FLOATING_TYPES_AND_HALF</code>? For other floating point types (fp32, fp64), I can use <code>tensor.data_ptr&lt;scalar_t&gt;()</code> and get the right native pointer type out. For fp16, I get back <code>c10::Half*</code> which isn’t type-compatible with <code>__half*</code>.</p>
<p>The best workaround I’ve come up with is:</p>
<pre><code class="lang-auto">template&lt;typename U&gt;
struct native_type {
  using T = U;
};

template&lt;&gt;
struct native_type&lt;c10::Half&gt; {
  using T = __half;
};

template&lt;typename U&gt;
typename native_type&lt;U&gt;::T* ptr(Tensor t) {
  return reinterpret_cast&lt;typename native_type&lt;U&gt;::T*&gt;(t.data_ptr&lt;U&gt;());
}
</code></pre>
<p>And then I’d call <code>ptr&lt;scalar_t&gt;(tensor)</code> every place I would normally call <code>tensor.data_ptr&lt;scalar_t&gt;()</code>. I also have to use <code>typename native_type&lt;scalar_t&gt;::T</code> for all template arguments that need <code>__half</code> instead of <code>c10::Half</code>.</p>
<p>Is there a better alternative or existing code in PyTorch that I can reuse?</p>
Resnet101 pretrained backbone on COCO

negreanu: <p>I have been searching for Resnet101 pretrained weights on COCO dataset but I haven’t found anything, does anyone know where can I find them?</p>

Nikronic: <p>Hi,<br>
It is just in the same package as other pretrained networks are, <a href="https://pytorch.org/docs/stable/torchvision/models.html#torchvision.models.segmentation.fcn_resnet101" rel="nofollow noopener">torchvision.models</a></p>
<p>Bests</p>
Non bmm batch matrix product operation

Mark_Gawron: <p>In exploring a word2vec variant, I’d like to find a way to do the following bmm like operation which bmm does not seem suited for. So far I can only write it with a for-loop. Is there a way to do this with pytorch tensor operations?</p>
<pre><code class="lang-auto">def combine(ivectors, ovectors):
    (batch_sz, sense_sz, context_sz, embedding_sz) = ovectors.shape
    LL = torch.zeros((batch_sz, sense_sz, context_sz))
    for i in range(batch_sz):
        LL[i,:,:] = torch.matmul(ovectors[i,:,:,:], ivectors[i,:].T)
    return LL
</code></pre>
<p>My problem seems to boil down to this:  I want to take an input tensor of shape, say, 2x2x3x5 (ovectors) and combine it with a tensor of shape 5x2 (ivectors.T) and output a tensor of shape (2,2,3).  What bmm and matmul seem to be designed for is a reduction that gives (2,2,3,2), 24 elements, but I want the reduction in the above for-loop, which gives only 12.</p>
<p>Is there a way to get rid of the for loop?</p>

googlebot: <p>einsum can express any reductions, perhaps what you want looks:</p>
<pre><code class="lang-auto">torch.einsum("bsce,be-&gt;bsc", ovectors, ivectors)
</code></pre>
<p>Matmul for this reduction can be done with shapes (2,2,3,5) @ (2,1,5,1) = (2,2,3,1) (this does bmm with two first dimensions joined&amp;broadcasted)</p>
<pre><code class="lang-auto">torch.matmul(ovectors, ivectors.reshape(batch_sz,1,embedding_sz,1)).squeeze(-1)
</code></pre>

Mark_Gawron: <p>Thanks. That works and your explanation is very helpful!</p>
Custom backward with staticmethod

maralm: <p>I have the following structure in my code to use custom backward:</p>
<pre><code class="lang-auto">class myclass(Function):

    @staticmethod
    def forward(ctx, inputs, model_cpu):
        
        ubatches =  embedding(inputs)
        sequence_output_list, output_cpu_list = bert(ubatches)
        pooled_output_list = pooler(sequence_output_list)  
        loss_list = classifier(pooled_output_list)

        ctx.args = args
        ctx.model_cpu = model_cpu

        return torch.stack(loss_list,dim=0).clone()#, output_cpu_list, bert_output  # (loss), logits, (hidden_states), (attentions)

    @staticmethod
    def backward (ctx, grad_hidden_states):
        print ('backward')
        model_cpu = ctx.model_cpu
        args = ctx.args

        model_cpu, claasifier_input_grads = classifier.backward(args, model_cpu, grad_hidden_states)
        model_cpu, BertModel_input_grads = bert.backward (args, model_cpu, claasifier_input_grads)
        model_cpu, Embedding_input_grads = embedding.backward (args, model_cpu, BertModel_input_grads)

        return model_cpu, None



class myclassRun(BertPreTrainedModel):

    def __init__(self, config):
        super(BertForSequenceClassificationL2LRun, self).__init__(config)
        #self.seq_classifier = BertForSequenceClassificationL2L_new (config)
        self.embedding = BertEmbedding(config)
        self.bert = BertModel(config)
        self.pooler = BertPooler(config)
        self.bertclassifier = BertClassifier(config)

    def forward (self, inputs, model_cpu):

        outputs = myclass.apply (inputs, model_cpu)

        print (outputs.grad_fn)
        print (outputs)
        return outputs
</code></pre>
<p>and each class defined in init has the following structure:</p>
<pre><code class="lang-auto">class BertEmbedding(BertPreTrainedModel):
    def __init__(self, config):
        super().__init__(config)
   
    def forward(self, inputs):    

    def backward(self, ):
        
</code></pre>
<p>In my code the custom backward is not called. Any idea how I can debug or any better way to restructure? My goal is to have loss.backward () as my final backward call.</p>

albanD: <p>Hi,</p>
<p>Which <code>backward()</code> is not called? The one from the Function?<br>
If your <code>BertEmbedding</code> is an <code>nn.Module</code>, then it is expected that it won’t be called.</p>

maralm: <p>when I call loss.backward(), I expect to go through the “myclassRun”'s backward which supposed to call “myclass” backward function which is in staticmethod but this staticmethod backward is not called. In that function, I call the custom backward for BertEmbedding.</p>

albanD: <p>And what are the prints in your Module’s forward printing?<br>
If you do <code>outputs.sum().backward()</code> there, it should call your custom backward just fine right?</p>
Selecting a set of rows from a 2D tensor

nowyouseeme: <p>I have a large tensor of size <code>[80000, 300]</code>. I want to select a set of n rows from this 2D tensor. So the resultant tensor should have a shape <code>[n, 300]</code>. I looked at <a href="https://discuss.pytorch.org/t/select-rows-from-a-2d-tensor/20839">this</a> similar topic, and it proposed a very simple solution of using <code>torch.ByteTensor</code>. However my problem is I can not initialise that large ByteTensor.</p>
<p>I tried create a ByteTensor for indexing the main 2D tensor as follows. I first create a zero tensor of size 80000, then I fill the positions of required rows with 1’s. For example, If I want to select the 1st, 5th, and 7th rows, I’d do the following.</p>
<pre><code class="lang-auto">ind = torch.zeros(80000)
ind[0] = 1
ind[4] = 1
ind[6] = 1
</code></pre>
<p>Then how do I obtain the ByteTensor out of this tensor? Simply doing <code>torch.ByteTensor(ind)</code> is raising error.</p>

ptrblck: <p>You could transform the tensor via <code>ind = ind.byte()</code>. However, if your number of indices is small, it should be beneficial to directly index the tensor via:</p>
<pre><code class="lang-python">x = torch.randn(80, 3)
idx = torch.tensor([0, 4, 6])
ret = x[idx]
</code></pre>

nowyouseeme: <p>Thank you so much <a class="mention" href="/u/ptrblck">@ptrblck</a></p>
When call to("cuda"), which one should be called

tgsmdww: <p>We have to.dtype_layout, to.device, to.dtype, to.other. I use gdb to trace and find that it calls to.dtype_layout instead of to.device.</p>
<pre><code class="lang-auto">import torch

a = torch.Tensor([1,2,3]).to("cuda")
</code></pre>
<p><div class="lightbox-wrapper"><a class="lightbox" href="https://discuss.pytorch.org/uploads/default/original/3X/9/6/961d85025fb9deae553a448d6d925b2a555e16e1.png" data-download-href="https://discuss.pytorch.org/uploads/default/961d85025fb9deae553a448d6d925b2a555e16e1" title="image"><img src="https://discuss.pytorch.org/uploads/default/original/3X/9/6/961d85025fb9deae553a448d6d925b2a555e16e1.png" alt="image" data-base62-sha1="lpYRGHT9TLfSzNdpr21H8UrFk2J" width="690" height="329" data-dominant-color="343434"><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">797×381 10.6 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>

ptrblck: <p>The <code>to()</code> method check for all input options. What kind of issue are you seeing?</p>

tgsmdww: <p>If I want to call <code>to.dtype</code> instead of <code>to.dtype_layout</code>, how to code?</p>

ptrblck: <p>If you want to change the <code>dtype</code>, you can directly pass the wanted <code>dtype</code> to the <code>to()</code> call:</p>
<pre><code class="lang-python">x = x.to(torch.float32)
x = x.to(torch.long)
</code></pre>
<p>or call the “dtype method” on the tensor:</p>
<pre><code class="lang-python">x = x.float()
x = x.long()
</code></pre>

tgsmdww: <p>Sorry, I’m wrong. When I use <code>tensor.to(torch.long)</code> it will call to.dtype. But When I call <code>tensor.to("cuda")</code> it will call to.dtype_layout instead of to.device.</p>
What am I doing wrong here?

Hiperdyne19012: <p>I’m getting this error: Unhandled exception at 0x00007FFC0F5CA799 in example-app.exe: Microsoft C++ exception: c10::Error at memory location 0x0000007EF84FC760.</p>
<p>I want to to max pooling of a random tensor like this:<br>
torch::Tensor det = torch::rand({ 512, 512 });<br>
torch::Tensor maxm = torch::max_pool2d(det, { 5, 5 }, {}, 2 );</p>

Hiperdyne19012: <p>Fixed it using 	torch::Tensor det = torch::rand({ 1,512,512 });<br>
instead of torch::Tensor det = torch::rand({ 512, 512 });<br>
also added torch::NoGradGuard no_grad_guard;  line right after for extra measure.</p>
Converting darknet pretrained weights to pytorch weights

ketulgupta: <p>I want to use the pretrained weights from Darkent library (<a href="https://github.com/pjreddie/darknet" rel="nofollow noopener">https://github.com/pjreddie/darknet</a>) in pytorch.</p>
<p>I have made the same network in pytorch, but iam not able to load pretrained weights from darknet .</p>
<p>There is a <a href="https://github.com/marvis/pytorch-caffe-darknet-convert" rel="nofollow noopener">https://github.com/marvis/pytorch-caffe-darknet-convert</a> but it is removed.</p>
<p>How can i convert the pretrained weights?</p>
<p>Please suggest.</p>
Tensorboard and RNNs

Alec_Wright: <p>Hi, I’ve been trying to get Tensorboard working for some recurrent models. I want to visualise the graph for a model, but I have this issue where when I try and use .add_graph, TensorBoard returns:</p>
<p>‘RuntimeError: Cannot insert a Tensor that requires grad as a constant. Consider making it a parameter or input, or detaching the gradient’</p>
<p>As far as I can tell, its complaining about the hidden state of the Recurrent unit. I can make the error go away if I tell the hidden state to detach during ‘forward()’, but then the network is unable to train. Besides, the network is able to run and update parameters properly, so I don’t really see why Tensorboard should be unable to create a graph from it. Thanks! Code is below.</p>
<pre><code class="lang-auto">import torch
from torch.utils.tensorboard import SummaryWriter
import torch.nn as nn

class Net(nn.Module):
    def __init__(self):
        super(Net, self).__init__()

        self.rec1 = nn.RNN(1, 10, 1, bias=False)
        self.lin1 = nn.Linear(10,1)
        self.hidden = None

    def forward(self, x):

        x, self.hidden = self.rec1(x, self.hidden)
        #self.hidden = self.hidden.detach()
        return self.lin1(x)


if __name__ == '__main__':

    in_data = torch.empty([100, 10, 1])
    tgt_data = torch.empty([100, 10, 1])

    network = Net()
    loss_fcn = nn.MSELoss()
    optimizer = torch.optim.Adam(network.parameters(), 0.0005)

    network.hidden = torch.empty([1, 10, 10])
    out = network(in_data)

    loss = loss_fcn(out, tgt_data)
    loss.backward()
    optimizer.step()

    writer = SummaryWriter('runs/test_icicles')
    writer.add_graph(network, in_data)
</code></pre>
Lasagne.layers.get_output equivalent method in pytorch

ahmadreza9: <p>Hello,<br>
I stuck in method <code>lasagne.layers.get_output</code> (<a href="https://lasagne.readthedocs.io/en/latest/user/layers.html" rel="nofollow noopener">https://lasagne.readthedocs.io/en/latest/user/layers.html</a>) in Theano library. Could you explain what is similar function of that in PyTorch?</p>
<pre><code class="lang-auto">l_in = lasagne.layers.InputLayer(
        shape=(None, 1, input_height, input_width),
    )

    l_hid = lasagne.layers.DenseLayer(
        l_in,
        num_units=20,
        # nonlinearity=lasagne.nonlinearities.tanh,
        nonlinearity=lasagne.nonlinearities.rectify,
        # W=lasagne.init.Normal(.0201),
        W=lasagne.init.Normal(.01),
        b=lasagne.init.Constant(0)
    )

    l_out = lasagne.layers.DenseLayer(
        l_hid,
        num_units=output_length,
        nonlinearity=lasagne.nonlinearities.softmax,
        # W=lasagne.init.Normal(.0001),
        W=lasagne.init.Normal(.01),
        b=lasagne.init.Constant(0)
    )
    return l_out
</code></pre>
<pre><code class="lang-auto">prob_act = lasagne.layers.get_output(self.l_out, states)
prob_act[T.arange(N), actions]
</code></pre>

ptrblck: <p>You could use forward hooks to get the output activations of specific layers as seen in <a href="https://discuss.pytorch.org/t/how-can-l-load-my-best-model-as-a-feature-extractor-evaluator/17254/6">this post</a>.<br>
Alternatively you could also return the intermediate activations from some layers directly in your <code>forward</code> method (if you are using a custom model).</p>
<p>Let me know, if this would work.</p>

ahmadreza9: <aside class="quote no-group" data-username="ptrblck" data-post="2" data-topic="85737">
<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/ptrblck/48/1823_2.png" class="avatar"> ptrblck:</div>
<blockquote>
<p>you could also return the intermediate activations from some layers directly in your <code>forward</code> method (if you are using a custom model).</p>
</blockquote>
</aside>
<p>I think this suggestion will be my answer, but why didn’t I think of this? <img src="https://discuss.pytorch.org/images/emoji/apple/sweat_smile.png?v=12" title=":sweat_smile:" class="emoji" alt=":sweat_smile:" loading="lazy" width="20" height="20"></p>
The procedure entry point

Gaju27: <p><strong>After updating pytorch to the latest version facing this issue.</strong></p>
<p>The procedure entry point ?is_variable@Tensorimpl@c10@<span class="mention">@Q3BA_NXZ</span> could not be located<br>
in the dynamic link library</p>
<p><div class="lightbox-wrapper"><a class="lightbox" href="https://discuss.pytorch.org/uploads/default/original/3X/3/4/34adeeffb28e4674d7c41319e2c9a77e74f5bf4c.png" data-download-href="https://discuss.pytorch.org/uploads/default/34adeeffb28e4674d7c41319e2c9a77e74f5bf4c" title="image"><img src="https://discuss.pytorch.org/uploads/default/optimized/3X/3/4/34adeeffb28e4674d7c41319e2c9a77e74f5bf4c_2_690x314.png" alt="image" data-base62-sha1="7w1tJ2Bwox6YhLJxOMsSxrsmpcg" width="690" height="314" srcset="https://discuss.pytorch.org/uploads/default/optimized/3X/3/4/34adeeffb28e4674d7c41319e2c9a77e74f5bf4c_2_690x314.png, https://discuss.pytorch.org/uploads/default/optimized/3X/3/4/34adeeffb28e4674d7c41319e2c9a77e74f5bf4c_2_1035x471.png 1.5x, https://discuss.pytorch.org/uploads/default/original/3X/3/4/34adeeffb28e4674d7c41319e2c9a77e74f5bf4c.png 2x" data-dominant-color="F3F2F2"><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">1109×505 28.4 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>

janki: <p>Did you manage to resolve this problem? I installed PyTorch for the first time today through Anaconda (using the command “conda install pytorch torchvision cudatoolkit=10.2 -c pytorch”). I then get the similar error displayed below when I try to run any code where I import ‘torch’.</p>
<p><img src="https://discuss.pytorch.org/uploads/default/original/3X/6/7/673c4a24a635cde879b66e805d7284daf657bc19.jpeg" alt="EntryPointNotFound" data-base62-sha1="eJglEZRpInPvVCV7s7oq217oHTb" width="620" height="338"></p>

Gaju27: <p>Create a virtual environment and re-install the dependencies, this will be fixed for sure.</p>
Dimesions in 3D convnet layer

Subhankar_Ghosh: <p>I have a 3D CNN whose initial layers look like:</p>
<pre><code class="lang-auto">(conv1): Conv3d(3, 64, kernel_size=(3, 3, 3), stride=(1, 1, 1), padding=(1, 1, 1))
    (pool1): MaxPool3d(kernel_size=3, stride=2, padding=0, dilation=1, ceil_mode=False)
    (conv2): Conv3d(64, 192, kernel_size=(3, 3, 3), stride=(1, 1, 1), padding=(1, 1, 1))
    (pool2): MaxPool3d(kernel_size=3, stride=2, padding=0, dilation=1, ceil_mode=False)
    (conv3): Conv3d(192, 384, kernel_size=(3, 3, 3), stride=(1, 1, 1), padding=(1, 1, 1))
    (conv4): Conv3d(384, 256, kernel_size=(3, 3, 3), stride=(1, 1, 1), padding=(1, 1, 1))
    (conv5): Conv3d(256, 256, kernel_size=(3, 3, 3), stride=(1, 1, 1), padding=(1, 1, 1))
    (pool5): MaxPool3d(kernel_size=3, stride=2, padding=1, dilation=1, ceil_mode=False)
</code></pre>
<p>Now I have extracted the 1st layer as:</p>
<p><code>cnn_weights = model.state_dict()['module.conv1.weight'].cpu()</code></p>
<p>The shape:<br>
<code>cnn_weights.shape</code></p>
<p>gives:</p>
<blockquote>
<p>torch.Size([64, 3, 3, 3, 3])</p>
</blockquote>
<p>Can you all please help me understand what each of those 5 dimensions represent, i.e. which one is height, depth, width, …</p>
<p>Also in the</p>
<blockquote>
<p>kernel_size=(3, 3, 3)</p>
</blockquote>
<p>what does each of the 3 dimensions represent.</p>
<p>Thanks!</p>

ptrblck: <p>The kernel dimensions are defined as:</p>
<pre><code class="lang-python">[out_channels = number of filters, in_channels, depth, height, width]
</code></pre>
<p>The kernel size is therefore <code>[depth, height, width]</code> of each filter.</p>
Issues with Chatbot NLP tutorial, Average loss not come down and generated sentence with meaningless ending

jobsfan: <p>Hello, I posted an issue on offilce tutorial github.<br>
Pls check <a href="https://github.com/pytorch/tutorials/issues/1029" rel="nofollow noopener">https://github.com/pytorch/tutorials/issues/1029</a></p>
<p>First of all I run Cornell Movie-Dialogs Corpus, it performs good.</p>
<p>Then I changed the corpus to <strong>qingyun</strong>  <a href="https://github.com/Doragd/Chinese-Chatbot-PyTorch-Implementation/blob/master/clean_chat_corpus/qingyun.tsv" rel="nofollow noopener">https://github.com/Doragd/Chinese-Chatbot-PyTorch-Implementation/blob/master/clean_chat_corpus/qingyun.tsv</a>.<br>
It works well too.</p>
<p>At last, I changed the corpus to our own chat corpus, this time it failed, when training peroid, the average loss stop coming down when reach to a high level, around 5%(when I use qingyun corpus it reached 0.32%). Besides the bot’s answer is obvious not correct, not related at all.</p>
<p>Do any one can help? Any parameters I can change, or our own corpus can not be used at all.<br>
Thanks in advance!</p>

ptrblck: <p>While I understand you might get frustrated seeing your use case fail, I would like to ask you to use the proper language in this board, please.</p>
<p>I would recommend to scale down the use case and try to overfit a small data samples (e.g. just 10 samples) by playing around with the hyper-parameters of your training (learning rate, weigth decay etc.).<br>
Once your model can properly overfit the sample, you could try to carefully scale up the use case again by adding more data and observing the training.</p>

jobsfan: <p>Thanks for your reply, sorry for my un-proper words.</p>
Alternate aproach to unbalanced classes

jahan123: <p>So my dataset is very unbalanced:</p>
<p><code>class-1</code>  samples: 76<br>
<code>class-2</code>  samples: 259</p>
<p>now before applying weighted loss function for each class, I want to know does it make any sense to use class <code>weight</code> when dataset is this much unbalanced ? Specially when my<code> batch_size=1</code>.<br>
(I can’t increase batch_size because of my system resources).</p>
<p>are there any other approaches I can use in that case ?</p>
<p>Thankyou</p>

ptrblck: <p>If you are using a batch size of 1, the loss weighting won’t have any effect in the default setup using <code>reduction='mean'</code>, since the weighted mean will be calculated.<br>
Here is a small example showing this behavior:</p>
<pre><code class="lang-python"># Setup
weight = torch.tensor([1., 10.])
criterion = nn.CrossEntropyLoss()
criterion_weighted = nn.CrossEntropyLoss(weight)

# All zeros
x = torch.randn(10, 2)
target = torch.zeros(10).long()
loss0_weighted = criterion_weighted(x, target)
loss0 = criterion(x, target)
print(loss0_weighted - loss0) # 0

# All ones
target = torch.ones(10).long()
loss1_weighted = criterion_weighted(x, target)
loss1 = criterion(x, target)
print(loss1_weighted - loss1) # 0

# Mixed
target = torch.randint(0, 2, (10,))
loss_mixed_weighted = criterion_weighted(x, target)
loss_mixed = criterion(x, target)
print(loss_mixed_weighted - loss_mixed) # difference
</code></pre>
<p>While the first two runs yield the same loss value, as all samples in the batch have the same target, the last one shows a difference.</p>
<p>You might want to use a <code>WeightedRandomSampler</code>, which could draw more samples from the minority class. <a href="https://discuss.pytorch.org/t/how-to-handle-imbalanced-classes/11264/2">Here</a> is an example of its usage.</p>
Model Training Issue in Image denoising

bolt25: <p>I’m currently working on image denoising.<br>
I have written a pretty basic CNN model architecture which has the following architecture:-</p>
<pre><code class="lang-auto">CNNmodel(
  (model): Sequential(
    (0): Conv2d(1, 8, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1))
    (1): BatchNorm2d(8, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
    (2): PReLU(num_parameters=1)
    (3): Conv2d(8, 16, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1))
    (4): BatchNorm2d(16, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
    (5): PReLU(num_parameters=1)
    (6): Dropout(p=0.2, inplace=False)
    (7): Conv2d(16, 32, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1))
    (8): BatchNorm2d(32, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
    (9): PReLU(num_parameters=1)
    (10): Dropout(p=0.2, inplace=False)
    (11): Conv2d(32, 16, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1))
    (12): BatchNorm2d(16, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
    (13): PReLU(num_parameters=1)
    (14): Dropout(p=0.2, inplace=False)
    (15): Conv2d(16, 8, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1))
    (16): BatchNorm2d(8, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
    (17): PReLU(num_parameters=1)
    (18): Dropout(p=0.2, inplace=False)
    (19): Conv2d(8, 1, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1))
    (20): PReLU(num_parameters=1)
  )
)
</code></pre>
<p>Few details of my project-<br>
</p>
<ol>The training data has 5k images and validation data has 500 images</ol>
<ol>Batch size is of  "1" and total iteration of 30k which brings num_epochs = 5</ol>
<p>Now the issue I’m facing is:-</p>
<ul>When I start training the model the 0th epoch (i.e. 5k iters)  takes around 70 minutes and rest 4 epochs get over in next 70 mins. Also the loss( I've selected MSE loss doesn't seem to reduce or vary much over 5 epochs either.</ul>
<p>I think this is due to vanishing gradients.<br>
Any comments why is this happening and what can be done to overcome this challenge?</p>
How to do row-wise masking as batch operation?

takiyu: <p>My question is the way of row-wise masking in batch manner.<br>
I wrote non-batch case in the following.</p>
<pre><code class="lang-auto">import torch

n_dim0, n_dim1 = 4, 100
threshold = 0.5

storage = torch.rand((n_dim0, n_dim1))
mask = (threshold &lt; storage)

print('storage:', storage.shape)  # [4, 100]
print('mask:   ', mask.shape)     # [4, 100]

mask_lens = torch.sum(mask, axis=1)   # [4]
max_mask_lens = torch.max(mask_lens)  # [1]

masked = torch.zeros((n_dim0, max_mask_lens))
masked[0, 0:mask_lens[0]] = storage[0, mask[0]]  #
masked[1, 0:mask_lens[1]] = storage[1, mask[1]]  # How to make these lines
masked[2, 0:mask_lens[2]] = storage[2, mask[2]]  # batch operation?
masked[3, 0:mask_lens[3]] = storage[3, mask[3]]  #

print('masked: ', masked.shape)  # [4, X]  (X is around 50)
</code></pre>
<p>I wish someone has great idea.<br>
Thank you.</p>

takiyu: <p>One tricky idea has come up. However this is not smart and not effective.</p>
<pre><code class="lang-auto">import torch

n_dim0, n_dim1 = 4, 100
threshold = 0.5

storage = torch.rand((n_dim0, n_dim1))
mask = (threshold &lt; storage)

print('storage:', storage.shape)  # [4, 100]
print('mask:   ', mask.shape)     # [4, 100]

mask_lens = torch.sum(mask, axis=1)   # [4]
max_mask_lens = torch.max(mask_lens)  # [1]

# Create mask indices
mask_idxs = torch.nonzero(mask)  # [X, 2]  (2 == storage.ndim)

# Append padding to `mask_idxs`
pad_sizes = max_mask_lens - mask_lens
pads = []
for row_idx, pad_size in enumerate(pad_sizes):
    pad = torch.zeros((pad_size, storage.ndim), dtype=torch.long)
    pad[:, 0] = row_idx
    pads.append(pad)
mask_idxs = torch.cat([mask_idxs] + pads, axis=0)

# Sort `mask_idxs` by first element
mask_idxs_argssort = torch.argsort(mask_idxs[:, 0])
mask_idxs = mask_idxs[mask_idxs_argssort]

# Masking
masked = storage[mask_idxs[:, 0], mask_idxs[:, 1]]

# Restore original dimension
masked = masked.reshape(n_dim0, -1)

print('masked: ', masked.shape)  # [4, X]  (X is around 50)
</code></pre>
Why are vhp and hpv results the same?

ShengweiAn: <p>In the PyTorch documentation, the results of <a href="https://pytorch.org/docs/stable/autograd.html#torch.autograd.functional.vhp" rel="noopener nofollow ugc">vhp</a> and <a href="https://pytorch.org/docs/stable/autograd.html#torch.autograd.functional.hvp" rel="noopener nofollow ugc">hvp</a> are same.</p>
<p>But according to the notes:</p>
<blockquote>
<p>If your functions is twice continuously differentiable, then hvp = vhp.t()</p>
</blockquote>
<p>Usually, they should be the same. Could anyone tell me the reasons or point out where I misunderstood? Thanks a lot!</p>
How to know the status for the model? train or eval?

Mengran_Wang: <p>Hi all,</p>
<p>It is a simple question. What function can help me to know whether my model is <strong>in train mode</strong> or <strong>eval mode</strong>? Just print out the status of the model is enough. Thanks!</p>

Nikronic: <p>Hi,</p>
<p>All <code>nn.Module</code>s have an attribute <code>.training</code> which is <code>True</code> by default.</p>
<pre><code class="lang-auto">nn.Module().training  # True
</code></pre>
<p>Bests</p>

Mengran_Wang: <aside class="quote no-group" data-username="Nikronic" data-post="2" data-topic="85718">
<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/nikronic/48/71873_2.png" class="avatar"> Nikronic:</div>
<blockquote>
<p><code>training</code></p>
</blockquote>
</aside>
<p>Thanks! That’s exactly what I need.</p>
Is that ok not using 'criterion.cuda()' before criterion?

samr1104: <p>Is that ok not using ‘criterion.cuda()’ before criterion?</p>

Nikronic: <p>Hi,</p>
<p>If I want to summarize, you only need to use <code>.cuda</code> on criterion if the criterion function has parameters.</p>
<p>Here is the posts related to this situation: <a href="https://discuss.pytorch.org/t/move-the-loss-function-to-gpu/20060" class="inline-onebox">Move the loss function to GPU</a> and <a href="https://discuss.pytorch.org/t/why-criterion-cuda-is-not-needed-but-model-cuda-is/17410/2" class="inline-onebox">Why `criterion.cuda()` is not needed but `model.cuda()` is?</a></p>
<p>Bests</p>

samr1104: <p>thx, it really helps! <img src="https://discuss.pytorch.org/images/emoji/apple/smiley.png?v=9" title=":smiley:" class="emoji" alt=":smiley:"></p>
Low inference speed with libtorch on win10

Wenfei_Yang: <p>I want to deploy pytorch YOLO model in windows c++ environment.<br>
The  environment is libtorch1.4-debug + vs2017 + cuda10.1,<br>
the c++ inference code can run successfully, but it takes about 0.16s for<br>
an input image, as a comparison, the time is 0.03s in python environment.<br>
And i also found that the GPU usage rate is rather low, any ideas on how to solve this problem?</p>

ptrblck: <p>How did you profile the libtorch and Python code?<br>
Note that CUDA operations are asynchronous, so you would need to synchronize the code before starting and stopping the timer.</p>

peterjc123: <p>Plus: why do you use the debug version for benchmarking? The optimization switches are turned off in those builds.</p>
Pytorch1.5.0/win7 64bit/ Didn't find engine for operation quantized::conv2d_prepack NoQEngine

zhijian_li: <p>Pytorch 1.5.0+cu92<br>
Windows7 64 bit<br>
<strong>model.qconfig = torch.quantization.default_qconfig<br>
torch.quantization.prepare(model, inplace=True)<br>
torch.quantization.convert(model, inplace=True)</strong><br>
&lt;---- Here<br>
<strong>Exception has occurred: RuntimeError</strong><br>
<strong>Didn’t find engine for operation quantized::conv2d_prepack NoQEngine</strong></p>

supriyar: <p>Could you check if the <code>USE_FBGEMM</code> macro is turned on in your build environment? I believe it should be supported.</p>
<p>cc <a class="mention" href="/u/dskhudia">@dskhudia</a> who might be able to add more info.</p>

peterjc123: <p>We use VS 14.11 to build binaries for CUDA 9.2, so there is no FBGEMM support. If you need FBGEMM, then please use the binaries with other CUDA versions instead.</p>
Torchtext Loading Pretrained Glove Vectors

AladdinPerzon: <p>I’m trying to learn how to load pretrained glove vectors using torchtext and I manage to get something to work but I’m confused of what it’s doing.</p>
<p>As an example I have something like this:</p>
<pre><code class="lang-auto">MyField.build_vocab(train_data, vectors='glove.6B.100d')
</code></pre>
<p>Then in my model I have 100 dimensional embeddings and I load these weights by</p>
<pre><code class="lang-auto">pretrained_embeddings = MyField.vocab.vectors
my_model.embedding.weight.data.copy_(pretrained_embeddings)
</code></pre>
<p><strong>My Question:</strong><br>
So it’s loading the pretrained embedding matrix which has been trained on a vocabulary of some size, how does that work with a vocabulary that is of different size and different words? My idea is that it should copy the vectors for the words that is in my vocabulary, and randomly initialize the rest, is that what it’s doing?</p>

AladdinPerzon: <p>The performance is actually worse for me when loading the pretrained glove vectors than just training the embedding from scratch. That’s weird and there’s definitely something I’m doing wrong /:</p>

Mengran_Wang: <p>For a pre-trained vocabulary and the vocabs in your datasets for training, it is common that there are<br>
some words in datasets not included in pre-trained vocabulary. To solve this, we can set <strong>max_size</strong> and <strong>unk_init</strong> when <strong>build_vocab</strong>. <strong>max_size</strong> means the number of most commonly occurred words for building your vocab, and for the rest of words, just use <strong>unk_init</strong> to represent them, since they are less occurring in the text and unimportant. For example:<br>
TEXT.build_vocab(train_data, max_size=25000, vectors=“glove.6B.100d”, unk_init=torch.Tensor.normal_)<br>
For details, check this <a href="https://torchtext.readthedocs.io/en/latest/data.html?highlight=build_vocab#torchtext.data.Field.build_vocab" rel="nofollow noopener">https://torchtext.readthedocs.io/en/latest/data.html?highlight=build_vocab#torchtext.data.Field.build_vocab</a><br>
Also, remember to fix this weight during training.</p>
Question about retrofitting

stvhuang: <p>Hi all. I’m currently doing fine-tuning by retrofitting on contextualized sentence embeddings. Meaning that given two sentences’ vector representations, eg. <code>vec1</code> and <code>vec2</code>, I want to make these two vectors get closer. What confused me is that if I can create a sentence encoder (<code>SentEncoder</code> like BERT) and pass two sentences to such model to get two vector representation respectively. Then calculate the distance loss and do back propagation and optimization. (Option 1)</p>
<p>Or I need to create two models (<code>model1</code> and <code>model2</code>) and pass one sentence to each. (Option 2)</p>
<p>Actually, I think the problem is that if I can forward the model multiple times and do backprop once.</p>
<p>It will be great if you can briefly describe how is the computation graph looks like in this situation.</p>
<pre><code class="lang-python">model = BERT(...)
criterion = lambda vec1, vec2: (vec1 - vec2).norm(dim=-1).sum()
optimizer = optim.Adam(model.parameters())

for epoch in range(n_epoch):
    for sent1, sent2 in sent_loader:

        ##### OPTION 1 #####
        vec1 = model(sents1)
        vec2 = model(sents2)
        ####################

        ##### OPTION 2 #####
        # mode1 and model2 share same parameters
        vec1 = model1(sents1)
        vec2 = model2(sents2)
        ####################

        optimizer.zero_grad()
        # use distance (norm)
        # between vec1 and vec2 as criterion
        loss = criterion(vec1, vec2)
        loss.backward()
        optimizer.step()
</code></pre>
<p>Thanks!</p>

Mengran_Wang: <aside class="quote no-group" data-username="stvhuang" data-post="1" data-topic="85666">
<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/s/da6949/48.png" class="avatar"> stvhuang:</div>
<blockquote>
<p>computation graph</p>
</blockquote>
</aside>
<p>Is this solution useful?<a href="https://discuss.pytorch.org/t/how-can-i-know-data-flow-in-the-network/7105/4" class="inline-onebox">How can i know data flow in the network? - #4 by Nick_Young</a></p>
Adding dropout layer in pretrained resnet3D

jahan123: <p>I want to add drop out layer before final layer of pretrained resnet3D model, can someone please verify my approach, am I doing it correctly?</p>
<pre><code class="lang-auto">model = torchvision.models.video.r3d_18(pretrained=True)
model.fc = nn.Sequential(
     nn.Dropout(0.5),
     nn.Linear(model.fc.in_features, num_classes)
)
</code></pre>
<p>Thankyou.</p>

Nikronic: <p>Hi,</p>
<p>Yes, you are managing it correctly. Just for clarification, this is the two last layer of the model:</p>
<pre><code class="lang-auto">(avgpool): AdaptiveAvgPool3d(output_size=(1, 1, 1))
(fc): Linear(in_features=512, out_features=400, bias=True)
</code></pre>
<p>So by your code it will be converted to:</p>
<pre><code class="lang-auto">(avgpool): AdaptiveAvgPool3d(output_size=(1, 1, 1))
(fc): Sequential(
    (0): Dropout(p=0.5, inplace=False)
    (1): Linear(in_features=512, out_features=#num_classes, bias=True)
  )
</code></pre>
<p>Bests</p>

jahan123: <p>ahh got it, thanks! <img src="https://discuss.pytorch.org/images/emoji/apple/slight_smile.png?v=9" title=":slight_smile:" class="emoji" alt=":slight_smile:"></p>
How could I delete one element from each row of a tensor

coincheung: <p>Hi,</p>
<p>Suppose these is one 2d tensor:</p>
<pre><code class="lang-auto">ten = torch.randn(4, 5)
</code></pre>
<p>Now I need to remove one element from each row of that tensor according to another tensor:</p>
<pre><code class="lang-auto">ids = torch.tensor([2, 1, 4, 1])
</code></pre>
<p>which means that remove the <code>ten[0, 2]</code>, <code>ten[1, 1]</code>, ten[2, 4]<code>, </code>ten[3, 1]<code>from the tensor, and results in a</code>4 x 4` tensor. How could I do this please ?</p>

ptrblck: <p>This should work:</p>
<pre><code class="lang-python">ten = torch.randn(4, 5)
ids = torch.tensor([2, 1, 4, 1])
mask = torch.ones_like(ten).scatter_(1, ids.unsqueeze(1), 0.)
res = ten[mask.bool()].view(4, 4)
</code></pre>

coincheung: <p>Thanks for replying !! what if the tensor has layout of nchw and I need to remove one element from dim=1 ？</p>
<pre><code class="lang-auto">ten = torch.randn(2, 4, 5, 5)
ids = torch.tensor([2, 1, 4, 1])
....
res = ... # should have shape of [2, 3, 5, 5]
</code></pre>

Caruso: <p>If you want the output shape <code>[2, 3, 5, 5]</code>, so you want to drop the indices of dim=1, your ids can only be of length 2 and with a max value of 3.</p>
<p>I got this running, but i dont think its the best way xD</p>
<pre><code class="lang-auto">ten = torch.randn(2, 4, 5, 5)
ids = torch.tensor([3, 1])
mask = torch.ones_like(ten).scatter_(1, ids[:, None, None, None].repeat(1, 1, 5, 5), 0.)
res = ten[mask.bool()].view(2, 3, 5, 5)
</code></pre>

coincheung: <p>HI, thanks for replying this. In my case, the ids is not a <code>(2, )</code> index tensor, it has shape of <code>(2, 5, 5)</code>.</p>
<pre><code class="lang-auto">ten = torch.randn(2, 4, 5, 5)
ids = torch.randint(0, 4, (2, 5, 5))
.....
res = ... # has shape of (2, 3, 5, 5)
</code></pre>
<p>I am afraid there should not be broadcast operation here. The <code>(2, 5, 5)</code> tensor of <code>ids</code> means that the element of the <code>[:, id, :, :]</code> should be removed according to the <code>ids</code>.</p>
<p>How could I do this please?</p>
Getting error: polygamma(n,x) is not implemented for n>=2, but was 2

Deep_Shankar_Pandey: <p>When I try to use loggamma and digamma functions, it gives me “RuntimeError: polygamma(n,x) is not implemented for n&gt;=2, but was 2” error. Here is a sample code reproducing the error:</p>
<p>import torch</p>
<p>init_var = torch.randn([1,2], requires_grad = True)</p>
<p>x = torch.randn([1,2])</p>
<p>y = (init_var*x)</p>
<p>gt = torch.randn([1,2])</p>
<p>log_gamma_res = torch.digamma(y) <span class="hashtag-raw">#y</span></p>
<p>loss = (gt - log_gamma_res).sum()</p>
<p>grad = torch.autograd.grad(loss,init_var, create_graph = True)[0]</p>
<p>loss2 = (y - grad).sum()</p>
<p>loss2.backward()</p>
<p>It would be great if someone can explain this error and suggest how I canfix this error?</p>
<p><div class="lightbox-wrapper"><a class="lightbox" href="https://discuss.pytorch.org/uploads/default/original/3X/4/0/40eae490d3d03ec85de0ffc51962eb4c019fc633.png" data-download-href="https://discuss.pytorch.org/uploads/default/40eae490d3d03ec85de0ffc51962eb4c019fc633" title="error"><img src="https://discuss.pytorch.org/uploads/default/optimized/3X/4/0/40eae490d3d03ec85de0ffc51962eb4c019fc633_2_690x388.png" alt="error" data-base62-sha1="9ghOrOhaeEtWn9BCTkXD9q8jNJx" width="690" height="388" srcset="https://discuss.pytorch.org/uploads/default/optimized/3X/4/0/40eae490d3d03ec85de0ffc51962eb4c019fc633_2_690x388.png, https://discuss.pytorch.org/uploads/default/optimized/3X/4/0/40eae490d3d03ec85de0ffc51962eb4c019fc633_2_1035x582.png 1.5x, https://discuss.pytorch.org/uploads/default/optimized/3X/4/0/40eae490d3d03ec85de0ffc51962eb4c019fc633_2_1380x776.png 2x" data-dominant-color="E0E0DF"><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">error</span><span class="informations">1920×1080 675 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>
Set torch.device to all outputs from multi-output model altogether

nobodykid: <p>Hello,</p>
<p>I have a multi-output model which returns 3 outputs when doing forward pass</p>
<pre><code class="lang-auto">for data in train_loader:
    input = Variable(data.type(torch.cuda.FloatTensor))
    out_a, out_b, out_c = multi_output_model(input)
</code></pre>
<p>I’d like to do some post-processing of the output, but because of limited-memory GPU I tried to design so that only the model inference happens in GPU (and other processing on CPU).</p>
<p>Is there a way to cast all of the model’s output directly to CPU (or vice versa)? I know that you can cast it directly after calling the model’s forward pass such as</p>
<pre><code class="lang-auto">device = torch.device('cpu')
out = model(input).to(device)
### OR ###
out = model(input).cpu()
</code></pre>
<p>But this can only be done with single output model. I hope to find a way I could do the same for multi-output model</p>

ptrblck: <p>You would have to call the <code>cpu()</code> or <code>to('cpu')</code> operation on each tensor separately either inside the model in its <code>forward</code> method or outside of the model, as you cannot use it on a <code>tuple</code>.</p>

nobodykid: <p>I see, thank you very much for the suggestion</p>
Medical Open Network for AI

MONAI: <p><a href="https://github.com/Project-MONAI/MONAI" rel="nofollow noopener">MONAI</a> is a PyTorch-based, open-source framework for deep learning in healthcare imaging, part of the official PyTorch Ecosystem.</p>
<p><em>The codebase is currently under active development and we are marching towards v0.2.0 targeted for release in July</em></p>
<p>MONAI includes Imaging workflow <a href="https://github.com/Project-MONAI/MONAI/tree/master/examples" rel="nofollow noopener">examples &amp; jupyter notebooks</a> to make it seamless for researchers &amp; developers to get started. We are committed to broadening our examples &amp; tutorials for other domain specialized workflows.</p>
<p>We would love to hear from you as we build MONAI collaboratively with the community.</p>
<p>Let’s keep the conversations flowing !</p>