我正在尝试使用 PyTorch 制作一个简单的图像分类器。这是我将数据加载到数据集和 dataLoader 中的方式:
batch_size = 64
validation_split = 0.2
data_dir = PROJECT_PATH+"/categorized_products"
transform = transforms.Compose([transforms.Grayscale(), CustomToTensor()])
dataset = ImageFolder(data_dir, transform=transform)
indices = list(range(len(dataset)))
train_indices = indices[:int(len(indices)*0.8)]
test_indices = indices[int(len(indices)*0.8):]
train_sampler = SubsetRandomSampler(train_indices)
test_sampler = SubsetRandomSampler(test_indices)
train_loader = torch.utils.data.DataLoader(dataset, batch_size=batch_size, sampler=train_sampler, num_workers=16)
test_loader = torch.utils.data.DataLoader(dataset, batch_size=batch_size, sampler=test_sampler, num_workers=16)
Run Code Online (Sandbox Code Playgroud)
我想分别打印出训练和测试数据中每个班级的图像数量,如下所示:
在火车数据中:
在测试数据中:
我试过这个:
from collections import Counter
print(dict(Counter(sample_tup[1] for sample_tup in dataset.imgs)))
Run Code Online (Sandbox Code Playgroud)
但我收到了这个错误:
AttributeError: 'MyDataset' object has no attribute 'img'
Run Code Online (Sandbox Code Playgroud) 当我尝试运行这行代码时,出现以下错误:
from torchtext.data import Field, TabularDataset, BucketIterator, Iterator
ImportError: cannot import name 'Field' from 'torchtext.data' (C:\Users\user1\anaconda3\lib\site-packages\torchtext\data\__init__.py)
Run Code Online (Sandbox Code Playgroud)
我想知道是否有人知道问题可能是什么以及如何解决?
我正在尝试按照 How-To-Geek 文章“如何在 Windows 上使用 GUI 在本地运行稳定扩散”在我的 PC (Windows 11 Pro x64) 上本地安装和配置稳定扩散AI
很自然,我遇到了问题,主要是(如下面的代码所示,Torch 安装和 Pip 版本:)
这是我运行稳定扩散批处理文件时得到的结果:
venv "D:\stable-diffusion-webui-master\venv\Scripts\Python.exe"
Python 3.6.1 (v3.6.1:69c0db5, Mar 21 2017, 18:41:36) [MSC v.1900 64 bit (AMD64)]
Commit hash: <none>
Installing torch and torchvision
Traceback (most recent call last):
File "launch.py", line 108, in <module>
run(f'"{python}" -m {torch_command}', "Installing torch and torchvision", "Couldn't install torch")
File "launch.py", line 55, in run
raise RuntimeError(message)
RuntimeError: Couldn't install torch.
Command: "D:\stable-diffusion-webui-master\venv\Scripts\python.exe" -m pip install torch==1.12.1+cu113 torchvision==0.13.1+cu113 …Run Code Online (Sandbox Code Playgroud) x=torch.Tensor({1,-1,3,-8})
如何进行转换x,使得 x 中的所有负值都替换为零,而不使用循环,使得张量必须看起来像
th>x
1
0
3
0
我有一个网络(https://github.com/TheAbhiKumar/tensorflow-value-iteration-networks),我试图在pytorch中实现(我对pytorch很新,但是,根本不是机器学习的新手) .
简而言之,我似乎无法弄清楚如何在pytorch中实现"纯"卷积.在张量流中,它可以像这样完成:
def conv2d_flipkernel(x, k, name=None):
return tf.nn.conv2d(x, flipkernel(k), name=name,
strides=(1, 1, 1, 1), padding='SAME')
Run Code Online (Sandbox Code Playgroud)
使用flipkernel函数:
def flipkernel(kern):
return kern[(slice(None, None, -1),) * 2 + (slice(None), slice(None))]
Run Code Online (Sandbox Code Playgroud)
如何在pytorch中完成类似的事情?
convolution neural-network torch conv-neural-network pytorch
我想通过索引数组或张量删除张量列。例如:
th> X = torch.rand(2,4)
th> X
0.7475 0.2512 0.6085 0.6414
0.7143 0.8299 0.2929 0.6945
[torch.DoubleTensor of size 2x4]
th> indices = torch.zeros(2)
th> indices[1] = 1
th> indices[2] = 3
th> indices
1
3
[torch.DoubleTensor of size 2]
th> X:delete(indices)
0.2512 0.6414
0.8299 0.6945
Run Code Online (Sandbox Code Playgroud) 使用 HuggingFace 的 Transformers 时,我遇到了编码和解码方法的问题。
我有以下字符串:
test_string = 'text with percentage%'
Run Code Online (Sandbox Code Playgroud)
然后我运行以下代码:
import torch
from transformers import BertTokenizer
tokenizer = BertTokenizer.from_pretrained('bert-base-cased')
test_string = 'text with percentage%'
# encode Converts a string in a sequence of ids (integer), using the tokenizer and vocabulary.
input_ids = tokenizer.encode(test_string)
output = tokenizer.decode(input_ids)
Run Code Online (Sandbox Code Playgroud)
输出如下所示:
'text with percentage %'
Run Code Online (Sandbox Code Playgroud)
在 % 前有一个额外的空格。我已经尝试了额外的参数,clean_up_tokenization_spaces 但这是不同的。
我应该如何在解码和编码中使用什么来获得前后完全相同的文本。这也发生在其他特殊标志上。
我在 pytorch 1.3.1 中使用tensorboard,并且我在pytorch 文档中为tensorboard做了完全相同的事情。运行后tensorboard --logdir=runs,我得到了这个:
。
$ tensorboard --logdir=runs
TensorFlow installation not found - running with reduced feature set.
Serving TensorBoard on localhost; to expose to the network, use a proxy or pass --bind_all
TensorBoard 2.1.0 at http://localhost:6006/ (Press CTRL+C to quit)
Run Code Online (Sandbox Code Playgroud)
打开 http://localhost:6006/ 后,我得到这样的空白页面
我也尝试了tensorboardX,并得到了相同的结果。您能告诉我如何解决这个问题吗?谢谢。
我有以下火炬张量:
tensor([[-0.2, 0.3],
[-0.5, 0.1],
[-0.4, 0.2]])
Run Code Online (Sandbox Code Playgroud)
和以下 numpy 数组:(如果需要,我可以将其转换为其他内容)
[1 0 1]
Run Code Online (Sandbox Code Playgroud)
我想得到以下张量:
tensor([0.3, -0.5, 0.2])
Run Code Online (Sandbox Code Playgroud)
即我希望 numpy 数组索引张量的每个子元素。最好不使用循环。
提前致谢
我正在尝试部署使用机器学习模型的 Django 应用程序。而机器学习模型需要pytorch来执行。当我尝试部署时,它给了我这个错误
ERROR: Could not find a version that satisfies the requirement torch==1.5.0+cpu (from -r /tmp/build_4518392d43f43bc52f067241a9661c92/requirements.txt (line 23)) (from versions: 0.1.2, 0.1.2.post1, 0.1.2.post2, 0.4.1, 0.4.1.post2, 1.0.0, 1.0.1, 1.0.1.post2, 1.1.0, 1.2.0, 1.3.0, 1.3.1, 1.4.0, 1.5.0)
ERROR: No matching distribution found for torch==1.5.0+cpu (from -r /tmp/build_4518392d43f43bc52f067241a9661c92/requirements.txt (line 23))
! Push rejected, failed to compile Python app.
! Push failed
Run Code Online (Sandbox Code Playgroud)
我的requirements.txt是
asgiref==3.2.7
certifi==2020.4.5.1
chardet==3.0.4
cycler==0.10.0
dj-database-url==0.5.0
Django==3.0.6
django-heroku==0.3.1
future==0.18.2
gunicorn==20.0.4
idna==2.9
imageio==2.8.0
kiwisolver==1.2.0
matplotlib==3.2.1
numpy==1.18.4
Pillow==7.1.2
psycopg2==2.8.5
pyparsing==2.4.7
python-dateutil==2.8.1
pytz==2020.1
requests==2.23.0
six==1.14.0 …Run Code Online (Sandbox Code Playgroud) torch ×10
python ×7
pytorch ×6
lua ×2
tensor ×2
convolution ×1
dataloader ×1
django ×1
heroku ×1
importerror ×1
indexing ×1
pip ×1
tensorboard ×1
tensorboardx ×1
tokenize ×1
windows ×1