- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
torch 中的一些操作是就地执行的。速记运算符,例如 +=。
其他操作是否可以就地执行,例如softmax
?
我目前正在从事语言处理工作。该模型在大量词汇上生成一长串概率分布。最终输出张量约占分配内存的 60%。这是一个很大的问题,因为我需要计算它的 softmax,这使得所需的内存加倍。
这是问题的一个示例。我对张量 t 不感兴趣,只对它的 softmax 感兴趣:
import numpy as np
import torch
import torch.nn.functional as F
t = torch.tensor(np.zeros((30000,30000))).cuda() #allocates 6.71 GB of GPU
softmax = F.softmax(t, 1) #out of memory error
del t #too late, program crashed
即使下面的方法也不起作用:
F.softmax(torch.tensor(np.zeros((30000,30000))).cuda(), 1)
最佳答案
我创建了一个就地版本的 softmax:
import numpy as np
import torch
import torch.nn.functional as F
# in-place version
t = torch.tensor(np.ones((100,200)))
torch.exp(t, out=t)
summed = torch.sum(t, dim=1, keepdim=True)
t /= summed
# original version
t2 = torch.tensor(np.ones((100,200)))
softmax = F.softmax(t2, 1)
assert torch.allclose(t, softmax)
回答我的问题:如果您想要就地函数,您必须通过将低级操作插入在一起来自己创建它们:
torch.exp
可以指定一个可选的 out
参数。t[idx] = some
就位/=
、*=
、+=
、-=
已到位<这需要仔细调试并且可能不直观:
t = t / summed #not in-place
t /= summed #in-place
我读到就地操作可能会产生渐变问题。我将使用此代码进行更多测试。
关于python - torch 就地操作以节省内存(softmax),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53732209/
我试图找出在将数据传输到设备之前将数据复制到固定内存是否有意义,因为我对输入数据的分配没有影响(它是一个库)。 std::vector idata(WORK_SIZE); int *idata_ali
model.save() 让我很困惑。 示例。我将我的 mongoose.model(mongoose.schema) 移动到单独的 model.js 文件中。当我使用这种方法创建模型时,以下问题困扰
我正在用 Java Swing 制作模拟时钟。要计算我需要的时钟指针角度: 我可以通过 System.currentTimeMillis() 获得的以毫秒为单位的 UTC 时间 时区偏移 TimeZo
编辑 我的应用程序中有声音,该声音在应用程序启动时开始播放。此外,我还有两种播放和停止声音的方法: -(void)playBgMusic { NSString *path = [[NSBundle m
所以我的应用有 1 个 Activity 和 4 个 fragment ,其中一个有 Chronometer 来显示经过了多少时间。 它工作正常,但我有一个问题,每次我移动到另一个 fragment
我是 FireDAC 新手,遇到问题。我想在Delphi XE7中使用FireDAC读写SQLite数据库。我尝试的大部分方法都有效,但我在将 TTime 保存到 SQLite DB 时遇到问题。 这
这是我使用基本 Node.js 服务器创建的示例站点。我能够成功发布、创建给定架构的用户,然后“保存”到数据库;但是,保存不会返回用户对象,并且在我的实际数据库中找不到。连接也返回成功。 main.j
我有一台配备 2 Gb Nvidia GPU 的 Mac Book Pro。我正在尝试利用我所有的 GPU 内存进行计算(python 代码)。如果我绕过 GUI 界面并仅通过命令行访问我的机器,我可
我希望使用 Powerpoint 创建一种测验,我想保存用户提供的输入。 例如:如果我问这个问题:你最喜欢什么颜色?当他们回答问题并单击下一步按钮时,我想保存文本框的值并将其附加到输出文件中。 我已经
我是一名优秀的程序员,十分优秀!