我面临着将三个张量连接在一起,使得3个px1张量成为一个3px1张量.
我能想出的最简洁的是:
torch.Tensor{v2:totable(),v4:totable(),v6:totable()}:view(3*p,1)
Run Code Online (Sandbox Code Playgroud)
有没有办法在不转换为表格和返回张量的情况下执行此操作?似乎应该有一种将张量连接到某个指定维度的通用方法,假设它们具有兼容的形状.
我可以看到如何编写这样一个函数,一个不存在吗?
我有一个整数类标签的字节张量,例如来自MNIST数据集.
1
7
5
[torch.ByteTensor of size 3]
Run Code Online (Sandbox Code Playgroud)
如何使用它来创建1-hot向量的张量?
1 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 1 0 0 0
0 0 0 0 1 0 0 0 0 0
[torch.DoubleTensor of size 3x10]
Run Code Online (Sandbox Code Playgroud)
我知道我可以通过一个循环来做到这一点,但我想知道是否有任何聪明的Torch索引会在一行中为我提供.
PyTorch的torch.transpose
功能仅转换2D输入.文档在这里.
另一方面,Tensorflow的tf.transpose
功能允许您转置N
任意尺寸的张量.
有人可以解释为什么PyTorch没有/不能具有N维转置功能吗?这是否是由于PyTorch中的计算图构造与Tensorflow的Define-then-Run范式的动态特性?
我在计算神经网络的损失时遇到了问题。我不确定为什么程序需要一个长对象,因为我所有的张量都是浮点形式。我查看了具有类似错误的线程,解决方案是将张量转换为浮点数而不是长整数,但这在我的情况下不起作用,因为我的所有数据在传递到网络时已经是浮点数形式。
这是我的代码:
# Dataloader
from torch.utils.data import Dataset, DataLoader
class LoadInfo(Dataset):
def __init__(self, prediction, indicator):
self.prediction = prediction
self.indicator = indicator
def __len__(self):
return len(self.prediction)
def __getitem__(self, idx):
data = torch.tensor(self.indicator.iloc[idx, :],dtype=torch.float)
data = torch.unsqueeze(data, 0)
label = torch.tensor(self.prediction.iloc[idx, :],dtype=torch.float)
sample = {'data': data, 'label': label}
return sample
# Trainloader
test_train = LoadInfo(train_label, train_indicators)
trainloader = DataLoader(test_train, batch_size=64,shuffle=True, num_workers=1,pin_memory=True)
# The Network
class NetDense2(nn.Module):
def __init__(self):
super(NetDense2, self).__init__()
self.rnn1 = nn.RNN(11, 100, 3)
self.rnn2 = nn.RNN(100, 500, 3)
self.fc1 = …
Run Code Online (Sandbox Code Playgroud) python floating-point torch recurrent-neural-network pytorch
我一直在尝试各种选项来加速 PyTorch 中的一些 for 循环逻辑。执行此操作的两个明显选项是使用numba或编写自定义 C++ 扩展。
\n作为一个例子,我从数字信号处理中选择了“可变长度延迟线”。使用简单的 Python for 循环可以简单但低效地编写此代码:
\ndef delay_line(samples, delays):\n """\n :param samples: Float tensor of shape (N,)\n :param delays: Int tensor of shape (N,)\n \n The goal is basically to mix each `samples[i]` with the delayed sample\n specified by a per-sample `delays[i]`.\n """\n for i in range(len(samples)):\n delay = int(delays[i].item())\n index_delayed = i - delay\n if index_delayed < 0:\n index_delayed = 0\n\n samples[i] = 0.5 * (samples[i] + samples[index_delayed])\n …
Run Code Online (Sandbox Code Playgroud) 我在将 torch 导入到我的项目中时遇到问题,当我尝试导入它时,出现以下错误:
ImportError: Failed to load PyTorch C extensions:
It appears that PyTorch has loaded the `torch/_C` folder
of the PyTorch repository rather than the C extensions which
are expected in the `torch._C` namespace. This can occur when
using the `install` workflow. e.g.
$ python setup.py install && python -c "import torch"
This error can generally be solved using the `develop` workflow
$ python setup.py develop && python -c "import torch" # This should succeed
or by running Python …
Run Code Online (Sandbox Code Playgroud) 当StanfordNLP的TreeLSTM与具有> 30K实例的数据集一起使用时,会导致LuaJit错误地显示"Not Enough Memory".我正在使用LuaJit Data Structures解决这个问题.为了将数据集放在lua堆之外,需要将树放在LDS.Vector中.
由于LDS.Vector包含cdata,第一步是将Tree类型转换为cdata对象:
local ffi = require('ffi')
ffi.cdef([[
typedef struct CTree {
struct CTree* parent;
int num_children;
struct CTree* children [25];
int idx;
int gold_label;
int leaf_idx;
} CTree;
]])
Run Code Online (Sandbox Code Playgroud)
在read_data.lua中还需要进行一些小的更改来处理新的cdata CTree类型.到目前为止,使用LDS似乎是解决内存限制的合理方法; 但是,CTree需要一个名为"composer"的字段.
Composer的类型为nn.gModule.继续使用此解决方案将涉及将nn.gModule的typedef创建为cdata,包括为其成员创建typedef.在继续之前,这似乎是正确的方向吗?有没有人遇到过这个问题?
我试图将一些字符串表保存到Torch中的文件.我尝试过使用Deepmind的这个Torch扩展:hdf5.
require 'hdf5'
label = {'a', 'b','c','d'}
local myFile = hdf5.open(features_repo .. 't.h5', 'w')
myFile:write('label', label)
myFile:close()
Run Code Online (Sandbox Code Playgroud)
我收到错误:
/home/user/torch/install/bin/luajit: ...e/user/torch/install/share/lua/5.1/hdf5/group.lua:222: torch-hdf5: writing data of type string is not supported
Run Code Online (Sandbox Code Playgroud)
Torch Tensors按预期写入文件.
我也尝试使用matio写入mat文件(对于MatLab).我收到此错误:
bad argument #1 to 'varCreate' (cannot convert 'number' to 'const char *')
Run Code Online (Sandbox Code Playgroud) 由于我只有AMD A10-7850 APU,而且没有足够的资金用于购买价值800美元至1200美元的NVIDIA显卡,我试图通过我拥有的资源来实现,以便通过tensorflow/keras加速深度学习.
最初,我使用了Tensorflow的预编译版本.InceptionV3需要大约1000-1200秒来计算1个纪元.它一直很缓慢.
为了加快计算速度,我首先使用优化器(使用AVX和SSE4指令)自行编译Tensorflow.这导致计算时间减少约40%.上面执行的相同计算现在只需要大约600秒来计算.它几乎可以忍受 - 有点像你可以看油漆干.
我正在寻找进一步减少计算时间的方法.我只有一个集成的AMD显卡,它是APU的一部分.(如何)(C/c)我利用这个资源来进一步加速计算?
更一般地说,让我们说有其他人有类似的货币限制和英特尔设置.没有分立的NVIDIA显卡的人如何利用他们的集成显卡芯片或其他非NVIDIA设置来实现比仅CPU性能更快的速度?那可能吗?为什么/为什么不呢?要实现这一目标需要做些什么?或者这可能在不久的将来(2-6个月)?怎么样?
我看了很多页面,或者不能按照他们的说法去做,因为他们不清楚和/或我的知识还不够.
我想跑:
luarocks install https://raw.githubusercontent.com/qassemoquab/stnbhwd/master/stnbhwd-scm-1.rockspec
这样我就可以使用GPU加速在一些图像上运行DenseCap.当我运行它时,我收到此错误:
$ luarocks install https://raw.githubusercontent.com/qassemoquab/stnbhwd/master/stnbhwd-scm-1.rockspec
Using https://raw.githubusercontent.com/qassemoquab/stnbhwd/master/stnbhwd-scm-1.rockspec... switching to 'build' mode
Cloning into 'stnbhwd'...
remote: Counting objects: 24, done.
remote: Compressing objects: 100% (23/23), done.
remote: Total 24 (delta 0), reused 14 (delta 0), pack-reused 0
Receiving objects: 100% (24/24), 19.42 KiB | 0 bytes/s, done.
Checking connectivity... done.
cmake -E make_directory build && cd build && cmake .. -DCMAKE_BUILD_TYPE=Release -DCMAKE_PREFIX_PATH="/home/tex/torch/install/bin/.." -DCMAKE_INSTALL_PREFIX="/home/tex/torch/install/lib/luarocks/rocks/stnbhwd/scm-1" && make
-- The C compiler identification is GNU 5.4.0
-- …
Run Code Online (Sandbox Code Playgroud)