标签: torch

与 torch.no_grad:AttributeError:__enter__

with torch.no_grad:AttributeError: __enter__
Run Code Online (Sandbox Code Playgroud)

我在运行 pytorch 代码时遇到此错误。

我有 torch==0.4.1 torchvision==0.3.0,我在 google colab 中运行代码。

machine-learning deep-learning torch pytorch

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

RuntimeError:预期标量类型 Double 但发现 Float

我是 PyTorch 的新手,我从 cnn 层收到以下错误:“RuntimeError:预期标量类型 Double 但发现 Float”。我将每个元素转换为.astype(np.double)但错误消息仍然存在。然后在转换后Tensor尝试使用.double(),但错误消息仍然存在。这是我的代码,以便更好地理解:

import torch.nn as nn
class CNN(nn.Module):
    
    # Contructor
    def __init__(self, shape):
        super(CNN, self).__init__()
        self.cnn1 = nn.Conv1d(in_channels=shape, out_channels=32, kernel_size=3)
        self.act1 = torch.nn.ReLU()
    # Prediction
    def forward(self, x):
        x = self.cnn1(x)
        x = self.act1(x)
    return x
    
    X_train_reshaped = np.zeros([X_train.shape[0],int(X_train.shape[1]/depth),depth])
    
    for i in range(X_train.shape[0]):
        for j in range(X_train.shape[1]): 
            X_train_reshaped[i][int(j/3)][j%3] = X_train[i][j].astype(np.double)
    
    X_train = torch.tensor(X_train_reshaped)
    y_train = torch.tensor(y_train)
    
    # Dataset w/o any tranformations
    train_dataset_normal = CustomTensorDataset(tensors=(X_train, y_train), transform=None)
    train_loader = …
Run Code Online (Sandbox Code Playgroud)

python torch conv-neural-network pytorch

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

torch.nn.Softmax、tor​​ch.nn.funtional.softmax、tor​​ch.softmax 和 torch.nn.function.log_softmax 有什么区别

我尝试查找文档,但找不到有关 torch.softmax 的任何内容。

torch.nn.Softmax、tor​​ch.nn.funtional.softmax、tor​​ch.softmax 和 torch.nn.function.log_softmax 之间有什么区别?

示例值得赞赏。

python torch softmax pytorch

7
推荐指数
2
解决办法
1万
查看次数

torch meshgrid 警告:在即将发布的版本中,将需要传递索引参数

我正在尝试执行 LIIF( https://github.com/yinboc/liif ​​) 并出现以下警告:

/usr/local/lib/python3.7/dist-packages/torch/functional.py:445: UserWarning: torch.meshgrid: in an upcoming release, it will be required to pass the indexing argument. (Triggered internally at  ../aten/src/ATen/native/TensorShape.cpp:2157.)
  return _VF.meshgrid(tensors, **kwargs)  # type: ignore[attr-defined]
Run Code Online (Sandbox Code Playgroud)

python grid user-warning torch pytorch

7
推荐指数
1
解决办法
2万
查看次数

AttributeError:“Upsample”对象没有属性“recompute_scale_factor”

我在线收到错误x_stats = dec(z).float()

import torch.nn.functional as F

z_logits = enc(x)
z = torch.argmax(z_logits, axis=1)
z = F.one_hot(z, num_classes=enc.vocab_size).permute(0, 3, 1, 2).float()

x_stats = dec(z).float()
x_rec = unmap_pixels(torch.sigmoid(x_stats[:, :3]))
x_rec = T.ToPILImage(mode='RGB')(x_rec[0])

display_markdown('Reconstructed image:')
display(x_rec)
Run Code Online (Sandbox Code Playgroud)

我尝试降级并重新安装该torch软件包,但这并不能解决问题。我的包版本是torch==1.11.0

完整回溯:

AttributeError                            Traceback (most recent call last)
/Users/hanpham/github/DALL-E/notebooks/usage.ipynb Cell 4' in <cell line: 7>()
      4 z = torch.argmax(z_logits, axis=1)
      5 z = F.one_hot(z, num_classes=enc.vocab_size).permute(0, 3, 1, 2).float()
----> 7 x_stats = dec(z).float()
      8 x_rec = unmap_pixels(torch.sigmoid(x_stats[:, :3]))
      9 x_rec = …
Run Code Online (Sandbox Code Playgroud)

python-3.x torch pytorch

7
推荐指数
2
解决办法
2万
查看次数

CUDA 11.8 和 Pytorch 以及 NVIDIA GeForce RTX 3050 Ti 笔记本电脑 GPU(不使用 GPU 作为计算引擎)

我正在尝试使用我的 GPU 作为 Pytorch 的计算引擎。

我在本地计算机上安装了所有带有 CUDA 11.8 的驱动程序 (522.06),但 Pytorch 无法识别我的 GPU。

我使用了不同的下载选项,最后一个:

conda install pytorch torchvision torchaudio pytorch-cuda=11.7 -c pytorch -c nvidia
Run Code Online (Sandbox Code Playgroud)

pip conda torch pytorch

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

Diffusers SDXL“TypeError:‘NoneType’类型的参数不可迭代”

刚刚访问了 SDXL 模型,希望对其即将发布的版本进行测试...不幸的是,我们当前用于服务的代码似乎不适用于stabilityai/stable-diffusion-xl-base-0.9,并且我不完全确定 SDXL 有何不同以及我需要什么改变。

我们使用不同的管道,以便可以生成图像预览,因此它不是 SDXL 模型自述文件中提供的典型模板。该错误似乎发生在unet_2d_condition.py(在扩散器库中)

Traceback (most recent call last):
  File "C:\Users\myalt\Desktop\testing image grid\main.py", line 159, in <module>
    socker_listener.generate_image()
  File "C:\Users\myalt\Desktop\testing image grid\main.py", line 154, in generate_image
    foo = self.blocking_code()
  File "C:\Users\myalt\Desktop\testing image grid\main.py", line 109, in blocking_code
    noise_pred = self.unet(latent_model_input, t,
  File "C:\Users\myalt\Desktop\testing image grid\venv\lib\site-packages\torch\nn\modules\module.py", line 1501, in _call_impl
    return forward_call(*args, **kwargs)
  File "C:\Users\myalt\Desktop\testing image grid\venv\lib\site-packages\diffusers\models\unet_2d_condition.py", line 839, in forward
    if "text_embeds" not in added_cond_kwargs:
TypeError: argument of type 'NoneType' is not …
Run Code Online (Sandbox Code Playgroud)

python torch stable-diffusion

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

如何基于二值掩码裁剪图像

我正在使用带有一些语义分割算法的 Torch 来生成分割图像的二进制掩码。然后我想根据该掩码裁剪图像。为了清楚起见,我需要在每个像素的基础上裁剪它。这似乎是一个简单的问题,但我能想到的唯一解决方案是反转draw maskCoco API 中的函数,或者迭代数组中的每个像素,并在不需要时将像素设置为黑色。我觉得有更好的方法可以做到这一点。Lua、Python、Go 或 C++ 中的库对我有用。有任何想法吗?

opencv image image-processing torch

6
推荐指数
2
解决办法
2万
查看次数

pytorch Network.parameters() 缺少 1 个必需的位置参数:'self'

当我在主函数的这一行中调用 pytorch 中的 Network.parameters() 时出现问题:optimizer = optim.SGD(Network.parameters(), lr=0.001,momentum=0.9)

我得到错误代码:

TypeError: parameters() missing 1 required positional argument: 'self'

我的网络在这个类中定义

class Network(nn.Module):
def __init__(self):
    super(Network, self).__init__()
    self.conv1 = nn.Conv2d(1, 32, 5)
    self.pool = nn.MaxPool2d(2, 2)
    self.conv2 = nn.Conv2d(32, 64, 5)
    self.pool2 = nn.MaxPool2d(2, 2)
    self.conv3 = nn.Conv2d(64, 64, 5)
    self.pool2 = nn.MaxPool2d(2, 2)
    self.fc1 = nn.Linear(64 * 5 * 5, 512)
    self.fc2 = nn.Linear(512, 640)
    self.fc3 = nn.Linear(640, 3756)

def forward(self, x):
    x = self.pool(F.relu(self.conv(x)))
    x = self.pool(F.relu(self.conv2(x)))
    x = self.pool(F.relu(self.conv3(x)))
    x …
Run Code Online (Sandbox Code Playgroud)

python machine-learning computer-vision torch pytorch

6
推荐指数
1
解决办法
2万
查看次数

从 torch.Tensor 中删除一个元素

我正在尝试从张量中删除一个项目。

在下面的示例中,如何从张量中删除第三项?

tensor([[-5.1949, -6.2621, -6.2051, -5.8983, -6.3586, -6.2434, -5.8923, -6.1901,
         -6.5713, -6.2396, -6.1227, -6.4196, -3.4311, -6.8903, -6.1248, -6.3813,
         -6.0152, -6.7449, -6.0523, -6.4341, -6.8579, -6.1961, -6.5564, -6.6520,
         -5.9976, -6.3637, -5.7560, -6.7946, -5.4101, -6.1310, -3.3249, -6.4584,
         -6.2202, -6.3663, -6.9293, -6.9262]], grad_fn=<SqueezeBackward1>)
Run Code Online (Sandbox Code Playgroud)

python torch pytorch

6
推荐指数
2
解决办法
7404
查看次数