Giter Site home page Giter Site logo

chenyuntc / pytorch-book Goto Github PK

View Code? Open in Web Editor NEW
12.0K 281.0 3.7K 122.04 MB

PyTorch tutorials and fun projects including neural talk, neural style, poem writing, anime generation (《深度学习框架PyTorch:入门与实战》)

License: MIT License

Jupyter Notebook 95.58% Python 4.23% C++ 0.09% Cuda 0.08% C 0.01%
pytorch pytorch-tutorials pytorch-tutorials-cn deep-learning neural-style charrnn gan caption neuraltalk image-classification

pytorch-book's Introduction

这是书籍《深度学习框架PyTorch:入门与实践(第2版)》的对应代码,但是也可以作为一个独立的PyTorch入门指南和教程。

全新升级

本书第2版基于第1版进行了全新升级,全书代码基于PyTorch 1.8版本进行编写,结合第一版广大读者的意见进行了全面更新,包含了基本使用、高级扩展以及实战应用三大模块。

内容

该书(教程/仓库)的内容如图所示: 思维导图

可以看出本教程可以分为三部分:

基本使用(第2~5章)讲解PyTorch内容,这部份介绍了PyTorch中主要的的模块,和深度学习中常用的一些工具。对于这部分内容,这里利用Jupyter Notebook作为教学工具,读者可以结合notebook修改运行,反复实验。

  • 第2章介绍PyTorch的安装以及相关学习环境的配置。同时,本章以概要的方式介绍PyTorch的主要内容,帮助读者初步了解PyTorch。
  • 第3章介绍PyTorch中多维数组Tensor以及自动微分系统autograd的使用,举例说明如何使用Tensor和autograd实现线性回归,并对比它们的不同点。本章对Tensor的基本结构以及autograd的原理进行了分析,帮助读者更加全面地了解PyTorch的底层模块。
  • 第4章介绍PyTorch中神经网络模块nn的基本用法,讲解了神经网络中的层、激活函数、损失函数以及优化器等,在本章的最后带领读者使用不到50行代码实现经典的网络结构ResNet。
  • 第5章介绍PyTorch中的数据加载、预训练模型、可视化工具以及GPU加速等工具,合理地使用这些工具可以提高用户的编程效率。

高级扩展(第6~8章)讲解PyTorch中的一些高级扩展,学习这部分内容可以帮助读者编写更加高效的程序。

  • 第6章介绍PyTorch中的向量化**,主要包括广播法则、基本索引、高级索引以及爱因斯坦操作。在本章的最后带领读者使用向量化**实现深度学习中的卷积操作、交并比、RoI Align以及反向Unique操作。
  • 第7章介绍PyTorch中的分布式操作。分布式计算和并行计算可以加速网络的训练过程,本章详细介绍了并行计算和分布式计算的基本原理,同时介绍了如何使用torch.distributed以及Horovod进行PyTorch的分布式训练。
  • 第8章介绍PyTorch中的CUDA扩展,带领读者使用CUDA实现Sigmoid函数。同时,本章对CUDA、NVIDIA-driver、cuDNN以及Python之间的关系进行了总结。

实战应用(第9~13章)利用PyTorch实现了几个酷炫有趣的应用,对于这部分的内容,本仓库给出完整的实现代码,并提供预训练好的模型作为demo,供读者测试。

  • 第9章是承上启下的一章,目标不是教会读者新函数、新知识,而实结合Kaggle中的一个经典比赛,实现深度学习中最为简单的图像二分类问题。在实现的过程中,将带领读者复习前5章的知识,并帮助读者合理地组织程序和代码,使程序更加易读且更好维护。同时,本章介绍了如何在PyTorch中调试。
  • 第10章介绍生成对抗网络的基本原理,带领读者从零开始实现一个动漫头像生成器,能够利用生成对抗网络生成风格多变的动漫头像。
  • 第11章介绍自然语言处理的一些基本知识,详细介绍了CharRNN以及Transformer的基本原理。本章带领读者使用Transformer实现自动写诗,该程序可以模仿古人进行诗词的续写以及藏头诗的生成。
  • 第12章介绍风格迁移的基本原理,带领读者实现支持任意风格迁移的神经网络。通过该网络,读者可以将任意图片转换为名画的风格。
  • 第13章介绍目标检测的基本原理,带领读者实现单阶段、无锚框、无非极大值抑制的目标检测算法CenterNet。CenterNet的设计思路可以迁移到三维图像的目标检测、人体姿态估计以及目标追踪等经典的计算机视觉问题中。

Notebook中的文字描述内容及部分Markdown内容属于本书的初稿,可能存在有描述不通顺之处,还请谅解,后续作者会逐渐校正。这部分内容与书籍保证了80%的一致性,但可能有部分语病语法问题,由于时间关系暂未完全校正,后续作者会逐渐更新。

是否需要买书

不是必要的,这个仓库包含书中60%以上的文字内容,90%以上的代码,尤其是前几章入门内容,几乎是完全保留了书中的讲解内容。读者即使不买书也能正常使用本教程。

如果你觉得你更喜欢纸质版的阅读体验,并想留下一本印刷精美、完全彩印的书籍方便翻阅,不妨小破费一笔,支持一下作者近一年以来的工作~

代码说明

代码主要在python3+PyTorch1.6~1.8下测试得到最终结果,python2暂未测试,更新版本的PyTorch暂未测试。

如果有任何不当,或者有待改进的地方,欢迎开issue讨论,或者提交pull request。

环境配置

  1. 安装PyTorch,请从官网选择指定的版本安装即可,一键安装。更多的安装方式请参阅书中说明。

  2. 克隆仓库

    git clone https://github.com/chenyuntc/PyTorch-book.git

^_^

有任何bug,解释不清楚的地方或者是困惑,欢迎开issue

欢迎pull requests

Happy Coding!

pytorch-book's People

Contributors

bobo199830 avatar chenyuntc avatar lucifer1004 avatar xinqiu avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

pytorch-book's Issues

chapter9 我跑了藏头诗有个报错希望得到解决

Traceback (most recent call last):
File "main.py", line 231, in
fire.Fire()
File "/Users/zhangxindong/Desktop/experiment/pytorch-book/pytorch/lib/python3.6/site-packages/fire/core.py", line 127, in Fire
component_trace = _Fire(component, args, context, name)
File "/Users/zhangxindong/Desktop/experiment/pytorch-book/pytorch/lib/python3.6/site-packages/fire/core.py", line 366, in _Fire
component, remaining_args)
File "/Users/zhangxindong/Desktop/experiment/pytorch-book/pytorch/lib/python3.6/site-packages/fire/core.py", line 542, in _CallCallable
result = fn(*varargs, **kwargs)
File "main.py", line 224, in gen
result = gen_poetry(model, start_words, ix2word, word2ix, prefix_words)
File "main.py", line 105, in gen_acrostic
w = ix2word[top_index]
KeyError: tensor(6522)

第二章 计算Loss维数问题

第二章 In [31]:

output = net(input)
target = Variable(t.arange(0,10))
criterion = nn.MSELoss()
loss = criterion(output, target)
loss

loss = criterion(output, target) 报错:
RuntimeError: input and target shapes do not match: input [1 x 10], target [10]

改成loss = criterion(output[0], target)后得到loss是28点多,环境是pycharm,Python 3.6,pytorch 0.4.0

大佬 第6章:python main.py test 进入了 ipdb 命令行(有点懵逼,怎么回事)

python main.py test        
--test-data-root=data/test1        
--load-model-path='checkpoints/resnet34.pth'        
--use-gpu=False        
--batch-size=30        
--num-workers=12

运行进入了 ipdb 命令行。。有点懵逼。。

user config:
env default
model ResNet34
...
result_file result.csv
max_epoch 10
lr 0.1
lr_decay 0.95
weight_decay 0.0001
parse <bound method parse of <config.DefaultConfig object at 0x104083c50>>
> /opt/git/pytorch-book/chapter6-实战指南/main.py(20)test()
     19     # configure model
---> 20     model = getattr(models, opt.model)().eval()
     21     if opt.load_model_path:

ipdb> 

第六章 Resnet out of memory错误

根据#23 的建议,换去ResNet, 出现out of memory的错误. 我的显卡是6GB.

THCudaCheck FAIL file=/opt/conda/conda-bld/pytorch_1518244421288/work/torch/lib/THC/generic/THCStorage.cu line=58 error=2 : out of memory
Traceback (most recent call last):
File "/home/ly/src/pytorch-examples/chenyun-pytorch/ch6/main.py", line 182, in
fire.Fire()
File "/home/ly/anaconda3/envs/learning/lib/python3.6/site-packages/fire/core.py", line 127, in Fire
component_trace = _Fire(component, args, context, name)
File "/home/ly/anaconda3/envs/learning/lib/python3.6/site-packages/fire/core.py", line 366, in _Fire
component, remaining_args)
File "/home/ly/anaconda3/envs/learning/lib/python3.6/site-packages/fire/core.py", line 542, in _CallCallable
result = fn(*varargs, **kwargs)
File "/home/ly/src/pytorch-examples/chenyun-pytorch/ch6/main.py", line 85, in train
score = model(input)
File "/home/ly/anaconda3/envs/learning/lib/python3.6/site-packages/torch/nn/modules/module.py", line 357, in call
result = self.forward(*input, **kwargs)
File "/home/ly/src/pytorch-examples/chenyun-pytorch/ch6/models/ResNet34.py", line 71, in forward
x = self.layer2(x)
File "/home/ly/anaconda3/envs/learning/lib/python3.6/site-packages/torch/nn/modules/module.py", line 357, in call
result = self.forward(*input, **kwargs)
File "/home/ly/anaconda3/envs/learning/lib/python3.6/site-packages/torch/nn/modules/container.py", line 67, in forward
input = module(input)
File "/home/ly/anaconda3/envs/learning/lib/python3.6/site-packages/torch/nn/modules/module.py", line 357, in call
result = self.forward(*input, **kwargs)
File "/home/ly/src/pytorch-examples/chenyun-pytorch/ch6/models/ResNet34.py", line 24, in forward
return F.relu(out)
File "/home/ly/anaconda3/envs/learning/lib/python3.6/site-packages/torch/nn/functional.py", line 583, in relu
return threshold(input, 0, 0, inplace)
RuntimeError: cuda runtime error (2) : out of memory at /opt/conda/conda-bld/pytorch_1518244421288/work/torch/lib/THC/generic/THCStorage.cu:58

第三章注释存在笔误

在纸质书中第三章,用Variable实现线性回归中,注释存在笔误:

# backward: 手动计算梯度

按上下文的意思应该是

# backward: 自动计算梯度

另外感谢作者,本仓库和对应书籍对我有很大帮助~

支持pytorch 0.4

pytorch 0.4中volatile被拿掉了,而代码中使用了很多的volatile,看看能不能把代码升级到pytorch 0.4?

文件名在Windows系统下不兼容

这个文件名不能在Windows系统下创建出来,因为文件名中包括英文冒号":":
chapter2-快速入门/chapter2: PyTorch快速入门.ipynb

第二章CIFAR-10小练习的编辑器问题

我的这段代码可以在jupyter notebook上运行, 但是使用pycharm运行失败
dataiter = iter(trainloader)
images, labels = dataiter.next() # 返回4张图片及标签
print(' '.join('%11s' % classes[labels[j]] for j in range(4)))
show(tv.utils.make_grid((images + 1) / 2)).resize((100, 100))

希望能解答一下, 谢谢!

No matching distribution found for torchvison(chapter 7)

安装环境出错:
pip3 install -r requirements.txt

Collecting git+https://github.com/pytorch/tnt.git@master (from -r requiments.txt (line 4))
Cloning https://github.com/pytorch/tnt.git (to master) to /private/var/folders/pt/7_xdqqdj6l1_xrmp5022g3_r0000gn/T/pip-kt8yy0lq-build
Requirement already satisfied (use --upgrade to upgrade): torchnet==0.0.1 from git+https://github.com/pytorch/tnt.git@master in /usr/local/lib/python3.6/site-packages (from -r requiments.txt (line 4))
Requirement already satisfied: visdom in /usr/local/lib/python3.6/site-packages (from -r requiments.txt (line 1))
Requirement already satisfied: fire in /usr/local/lib/python3.6/site-packages (from -r requiments.txt (line 2))
Collecting torchvison (from -r requiments.txt (line 3))

error:
Could not find a version that satisfies the requirement torchvison (from -r requiments.txt (line 3)) (from versions: )
No matching distribution found for torchvison (from -r requiments.txt (line 3))

希望能得到你的帮助

你好,我们团队最近翻译了pytorch的官方文档,感觉你的项目教程很不错,为了帮助更多的人学习pytorch,我想参考你的项目做一份教程可以吗?(会在项目中标注参考来源的。)

第十章,demo.ipynb

有一个可能是个人问题。。。我的resnet50去除FC层输出尺寸竟然是(1,8192)不是(1,2048),百思不得其解,打印出内容也不是0,是有值的。我换了种写法,结果还是差不多,尺寸变成了(1,2048,2,2),然后调用(:,:,0,0)的输出和标准答案是一样的,(:,:,0,1),(:,:,1,1),(:,:,1,0)都有所偏差。修改后的代码如下:

resnet50 = tv.models.resnet50(pretrained = True).eval()
# del resnet50.fc
modules = list(resnet50.children())[:-1]
resnet50 = t.nn.Sequential(*modules)
# resnet50.fc = lambda x:x
if opt.use_gpu:
    resnet50.cuda() 
    img = img.cuda()
img_feats = resnet50(Variable(img,volatile=True))
img_feats = img_feats[:,:,0,0]
print(img_feats.data.squeeze(0).size())
print(img_feats)

第159页错误

“它们都带有一个.cuda方法”这一段的最后一句话。
是想说这两种方法的效果是一样的吗?

关于Variable的一个问题

陈老师您好!看了您的书受益匪浅,看到Variable这一节,我想试着让两个全连接层共享权重:一个全连接层的权重是另一个的转置,我是这样做的:

import torch
import torch.nn as nn


class Net(nn.Module):
    def __init__(self):
        super(Net, self).__init__()
        self.linear1 = nn.Linear(5, 10)
        self.linear2 = nn.Linear(10, 5)

        self.linear2.weight = self.linear1.weight.t()

    def forward(self, x):
        x = self.linear1(x)
        x = self.linear2(x)

        return x

net = Net()

但是程序运行时报错:
TypeError: cannot assign 'torch.autograd.variable.Variable' as parameter 'weight' (torch.nn.Parameter or None expected)

这是哪里不对呢?

Chapter 10, python feature_extract.py has the following error

Traceback (most recent call last):
File "feature_extract.py", line 76, in
features = resnet50(imgs)
File "/home/dand/anaconda3/lib/python3.6/site-packages/torch/nn/modules/module.py", line 491, in call
result = self.forward(*input, **kwargs)
File "/home/dand/anaconda3/lib/python3.6/site-packages/torchvision-0.2.1-py3.6.egg/torchvision/models/resnet.py", line 144, in forward
File "/home/dand/anaconda3/lib/python3.6/site-packages/torch/nn/modules/module.py", line 491, in call
result = self.forward(*input, **kwargs)
File "/home/dand/anaconda3/lib/python3.6/site-packages/torch/nn/modules/container.py", line 91, in forward
input = module(input)
File "/home/dand/anaconda3/lib/python3.6/site-packages/torch/nn/modules/module.py", line 491, in call
result = self.forward(*input, **kwargs)
File "/home/dand/anaconda3/lib/python3.6/site-packages/torchvision-0.2.1-py3.6.egg/torchvision/models/resnet.py", line 88, in forward
File "/home/dand/anaconda3/lib/python3.6/site-packages/torch/nn/modules/module.py", line 491, in call
result = self.forward(*input, **kwargs)
File "/home/dand/anaconda3/lib/python3.6/site-packages/torch/nn/modules/container.py", line 91, in forward
input = module(input)
File "/home/dand/anaconda3/lib/python3.6/site-packages/torch/nn/modules/module.py", line 491, in call
result = self.forward(*input, **kwargs)
File "/home/dand/anaconda3/lib/python3.6/site-packages/torch/nn/modules/batchnorm.py", line 49, in forward
self.training or not self.track_running_stats, self.momentum, self.eps)
File "/home/dand/anaconda3/lib/python3.6/site-packages/torch/nn/functional.py", line 1194, in batch_norm
training, momentum, eps, torch.backends.cudnn.enabled
RuntimeError: cuda runtime error (2) : out of memory at /opt/conda/conda-bld/pytorch_1524586445097/work/aten/src/THC/generic/THCStorage.cu:58

CuDNNError: 1: CUDNN_STATUS_NOT_INITIALIZED

try to train the chapter9 , gpu=True, seems got the cuda problem

File "main.py", line 188, in train
model.cuda()

return self._apply(lambda t: t.cuda(device))
File "/usr/local/lib/python2.7/dist-packages/torch/nn/modules/module.py", line 146, in _apply
module._apply(fn)
File "/usr/local/lib/python2.7/dist-packages/torch/nn/modules/rnn.py", line 123, in _apply
self.flatten_parameters()
File "/usr/local/lib/python2.7/dist-packages/torch/nn/modules/rnn.py", line 85, in flatten_parameters
handle = cudnn.get_handle()
File "/usr/local/lib/python2.7/dist-packages/torch/backends/cudnn/init.py", line 296, in get_handle
handle = CuDNNHandle()
File "/usr/local/lib/python2.7/dist-packages/torch/backends/cudnn/init.py", line 110, in init
check_error(lib.cudnnCreate(ctypes.byref(ptr)))

CuDNNError: 1: CUDNN_STATUS_NOT_INITIALIZED

第六章训练部分 Runtime Error

在第六章的猫狗训练过程中:
File "main.py", line 123, in train
if loss_meter.value()[0] > previous_loss:
RuntimeError: value cannot be converted to type float without overflow: 10000000000000000159028911097599180468360808563945281389781327557747838772170381060813469985856815104.000000

第十章 图像描述 在更新pytorch以后出现问题

运行命令:python feature_extract.py 出现以下错误

Traceback (most recent call last):
  File "<string>", line 1, in <module>
  File "F:\Anaconda\lib\multiprocessing\spawn.py", line 105, in spawn_main
    exitcode = _main(fd)
  File "F:\Anaconda\lib\multiprocessing\spawn.py", line 114, in _main
    prepare(preparation_data)
  File "F:\Anaconda\lib\multiprocessing\spawn.py", line 225, in prepare
    _fixup_main_from_path(data['init_main_from_path'])
  File "F:\Anaconda\lib\multiprocessing\spawn.py", line 277, in _fixup_main_from_path
    run_name="__mp_main__")
  File "F:\Anaconda\lib\runpy.py", line 263, in run_path
    pkg_name=pkg_name, script_name=fname)
  File "F:\Anaconda\lib\runpy.py", line 96, in _run_module_code
    mod_name, mod_spec, pkg_name, script_name)
  File "F:\Anaconda\lib\runpy.py", line 85, in _run_code
    exec(code, run_globals)
  File "G:\图像描述(Image Caption)\feature_extract.py", line 68, in <module>
    for ii,(imgs, indexs)  in tqdm.tqdm(enumerate(dataloader)):
  File "F:\Anaconda\lib\site-packages\torch\utils\data\dataloader.py", line 417, in __iter__
    return DataLoaderIter(self)
  File "F:\Anaconda\lib\site-packages\torch\utils\data\dataloader.py", line 234, in __init__
    w.start()
  File "F:\Anaconda\lib\multiprocessing\process.py", line 105, in start
    self._popen = self._Popen(self)
  File "F:\Anaconda\lib\multiprocessing\context.py", line 223, in _Popen
    return _default_context.get_context().Process._Popen(process_obj)
  File "F:\Anaconda\lib\multiprocessing\context.py", line 322, in _Popen
    return Popen(process_obj)
  File "F:\Anaconda\lib\multiprocessing\popen_spawn_win32.py", line 33, in __init__
    prep_data = spawn.get_preparation_data(process_obj._name)
  File "F:\Anaconda\lib\multiprocessing\spawn.py", line 143, in get_preparation_data
    _check_not_importing_main()
  File "F:\Anaconda\lib\multiprocessing\spawn.py", line 136, in _check_not_importing_main
    is not going to be frozen to produce an executable.''')
RuntimeError:
        An attempt has been made to start a new process before the
        current process has finished its bootstrapping phase.

        This probably means that you are not using fork to start your
        child processes and you have forgotten to use the proper idiom
        in the main module:

            if __name__ == '__main__':
                freeze_support()
                ...

        The "freeze_support()" line can be omitted if the program
        is not going to be frozen to produce an executable.
Traceback (most recent call last):
  File "feature_extract.py", line 68, in <module>
    for ii,(imgs, indexs)  in tqdm.tqdm(enumerate(dataloader)):
  File "F:\Anaconda\lib\site-packages\torch\utils\data\dataloader.py", line 417, in __iter__
    return DataLoaderIter(self)
  File "F:\Anaconda\lib\site-packages\torch\utils\data\dataloader.py", line 234, in __init__
    w.start()
  File "F:\Anaconda\lib\multiprocessing\process.py", line 105, in start
    self._popen = self._Popen(self)
  File "F:\Anaconda\lib\multiprocessing\context.py", line 223, in _Popen
    return _default_context.get_context().Process._Popen(process_obj)
  File "F:\Anaconda\lib\multiprocessing\context.py", line 322, in _Popen
    return Popen(process_obj)
  File "F:\Anaconda\lib\multiprocessing\popen_spawn_win32.py", line 65, in __init__
    reduction.dump(process_obj, to_child)
  File "F:\Anaconda\lib\multiprocessing\reduction.py", line 60, in dump
    ForkingPickler(file, protocol).dump(obj)
BrokenPipeError: [Errno 32] Broken pipe

问题描述:

因为我的电脑没有Nvidia的显卡,所以之前运行python feature_extract.py时出现错误:Torch not compiled with CUDA enabled,所以就安装了带有CUDA版本的pytorch,结果导入错误,于是就把所有的pytorch框架都删了,装上了一个最新的(其实版本好像还是0.3.1),重新运行了命令:python feature_extract.py,就报了以上的错误。至于报错里提到的:if name == 'main': 问题,我也在feature_extract.py文件里加上了这句判断,可是依然报这个错。google了BrokenPipeError: [Errno 32] Broken pipe的相关问题,基本都是socket的报错,帮助不大,特地跑来发问。深度学习的小白,坐等大神解答疑惑。

Chap Autograd 中无法查看saved_variables

正在阅读您的Pytorch-book 这个教程,在进行 Chap 3 Autograd 时,遇到了一个在 Google 和 Github issues 中没出现的问题,想询问您一下。在第 25 行,执行 y.grad_fn.saved_variables 时,显示如下错误

-----------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
<ipython-input-25-3ba15276330c> in <module>()
----> 1 y.grad_fn.saved_variables

AttributeError: 'MulBackward1' object has no attribute 'saved_variables'

没有能够显示应该显示的结果

(Variable containing:
  0.3356
 [torch.FloatTensor of size 1], Variable containing:
  1
 [torch.FloatTensor of size 1])

之后也是,也就是说,我无法得到 saved_variables 这个变量的值

torch==0.3.0.post4
python 2.7.10(14)/3.5.2 均无法成功运行
Ubuntu / Mac OS X 10.12.6 均尝试无果

第九章训练时遇到下面的问题

python main.py train --plot-every=150 --batch-size=128 --pickle-path='tang.npz' --lr=1e-3 --env='poetry3' --epoch=50
Traceback (most recent call last):
File "main.py", line 225, in
fire.Fire()
File "/home/lyerox/.virtualenvs/RNN-fXB3zvlI/lib/python3.6/site-packages/fire/core.py", line 127, in Fire
component_trace = _Fire(component, args, context, name)
File "/home/lyerox/.virtualenvs/RNN-fXB3zvlI/lib/python3.6/site-packages/fire/core.py", line 366, in _Fire
component, remaining_args)
File "/home/lyerox/.virtualenvs/RNN-fXB3zvlI/lib/python3.6/site-packages/fire/core.py", line 542, in _CallCallable
result = fn(*varargs, **kwargs)
File "main.py", line 131, in train
data = t.from_numpy(data)
RuntimeError: the given numpy array has zero-sized dimensions. Zero-sized dimensions are not supported in PyTorch

If you suspect this is an IPython bug, please report it at:
https://github.com/ipython/ipython/issues
or send an email to the mailing list at [email protected]

You can print a more detailed traceback right now with "%tb", or use "%debug"
to interactively debug it.

Extra-detailed tracebacks for bug-reporting purposes can be enabled via:
%config Application.verbose_crash=True

第10章feature_extract RuntimeError size not matched.

直接运行feature_extract.py会运行出现下面错误.

results[ii * batch_size:(ii + 1) * batch_size] = features.data.cpu()
RuntimeError: The expanded size of the tensor (2048) must match the existing size (8192) at non-singleton dimension 1
terminate called without an active exception

图像描述在运行作者给的demo时报错

Traceback (most recent call last):
File "D:/softWare/Python/pythonrun/Machine-Learning/dataClass/test.py", line 9, in
data = t.load(opt.caption_data_path)
File "D:\softWare\Python\pytorch\Anaconda3-5\anaconda\lib\site-packages\torch\serialization.py", line 227, in load
f = open(f, 'rb')
FileNotFoundError: [Errno 2] No such file or directory: 'caption.pth'

If you suspect this is an IPython bug, please report it at:
https://github.com/ipython/ipython/issues
or send an email to the mailing list at [email protected]

You can print a more detailed traceback right now with "%tb", or use "%debug"
to interactively debug it.

Extra-detailed tracebacks for bug-reporting purposes can be enabled via:
%config Application.verbose_crash=True

ch9 train运行错误

pytorch version: 0.4.0

debug看到_word变量是没有dimension的tensor,而且拿不到里面的值(_word.data还是这个tensor).

python main.py train --data_path=xxx

Traceback (most recent call last):
File "main.py", line 232, in
fire.Fire()
File "/home/ly/anaconda3/envs/learning/lib/python3.6/site-packages/fire/core.py", line 127, in Fire
component_trace = _Fire(component, args, context, name)
File "/home/ly/anaconda3/envs/learning/lib/python3.6/site-packages/fire/core.py", line 366, in _Fire
component, remaining_args)
File "/home/ly/anaconda3/envs/learning/lib/python3.6/site-packages/fire/core.py", line 542, in _CallCallable
result = fn(*varargs, **kwargs)
File "main.py", line 180, in train
for iii in range(data.size(1))][:16]
File "main.py", line 180, in
for iii in range(data.size(1))][:16]
File "main.py", line 179, in
poetrys = [[ix2word[_word] for word in data[:, _iii]]
KeyError: tensor(8542, device='cuda:0')

torch.utils.data.DataLoader 类问题

你好,我在用书中的方法使用 DataLoader 类时发现了一些问题:

  1. 经过多次测试我发现,使用方法:
dataiter = iter(dataloader)
batch_datas, batch_labesl = next(dataiter)

生成的迭代器只能遍历一次数据,之后会自动生成异常 StopIteration 然后停止,这个最好能在文档中注明一下,以及希望能提供一个循环遍历数据的方法。

  1. 我发现在执行命令:
dataiter = iter(dataloader)

时,程序会在整个数据集上执行 dataset.__getitem__ 函数。例如数据库中有100个数据,则会对这100个数据依次执行__getitem__。不知道这样做的原因是为什么?如果我dataset中有10万个数据,那岂不是要对10万个数据都执行一遍?

hi python3.5 run chapter9 code got encoding problem

Hi,
try to run the chapter9 main.py code, and got the error as below:

File "main.py", line 212, in gen
start_words = opt.start_words.encode('ascii', 'surrogateescape').decode('utf8')
UnicodeEncodeError: 'ascii' codec can't encode characters in position 0-3: ordinal not in range(128)

my enviorment is python 3.5,mac os, pytorch is 0.3.0.post4

运行demo.ipynb时报错

错误如图所示:
qq 20180519171627

我用的预训练模型不是自己训练的,是下载到本地的,caption.pth是用自己的电脑生成的,不过我的电脑没有GPU,所以feature.pth文件就没有生成,运行的时候就报了这样的错误,感觉应该是遍历张量里面的字典的时候出错了,希望有解决思路的小伙伴解答疑惑 !

Run out of Memory for Chapt. 10 with Feature_extraction

I am sorry to open this issue because while running the feature_exact.py file from Chapt. 10, I have encountered the problem of running out of memory. I observed that my RAM increases from 10G to 25 G then an error raised RuntimeError: Couldn't open shared file mapping: <torch_17120_2057656553>, error code: <1455> at C:\Anaconda2\conda-bld\pytorch_1519496000060\work\torch\lib\TH\THAllocator.c:157

I have tried to use gc.collect and delete "imgs" and "features", but the memory use still gets increased.
I am using pytorch 0.3.1 in windows from @peterjc123.

image
image

对于 Chap 6 的一些想法

首先 文件夹里有两个重复的文件,建议删除一个。

README里的使用方法要是和实战指南中 6.1.8 使用相互一致或者类似更好。实战指南中的使用方式更为可靠。在实际使用中,--load-model-path=None 参数不能少,其实要是能够提供训练好的pth那就更好了,没用 GPU 来训练模型真是太痛苦了。

main.pytrain, test, val 函数中使用 tqdm,这样能看出进度> <,不然有一种死机的感觉。

另外,Chap 5 的 visdom 部分有些和预计结果不同,在 Chap 6 中,loss 图和 val_accuracy 都不能得到曲线,不知道是不是因为版本的原因。

visdom是否开启正常

感谢下楼主辛苦付出
visdom替换前

image

替换index.html后
image

请问visdom是否正常.

另外
python main.py stylize --model-path='transformer.pth'
--content-path='amber.jpg'\
--result-path='output2.png'\
--use-gpu=False

是如何执行的,在终端还是编译的,本人新手 ,谢谢

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.