akankshakshurai/cuda-error-resolution-analysis huggingface dataset:
DQN noob question

username: afulm
cooked: <p>I’m trying to implement a simple DQN. And I wonder if I have understood it correctly that it’s fine to apply the loss function to only the difference between the (scalar) target and just one element of the output, something like this:</p>
<pre><code class="lang-auto">def fn_reinforce(self,batch): # (state, action, reward, next_state)
        for i in range(self.batch_size):
            if batch[i].next_state is None:
                Q_target = batch[i].reward
                Q_predict = self.policy_net(batch[i].state)[0,batch[i].action]
                loss = self.loss_fn(Q_predict, Q_target)
            else:
                with torch.no_grad():
                    next_Q = torch.max(self.target_net(batch[i].next_state))
                Q_target = batch[i].reward + next_Q
                Q_predict = self.policy_net(batch[i].state)[0, batch[i].action]
                loss = self.loss_fn(Q_predict, Q_target)
            self.optimizer.zero_grad()
            loss.backward()
            for param in self.policy_net.parameters():
                param.grad.data.clamp_(-1, 1)
            self.optimizer.step()
</code></pre>
Torch.topk sorted parameter

username: Rohit_Keshari
cooked: <p>In the documentation torch.topk uses four parameter such as topk(self, k, dim=None, largest=True, sorted=True). In case of sorted=False, it gives ascending order sorted elements. This is quite confusing. If sorted=False it should provide first come first serve elements. Essentially index should be sorted.</p>
<p><a class="mention" href="/u/soumith_chintala">@Soumith_Chintala</a></p>

username: yyzhang
cooked: <p>I met the same error! Hope someone know how to fix it.</p>

username: ptrblck
cooked: <p>The <code>sorted=True</code> setup guarantees that the results will be sorted, but leaving <code>sorted=False</code> can return any ordering (including a sorted one) depending on the used device.</p>

username: yyzhang
cooked: <p>That helps a lot! Thank you very much.</p>
Need help finding good resources to help me load in Video Data in Pytorch

username: SarthakJain
cooked: <p>Hi there,<br>
Recently, I wanted to extend the faster R CNN to a large video dataset that I found called ILSVRC2015.</p>
<p>So, when I started I got it to work on individual images. While I had created the dataset loading functionality as almost per pytorch documentation on object detection, I am not quite sure how to translate or alter that class to a video dataset. I am not really looking for anything specific, but if someone can tell me what I would usually need to include when loading a video dataset class that is different than the class for loading an image dataset, then I would appreciate it. I was having trouble finding good resources that answer this using pytorch, so any documentation/resources will also help.</p>
<p>I have shown my code for loading in individual Image Data incase you are willing to give me specific advice on what changes when switching to processing Video Data. if you want the entire github file for context please respond and I will share it with you.</p>
<pre><code class="lang-auto">class FruitDetectDataset(object):
  def __init__(self, id_labels, id_bounding_boxes, transforms, mode):

    assert len(id_labels) == len(id_bounding_boxes)
    assert sorted(id_labels.keys()) == sorted(id_bounding_boxes.keys())
    self.imgs_key = sorted(id_labels.keys())

    np.random.shuffle(self.imgs_key)
    if (mode == "train"):
      self.imgs_key = self.imgs_key[:int(len(self.imgs_key) * 0.8)]
    else:
      self.imgs_key = self.imgs_key[int(len(self.imgs_key) * 0.8):]

    self.id_labels = id_labels
    self.id_bounding_boxes = id_bounding_boxes
    self.full_image_file_paths = glob.glob("/content/Fruit Defects Dataset /Train/*/*/*.jpeg")

    self.transforms = transforms

  def __getitem__(self, idx):

    img_path = ffile_path(self.imgs_key[idx], self.full_image_file_paths) 
    img = cv2.cvtColor(cv2.imread(img_path, cv2.IMREAD_COLOR), cv2.COLOR_BGR2RGB).astype(np.float32) / 255.0

    boxes = convert_min_max(torch.as_tensor(self.id_bounding_boxes[self.imgs_key[idx]], dtype=torch.float32))
    
    labels = torch.as_tensor(self.id_labels[self.imgs_key[idx]], dtype=torch.int64)
    image_id = torch.tensor([idx])
    area = find_area_bb(boxes)

    target = {}
    target["boxes"] = boxes
    target["labels"] = labels
    target["image_id"] = image_id
    target["area"] = area
    
    #Query about transforms for labels of images
    if self.transforms: 
      sample = {
                'image': img,
                'bboxes': target['boxes'],
                'labels': labels
            }

      sample = self.transforms(**sample)
      img = sample['image']
      target['boxes'] = torch.stack(tuple(map(torch.tensor, zip(*sample['bboxes'])))).permute(1, 0)
    
    
    return img, target

  def __len__(self):
    return len(self.imgs_key) ```

Thanks, 
Sarthak Jain</code></pre>
Renet-runtime and training execusion fails

username: jr.r
cooked: <p>Hi, I am trying to execute resnet-runtime example provided in the glow. Here I am getting the following error:<br>
<strong>$./bin/resnet-runtime /test/imagenet/n04548280/ -backend=CPU -max-images=1</strong></p>
<p>glow/lib/Base/Tensor.h:422: glow::Tensor glow::Tensor::getUnowned(llvm::ArrayRef&lt;dim_t&gt;, llvm::ArrayRef&lt;dim_t&gt;) const: Assertion `actualSize() == unownedTensor.actualSize() &amp;&amp; "The size of the unowned tensor " "should be the same as the size of " “the original tensor”’ failed.<br>
Aborted (core dumped)</p>
<p>and I got another error while executing the resenet-training example as below</p>
<p><strong>$./bin/resnet-training /test/image_path_list.txt -backend=CPU</strong><br>
Loading resnet50 model.<br>
WARNING: Logging before InitGoogleLogging() is written to STDERR<br>
I0327 14:22:28.656437 11843 Graph.cpp:5912] Layout requirements checking is enabled<br>
Preparing for training.<br>
Differentiating graph.<br>
Compiling backend.<br>
I0327 14:22:28.959044 11843 Partitioner.cpp:482] The model is too small for applying partition.<br>
Model size : 103058848<br>
Backend Name : CPU<br>
Device memory: 2000000000<br>
I0327 14:22:33.969435 11843 Partitioner.cpp:88] The number of partitions is : 2<br>
I0327 14:22:33.969450 11843 PartitionerUtils.cpp:549] 	 Partition 0:<br>
Name :	resnet50<br>
BackendKind :	CPU<br>
context count :	1<br>
total Memory :	103062848<br>
input size:	103042720<br>
input count :	268<br>
input only from peers count :	267<br>
output size:	4000<br>
constant size:	16128<br>
I0327 14:22:33.969477 11843 PartitionerUtils.cpp:570] 		 LogicalDeviceIDs :	0<br>
I0327 14:22:33.969482 11843 PartitionerUtils.cpp:549] 	 Partition 1:<br>
Name :	resnet50_grad<br>
BackendKind :	CPU<br>
context count :	1<br>
total Memory :	409959712<br>
input size:	205270848<br>
input count :	429<br>
input only from peers count :	0<br>
output size:	204672736<br>
constant size:	16128<br>
I0327 14:22:33.969525 11843 PartitionerUtils.cpp:570] 		 LogicalDeviceIDs :	0<br>
F0327 14:23:07.816742 11843 Image.cpp:638] Check failed: std::equal(localCopy.dims().begin(), localCopy.dims().end(), inputImageData.dims().begin() + 1) All images must have the same dimensions<br>
*** Check failure stack trace: ***<br>
Aborted (core dumped)</p>
<p>All the images are png format with resolution 300x300. I coun’t get the issue here.</p>
<p>Thank you</p>

username: jfix
cooked: <p>I think this might have to do with the image resolution, or something else about the input images. Can you try with the images located at <code>glow/tests/images/imagenet/</code> to see if that works?</p>
Rtx 3060 ti is slow in pytorch

username: ghost86
cooked: <p>I am running the same code on my personal PC with an Rtx 3060 ti and on computing clusters with an old Tesla P100 12gb. On both systems, I installed the latest version of PyTorch and Cuda toolkit with conda.</p>
<p>Could you help me why training on my PC is significantly slower, although my GPU should be stronger.</p>

username: omarfoq
cooked: <p>Hello,</p>
<p>The strength of the GPU is not the only factor, bus size may also play a role. In order to verify can you time the time needed to do some simple operation, without taking into account data transfer</p>

username: nivesh_gadipudi
cooked: <p>Both the GPU’s are different in architectures. However, RTX 30 series are latest and performs better than Tesla in terms of TFLOPS. But, i would suggest you to Change the variables in dataloader namely pin_memory = True, num_workers = (vary according to your system specifications). For instance if you have 20 threads processor do not use all the processors to load your data to GPU. Try to find that sweet spot of num_workers according to your system specifications.</p>

username: ptrblck
cooked: <p>If you are installing the conda binaries or pip wheels, you might be facing <a href="https://github.com/pytorch/pytorch/issues/50153">this issue</a>.<br>
A workaround would be to build PyTorch from source with the latest cudnn version.</p>
Positive and negative edges in a graph using Pytorch geometric

username: mmr
cooked: <p>So I was looking about the definition of positive and negative edges in a graph. I came across this function train_test_split_edges in <a href="https://pytorch-geometric.readthedocs.io/en/latest/_modules/torch_geometric/utils/train_test_split_edges.html#train_test_split_edges" rel="noopener nofollow ugc">this</a> page. According to this function the positive edges in a graph are the upper triangle in an adjacency matrix in a graph and the negative edges are the edges in the lower triangle of the adjacency matrix in a graph, assuming it is an undirected graph. This is my assumption , am I correct about my assumption.<br>
Thanks.</p>
TypeError: expected np.ndarray (got int)

username: shgh
cooked: <p>I get <code>TypeError: expected np.ndarray (got int)</code> from this line in pytorch:</p>
<p><code>a = Variable(torch.from_numpy(a).float().unsqueeze(0))</code></p>
<p>any help for solving this would be appreciated.</p>

username: Dwight_Foster
cooked: <p>Is the variable a an int? Can you print out what it is and the type.</p>

username: shgh
cooked: <p>It is <code>0</code> and the type is <code>&lt;class 'int'&gt;</code></p>

username: Dwight_Foster
cooked: <p>well that would be your problem. You can’t use from numpy on an int. Just do this</p>
<pre><code class="lang-auto">a = Variable(torch.tensor(a).float().unsqueeze(0))
</code></pre>

username: shgh
cooked: <p>thanks but the change would lead to another unsolvable error:</p>
<pre><code class="lang-auto">RuntimeError: mat1 and mat2 shapes cannot be multiplied (1x1 and 3x256)
</code></pre>
<p>which comes from this:</p>
<pre><code class="lang-auto"> def forward(self, a):

        x = F.relu(self.linear1(a))
        x = F.relu(self.linear2(x))
        x = torch.tanh(self.linear3(x))

        return x
</code></pre>

username: Dwight_Foster
cooked: <p>You need to change your input shape in your linear layer to 1.</p>

username: shgh
cooked: <p>How is it possible to reshape the input which is <code>a</code>? I used <code> a = a.view(1)</code> and error still exist.</p>

username: Dwight_Foster
cooked: <p>No not to that large unless you want to copy it a bunch of times. If you want to do that and just copy the value to the shape you need you can do this:</p>
<pre><code class="lang-auto">a = torch.full(shape_you_need, a)
</code></pre>

username: shgh
cooked: <p>I don’t fully understand the error <code>RuntimeError: mat1 and mat2 shapes cannot be multiplied (1x1 and 3x256)</code>. Does it say the input of linear layer should be changed to (1x1)? Tried different things in <code>a = torch.full(shape_you_need, a)</code> and the following error persists:</p>
<pre><code class="lang-auto">TypeError: full() received an invalid combination of arguments - got (tuple, Tensor), but expected one of:
 * (tuple of ints size, Number fill_value, *, Tensor out, torch.dtype dtype, torch.layout layout, torch.device device, bool pin_memory, bool requires_grad)
 * (tuple of ints size, Number fill_value, *, tuple of names names, torch.dtype dtype, torch.layout layout, torch.device device, bool pin_memory, bool requires_grad)

</code></pre>

username: Dwight_Foster
cooked: <p>Yes the error is basically saying that the input size of your linear layer and the shape of your input need to match but they do not. And can you provide the new code with torch.full?</p>

username: shgh
cooked: <pre><code class="lang-auto"> a = torch.full((1,1), a)

</code></pre>

username: Dwight_Foster
cooked: <p>The shape needs to be the same shape as the number of linear inputs so</p>
<pre><code class="lang-auto"> a = torch.full((1,256), a)
</code></pre>
<p>also it needs to be the a before you turn it into a tensor so when it is still an int.</p>
Create Dataloader that samples even number of data from each class

username: Dylan_Yung
cooked: <p>I need to create a dataloader that samples random datapoints from each class or even maybe given a probability distribution it samples that proportion from each class. Is this possible?</p>

username: omarfoq
cooked: <p>Hello,</p>
<p>Yes you can  implement a <code>Sampler</code> (See <a href="https://pytorch.org/docs/stable/data.html#torch.utils.data.Sampler" rel="noopener nofollow ugc">here</a> for details)</p>

username: Dylan_Yung
cooked: <p>may I have an example of creating your own sampler for something like this? I’m assuming I have to got through the dataset sequentially and separate it into different classes as well?</p>

username: urukhai
cooked: <p>Have a look at Detectron2’s <a href="https://detectron2.readthedocs.io/en/latest/modules/data.html?highlight=RepeatFactorTrainingSampler#detectron2.data.samplers.RepeatFactorTrainingSampler" rel="noopener nofollow ugc">RepeatFactorTrainingSampler</a>, for an example of one approach. Even if you don’t use a Detectron2 model, the sampler is just standard Pytorch code and should work fine with other models.  I found that it didn’t deal properly with empty images, however, and I modified it a bit for my own use.</p>
Data loader for continuous outcome

username: kindlychung
cooked: <p>I have a directory of images, the label for each of them is a float between 0 and 1. How can I load this dataset? The plan is to build a neural net regression model.  Thanks!</p>

username: marksale
cooked: <p>Kaiyin,<br>
I have the same question, I’m trying to predicted users rating of an image. I actually have the raw data that generated the image (a scientific plot, the user is asked to rate how well the plot “fits” the data), did  you get an answer to this question?<br>
thanks<br>
Mark</p>
'torch.classes' has no attribute 'load_library'

username: G.M
cooked: <p>I  was following the instructions <a href="https://pytorch.org/tutorials/advanced/torch_script_custom_classes.html" rel="nofollow noopener">here</a> to create a custom c++ class and use it in Python with TouchScript. But I’m having trouble loading the library. Here’s my code and the stacktrace:</p>
<pre><code class="lang-auto">&gt;&gt;&gt; import torch
&gt;&gt;&gt; torch.classes.load_library('Array.cp37-win_amd64.pyd')
Traceback (most recent call last):
  File "&lt;stdin&gt;", line 1, in &lt;module&gt;
AttributeError: module 'torch.classes' has no attribute 'load_library'
</code></pre>
<p>The custom operator <a href="https://pytorch.org/tutorials/advanced/torch_script_custom_ops.html" rel="nofollow noopener">tutorial</a> worked fine for me: I can load the script properly. Does anyone know the reason behind this error or how to solve it?</p>
<p>Thanks.</p>

username: ptrblck
cooked: <p>Could you print your PyTorch version via <code>print(torch.__version__)</code>?<br>
The method seems to have landed on Jan 23rd (<a href="https://github.com/pytorch/pytorch/commit/8ed1dd528e38b7c433b4a2a2fa05bd01e1cbba9e">PR</a>), so you might want to install a nightly build.</p>

username: G.M
cooked: <p>Thanks for replying.<br>
I’m using pytorch 1.4.0 on cpu so I guess this isn’t supported. Still, I think that the tutorial was there long before Jan 23rd, am I correct? If I’m correct, what was used to load classes then?</p>

username: 111497
cooked: <p>i meet the same error in run .py file, i want to konw the solution when you work out the problem.Thanks.</p>
Pytorch on jetson nano cuda 10.0

username: elektrische
cooked: <p>How can I install pytorch for jetson nano on cuda 10.0, pip3 and python 3.6</p>

username: ptrblck
cooked: <p>You could use <a href="https://github.com/pytorch/pytorch#nvidia-jetson-platforms">these binaries</a>. <img src="https://discuss.pytorch.org/images/emoji/apple/wink.png?v=9" title=":wink:" class="emoji" alt=":wink:"></p>

username: mheriyanto
cooked: <p>Thanks <a class="mention" href="/u/ptrblck">@ptrblck</a> <img src="https://discuss.pytorch.org/images/emoji/apple/grinning.png?v=9" title=":grinning:" class="emoji" alt=":grinning:"></p>
Deep learning in real world

username: Tornike
cooked: <p>Hello, I have a non-proffesional question.</p>
<p>I wonder how deep learning code is used in real world?<br>
Looking at training Time which sometimes takes too long. How Deep learning code is used in real program ? Does it Train the model on every execution of “exe” file ?</p>

username: AbdulsalamBande
cooked: <p>Deep Learning is trained on data not exe Files. Lets take an example.<br>
Lets say you want to make a recognition software to tell you if an image is a dog or a  cat. Deep Learning Code (Model) takes thousands of images of dogs and cats. Then it will learn the difference between them using some mathematical function.</p>

username: aza
cooked: <p>Training time vs inference (“real world”) time have many different features.</p>
<p>Training time for starters is longer because we are exposing the model to considerably more samples – multiple times → a dataset set size of 10,000 samples for 10 epochs is 100,000 operations of the data being passed through the model. In addition at training time we are doing calculations on the backward pass (to train the model) – this takes times. Luckily GPUs allow us to parallelize much of this</p>
<p>At inference time you are only working with one (or a few samples) and the model is only being exposed to that one time – we also have the benefit of not needing to do some work on the backward pass. Depending on how big your inference batch is - oftentimes a CPU may suffice – making the real world problem much more tenable</p>
Porting batch normalization from TF to PyTorch

username: sans_sense
cooked: <p>A cheat sheet for porting batch normalization from TF model to Pytorch</p>
<p><strong>Param Mapping</strong><br>
bn/gamma →  bn.weight<br>
bn/beta → bn.bias<br>
bn/moving_mean → bn.running_mean<br>
bn/moving_variance → bn.running_var</p>
<p>mean and variance are not trainable params, we will need to read them in for inference.</p>
<p><strong>eps</strong><br>
Also set eps explicitly to 1e-3 if you are using tf default as default in pytorch is 1e-5.</p>
Does pytorch do matrix multiplication faster than matrix inverse

username: Qinsheng_Zhang
cooked: <p>In <a href="https://en.wikipedia.org/wiki/Computational_complexity_of_mathematical_operations" rel="nofollow noopener">wiki</a>, it is reported that the matrix multiplication and matrix inverse similar time complexity, Optimized CW-like algorithms method. Pytorch does some optimization on matrix manipulations. So in practice, are matrix multiplication and inverse consumed similar time or multiplication is much cheaper? is it different between CPU  and GPU?</p>

username: Sia_Rezaei
cooked: <p>Did you find the answer to this? I’m interested as well.</p>

username: nivesh_gadipudi
cooked: <p>If the matrices are square with same dimensions, yes the time complexity of matrix manipulation’s are similar.</p>
<p>Coming to GPU or CPU is faster. From my personal experience if you are using very very small networks and you have a higher core processor which can fit your data, you can go for CPU, in this case the device to host and host to device data transfer overhead seems higher compared to calculations. But when you have a larger model like 2D/3D CNNs etc,. You choose to GPU for parallel tensor computations.</p>
How to reduce size of a tensor

username: Simeon
cooked: <p>Hello All,<br>
I have a tensor of size(2, 107136, 7) with 1499904 (2x107136x7) total elements and I want to reshape this into (2, 488, 7 = 2x488x7=6832 elements in total).   Does anyone have an idea how to reshape by reducing the number of elements? It generated an error RuntimeError: shape ‘[2, 488, 7]’ is invalid for the input of size 1499904(multiple of sizes). Here is what I did it.<br>
x  has a size of (2, 107136, 7) and then I did:<br>
x = torch.reshape(x, (2, 488, 7))</p>

username: AbdulsalamBande
cooked: <p>The error is because <code>(2 x 488 x 7)</code> is not equal to <code>( 2 x 107136 x 7)</code>. You can tru slicing<br>
Example</p>
<blockquote>
<p>a  = a[:,0:488,:]</p>
</blockquote>
<p>EDIT : But you will loose some information</p>

username: Simeon
cooked: <p>Thanks, <a class="mention" href="/u/abdulsalambande">@AbdulsalamBande</a>. It seems it’s working. Slicing works.</p>

username: nivesh_gadipudi
cooked: <p>You can’t reshape the tensor into an other having unequal elements and slicing is not a way to reshape, you are going to lose a lot of valuable information.</p>

username: Simeon
cooked: <p><a class="mention" href="/u/nivesh_gadipudi">@nivesh_gadipudi</a> That’s true.</p>
Torch profiler model_inference time discrepency

username: Sushma_Rao
cooked: <p>I ran my model with the torch profiler enabled, based on the example provided here - <a href="https://pytorch.org/tutorials/recipes/recipes/profiler_recipe.html#steps" class="inline-onebox" rel="noopener nofollow ugc">PyTorch Profiler — PyTorch Tutorials 2.2.0+cu121 documentation</a></p>
<p>with profiler.profile(record_shapes=True) as prof:<br>
with profiler.record_function(“model_inference”):<br>
model(inputs)</p>
<p>The resulting table has a large number for model_inference time, but the sum of all the individual calls don’t add up to this number. I’ve checked all columns - self cpu, cpu total, cuda total etc. Does this imply that there is some time that is not accounted for in the profile? How should I interpret model_inference time? How can I go about figuring out where that extra time is going? Any suggestions are much appreciated.</p>
<p><div class="lightbox-wrapper"><a class="lightbox" href="https://discuss.pytorch.org/uploads/default/original/3X/a/4/a4e9c8218037a522b339b8c09cacde41bfbca2a7.jpeg" data-download-href="https://discuss.pytorch.org/uploads/default/a4e9c8218037a522b339b8c09cacde41bfbca2a7" title="Screen Shot 2021-04-01 at 12.39.03 PM"><img src="https://discuss.pytorch.org/uploads/default/optimized/3X/a/4/a4e9c8218037a522b339b8c09cacde41bfbca2a7_2_690x159.jpeg" alt="Screen Shot 2021-04-01 at 12.39.03 PM" data-base62-sha1="nwTbaWFgIVXmEWjE5lJ3kX0dB5B" width="690" height="159" srcset="https://discuss.pytorch.org/uploads/default/optimized/3X/a/4/a4e9c8218037a522b339b8c09cacde41bfbca2a7_2_690x159.jpeg, https://discuss.pytorch.org/uploads/default/optimized/3X/a/4/a4e9c8218037a522b339b8c09cacde41bfbca2a7_2_1035x238.jpeg 1.5x, https://discuss.pytorch.org/uploads/default/optimized/3X/a/4/a4e9c8218037a522b339b8c09cacde41bfbca2a7_2_1380x318.jpeg 2x" data-dominant-color="F1F1F1"><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">Screen Shot 2021-04-01 at 12.39.03 PM</span><span class="informations">3986×922 544 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>
Linear Layer memory leak

username: Kabir_Nagrecha
cooked: <p>When I run a basic linear layer, I encounter a strange memory leak. See the code below:</p>
<pre><code class="lang-auto">from pynvml import nvmlInit, nvmlDeviceGetHandleByIndex, nvmlDeviceGetMemoryInfo
import torch
import gc

def get_free_space(idx=0):
    nvmlInit()
    h = nvmlDeviceGetHandleByIndex(idx)
    info = nvmlDeviceGetMemoryInfo(h)
    return info.free


linear_layer = torch.nn.Linear(768, 768).to("cuda:0")

with torch.no_grad():
    print(get_free_space(0))
    a_detach = torch.zeros((128, 129, 768)).to("cuda:0")
    d = linear_layer(a_detach)
    del d
    del a_detach

    gc.collect()
    torch.cuda.empty_cache()
    print(get_free_space(0))
</code></pre>
<p>Output:<br>
7680950272<br>
7639007232</p>
<p>The code run within the no_grad loop should ONLY be creating two tensors a_detach and d, both of which are promptly deleted. So why was the additional 50MB of memory lost?</p>
<p>Note that this leak continues to occur even after deleting the layer itself! (del linear_layer after the other dels).</p>

username: googlebot
cooked: <p>perhaps for cublas kernels | context | buffers - this should be a one-time thing (i.e. lazy library init)</p>

username: Kabir_Nagrecha
cooked: <p>Is there a way to explicitly call this in advance? I need to ensure replicable memory usage.</p>

username: googlebot
cooked: <p>just do some linear algebra operation:<br>
a=torch.zeros(2,2,device=“cuda”)<br>
a.mm(a)</p>
<p>though I don’t see a point, this initialization is usually deterministic</p>
Since Pytorch for Radeon GPUs is available, how to put data on device

username: Horst_G
cooked: <p>Hey,<br>
since there is the ROCm 4.0 version provided from PyTorch, which (if I am not wrong) provides the interfaces for other GPUs than nvidia, how can I select my gpu as device?<br>
E.g. device = torch.device(“cuda”) does not work with radeons.<br>
I want to know, how I can put the e.g. CNN onto the GPU. I am glad for any help.</p>
<p>Best</p>
<p>Horst</p>
How do I compile libtorch for nvidia jetson nano

username: Kiki_Rizki_Arpiandi
cooked: <p>I have follow the instruction <a href="https://forums.developer.nvidia.com/t/pytorch-for-jetson-nano-version-1-5-0-now-available/72048" rel="nofollow noopener">here</a> to build libtorch from source, the compilation complete without error but when I run the minimum c++ example from the official <a href="https://pytorch.org/tutorials/advanced/cpp_frontend.html" rel="nofollow noopener">website</a></p>
<p>It raise compilation error :</p>
<pre><code class="lang-auto"> error: ‘size_t’ is not a member of ‘std’
     : public integral_constant&lt;std::size_t, sizeof...(_Elements)&gt; { };


Please help !
</code></pre>

username: mheriyanto
cooked: <p>Is there any update here? <img src="https://discuss.pytorch.org/images/emoji/apple/grinning.png?v=9" title=":grinning:" class="emoji" alt=":grinning:"></p>
How to do metric learning for image based recommendations

username: ElisonSherton
cooked: <p>Hi guys!</p>
<p>I am new to metric learning and trying to understand how it works at scale for building content based recommendations by looking at image similarity by representing an image as a n-dimensional feature vector.(n = 128, 512 etc.)</p>
<p>I am looking at <a href="http://mmlab.ie.cuhk.edu.hk/projects/DeepFashion/InShopRetrieval.html" rel="noopener nofollow ugc">in shop clothes retrieval dataset</a> which contains quite a handful of products. Each product has around 3-5 images and theres close to a few thousand products.</p>
<p>Now, many papers recommend variants of contrastive loss or triplet loss functions for training. The definition of a class in these media is mentioned to be at a product level.</p>
<p>However, if we use product as a class, particularly wrt this dataset, then we risk separating the similar items because the way triplet loss works/contrastive loss works is it tries to maximize the embedding distance of images across classes while trying to bring the embeddings of images in one class closer and closer.</p>
<p>But the results obtained are really good which means inter class variance (for similar products) is somehow accounted for and not exploded by use of triplet or contrastive losses.</p>
<p>Can anyone help me understand the intuition behind setting the problem like this?</p>
<p>Thanks and Regards,<br>
Vinayak.</p>
Statements are executed out of order

username: sebastienwood
cooked: <p>Hello !<br>
I am in the process of writing a library to emulate stochastic hardware in Pytorch. A simple statement allows me to turn on/off the stochasticity. However, Pytorch code get executed before this simple statement, even though they are declared in the right order, i.e.:</p>
<pre><code class="lang-auto">manager.random_off() # get executed second
my_module(tensor) # get executed first
</code></pre>
<p>All of this happens in the context of a small test script, on CPU. A few prints helped me to notice it happening. I’m not sure what is happening here, I’m a taker for any help/input/pointer ! Thanks.</p>

username: KFrank
cooked: <p>Hi Sebastien!</p>
<aside class="quote no-group" data-username="sebastienwood" data-post="1" data-topic="116661" 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/sebastienwood/48/9787_2.png" class="avatar"> sebastienwood:</div>
<blockquote>
<p>Pytorch code get executed before this simple statement, even though they are declared in the right order,</p>
</blockquote>
</aside>
<p>Could you simplify and trim down your code as much as possible<br>
while still having it display this behavior?  Then post a self-contained,<br>
runnable script so that we can try to reproduce your issue.  Also, do<br>
tell us what version of pytorch you are using.  For example, you can<br>
include <code>print (torch.__version__)</code> in your script.</p>
<p>Good luck.</p>
<p>K. Frank</p>

username: sebastienwood
cooked: <p>Hi Frank!<br>
My apologies, after booting my PC this morning the issue is not willing to repeat itself. I am using Torch v.1.8.1+cu102 on WSL2 for reference.</p>
<p>Thanks for your time,<br>
Sébastien</p>
Batch processing when samples should go through different layers at the end of the network

username: gebrahimi
cooked: <p>I do have data where not all samples go through same network. To be exact, all data go through the same layers up until some layer in network and after that they diverge based on an id that the sample has. So for instance i have 6 layers of decoder in transformer. all samples go through the first 4 decoders, but the 6th decoder is designed for each specific sample id, and each sample depending on their ID pass through seperate 6th decoder. I am wondering what is the most efficient way to handle that. Obviously one way is to take batches with same id, but this messes up with random sampling. I appreciate any help.</p>
How to batch multiple sequences in an autoencoder structure?

username: samim
cooked: <p>I am having trouble to understand what kind of input  / output mapping would make most sense if I have a vanilla autoencoder structure (unlike LSTM AE).</p>
<p>I have 6 input sequences (sine signals) that I want to map to two output quasi sine signals. The full dataset has the shape (8, 2000000).</p>
<p>Inputs:<br>
<img src="https://discuss.pytorch.org/uploads/default/original/3X/d/d/ddb9c0aa53b89570ed9add67ab614d58016aad38.png" alt="inputs" data-base62-sha1="vDtuDepIpSzPHFuhDFIxJ9kAGZa" width="386" height="262"></p>
<p>Outputs:<br>
<img src="https://discuss.pytorch.org/uploads/default/original/3X/0/1/01f773c2248e3e95b9373d04f25451428ee18a12.png" alt="outputs" data-base62-sha1="hoDLfkCvDfivj3myOWGmQueI82" width="397" height="259"></p>
<p>How should one batch look like? (One period is 400 time steps long.)</p>
<ul>
<li>Do I take 400 steps of each signal and concatenate them? (this would make one very long vector)</li>
<li>Do I take 400 steps of each signal and input each signal separately (mapping it to the same output segment)</li>
<li>Do I take 1 step of each signal and concatenate them?</li>
<li>Do I take X step of each signal, concatenate them, and slide a moving window on the data?</li>
</ul>

username: samim
cooked: <p>Does anyone have a direction for me to go first?</p>
Fine tuning generator from pre-trained discriminator

username: edshkim98
cooked: <p>Hi, I am currently doing a research on semantic segmentation using adversarial loss or GAN.<br>
I have a pre-trained segmentation (generator) and discriminator and I would like to use the pre-trained<br>
discriminator to fine tune my generator. However, as it iterates, the loss or accuracy only changes very little, so I tried changing hyperparamter to extreme, but they are still the same.<br>
For example, I once fine tuned my generator with learning rate 0.005, and the accuracy reached 86.3 at epoch 40 and I tried changing learning rate to extreme like 0.8 and the accuracy reached 86.3 at epoch 40 as well which doesn’t make any sense to me.<br>
I’ve checked that requires_grad is True for all my generator and discriminator.<br>
Can somebody help me with this problem?<br>
Thank you!</p>
Autograd for second derivative

username: Shuo_HAO
cooked: <p>Dear all, I’m working on the physics-informed neural network (PINN), which aims to solve partial differential equations. In the program, I need to implement the second derivative, but the solution on which seems to be not right. Here is my code for doing autograd:</p>
<pre><code class="lang-auto">    def predict(self, xf, tf):
        X = torch.cat((xf,tf),1)
        uv = self.forward(X)
        u = uv[:,0]
        v = uv[:,1]
        
        u_x = torch.autograd.grad(u.sum(), xf, create_graph=True)[0]
        v_x = torch.autograd.grad(v.sum(), xf, create_graph=True)[0]
        
        u_t = torch.autograd.grad(u.sum(), tf, create_graph=True)[0]
        v_t = torch.autograd.grad(v.sum(), tf,  create_graph=True)[0]
        
        u_xx = torch.autograd.grad(u_x.sum(), xf, create_graph=True)[0]
        v_xx = torch.autograd.grad(v_x.sum(), xf, create_graph=True)[0]
        
        f_u = u_t + torch.mul(v_xx,0.5) + torch.mul((u**2 + v**2),v)
        f_v = v_t - torch.mul(u_xx,0.5) - torch.mul((u**2 + v**2),u)
        
        return u, v, u_x, v_x, f_u, f_v
    
    def loss_function(self):
        u0_pred, v0_pred, _, _, _, _ = self.predict(self.x0, self.t_x0)
        u_lb_pred, v_lb_pred, u_x_lb_pred, v_x_lb_pred, _, _= self.predict(self.x_lb,self.t_lb)
        u_ub_pred, v_ub_pred, u_x_ub_pred, v_x_ub_pred, _, _ = self.predict(self.x_ub,self.t_ub)
        _, _, _, _, f_u_pred, f_v_pred = self.predict(self.x_f, self.t_f)
        
        lossmse = self.loss1(u0_pred,self.u0) + \
                  self.loss1(v0_pred,self.v0) + \
                  self.loss1(u_lb_pred,u_ub_pred) + \
                  self.loss1(v_lb_pred,v_ub_pred) + \
                  self.loss1(u_x_lb_pred,u_x_ub_pred) + \
                  self.loss1(v_x_lb_pred,v_x_ub_pred) + \
                  self.loss1(f_u_pred,self.f) + \
                  self.loss1(f_v_pred,self.f)
        return lossmse
</code></pre>
Just what model should I use for my data? Evaluation and graph description of time series data

username: Marvin_Kemper
cooked: <p>2 Infrared Sensors have been deployed in an experiment. They basically function as a simple light barrier, giving out low voltage values when something is between sensor and emitter and high values for a clear path. Objects regularly pass through both sensors right after one another. Parameters of interest are the current object speed (obtainable through sensor distance and the time delay between sensors), object length (from speed and low voltage time), and distance between objects (from speed and high voltage time).<br>
It is actually not that hard to just calculate it using traditional programming, but I am curious if I can solve this problem using machine learning. Also, it could be more efficient when using hundreds or thousands of such setups to evaluate experiments in parallel. Thus I need to solve this with machine learning, yet I just can’t find the right model to make it work.</p>
<p>I have tried using a normal RNN layer, an LSTM layer, multiple LSTM layers (plus linears in between) and a combination of 2 LSTMCells. For Optimizers I tried normal gradient descend, sgd and Adam. I also tried solving it with the LBFGS Algorithm but that did not work either.<br>
I basically need a time averaged output from an alternating input function, yet the average should also change over time according to changes in the input data. The biggest problem i have so far, is that I usually get one constant, averaged value as output after training the network, no matter the optimizer or model I use. So for example to make this as clear as possible, if the object speed during the experiment changes once per minute to a random value between 10 and 30 m/s, the trained network will most definitely always output 20 m/s. No matter what is used as the current input.</p>
<p>If anyone has experience with similar problems or ideas on which model / algorithm / optimizer I should try, please let me know. I am very thankful for all opinions on the matter!</p>