tensor

How can I slice a PyTorch tensor with another tensor?

醉酒当歌 提交于 2020-05-29 05:12:57
问题 I have: inp = torch.randn(4, 1040, 161) and I have another tensor called indices with values: tensor([[124, 583, 158, 529], [172, 631, 206, 577]], device='cuda:0') I want the equivalent of: inp0 = inp[:,124:172,:] inp1 = inp[:,583:631,:] inp2 = inp[:,158:206,:] inp3 = inp[:,529:577,:] Except all added together, to have a .size of [4, 48, 161] . How can I accomplish this? Currently, my solution is a for loop: left_indices = torch.empty(inp.size(0), self.side_length, inp.size(2)) for batch

Why is pyTorch “destroying” my numpy arrays?

余生长醉 提交于 2020-05-24 05:46:32
问题 I have am working with numpy tensors of shape (N,2,128,128). When trying to visualize these as images (I reconstruct via ifft2), numpy and pyTorch seems to mix things up in an crazy manner ... I have checked with small dummy arrays and when I pass a numpy ndarray to a torch.FloatTensor the values are exactly the same at the same positions (same shape!), but when I try to do an ifft2 on the torch tensor ones, the result is different than on the non-torch tensor! Can someone help me make sense

Pytorch preferred way to copy a tensor

北城以北 提交于 2020-04-07 12:16:12
问题 There seems to be several ways to create a copy of a tensor in Pytorch, including y = tensor.new_tensor(x) #a y = x.clone().detach() #b y = torch.empty_like(x).copy_(x) #c y = torch.tensor(x) #d b is explicitly preferred over a and d according to a UserWarning I get if I execute either a or d . Why is it preferred? Performance? I'd argue it's less readable. Any reasons for/against using c ? 回答1: According to Pytroch documentation #a and #b are equivalent. It also say that The equivalents

Pytorch preferred way to copy a tensor

我的梦境 提交于 2020-04-07 12:08:08
问题 There seems to be several ways to create a copy of a tensor in Pytorch, including y = tensor.new_tensor(x) #a y = x.clone().detach() #b y = torch.empty_like(x).copy_(x) #c y = torch.tensor(x) #d b is explicitly preferred over a and d according to a UserWarning I get if I execute either a or d . Why is it preferred? Performance? I'd argue it's less readable. Any reasons for/against using c ? 回答1: According to Pytroch documentation #a and #b are equivalent. It also say that The equivalents

Exposing Eigen::Tensor To Python Using Pybind11

心已入冬 提交于 2020-04-07 09:40:29
问题 I am trying to expose an Eigen tensor to python using pybind11. I can compile everything with no issue and can successfully import it to python. However, the data cannot be converted to a python type. I tried two methods. One is directly exposing the data and second using mapping. Both fail in python environment. #include <pybind11/pybind11.h> #include <pybind11/stl.h> #include <pybind11/stl_bind.h> #include <pybind11/numpy.h> #include <pybind11/eigen.h> #include <unsupported/Eigen/CXX11

如何挑选深度学习 GPU?

无人久伴 提交于 2020-03-29 21:12:50
如何挑选深度学习 GPU? 深度学习是一个对计算有着大量需求的领域,从一定程度上来说,GPU的选择将从根本上决定深度学习的体验。因此,选择购买合适的GPU是一项非常重要的决策。那么2020年,如何选择合适的GPU呢?这篇文章整合了网络上现有的GPU选择标准和评测信息,希望能作为你的购买决策的参考。 1 是什么使一个GPU比另一个GPU更快? 有一些可靠的性能指标可以作为人们的经验判断。以下是针对不同深度学习架构的一些优先准则: Convolutional networks and Transformers: Tensor Cores > FLOPs > Memory Bandwidth > 16-bit capability Recurrent networks: Memory Bandwidth > 16-bit capability > Tensor Cores > FLOPs 2 如何选择NVIDIA/AMD/Google NVIDIA的标准库使在CUDA中建立第一个深度学习库变得非常容易。早期的优势加上NVIDIA强大的社区支持意味着如果使用NVIDIA GPU,则在出现问题时可以轻松得到支持。但是NVIDIA现在政策使得只有Tesla GPU能在数据中心使用CUDA,而GTX或RTX则不允许,而Tesla与GTX和RTX相比并没有真正的优势,价格却高达10倍。

tensorflow数据读取机制tf.train.slice_input_producer 和 tf.train.batch 函数

a 夏天 提交于 2020-03-27 21:56:54
tensorflow中为了充分利用 GPU ,减少GPU等待数据的空闲时间,使用了两个线程分别执行数据读入和数据计算。 具体来说就是使用一个线程 源源不断 的将硬盘中的图片数据读入到一个内存队列中,另一个线程负责计算任务,所需数据直接从内存队列中获取。 tf在内存队列之前,还设立了一个文件名队列,文件名队列存放的是参与训练的文件名,要训练 N个epoch,则文件名队列中就含有N个批次的所有文件名。而创建tf的文件名队列就需要使用到 tf.train.slice_input_producer 函数。 tf.train.slice_input_producer是一个tensor生成器,作用是按照设定,每次从一个tensor列表中按顺序或者随机抽取出一个tensor放入文件名队列。 tf.train.slice_input_producer(tensor_list, num_epochs=None, shuffle=True, seed=None, capacity=32, shared_name=None, name=None)  第一个参数 tensor_list:包含一系列tensor的列表,表中tensor的第一维度的值必须相等,即个数必须相等,有多少个图像,就应该有多少个对应的标签。 第二个参数num_epochs: 可选参数,是一个整数值,代表迭代的次数,如果设置 num

tensorflow中协调器 tf.train.Coordinator 和入队线程启动器 tf.train.start_queue_runners

故事扮演 提交于 2020-03-27 21:28:50
TensorFlow的Session对象是支持多线程的,可以在同一个会话(Session)中创建多个线程,并行执行。在Session中的所有线程都必须能被同步终止,异常必须能被正确捕获并报告,会话终止的时候, 队列必须能被正确地关闭。 TensorFlow提供了两个类来实现对Session中多线程的管理:tf.Coordinator和 tf.QueueRunner,这两个类往往一起使用。 Coordinator类用来管理在Session中的多个线程,可以用来同时停止多个工作线程并且向那个在等待所有工作线程终止的程序报告异常,该线程捕获到这个异常之后就会终止所有线程。 使用 tf.train.Coordinator()来创建一个线程管理器(协调器)对象 。 QueueRunner类用来启动tensor的入队线程,可以用来启动多个工作线程同时将多个tensor(训练数据)推送入文件名称队列中,具体执行函数是 tf.train.start_queue_runners , 只有调用 tf.train.start_queue_runners 之后,才会真正把tensor推入内存序列中,供计算单元调用,否则会由于内存序列为空,数据流图会处于一直等待状态 。 tf中的数据读取机制如下图: 调用 tf.train.slice_input_producer,从 本地文件里抽取tensor

Problem with tf.print for tensor (unable to print single values)

ε祈祈猫儿з 提交于 2020-03-25 13:47:14
问题 here there is my code: https://github.com/Franco7Scala/RestrictedAreaImageAdversarialAttack/tree/master/src You should see test_attack.py and l2_attack.py. Now we can go to the problem, in this place of l2_attack-py: mask = tf.ones((batch_size, image_size, image_size, num_channels), tf.float32) # Get input shapes modifier_shape = tf.shape(modifier) mask_shape = tf.shape(mask) # Make indices grid oo, ii, jj, kk = tf.meshgrid(tf.range(modifier_shape[0]), tf.range(modifier_shape[1]), tf.range

Pytorch实现线性回归

那年仲夏 提交于 2020-03-23 05:00:30
前向传播:   tensor中的require_grad参数:     设置为True时,会记录该tensor的计算过程;   tensor中的grad_fn属性:     用来保存计算过程;   tensor设置requires_grad=True后不保留计算过程可使用:     with torch.no_grad():       pass 反向传播:   loss.backward()  #适用于loss(损失值)为一个常数时,大多数时候损失值都为一个标量(常数)   导数保存在tensor.grad中(tensor为参数矩阵)   若设置loss.backward(retain_graph=True),可进行多次backward操作,tensor.grad会累加   只是根据损失值去计算了tensor的梯度,并未更新参数 tensor.data:   在tensor的requires_grad=False(默认)情况下,tensor.data与tensor等价;   在tensor的requires_grad=True时,tensor.data值获取tensor中的数据; tensor.numpy():   requires_grad=True时,tensor.numpy()无法直接转换,需使用tensor.detach().numpy()(等同于tensor.data