我正在尝试深入了解PyTorch Tensor内存模型的工作原理.
# input numpy array
In [91]: arr = np.arange(10, dtype=float32).reshape(5, 2)
# input tensors in two different ways
In [92]: t1, t2 = torch.Tensor(arr), torch.from_numpy(arr)
# their types
In [93]: type(arr), type(t1), type(t2)
Out[93]: (numpy.ndarray, torch.FloatTensor, torch.FloatTensor)
# ndarray
In [94]: arr
Out[94]:
array([[ 0., 1.],
[ 2., 3.],
[ 4., 5.],
[ 6., 7.],
[ 8., 9.]], dtype=float32)
Run Code Online (Sandbox Code Playgroud)
我知道PyTorch张量器共享 NumPy ndarrays 的内存缓冲区.因此,改变一个将反映在另一个.所以,在这里我正在切片并更新Tensor中的一些值t2
In [98]: t2[:, 1] = 23.0
Run Code Online (Sandbox Code Playgroud)
正如预期的那样,它已经更新t2
,arr
因为它们共享相同的内存缓冲区.
In …
Run Code Online (Sandbox Code Playgroud) 我是pytorch的新手,我很难理解它是如何torch.nn.Parameter()
工作的.
我已经浏览了https://pytorch.org/docs/stable/nn.html中的文档,但可能会对此有所了解.
有人可以帮忙吗?
我正在处理的代码片段:
def __init__(self, weight):
super(Net, self).__init__()
# initializes the weights of the convolutional layer to be the weights of the 4 defined filters
k_height, k_width = weight.shape[2:]
# assumes there are 4 grayscale filters
self.conv = nn.Conv2d(1, 4, kernel_size=(k_height, k_width), bias=False)
self.conv.weight = torch.nn.Parameter(weight)
Run Code Online (Sandbox Code Playgroud) 使用了pytorch.org
有关如何安装它的安装指南,我正在使用的命令是
pip install torch===1.4.0 torchvision===0.5.0 -f https://download.pytorch.org/whl/torch_stable.html
Run Code Online (Sandbox Code Playgroud)
但它出现了这个错误;
错误:找不到满足要求的版本 torch===1.4.0(来自版本:0.1.2、0.1.2.post1、0.1.2.post2)
错误:没有找到与 torch===1.4.0 匹配的分布
这甚至是与我有关的问题吗?其他人可以使用这个命令吗?
Pip 已安装并适用于其他模块、Python 3.8、CUDA 版本 10.1、Windows 10 Home 2004
我正在使用迁移学习ResNet-18
为斯坦福汽车数据集构建分类模型。我想实施标签平滑来惩罚过度自信的预测并提高泛化能力。
TensorFlow
在 中有一个简单的关键字参数CrossEntropyLoss
。有没有人为PyTorch
我可以即插即用的类似功能?
我使用的是配备 Intel Corporation HD Graphics 5500 (rev 09) 和 AMD Radeon r5 m255 显卡的笔记本电脑。
有谁知道如何为深度学习设置它,特别是 fastai/Pytorch?
我正在尝试从书中学习 Pytorch,但这对我来说似乎不是一条直线。我复制了下面的代码并粘贴到我的 jupyter 笔记本中。它给了我一个我无法解释的错误。
from torchvision import models
model = models.alexnet(pretrained=True)
# set the device
device = 'cuda' if torch.cuda.is_available() else 'cpu'
print(f'Device: {device}')
model.eval()
model.to(device)
y = model(batch.to(device))
print(y.shape)
Run Code Online (Sandbox Code Playgroud)
这是错误:
---------------------------------------------------------------------------
RuntimeError Traceback (most recent call last)
<ipython-input-50-03488007067c> in <module>
1 from torchvision import models
----> 2 model = models.alexnet(pretrained=True)
3
4 # set the device
5 device = 'cuda' if torch.cuda.is_available() else 'cpu'
~\anaconda3\lib\site-packages\torchvision\models\alexnet.py in alexnet(pretrained, progress, **kwargs)
61 model = AlexNet(**kwargs)
62 if pretrained:
---> 63 state_dict …
Run Code Online (Sandbox Code Playgroud) 有什么办法,我可以在PyTorch中添加简单的L1/L2正则化吗?我们可以通过简单地添加data_loss
with 来计算正则化损失reg_loss
但是有没有明确的方法,PyTorch库的任何支持都可以更轻松地完成它而不需要手动执行它?
ipdb> outputs.size()
torch.Size([10, 100])
ipdb> print sum(outputs,0).size(),sum(outputs,1).size(),sum(outputs,2).size()
(100L,) (100L,) (100L,)
Run Code Online (Sandbox Code Playgroud)
如何对列进行求和?
在 fastai 编码人员的前沿深度学习课程第 7 课中。
self.conv1 = nn.Conv2d(3,10,kernel_size = 5,stride=1,padding=2)
Run Code Online (Sandbox Code Playgroud)
10 是否意味着过滤器的数量或过滤器将提供的激活数量?