akankshakshurai/cuda-error-resolution-analysis huggingface dataset:
How to initialise weights for the first layer

username: jpj
cooked: <pre><code class="lang-auto"> # layers
  self.hidden_layer_1 = torch.nn.Linear(80,100)
  self.hidden_layer_2 = torch.nn.Linear(100,100)
  self.output = torch.nn.Linear(100,15)
</code></pre>
<pre><code class="lang-auto"> def forward(self, input):
        H1 = self.hidden_layer_1(input)
        H1 = self.ReLU(H1)
        H2 = self.hidden_layer_2(H1)
        H2 = self.ReLU(H2)
        final_inputs = self.output(H2)
        # not applying activation on final_inputs because CrossEntropyLoss does that
        return final_inputs
</code></pre>
<p>I have a feed forward neural network with 80 input features, two hidden layers with 100 nodes each and 15 outputs(one for each class).</p>
<p>I save weights from a restricted boltzmann machine on tensor flow in a <em>.pkl</em> file.</p>
<p>How do I initialise the above code with these weights?</p>

username: ptrblck
cooked: <p>Assuming you’ve stored the TF weights as numpy arrays, you could take a look at <a href="https://github.com/lernapparat/lernapparat/blob/master/style_gan/pytorch_style_gan.ipynb">this notebook</a>, which <a class="mention" href="/u/tom">@tom</a> and I created to port the StyleGAN parameters to our PyTorch implementation.</p>
X.to(device) vs Variable(x)

username: moinudin
cooked: <p>I have seen tutorials use <code>x.to(device)</code>, which I understand sends <code>x</code> to the computation device (cpu/gpu) and I have seen others use <code>Variable(x)</code>, which I understand is for <code>autograd</code>, without calling <code>.to(device)</code>. Does <code>x.to(device)</code> implicitly create an autograd variable? Or can you use <code>Variable(x).to(device)</code>?</p>

username: ptrblck
cooked: <p><code>Variable</code>s are deprecated since PyTorch <code>0.4</code> and you should use tensors now.<br>
Autograd is able to track operations of tensors, if they require gradients, so there is no need to use the tensor vs. <code>Variable</code> split anymore.</p>
Calculating gradients w.r.t another definied parameter

username: mahdi_morafah
cooked: <p>Hello,</p>
<p>I have a model and it’s weight are a.W1 + b.W2. Where W1 and W2 are let’s say two randomly generated weights. I want to freeze W1, W2 and only update a, and b through the training. I’m wondering how can I implement this in Pytorch?</p>
<p>Thanks a lot,</p>

username: KFrank
cooked: <p>Hi Mahdi!</p>
<aside class="quote no-group" data-username="mahdi_morafah" data-post="1" data-topic="117549" data-full="true">
<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/mahdi_morafah/48/27971_2.png" class="avatar"> mahdi_morafah:</div>
<blockquote>
<p>I want to freeze W1, W2 and only update a, and b through the training.</p>
</blockquote>
</aside>
<p>Let me assume that <code>W1</code>, <code>W2</code>, <code>a</code>, and <code>b</code> are some sort of pytorch<br>
tensors (and that your “.” represents some sort of multiplication).</p>
<p>Simply set</p>
<pre data-code-wrap="python"><code class="lang-python">W1.requires_grad = False
W2.requires_grad = False
a.requires_grad = True
b.requires_grad = True
</code></pre>
<p>When you backpropagate, pytorch will only calculate gradients<br>
for <code>a</code> and <code>b</code>, treating <code>W1</code> and <code>W2</code> as fixed (not trained) parameters.</p>
<p>Best.</p>
<p>K. Frank</p>
Pruning removes everything

username: mfikryrizal
cooked: <p>I used <code>torch.utils.prune.*_unstructured</code> on a saved pre-trained model to trim <code>weight</code> but it removed everything.</p>
<p>Model:</p>
<pre><code class="lang-auto">FullyConnected(
  (net): Sequential(
    (0): Linear(in_features=72, out_features=1024, bias=True)
    (1): ReLU(inplace=True)
    (2): Dropout(p=0.2, inplace=False)
    (3): Linear(in_features=1024, out_features=1024, bias=True)
    (4): ReLU(inplace=True)
    (5): Linear(in_features=1024, out_features=23106, bias=True)
  )
)
</code></pre>
<p>Result:</p>
<pre><code class="lang-auto">Before:
Linear(in_features=1024, out_features=23106, bias=True) =&gt; odict_keys(['weight', 'bias'])
[('weight', Parameter containing:
tensor([[-3.4171e-03,  7.4451e-03, -3.2716e-03,  ...,  4.3759e-03,
         -4.4775e-03,  4.9997e-04],
        [-9.5993e-03,  1.8929e-03, -9.2918e-04,  ...,  5.8639e-03,
         -2.0274e-03, -1.9497e-05],
        [ 6.6112e-05,  5.2332e-03, -5.2656e-03,  ..., -1.0969e-03,
         -6.8079e-03,  1.7691e-04],
        ...,
        [ 1.7492e-02,  1.6093e-03, -4.0479e-03,  ...,  4.3377e-05,
         -8.4499e-04,  2.9180e-05],
        [-8.2002e-03,  7.4289e-03,  6.5624e-03,  ..., -9.8359e-04,
          2.0093e-03,  3.9837e-05],
        [-8.1944e-03,  8.7170e-03,  1.1283e-02,  ...,  1.4373e-03,
         -1.2853e-03,  5.4475e-04]], requires_grad=True)), ('bias', Parameter containing:
tensor([ 0.0008, -0.0027,  0.0017,  ..., -0.0014,  0.0007,  0.0022],
       requires_grad=True))]

Pruning module...
Deleting artifacts...

After:
Linear(in_features=1024, out_features=23106, bias=True) =&gt; odict_keys(['bias'])
[('bias', Parameter containing:
tensor([ 0.0008, -0.0027,  0.0017,  ..., -0.0014,  0.0007,  0.0022],
       requires_grad=True))]
</code></pre>

username: mfikryrizal
cooked: <p>I found out that the weight mask is all 1, but why?</p>
<pre><code class="lang-auto">tensor([[1., 1., 1.,  ..., 1., 1., 1.],
        [1., 1., 1.,  ..., 1., 1., 1.],
        [1., 1., 1.,  ..., 1., 1., 1.],
        ...,
        [1., 1., 1.,  ..., 1., 1., 1.],
        [1., 1., 1.,  ..., 1., 1., 1.],
        [1., 1., 1.,  ..., 1., 1., 1.]])
</code></pre>
Detect when scheduler changes the learning rate

username: Lucas
cooked: <p>Hi,</p>
<p>I need to divide my learning rate and change my loss when my model reaches a plateau.<br>
I’m using <a href="https://pytorch.org/docs/stable/optim.html#torch.optim.lr_scheduler.ReduceLROnPlateau" rel="noopener nofollow ugc">ReduceLROnPlateau</a> for the learning rate part, and I would like to change the loss when the learning rate gets updated.<br>
Is there any way to know from inside the script when the learning rate is being updated?</p>

username: PatrickKr
cooked: <p>Hi,</p>
<p>you can at least check the learning rate with</p>
<blockquote>
<p>optimizer.param_groups[0][‘lr’]</p>
</blockquote>
<p>but I don’t know if there is a more efficient way with a callback function.</p>

username: Lucas
cooked: <p>Hi,</p>
<p>Thank you for your answer.<br>
I have switched from  <a href="https://pytorch.org/docs/stable/optim.html?highlight=reducelronplateau#torch.optim.lr_scheduler.ReduceLROnPlateau" rel="noopener nofollow ugc"> ReduceLROnPlateau</a> to editing the learning rate by doing:</p>
<pre><code class="lang-python">for g in optimizer.param_groups:
    g["lr"] = new_learning_rate
</code></pre>
<p>and detecting a plateau in the val loss myself, but I’m missing some features of ReduceLROnPlateau like <code>threshold</code> and <code>threshold_mode</code>.</p>
<p>Maybe the easiest would be to copy the code for <code>ReduceLROnPlateau</code> and call the function changing my loss at the end of their <code>_reduce_lr</code> function.</p>

username: Lucas
cooked: <p>Looking into it, there isn’t any simple way to access the last learning rate because <a href="https://pytorch.org/docs/stable/optim.html?highlight=reducelronplateau#torch.optim.lr_scheduler.ReduceLROnPlateau" rel="noopener nofollow ugc"> ReduceLROnPlateau</a> inherits from <code>object</code> contrary to the other schedulers inheriting from <code>_LRScheduler</code>.<br>
Is there a reason behind this?</p>
Need help in understanding feature extraction using transfer learning

username: Myailab
cooked: <p>Hi</p>
<p>I need help in understanding the following behavior of feature extraction from the pretrained model</p>
<p><strong>Output1:</strong></p>
<pre><code class="lang-auto">def get_net():
    model = bninception(pretrained="imagenet")
    model.gp = nn.AdaptiveAvgPool2d(1)
    model.conv = nn.Conv2d(config.channels, 64, kernel_size=(7, 7), stride=(2, 2), padding=(3, 3))
    model.last = nn.Sequential(
                nn.BatchNorm1d(1024),
                nn.Dropout(0.5),
                nn.Linear(1024, config.num_classes),
            )
    return model
</code></pre>
<pre><code class="lang-auto">model = get_net()
model.cuda()
output1 =model(images)
</code></pre>
<p><strong>Output2:</strong><br>
I am after features obtained at ‘model.conv’ , therefore i removed the later section as follows.</p>
<pre><code class="lang-auto">def get_net():
    model = bninception(pretrained="imagenet")
    model.gp = nn.AdaptiveAvgPool2d(1)
    model.conv = nn.Conv2d(config.channels, 64, kernel_size=(7, 7), stride=(2, 2), padding=(3, 3))
    return model
</code></pre>
<pre><code class="lang-auto">model = get_net()
model.cuda()
features = model(images)
</code></pre>
<p>and got the features for N images as : [N, 1000]. I fed these features into the following as follows</p>
<pre><code class="lang-auto">class ManualLinearRegression(nn.Module):
    def __init__(self):
        super(ManualLinearRegression,self).__init__()
        #self.linear = nn.Linear(1000, 28)
        self.linear = nn.Sequential(
                nn.BatchNorm1d(1000),#1024
                nn.Dropout(0.5),
                nn.Linear(1000, config.num_classes),
            )

    def forward(self, x):
        return self.linear(x)
</code></pre>
<pre><code class="lang-auto">model = ManualLinearRegression().cuda()
output2 = model(images)
</code></pre>
<p>I was expecting Output1 almost similar to Output 2. But it is a lot different. On computation of F1 score, output1 is 0.44 while Output2 is 0.09.</p>
<p>I think i am falling short in understanding some major aspect here. May I know what am I doing wrong here.</p>
<p>Just to add on, ultimately I want to extract features from convolutional layer, apply oversampling and perform classification.</p>
<p>Thank you.</p>
Operators call and dispatch (binding)

username: ConnollyLeon
cooked: <p>Currently I understand most of the torch operators are registered in <a href="https://github.com/pytorch/pytorch/blob/master/aten/src/ATen/native/native_functions.yaml" rel="noopener nofollow ugc">native_functions.yaml</a>. With their implementations in folders like aten/src/Aten/native/<em>, aten/src/Aten/native/cpu/</em> and aten/src/Aten/native/cuda* .</p>
<p>But I am still confused about HOW exactly PyTorch calls its own operators.</p>
<p>Now I have an image like this, which is from website.<br>
!<a href="https://upload-images.jianshu.io/upload_images/13575947-68ee8c4b75723454.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240" rel="noopener nofollow ugc">https://upload-images.jianshu.io/upload_images/13575947-68ee8c4b75723454.png?imageMogr2/auto-orient/strip|imageView2/2/w/1240</a></p>
<p>Could anybody please show the specific code that are doing these dispatching things?</p>
How to group a tensor into a 3x3 blocks using sliding window of stride 1

username: Peter_Featherstone
cooked: <p>How do you group a tensor into 3x3 blocks using sliding window of stride 1.<br>
Basically how can I use torch functions to accelerate this:</p>
<pre><code class="lang-auto">grp = []
m, n = I.shape[-2:]
for i in range(m-3):
    for j in range(n-3):
        grp.append(x[..., i:(i+3), j:(j+3)])
grp = torch.stack(grp, dim=-3)
return grp
</code></pre>
<p>Basically i want to apply a filter but it’s not a convolutional one, instead it’s doing some XOR’ing. The only way i can think of doing this is to prepare my 3x3 blocks ahead of time then do some XOR’ing with a 3x3 mask/filter that’s being suitably broadcast along the first few axes.</p>

username: Peter_Featherstone
cooked: <p>Is <a href="https://pytorch.org/docs/stable/generated/torch.nn.Unfold.html" class="inline-onebox" rel="noopener nofollow ugc">Unfold — PyTorch 1.8.1 documentation</a> what i want ?</p>

username: Peter_Featherstone
cooked: <p>So <a href="https://pytorch.org/docs/stable/generated/torch.nn.Unfold.html" rel="noopener nofollow ugc">Unfold</a> does exactly what i want but this is a very memory hungry way of doing things, since the data is duplicated. It’s probably worth writing a c++ extension…</p>
NaN gradient in torch.hypot

username: Latope2-150
cooked: <p>Hi all,</p>
<p>Back in 2017, it was decided that torch.norm would have a zero subgradient at zero (<a href="https://github.com/pytorch/pytorch/pull/2775" class="inline-onebox" rel="noopener nofollow ugc">Norm subgradient at 0 by albanD · Pull Request #2775 · pytorch/pytorch · GitHub</a>).<br>
Applying the same logic, shouldn’t torch.hypot have a zero subgradient at (0, 0)?</p>
<p>Currently, torch.hypot gives NaNs in gradient for (0, 0) inputs but is otherwise equivalent to torch.norm of the concatenation/stacking of two tensors.</p>

username: tom
cooked: <p>I’m sure such a change would be acceptable if it comes with tests etc.</p>
ReflectionPad3d

username: palimboa
cooked: <p>Why is there no <code>ReflectionPad3d</code>, but <code>ConstantPad3D</code> and <code>ReplicationPad3d</code> ? Is this on purpose?</p>

username: tom
cooked: <p>I think the fundamental reason is that people tend to use the functional interface for that… But you could easily do this yourself as</p>
<pre><code class="lang-auto">class ReflectionPad3d(torch.nn.modules.padding._ReflectionPadNd):
    def __init__(self, padding):
        super(ReflectionPad3d, self).__init__()
        self.padding = _ntuple(6)(padding)
</code></pre>
<p>or so.</p>
<p>Best regards</p>
<p>Thomas</p>

username: jerofad
cooked: <p>Hi,<br>
Trying to get this <code>ReflectionPad3d</code> to work as well but keep getting <code>NotImplementedError</code> I also cannot find the version for which pytorch implemented this mode</p>

username: tom
cooked: <p>Right. <a href="https://github.com/pytorch/pytorch/issues/27655" class="inline-onebox" rel="noopener nofollow ugc">Why we do not have torch.nn.ReflectionPad3d? · Issue #27655 · pytorch/pytorch · GitHub</a> . It’s probably relatively easy to adapt reflection_pad2d to the 3d case.</p>
<p>Best regards</p>
<p>Thomas</p>
"module 'torch' has no attribute 'count_nonzero' "

username: PhysicsIsFun
cooked: <p>Greetings,</p>
<p>can anyone tell me why I am getting this error?<br>
The sample code from the documentation does not even work for me:<br>
<a href="https://pytorch.org/docs/stable/generated/torch.count_nonzero.html" rel="noopener nofollow ugc">https://pytorch.org/docs/stable/generated/torch.count_nonzero.html</a></p>
<p>Is the function outdated? I am using Spyder.</p>
<p>Regards!</p>

username: Dwight_Foster
cooked: <p>What is your pytorch version?</p>

username: PhysicsIsFun
cooked: <p>Typing <code>print(torch.__version__)</code> in Spyder gives 1.6.0.</p>

username: Dwight_Foster
cooked: <p>Ok that is the problem torch 1.6.0 does not have a torch.count_nonzero function. There is no alternative to it so you may want to upgrade your pytorch version.</p>

username: PhysicsIsFun
cooked: <p>Thank you!</p>
<p>I tried “conda update pytorch”. Now it was downgraded to 1.3.1 <img src="https://discuss.pytorch.org/images/emoji/apple/joy.png?v=9" title=":joy:" class="emoji" alt=":joy:"></p>

username: Dwight_Foster
cooked: <p>Have you tried just reinstalling it. Try going to the pytorch page and reinstalling the new version with whatever os you need.</p>
Can't find nn.ReflectionPad3d layer

username: jerofad
cooked: <p>I am having a bit of difficulty in implementing a custom layer for <code>ReflectionPad3d</code> as this is not implemented in the official release.</p>
<p>What I have tried is this:</p>
<pre><code class="lang-auto">import torch
import torch.nn.functional as F

class ReflectionPad3d(torch.nn.Module):
  def __init__(self, padding):
    self.padding = padding
  def forward(self, x):
    return F.pad(x. self.padding, mode='reflect')
</code></pre>
<p>Aim is to wrap the functional as a module. But then I actually discovered that <code>"reflect"</code> for 3d padding is not implemented.</p>
<p>Is there a way to get 3d reflection padding?</p>
Use single optimizer for multiple nets

username: tantheta01
cooked: <p>I am training a GAN and I have a discriminator, a generator and one more tensor which are being used for loss calculation.<br>
Psuedo code:<br>
other_tensor = torch.tensor(require_grad=True)<br>
loss = disc_loss + gen_loss + f(other_tensor)<br>
optimizer = Adam(list(gen.parameters) + list(self._measure.parameters), lr=1e-4)</p>
<p>But this shall not optimize the other_tensor. How should I add that to my optimizer?</p>

username: Nikronic
cooked: <p>Hi,</p>
<p>You can simply pass the <code>other_tensor</code> to the optimizer like other parameters.</p>
<p>For instance, let’s assume we have two models <code>model0</code> and <code>model1</code> and <code>other_tensor</code> to be optimized:</p>
<pre><code class="lang-auto">optim = torch.optim.Adam(params=itertools.chain(list(model0.parameters()), list(model1.parameters()), [other_tensor]))
</code></pre>
<p>Bests</p>

username: tantheta01
cooked: <p>Thanks <a class="mention" href="/u/nikronic">@Nikronic</a>, it worked that way. I am struck with another problem. I have created the optimizer and passed the parameters. Suppose in some training step I want to freeze one of the nets and only want to update weights of the others. Is that possible?</p>

username: Nikronic
cooked: <p>You can wrap the computation for the model you don’t want to update inside <code>torch.no_grad()</code> then no grad will be accumulated for those parameters inside enclosed inside <code>.no_grad</code>:</p>
<pre><code class="lang-auto">x = torch.tensor([3.])  # input
w = torch.tensor([1.]).requires_grad_(True)  # params of model0
b = torch.tensor([0.]).requires_grad_(True)  # params of model1

# now skip updating model0
with torch.no_grad():
  y = x * w
y = y + b

# update params
y.backward()
optim.step()  # this will only `model1` since `model0.grad = None` as it's like it has never being passed to optim object

# print some grads
print(w.grad) = None
print(b.grad) = # a tensor
</code></pre>
PyTorch (Yolov5) + React

username: Alessandro_Perez
cooked: <p>Hello everyone, first of all, I am sorry if this topic is not in the right place or if the solution is or too simple (and I am missing) or impossible from a technical point. I am fairly new to PyTorch and machine learning in general.</p>
<p>I would like to know if it is possible to integrate a model (.pt) created using Yolov5 to a react project. My group is preparing a landing page for a product that uses object recognition as one of the features and I would like to know how can I make the link in between the created model and react. Any help on the deployment of a model is also very welcome since I have never deployed one yet.</p>
'tuple' argument has no attribute 'repeat'

username: eam
cooked: <p>Hi!</p>
<p>I am writing a neural network in pytorch and I am running into some issues. I am implementing an attention layer, however I get a runtime error when I want to repeat my hidden layer. My model is as follows:</p>
<p>class Model_P(nn.Module):<br>
def <strong>init</strong>(self, hidden_size, input_size):<br>
super(Model_P, self).<strong>init</strong>()<br>
self.hidden_size = hidden_size<br>
self.input_size = input_size<br>
self.lstm = nn.LSTM(input_size, hidden_size, 1, bias=False, batch_first=True, bidirectional=True)<br>
self.dropout = nn.Dropout(0.5)<br>
self.softsign_1 = nn.Softsign()<br>
self.attention_hidden_vector = nn.Linear(hidden_size + 2 * hidden_size, hidden_size)<br>
self.attention_scoring_fn = nn.Linear(hidden_size, 1, bias=False)<br>
self.dense = nn.Linear(hidden_size,1)<br>
self.relu_1 = nn.ReLU()</p>
<pre><code>def forward(self, x):
    #model en data parameters must be the same, thus convert to float
    x.float()
    output, hidden = self.lstm(x.float())
    output = self.softsign_1(output.float())
    src_len = output.shape[0]
    hidden = hidden.repeat(src_len, 1, 1)
    attn_hidden = torch.tanh(self.attn_hidden_vector(torch.cat((hidden, output), dim=2)))
    attn_scoring_vector = self.attn_scoring_fn(attn_hidden).squeeze(2)
    attn_scoring_vector = attn_scoring_vector.permute(1, 0)
    output = F.softmax(attn_scoring_vector, dim=1)
    output = self.dense(output)
    output = self.relu_1(output)

    return output
</code></pre>
<p>I get the following error: AttributeError: ‘tuple’ object has no attribute ‘repeat’</p>
<p>I have already tried to convert the tuple ‘hidden’ to a tensor, by converting the tuple to an numpy array, and then creating a tensor, via the following code:</p>
<p>hidden = np.asarray(hidden)<br>
hidden = torch.from_numpy(hidden)</p>
<p>However this raises other erros, which I have not succeeded to solve. Does anybody know of a solution? Many thanks in advance!</p>

username: Dwight_Foster
cooked: <p>So what is happening is your hidden output from the lstm is a tuple made up of both the hidden state and the cell state. The lstm has three outputs, the output of the model, the hidden state, and the cell state. When you put only two variables one becomes a tuple that includes the hidden and cell state. If you only want to repeat the hidden state just add another variable for the cell state like this:</p>
<pre><code class="lang-auto">    output, hidden, _ = self.lstm(x.float())
</code></pre>
GTZAN CNN genre classifier only predicts 1 class

username: SomaF
cooked: <p>I’m making a genre classifier using the GTZAN dataset, where I’m generating overlapping patches of Melspectrograms for each input. The final output of the model is the sum (or average) output for each patch.<br>
<a href="https://github.com/TheSoma300/final-year-project-tp/blob/main/full_pipe.ipynb" rel="noopener nofollow ugc">Jupyter notebook with outputs</a><br>
The problem is that after the second batch or so the network is not deviating from 1 class prediction (even with ‘average’ combination for the final prediction). The output from the model is also weird, because the predictions are identical for each input of the batch. Same with no activation on the output.</p>
<p>The network is 2 conv2D layers with maxpooling and a fully connected one with 10 outputs.<br>
Optimizer: Adam with weight decay (5e-4) and lr (0.01)<br>
Loss: CrossEntropy</p>
<p>Is the network built wrong? All the help would be much appreciated!</p>
<p>EDIT: Fixed the issue, remember to activate your fully connected layers kids <img src="https://discuss.pytorch.org/images/emoji/apple/wink.png?v=9" title=":wink:" class="emoji" alt=":wink:"></p>
Why is my loss stuck after 2nd Epoch?

username: samim
cooked: <p>I am really not sure, why my loss is stuck after the second epoch.<br>
Likewise, the RMS and R² score wont change after the second epoch.</p>
<pre><code class="lang-auto">
Epoch 1 -- loss 17.611322, RMS error 0.149893, R² score -0.002587 
Epoch 2 -- loss 9.828691, RMS error 0.139825, R² score 0.140504 
Epoch 3 -- loss 9.810824, RMS error 0.139688, R² score 0.141909 
Epoch 4 -- loss 9.756177, RMS error 0.139007, R² score 0.150746 
Epoch 5 -- loss 9.712296, RMS error 0.138353, R² score 0.158283
</code></pre>
<p>The training loop:</p>
<pre><code class="lang-auto">for epoch in range(epochs):
    epoch_error = []
    epoch_loss = []
    epoch_r2 = []
    for i_batch, minibatch in enumerate(dataloader):
        inputs, outputs = minibatch
        optimizer.zero_grad()
        pred = model.forward(inputs)
        loss = target_loss(pred, outputs) + beta * model.kl_loss
        loss.backward()
        optimizer.step()
        error = torch.sqrt(torch.mean((torch.flatten(pred) - torch.flatten(outputs)) ** 2)).detach().numpy()
        r2 = r2_score(torch.flatten(outputs).detach().numpy(), torch.flatten(pred).detach().numpy())
        epoch_error.append(error)
        epoch_loss.append(loss.data.detach().numpy())
        epoch_r2.append(r2)
    hist_error.append(np.mean(epoch_error))
    hist_loss.append(np.mean(epoch_loss))
    hist_r2.append(np.mean(epoch_r2))
    print("Epoch %d -- loss %f, RMS error %f, R² score %f " % (epoch+1, hist_loss[-1], hist_error[-1], hist_r2[-1]))

</code></pre>
<p>Where target_loss is simply torch.mean(torch.sum((pred - out) ** 2))</p>
Convolution failed on arm macOSX

username: Sumanth_Jain
cooked: <p>Hi,<br>
I was trying to train a model natively on arm Mac. It ran into a RunTime error</p>
<p><strong>RuntimeError: NNPACK SpatialConvolution_updateOutput failed</strong></p>
<p>I found neither solutions nor the descriptions regarding the error in internet. Can someone Please Help</p>
<p>Thank You</p>
Is there an implementation of MDLSTM?

username: jleobernard
cooked: <p>Hello,<br>
I was working on an implementation  of a MDLSTM as described in <a href="https://arxiv.org/abs/0705.2011" rel="noopener nofollow ugc">this paper</a> but I can’t have it predict anything.<br>
Therefore I wanted to compare my implementation to an existing functioning one but couldn’t find any.<br>
Does any one of you guys where I could find one ? Does pytorch plan to provide such a component in the future ?<br>
Thanks for your help</p>

username: turian
cooked: <p>I would also find this useful.</p>
<p>This <a href="https://pypi.org/project/torchmore/" rel="noopener nofollow ugc">package</a> has " * <code>layers.BDHW_LSTM</code>: an MDLSTM variant that is a drop-in replacement for a <code>Conv2d</code> layer" but I haven’t looked at the code.</p>

username: jleobernard
cooked: <p>Thanks a lot for the information. I’ll look into it.</p>
RuntimeError: CUDA error: device-side assert triggered... When train parallel and 4 classes

username: jS5t3r
cooked: <p>I am using this code: <a href="https://github.com/weiaicunzai/pytorch-cifar100/blob/master/train.py" class="inline-onebox" rel="noopener nofollow ugc">pytorch-cifar100/train.py at master · weiaicunzai/pytorch-cifar100 · GitHub</a></p>
<p>I am getting this error, when I increase the image size of the dataset:</p>
<pre><code class="lang-auto">/home/user/.conda/envs/bm/lib/python3.7/site-packages/torch/jit/_trace.py:152: UserWarning: The .grad attribute of a Tensor that is not a leaf Tensor is being accessed. Its .grad attribute won't be populated during autograd.backward(). If you indeed want the gradient for a non-leaf Tensor, use .retain_grad() on the non-leaf Tensor. If you access the non-leaf Tensor by mistake, make sure you access the leaf Tensor instead. See github.com/pytorch/pytorch/pull/30531 for more informations.
  if a.grad is not None:
checkpoint_path:  checkpoint/resnet50/128x128_16_0.1_Hair_Color_Wednesday_07_April_2021_23h_21m_26s
Training Epoch: 1 [16/13670]    Loss: 1.5864    Acc: 1.0000     LR: 0.000000
epoch 1 training time consumed: 241.82s, Elapsed time: 0:04:01.824466
/opt/conda/conda-bld/pytorch_1607370128159/work/aten/src/THCUNN/ClassNLLCriterion.cu:108: cunn_ClassNLLCriterion_updateOutput_kernel: block: [0,0,0], thread: [0,0,0] Assertion `t &gt;= 0 &amp;&amp; t &lt; n_classes` failed.
/opt/conda/conda-bld/pytorch_1607370128159/work/aten/src/THCUNN/ClassNLLCriterion.cu:108: cunn_ClassNLLCriterion_updateOutput_kernel: block: [0,0,0], thread: [13,0,0] Assertion `t &gt;= 0 &amp;&amp; t &lt; n_classes` failed.
Traceback (most recent call last):
  File "train.py", line 289, in &lt;module&gt;
    acc, total_time = eval_training(epoch, total_time)
  File "/home/user/.conda/envs/bm/lib/python3.7/site-packages/torch/autograd/grad_mode.py", line 26, in decorate_context
    return func(*args, **kwargs)
  File "train.py", line 124, in eval_training
    test_loss += loss.item()
RuntimeError: CUDA error: device-side assert triggered
</code></pre>
<p>I train a classifier with 4 classes.</p>

username: ptrblck
cooked: <p>Could you check the target indices and make sure they are in the expected range <code>[0, nb_classes-1]</code>?<br>
Based on the error message the criterion raises the exception as the target values are outside of the valid range.</p>

username: jS5t3r
cooked: <p>My classes are 0,1,2,3  and should be correct…</p>
Need help! Encountered some errors while using cmake

username: QixianX
cooked: <p>The mistake is shown in the following picture<br>
<div class="lightbox-wrapper"><a class="lightbox" href="https://discuss.pytorch.org/uploads/default/original/3X/1/8/18dc88c1e75f960dff1c4baf40941850a9c5537b.png" data-download-href="https://discuss.pytorch.org/uploads/default/18dc88c1e75f960dff1c4baf40941850a9c5537b" title="image"><img src="https://discuss.pytorch.org/uploads/default/original/3X/1/8/18dc88c1e75f960dff1c4baf40941850a9c5537b.png" alt="image" data-base62-sha1="3xVX09Lz56nMVs4bn1urbvI6EAP" width="690" height="246" data-dominant-color="090909"><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">805×288 9.14 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>But my computer have no disk called C, and Ive added the path into environment variable<br>
<em>I:\Program Files\NVIDIA Corporation\NvToolsExt\include</em></p>
<hr>
<p>– libtorch 1.7.1 release<br>
– cuda 10.1<br>
– cmake 3.20.0<br>
– vs 2017</p>
Slow inference speed with Glow

username: Adithya_Chandra
cooked: <p>Hi,</p>
<p>I have tried the Ahead of time(AOT) complied executable bundles in glow using  <a href="https://github.com/pytorch/glow/blob/master/docs/AOT.md" rel="noopener nofollow ugc">this</a> documentation.</p>
<p>These are the two models which i have tried(Pytorch models converted to ONNX model).</p>
<ol>
<li>Resnet 18</li>
<li>VGG 16</li>
</ol>
<p><strong>Command for compiling the model to glow AOT bundles</strong><br>
model-compiler -backend=CPU -target=x86-64 -model=model.onnx -emit-bundle=./Model -bundle-api=dynamic</p>
<p><strong>Command for generating executable</strong><br>
clang++ main.cpp model.o -lpng -o Model</p>
<p><strong>Glow bundle Inference time vs Pytorch model inference time:</strong></p>
<p><strong>System configuration:</strong><br>
CPU: i3-6006U<br>
RAM: 8 GB<br>
OS: Ubuntu 16.04<br>
LLVM Version: 8.0.1<br>
Clang Version: 8.0.1</p>
<p><strong>1) Resnet 18:</strong><br>
Glow Bundle: 620 ms<br>
Pytorch : 100 ms<br>
<strong>2) VGG 16:</strong><br>
Glow Bundle: 7680ms<br>
Pytorch: 410ms</p>
<p><strong>Questions:</strong></p>
<ol>
<li>why there is a huge gap in glow and Pytorch inference performance?</li>
<li>Is there any way i can improve the glow compiled model inference time?</li>
</ol>
<p>Thanks and Regards<br>
Adithya</p>

username: jfix
cooked: <p>Hi Adithya,</p>
<blockquote>
<ol>
<li>why there is a huge gap in glow and Pytorch inference performance?</li>
</ol>
</blockquote>
<p>OSS backends in Glow are not primarily focused on optimizing CPU performance. Additionally it really depends on what CPU architecture you’re using. For example we have some convolution implementation that we know has good performance on x86, but is bad on ARM. Since you’re on x86, I should note that I think the fast x86 version was disabled accidentally and needs to be fixed – see <a href="https://github.com/pytorch/glow/pull/4971#issuecomment-747136613" rel="noopener nofollow ugc">this comment which I don’t think was yet resolved</a>.</p>
<blockquote>
<ol start="2">
<li>Is there any way i can improve the glow compiled model inference time?</li>
</ol>
</blockquote>
<p>I imagine enabling the well-performing conv kernel will get you much closer to the PyTorch performance. However as I mentioned, in general CPU performance is not something we’re focused hugely on. One way you could probably get to parity w/ PyTorch is to copy their convolution kernel source code and use it in libjit*.cpp as the source code for the kernels Glow compiles. I don’t know how easily that could be done but it should be doable. Then you could benefit from Glow’s high level Graph optimizations, memory optimization, and PyTorch’s optimized kernels.</p>
How to combine LR schedulers?

username: rafi-cohen
cooked: <p>Hi,<br>
I’m trying to use a couple of <code>torch.optim.lr_scheduler</code>s together, but I don’t seem to be getting the results I’m expecting.</p>
<p>I read <a href="https://github.com/pytorch/pytorch/issues/13022" rel="noopener nofollow ugc">#13022</a> and <a href="https://github.com/pytorch/pytorch/pull/26423" rel="noopener nofollow ugc">#26423</a>, and my understanding is that one should simply create multiple <code>lr_scheduler</code>s and call <code>step</code> on all of them at the end of each epoch.</p>
<p>However, running:</p>
<pre><code class="lang-python">from torch.optim import SGD, lr_scheduler
model = ...  # Doesn't really matter, use anything you like
optim = SGD(model.parameters(), 0.1)
scheduler1 = lr_scheduler.LambdaLR(optim, lambda epoch: min(epoch / 3, 1))
scheduler2 = lr_scheduler.MultiStepLR(optim, [5, 8])
for epoch in range(10):
    print(epoch, optim.param_groups[0]['lr'])
    scheduler1.step()
    scheduler2.step()
</code></pre>
<p>The output I expected is:</p>
<pre><code class="lang-auto">0 0.0333...
1 0.0666...
2 0.1
3 0.1
4 0.1
5 0.01
6 0.01
7 0.01
8 0.001
9 0.001
</code></pre>
<p>i.e. a linear decrease in the first 3 epochs, constant lr until epoch 5, a decrease by a factor of 0.1, and again constant lr until epoch 8, followed with a decrease by a factor of 0.1.</p>
<p>But the actual output is:</p>
<pre><code class="lang-auto">0 0.03333333333333333
1 0.06666666666666667
2 0.1
3 0.1
4 0.1
5 0.010000000000000002
6 0.1
7 0.1
8 0.010000000000000002
9 0.1
</code></pre>
<p>the initial linear decrease happens as expected, however, after each milestone, the lr reverts back to 0.1.</p>
<p>Am I doing something wrong? Any help would be appreciated.</p>
Tips for implementing knowledge distillation

username: seewoo5
cooked: <p>I’m trying to implement a vanilla knowledge distillation (compare outputs of teacher and student models via cross-entropy loss) and I want to get some tips for implementation. Especially, I want to know if there’s a way to save memory since we need to load two models on GPU and train the student model with output of both models, which requires additional memory cost compared to training only a student model without distillation. Is there a way to save memory by, for example, using multiple GPUs? I think it is theoretically possible to load two models on different GPUs and train the student model by using a single batch that is copied to both GPUs. It seems that most of the publicly available implementations load two models in the same GPU.</p>
Make a single prediction with pytorch geometric GCNN

username: zkasper99
cooked: <p>Hello, I am a beginner with machine learning so please forgive me if this is a stupid question.</p>
<p>I’m trying to use a graph convolutional neural network to predict the classification of 3D data, specifically cell morphology. This is my testing method, where target is a one dimensional matrix of size n, n being the number of vertices.</p>
<p><code>def test(model, test_loader, num_nodes, target, device):</code><br>
<code>model.eval()</code><br>
<code>correct = 0</code><br>
<code>total_loss = 0</code><br>
<code>n_graphs = 0</code><br>
<code>with torch.no_grad():</code><br>
<code>for idx, data in enumerate(test_loader):</code><br>
<code>out = model(data.to(device))</code><br>
<code>total_loss += F.nll_loss(out, target).item()</code><br>
<code>pred = out.max(1)[1]</code><br>
<code>correct += pred.eq(target).sum().item()</code><br>
<code>n_graphs += data.num_graphs</code><br>
<code>return correct / (n_graphs * num_nodes), total_loss / len(test_loader)</code></p>
<p>How could I produce a single prediction for a piece of data instead of the tensor of predictions?</p>

username: ptrblck
cooked: <p>Assuming your input uses a shape of <code>[batch_size, *]</code>, you could set the <code>batch_size</code> to <code>1</code> and pass this single sample to the model.<br>
The rest of the code should stay the same, as the used method should not depend on the actual batch size.</p>
Add additional layer to ResNet

username: atefeh_baniasadi
cooked: <p>Hi <a class="mention" href="/u/ptrblck">@ptrblck</a></p>
<p>Before anything else, many thanks for your advice because it is always beneficial.</p>
<p>I want to use Resnet model with pre-trained weights,  but I want to use additional input data (mean value of each image channel) besides the image at the end layer of Resnet. To do that, I plan to use a standard Resnet model, take one of its last FC layers, concatenate it with the additional input data and add FC layers processing both inputs. I changed my code as below:</p>
<pre><code>if model_name == "resnet":
    class MyModel(nn.Module):
        def __init__(self):
            super(MyModel, self).__init__()
            self.model = models.resnet18(pretrained=True)
            self.model.fc = nn.Linear(self.model.fc.in_features, 515)
            # new layer
            self.fc1 = nn.Linear(515, 2)

        def forward(self, image, data):
            x1 = self.model(image)
            x2 = data
            x = torch.cat((x1, x2), dim=1)
            x = torch.nn.functional.relu(self.fc1(x))
            return x
    model = MyModel()
</code></pre>
<p>but while running my program I faced this error about the data loader</p>
<pre><code>return self._new(self.im.resize(size, resample, box))
</code></pre>
<p>ValueError: height and width must be &gt; 0</p>
<p>please guide me on what the error is related to?</p>
<p>and where I must use “param.requires_grad = True”?,  because I want to use pre-trained weights in all layers to fine-tune my project.</p>
Can pytorch profiler add s3 support

username: weiqqi1028
cooked: <p>I was looking at the new Pytorch profiler here: <a href="https://pytorch.org/blog/introducing-pytorch-profiler-the-new-and-improved-performance-tool/" class="inline-onebox" rel="noopener nofollow ugc">Introducing PyTorch Profiler - the new and improved performance tool | PyTorch</a></p>
<p>I tried it and it worked well at local disk, but seems that <code>tensorboard_trace_handler</code> does not support s3 path as log dir. Can we add s3 support?</p>

username: ptrblck
cooked: <p>This might be a good feature request. Could you create it on <a href="https://github.com/pytorch/pytorch/issues">GitHub</a> and explain your use case a bit?<br>
Also, would you be interested in implementing it, if the code owners would like to add it?</p>
CMake can not find package after install with conda

username: j-eser
cooked: <p>If I install pytorch with conda the relevant cmake files fro find_package are not stored in the default folder that cmake checks. The only way for me to make it work is to specificaly set the CMAKE_PREFIX_PATH.<br>
This is unfortunate as it is python version dependent and any update would require to also change the Cmake file. Is there any way around that? Could the conda package just add the files to the default CMAKE_PREFIX_PATH?</p>
<p>Thanks</p>

username: crcrpar
cooked: <p>I’ve not faced the same error but if installing from source, yes.</p>
<p>It’s documented to manually set <code>CMAKE_PREFIX_PATH</code> before <code>python setup.py install</code> or <code>python setup.py develop</code>: <a href="https://github.com/pytorch/pytorch#install-pytorch" rel="noopener nofollow ugc">pytorch/pytorch: Tensors and Dynamic neural networks in Python with strong GPU acceleration (github.com)</a>.</p>

username: j-eser
cooked: <p>well installing with conda pytorch_cpu the needed cmake files are in anaconda/envs/&lt;env_name&gt;/lib/python3.8/site-packages/torch/share/cmake/<br>
That is not a path that cmake chacks by default as far as I can tell</p>
Custom Clipped ReLu Activation

username: Alex_NG
cooked: <p>Dear All,<br>
Here is my code for Clipped ReLU. Do I mistake? I am not sure about the backward part.<br>
class MyReLU(torch.autograd.Function):<br>
<span class="mention">@staticmethod</span><br>
def forward(ctx,input):<br>
output = input.clone()<br>
input.clamp(min=0,max=1)<br>
ctx.save_for_backward(input)<br>
return output<br>
<span class="mention">@staticmethod</span><br>
def backward(ctx, grad_output):<br>
input, = ctx.saved_tensors<br>
grad_input = grad_output.clone()<br>
grad_input[input &lt; 0] = 0<br>
grad_input[input &gt; 1] = 0<br>
return grad_input<br>
dtype = torch.float<br>
device = torch.device(“cpu”)<br>
relu = MyReLU().apply</p>
Will Torch automatically free activation memory?

username: ConnollyLeon
cooked: <p>Will Torch free the gpu memory of activation values of a specific layer after its backward has done? Since the activation values are no more needed after calculating gradients.</p>

username: ptrblck
cooked: <p>Yes, the intermediate tensors will be removed after the <code>backward()</code> operation is done, if <code>retain_graph=True</code> is not used.<br>
This is also the reason, why a second <code>backward()</code> call right after the first one will raise an error (if the intermediate tensors were needed to calculate the gradients).</p>

username: ConnollyLeon
cooked: <blockquote>
<p>The intermediate tensors will be removed after the <code>backward()</code> operation is done</p>
</blockquote>
<p>That’s awesome.<br>
Could you please show me in which file this “remove operation” is executed?</p>

username: ptrblck
cooked: <p>I don’t know where exactly the intermediates are freed, but <a class="mention" href="/u/alband">@albanD</a> might know. <img src="https://discuss.pytorch.org/images/emoji/apple/slight_smile.png?v=9" title=":slight_smile:" class="emoji" alt=":slight_smile:"></p>

username: albanD
cooked: <p>Hey,</p>
<p>Each “Node” in the graph implements a special functions to release all the resources it can here: <a href="https://github.com/pytorch/pytorch/blob/a46d56f988505547b0779838a022970b79123b3c/torch/csrc/autograd/function.h#L430-L431" class="inline-onebox">pytorch/function.h at a46d56f988505547b0779838a022970b79123b3c · pytorch/pytorch · GitHub</a><br>
This is called from the execution engine when <code>retain_graph=False</code> here: <a href="https://github.com/pytorch/pytorch/blob/a46d56f988505547b0779838a022970b79123b3c/torch/csrc/autograd/engine.cpp#L667-L669" class="inline-onebox">pytorch/engine.cpp at a46d56f988505547b0779838a022970b79123b3c · pytorch/pytorch · GitHub</a></p>

username: ConnollyLeon
cooked: <p>That’s incredible. Thank you!</p>
How to initialize the weights of LSTM

username: Sjk_Jae
cooked: <p><img src="https://discuss.pytorch.org/uploads/default/original/3X/0/4/0470792953456407fa89b54aa134fe22c56dea80.png" alt="image" data-base62-sha1="DgSJqY8ztaAaPNRb3nq9Rh9fVe" width="271" height="43"><br>
<img src="https://discuss.pytorch.org/uploads/default/original/3X/6/0/60f85cabcf93f2261db380b3ce48b6db6ae65fb4.png" alt="image" data-base62-sha1="dPPWUTX3cCjEn1ZIASSY9E0R2JK" width="600" height="55"><br>
Those weight variables cannot be used according to the LSTM documentation. How can I initialize those weight variables?</p>

username: ptrblck
cooked: <p>You are trying to access an attribute of <code>nn.LSTM</code>:</p>
<pre><code class="lang-python">model = nn.LSTM(1, 1)
print(model.weight_ih_l0)
&gt; Parameter containing:
  tensor([[-0.0077],
          [ 0.1228],
          [-0.4166],
          [-0.9885]], requires_grad=True)
</code></pre>
<p>while your current object seems to be an object of the <code>LstmRNN</code> class, which is not a PyTorch module, but seems to be a custom class.<br>
Depending on the class definition I assume that you might be using an <code>nn.LSTM</code> module internally, e.g. via <code>lstm_model.lstm</code> or any other name.</p>
I want to check the compilation and distribution parts

username: j_sun
cooked: <p>Hi. I want to check the compilation and distribution parts of Pytorch.<br>
It would be appreciated if you let me know which part of the code is the part that compiles and distributes Pytorch.</p>

username: ptrblck
cooked: <p>I assume you are interested in the building steps to create the pip wheels and conda binaries.<br>
If so, then take a look at the <a href="https://github.com/pytorch/builder">pytorch/builder</a> repository.</p>
Dataset for CNN regression?

username: mattbev
cooked: <p>Wondering if there are any datasets available that have image data and a numerical target (not categorical)? I.e. for image-based regression where the target label could be a <code>1xN</code> numerical tensor.</p>
<p>I’m working on a conditional CVAE to generate images, however my custom dataset is small and I am looking for a large one to validate the architecture on.</p>

username: fadetoblack
cooked: <p>Hi <a class="mention" href="/u/mattbev">@mattbev</a></p>
<p>Welcome to the PyTorch community!</p>
<p>You can consider object counting datasets, the idea is that object counting can be formulated as a regression problem. Here are some links:</p>
<ol>
<li><a href="https://www.robots.ox.ac.uk/~vgg/research/counting/index.html" class="inline-onebox" rel="noopener nofollow ugc">Visual Geometry Group - University of Oxford</a></li>
<li><a href="https://paperswithcode.com/task/object-counting" class="inline-onebox" rel="noopener nofollow ugc">Object Counting | Papers With Code</a></li>
<li><a href="https://arxiv.org/abs/2008.12470" class="inline-onebox" rel="noopener nofollow ugc">[2008.12470] Counting from Sky: A Large-scale Dataset for Remote Sensing Object Counting and A Benchmark Method</a></li>
<li><a href="https://www.kaggle.com/fmena14/crowd-counting" class="inline-onebox" rel="noopener nofollow ugc">Crowd Counting | Kaggle</a></li>
<li><a href="http://visal.cs.cityu.edu.hk/static/pubs/conf/cvpr08-peoplecnt.pdf" rel="noopener nofollow ugc">http://visal.cs.cityu.edu.hk/static/pubs/conf/cvpr08-peoplecnt.pdf</a></li>
</ol>
<p>Hope this helps!</p>
Best way to extract image patches around a set of landmarks

username: tdeboissiere
cooked: <p>Gday all,</p>
<p>say I have</p>
<ul>
<li>a batch of images (batch_size,  channel, height, width).</li>
<li>a batch of landmarks (batch_size, n_landmarks,  2) (i.e. for each image, we have n_landmarks, each landmark is a (x,y) tuple of coordinates in the image)</li>
</ul>
<p>For each image, I want to extract a patch of size (channel, patch_height, width_height) around each of the corresponding landmarks. The tensorflow equivalent is <a href="https://www.tensorflow.org/api_docs/python/tf/image/extract_glimpse" rel="nofollow noopener">https://www.tensorflow.org/api_docs/python/tf/image/extract_glimpse</a></p>
<p>I have a numpy implementation here:  <a href="https://gist.github.com/tdeboissiere/4e1ff2de0ceae5704bbaf06ceb9fd301" rel="nofollow noopener">https://gist.github.com/tdeboissiere/4e1ff2de0ceae5704bbaf06ceb9fd301</a></p>
<p>This patch extraction occurs several times within the pipeline so I’d like to do it on the GPU side. Is there any smart way to do it without having to write tons of loops ?</p>

username: Frida
cooked: <p><img src="https://discuss.pytorch.org/images/emoji/apple/grinning.png?v=9" title=":grinning:" class="emoji" alt=":grinning:"> solution?</p>

username: marioviti
cooked: <p>I’m also very interested!</p>

username: sjzcv
cooked: <p>Here is an implementation:</p><aside class="onebox githubblob">
  <header class="source">
      <a href="https://github.com/jimmysue/xvision/blob/main/xvision/ops/extract_glimpse.py#L14" target="_blank" rel="noopener nofollow ugc">github.com</a>
  </header>
  <article class="onebox-body">
    <h4><a href="https://github.com/jimmysue/xvision/blob/main/xvision/ops/extract_glimpse.py#L14" target="_blank" rel="noopener nofollow ugc">jimmysue/xvision/blob/main/xvision/ops/extract_glimpse.py#L14</a></h4>
<pre class="onebox"><code class="lang-py"><ol class="start lines" start="4" style="counter-reset: li-counter 3 ;">
<li>from typing import Union, Tuple</li>
<li>
<li>"""</li>
<li>tf.image.extract_glimpse(</li>
<li>    input, size, offsets, centered=True, normalized=True, noise='uniform',</li>
<li>    name=None</li>
<li>)</li>
<li>"""</li>
<li>
<li>
<li class="selected">def extract_glimpse(input: torch.Tensor, size: Tuple[int, int], offsets, centered=True, normalized=True, mode='bilinear'):</li>
<li>    # similar usage with ft.image.extract_glimpse:</li>
<li>    #   https://www.tensorflow.org/api_docs/python/tf/image/extract_glimpse</li>
<li>    # input: [B, C, H, W]</li>
<li>    # size:  [int, int]  specified the size of glimpse, height comes first</li>
<li>    # offsets: [B, 2]</li>
<li>    W, H = input.size(-1), input.size(-2)</li>
<li>
<li>    if normalized and centered:</li>
<li>        offsets = (offsets + 1) * offsets.new_tensor([W/2, H/2])</li>
<li>    elif normalized:</li>
</ol></code></pre>


  </article>
  <div class="onebox-metadata">
    
    
  </div>
  <div style="clear: both"></div>
</aside>