小编uke*_*emi的帖子

无法将 pytorch 张量发送到 cuda

我创建了一个火炬张量,我希望它进入 GPU,但它没有。这太破了。怎么了?

def test_model_works_on_gpu():
    with torch.cuda.device(0) as cuda:
        some_random_d_model = 2 ** 9
        five_sentences_of_twenty_words = torch.from_numpy(np.random.random((5, 20, T * d))).float()
        five_sentences_of_twenty_words_mask = torch.from_numpy(np.ones((5, 1, 20))).float()
        pytorch_model = make_sentence_model(d_model=some_random_d_model, T_sgnn=T, d_sgnn=d)

        five_sentences_of_twenty_words.to(cuda)
        five_sentences_of_twenty_words_mask.to(cuda)
        print(type(five_sentences_of_twenty_words), type(five_sentences_of_twenty_words_mask))
        print(five_sentences_of_twenty_words.is_cuda, five_sentences_of_twenty_words_mask.is_cuda)
        pytorch_model.to(cuda)
        output_before_match = pytorch_model(five_sentences_of_twenty_words, five_sentences_of_twenty_words_mask)

        assert output_before_match.shape == (5, some_random_d_model)
        print(type(output_before_match))
        print(output_before_match.is_cuda, output_before_match.get_device())
Run Code Online (Sandbox Code Playgroud)
tests/test_model.py:58: RuntimeError

<class 'torch.Tensor'> <class 'torch.Tensor'>
False False
<class 'torch.Tensor'>

>       print(output_before_match.is_cuda, output_before_match.get_device())
E       RuntimeError: get_device is not implemented for tensors with CPU backend
Run Code Online (Sandbox Code Playgroud)

还:

>>> torch.cuda.is_available()
True
>>> torch.cuda.device_count()
2 …
Run Code Online (Sandbox Code Playgroud)

python gpu pytorch

1
推荐指数
1
解决办法
9849
查看次数

Torchvision.transforms 的 Flatten() 实现

我有灰度图像,但我需要将其转换为一维向量的数据集我该怎么做?我在转换中找不到合适的方法:

train_dataset = torchvision.datasets.ImageFolder(root='./data',train=True, transform=transforms.ToTensor())
test_dataset = torchvision.datasets.ImageFolder(root='./data',train=False, transform=transforms.ToTensor())

train_loader = torch.utils.data.DataLoader(dataset=train_dataset, batch_size=4, shuffle=True)
test_loader = torch.utils.data.DataLoader(dataset=test_dataset, batch_size=4, shuffle=False)
Run Code Online (Sandbox Code Playgroud)

python flatten pytorch torchvision

1
推荐指数
1
解决办法
1155
查看次数

如何指定pytorch nn.Linear的输入维度?

例如,我定义了一个模型,如下所示:

class Net(nn.module):
   def __init__():
      self.conv11 = nn.Conv2d(in_channel,out1_channel,3)
      self.conv12 = nn.Conv2d(...)
      self.conv13 = nn.Conv2d(...)
      self.conv14 = nn.Conv2d(...)
      ...
      #Here is the point
      flat = nn.Flatten()
      #I don't want to compute the size of data after flatten, but I need a linear layer.

      fc_out = nn.Linear(???,out_dim)
Run Code Online (Sandbox Code Playgroud)

问题是线性层,我不想计算线性层输入的大小,但定义模型需要指定它。我怎么解决这个问题?

python deep-learning pytorch

1
推荐指数
1
解决办法
4737
查看次数

如何生成单词中低位和高位字符的所有组合?

如何生成单词中低位和高位字符的所有组合?例如:

'abc'['abc', 'ABC', 'Abc', 'ABc', 'aBC', 'aBc', 'abC', 'Abc']

'ab'['ab', 'AB', 'Ab', 'aB']

python python-3.x

-5
推荐指数
1
解决办法
280
查看次数

标签 统计

python ×4

pytorch ×3

deep-learning ×1

flatten ×1

gpu ×1

python-3.x ×1

torchvision ×1