当我想将模型放到GPU上时,我会得到以下错误:
"RuntimeError:输入和隐藏张量不在同一设备上,在cuda:0处找到输入张量,在cpu上找到隐藏张量“。
然而,上述所有这些都已放在GPU上:
for m in model.parameters():
print(m.device) #return cuda:0
if torch.cuda.is_available():
model = model.cuda()
test = test.cuda() # test is the Input
Windows 10服务器
Pytorch 1.2.0 +库
尽管我尽了最大的努力,遇到了torch.jit.trace,但我还是没能运行RuntimeError: Input, output and indices must be on the current device
我有一个(相当复杂的)模型,我已经把它放在GPU上了,还有一组输入,也放在GPU上。我可以验证所有输入张量和模型参数&缓冲区在同一个设备上:
(Pdb) {p.device for p in self.parameters()}
{device(type='cuda', index=0)}
(Pdb) {p.device for p in self.buff
我正在上关于PyTorch的课程。我想知道为什么我们需要分别告诉torch.utils.data.DataLoader输出它运行在什么设备上。如果模型已经在CUDA上了,为什么它不相应地自动更改输入呢?在我看来,这个模式很有趣:
model.to(device)
for ii, (inputs, labels) in enumerate(trainloader):
# Move input and label tensors to the GPU
inputs, labels = inputs.to(device), labels.to(device)
是否有一个用例,我希
是否有一种方法可以在特定(GPU)设备的上下文中运行pytorch (而不必为每个新的张量(如.to选项)指定设备)?
类似于tensorflow with tf.device('/device:GPU:0'):..。
默认设备似乎是cpu (除非我做错了):
with torch.cuda.device('0'):
a = torch.zeros(1)
print(a.device)
>>> cpu
我是Pytorch的初学者,我被一个问题困扰了好几天。我想保存一个图像,这是在Pytorch张量形式为.mat文件。我看过了,但似乎没有直接的方法将Pytoch张量转换为.mat文件。我找到的一个可能的解决方案是将其转换为numpy数组,但由于我使用的是Nvidia GPU,所以当我尝试将Pytorch张量转换为numpy数组时,它给出了这个错误: fake_images[0] = fake_images[0].numpy() TypeError: can't convert cuda:0 device type tensor to numpy. Use Tensor.cpu() t
我正在尝试在GPU设备上训练pytorch神经网络。为了做到这一点,我将我的输入和网络加载到默认的cuda启用的GPU decive上。然而,当我加载输入时,模型的权重不会停留在cuda张量上。这是我的火车函数
def train(network: nn.Module, name: str, learning_cycles: dict, num_epochs):
# check we have a working gpu to train on
assert(torch.cuda.is_available())
# load model onto gpu
net
在我使用经过训练的PyTorch模型的推理代码中,出了什么问题?
有一条运行时错误消息:“预期CPU张量(获取CUDA张量)”。
import torch
import torch.nn as nn
#from __future__ import print_function
import argparse
from PIL import Image
import torchvision.models as models
import skimage.io
from torch.autograd import Variable as V
from torch.nn import functi
在之后,我运行了:
# confirm TensorFlow sees the GPU
from tensorflow.python.client import device_lib
assert 'GPU' in str(device_lib.list_local_devices())
# confirm Keras sees the GPU
from keras import backend
assert len(backend.tensorflow_backend._get_available_gpus()) > 0
# confirm PyTorch sees
我遵循关于使用V100张量核的方法。我的MWE代码:
$ cat src/wmma.cu
#include <cuda_runtime_api.h>
#include <mma.h>
using namespace nvcuda;
int main(void){
return 0;
}
用CUDA 9.0编写,
$ nvcc src/wmma.cu
src/wmma.cu(10): error: name must be a namespace name
1 error detected in the compilation of "/gpfs0/s
以下两个张量x(形状(BS,N,C))和y(形状(BS,1,C))如下:
BS = x.shape[0]
N = x.shape[1]
out = torch.zeros(size=x.shape)
for i in range(BS):
for j in range(N):
out[i, j, :] = torch.mul(x[i, j, :], y[i, 0, :])
return out
通过这种方式实现的wrapper_native_layer_norm)" 会产生一个错误"RuntimeError:预期所有张量都在同一设备上,但是至少发现了两个
我有一个现有的模型,在这个模型中,我加载了一些预先训练好的权重,然后在pytorch中进行预测(一次一个图像)。我正在尝试将它基本上转换为pytorch闪电模块,并对一些事情感到困惑。 因此,目前,我的模型__init__方法如下所示: self._load_config_file(cfg_file)
# just creates the pytorch network
self.create_network()
self.load_weights(weights_file)
self.cuda(device=0) # assumes GPU and uses one. This
如果我做了这样的事
import torch
x = torch.randn(3)
y = x + 5
默认情况下,所有张量都对应于"cpu“设备。在默认情况下,是否有办法使所有张量都在另一个设备上(例如,"cuda:0")?
我知道,每当创建张量时,我都可以小心地添加.cuda()或指定cuda,但是如果我能够在程序开始时直接更改默认设备并完成它,这样torch.randn(3)就可以从所需的设备中获得,而不必每次都指定它,那就太好了。
还是因为某种原因,这是一件坏事?是否有任何理由我不希望每个张量/操作在默认情况下对库达进行?
我有一个使用BertModel.from_pretrained('test_model')保存的转换器模型
我已经使用google colab的GPU训练了这个模型
然后,我想用BertModel.from_pretrained('test_model/')打开它,但我的本地PC上没有图形处理器。我明白了:
/home/seiji/.local/lib/python3.8/site-packages/torch/cuda/__init__.py:52: UserWarning: CUDA initialization: Found no NVIDIA drive
来自PyTorch
b = torch.rand(10, requires_grad=True).cuda()
b.is_leaf
False
# b was created by the operation that cast a cpu Tensor into a cuda Tensor
e = torch.rand(10).cuda().requires_grad_()
e.is_leaf
True
# e requires gradients and has no operations creating it
f = torch.rand(10, requires_grad=Tr