zero_grad()
需要在训练期间调用该方法.但文档不是很有帮助
| zero_grad(self)
| Sets gradients of all model parameters to zero.
Run Code Online (Sandbox Code Playgroud)
为什么我们需要调用这个方法?
python neural-network gradient-descent deep-learning pytorch
在numpy中我可以做一个简单的矩阵乘法,如下所示:
a = numpy.arange(2*3).reshape(3,2)
b = numpy.arange(2).reshape(2,1)
print(a)
print(b)
print(a.dot(b))
Run Code Online (Sandbox Code Playgroud)
但是,当我使用PyTorch Tensors进行此操作时,这不起作用:
a = torch.Tensor([[1, 2, 3], [1, 2, 3]]).view(-1, 2)
b = torch.Tensor([[2, 1]]).view(2, -1)
print(a)
print(a.size())
print(b)
print(b.size())
print(torch.dot(a, b))
Run Code Online (Sandbox Code Playgroud)
此代码抛出以下错误:
RuntimeError:/Users/soumith/code/builder/wheel/pytorch-src/torch/lib/TH/generic/THTensorMath.c:503中的张量大小不一致
有关在PyTorch中如何进行矩阵乘法的任何想法?
我认为对于 GPU 内存较低的 PyTorch 用户来说,这是一个非常普遍的信息:
RuntimeError: CUDA out of memory. Tried to allocate MiB (GPU ; GiB total capacity; GiB already allocated; MiB free; cached)
Run Code Online (Sandbox Code Playgroud)
我想为我的课程研究对象检测算法。许多深度学习架构需要大容量的 GPU 内存,所以我的机器无法训练这些模型。我尝试通过将每一层加载到 GPU 然后将其加载回来来处理图像:
RuntimeError: CUDA out of memory. Tried to allocate MiB (GPU ; GiB total capacity; GiB already allocated; MiB free; cached)
Run Code Online (Sandbox Code Playgroud)
但它似乎不是很有效。我想知道在使用很少的 GPU 内存的同时训练大型深度学习模型是否有任何提示和技巧。提前致谢!
编辑:我是深度学习的初学者。如果这是一个愚蠢的问题,请道歉:)
我通过 pytorch import 收到此错误python -c "import torch"
:
Traceback (most recent call last):
File "<string>", line 1, in <module>
File "/afs/cs.stanford.edu/u/brando9/ultimate-utils/ultimate-utils-proj-src/uutils/__init__.py", line 13, in <module>
import torch
File "/dfs/scratch0/brando9/miniconda/envs/metalearning_gpu/lib/python3.9/site-packages/torch/__init__.py", line 191, in <module>
_load_global_deps()
File "/dfs/scratch0/brando9/miniconda/envs/metalearning_gpu/lib/python3.9/site-packages/torch/__init__.py", line 153, in _load_global_deps
ctypes.CDLL(lib_path, mode=ctypes.RTLD_GLOBAL)
File "/dfs/scratch0/brando9/miniconda/envs/metalearning_gpu/lib/python3.9/ctypes/__init__.py", line 382, in __init__
self._handle = _dlopen(self._name, mode)
OSError: /dfs/scratch0/brando9/miniconda/envs/metalearning_gpu/lib/python3.9/site-packages/torch/lib/../../nvidia/cublas/lib/libcublas.so.11: symbol cublasLtHSHMatmulAlgoInit, version libcublasLt.so.11 not defined in file libcublasLt.so.11 with link time reference
Run Code Online (Sandbox Code Playgroud)
如何解决它?
有关的:
在训练模型时,我遇到了以下问题:
RuntimeError: CUDA out of memory. Tried to allocate 304.00 MiB (GPU 0; 8.00 GiB total capacity; 142.76 MiB already allocated; 6.32 GiB free; 158.00 MiB reserved in total by PyTorch) If reserved memory is >> allocated memory try setting max_split_size_mb to avoid fragmentation. See documentation for Memory Management and PYTORCH_CUDA_ALLOC_CONF
正如我们所看到的,当尝试分配 304 MiB 内存时发生错误,而 6.32 GiB 是空闲的!问题是什么?正如我所看到的,建议的选项是设置 max_split_size_mb 以避免碎片。它会有帮助吗?如何正确地做到这一点?
这是我的 PyTorch 版本:
火炬==1.10.2+cu113
火炬视觉==0.11.3+cu113
火炬音频===0.10.2+cu113
我试图做一个简单的事情,用火炬训练带有随机梯度下降(SGD)的线性模型:
import numpy as np
import torch
from torch.autograd import Variable
import pdb
def get_batch2(X,Y,M,dtype):
X,Y = X.data.numpy(), Y.data.numpy()
N = len(Y)
valid_indices = np.array( range(N) )
batch_indices = np.random.choice(valid_indices,size=M,replace=False)
batch_xs = torch.FloatTensor(X[batch_indices,:]).type(dtype)
batch_ys = torch.FloatTensor(Y[batch_indices]).type(dtype)
return Variable(batch_xs, requires_grad=False), Variable(batch_ys, requires_grad=False)
def poly_kernel_matrix( x,D ):
N = len(x)
Kern = np.zeros( (N,D+1) )
for n in range(N):
for d in range(D+1):
Kern[n,d] = x[n]**d;
return Kern
## data params
N=5 # data set size
Degree=4 # number dimensions/features
D_sgd = …
Run Code Online (Sandbox Code Playgroud) 如何计算PyTorch模型中的参数总数?类似于model.count_params()
Keras的东西.
我运行了一个关于深度学习网络的代码,首先我训练了网络,它运行良好,但是运行到验证网络时出现此错误。
我有五个 epoch,每个 epoch 都有一个训练和验证的过程。我在第一个纪元验证时遇到了错误。所以我没有运行验证代码,我发现代码可以运行到第二个纪元并且没有错误。
我的代码:
for epoch in range(10,15): # epoch: 10~15
if(options["training"]["train"]):
trainer.epoch(model, epoch)
if(options["validation"]["validate"]):
#if(epoch == 14):
validator.epoch(model)
Run Code Online (Sandbox Code Playgroud)
我觉得验证代码可能有一些错误。但我找不到那个。
它调用forward()
的nn.Module
?我想当我们调用模型时,forward
正在使用方法.为什么我们需要指定train()?