- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
下面代码中的self.hidden
是什么?
import torch.nn as nn
import torch.nn.functional as F
class Network(nn.Module):
def __init__(self):
super().__init__()
self.hidden = nn.Linear(784, 256)
self.output = nn.Linear(256, 10)
def forward(self, x):
x = F.sigmoid(self.hidden(x))
x = F.softmax(self.output(x), dim=1)
return x
self.hidden
是 nn.Linear
并且它可以将张量 x
作为参数。
最佳答案
What is the class definition of nn.Linear in pytorch?
来自 documentation :
类 torch.nn.Linear(in_features, out_features, bias=True)
对传入数据应用线性变换:y = x*W^T + b
参数:
请注意,权重 W
的形状为 (out_features, in_features)
,偏差 b
的形状为 (out_features)
。它们是随机初始化的,以后可以更改(例如,在神经网络的训练过程中,它们会通过某种优化算法进行更新)。
在您的神经网络中,self.hidden = nn.Linear(784, 256)
定义了一个隐藏(意味着它位于输入和输出之间层),完全连接的线性层,它接受形状为(batch_size,784)
的输入x
,其中批量大小是输入的数量(每个大小为 784) 立即传递给网络(作为单个张量),并通过线性方程 y = x*W^T + b
将其转换为张量 y
形状 (batch_size, 256)
。它由 sigmoid 函数进一步转换,x = F.sigmoid(self.hidden(x))
(它不是 nn.Linear
的一部分,而是一个额外的步骤)。
让我们看一个具体的例子:
import torch
import torch.nn as nn
x = torch.tensor([[1.0, -1.0],
[0.0, 1.0],
[0.0, 0.0]])
in_features = x.shape[1] # = 2
out_features = 2
m = nn.Linear(in_features, out_features)
其中 x
包含三个输入(即批量大小为 3)、x[0]
、x[1]
和 x[3]
,每个大小为 2,输出的形状为 (batch size, out_features) = (3, 2)
。
参数(权重和偏差)的值是:
>>> m.weight
tensor([[-0.4500, 0.5856],
[-0.1807, -0.4963]])
>>> m.bias
tensor([ 0.2223, -0.6114])
(因为它们是随机初始化的,很可能你会得到与上面不同的值)
输出是:
>>> y = m(x)
tensor([[-0.8133, -0.2959],
[ 0.8079, -1.1077],
[ 0.2223, -0.6114]])
并且(在幕后)它被计算为:
y = x.matmul(m.weight.t()) + m.bias # y = x*W^T + b
即
y[i,j] == x[i,0] * m.weight[j,0] + x[i,1] * m.weight[j,1] + m.bias[j]
其中i
在区间[0, batch_size)
中,j
在[0, out_features)
中。
关于python - PyTorch 中 nn.Linear 的类定义是什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54916135/
已关闭。此问题不符合Stack Overflow guidelines 。目前不接受答案。 这个问题似乎与 help center 中定义的范围内的编程无关。 . 已关闭 3 年前。 此帖子于去年编辑
据我所知,在使用 GPU 训练和验证模型时,GPU 内存主要用于加载数据,向前和向后。据我所知,我认为 GPU 内存使用应该相同 1) 训练前,2) 训练后,3) 验证前,4) 验证后。但在我的例子中
我正在尝试在 PyTorch 中将两个复数矩阵相乘,看起来 the torch.matmul functions is not added yet to PyTorch library for com
我正在尝试定义二分类问题的损失函数。但是,目标标签不是硬标签0,1,而是0~1之间的一个 float 。 Pytorch 中的 torch.nn.CrossEntropy 不支持软标签,所以我想自己写
我正在尝试让 PyTorch 与 DataLoader 一起工作,据说这是处理小批量的最简单方法,在某些情况下这是获得最佳性能所必需的。 DataLoader 需要一个数据集作为输入。 大多数关于 D
Pytorch Dataloader 的迭代顺序是否保证相同(在温和条件下)? 例如: dataloader = DataLoader(my_dataset, batch_size=4,
PyTorch 的负对数似然损失,nn.NLLLoss定义为: 因此,如果以单批处理的标准重量计算损失,则损失的公式始终为: -1 * (prediction of model for correct
在PyTorch中,new_ones()与ones()有什么区别。例如, x2.new_ones(3,2, dtype=torch.double) 与 torch.ones(3,2, dtype=to
假设我有一个矩阵 src带形状(5, 3)和一个 bool 矩阵 adj带形状(5, 5)如下, src = tensor([[ 0, 1, 2], [ 3, 4,
我想知道如果不在第 4 行中使用“for”循环,下面的代码是否有更有效的替代方案? import torch n, d = 37700, 7842 k = 4 sample = torch.cat([
我有三个简单的问题。 如果我的自定义损失函数不可微会发生什么? pytorch 会通过错误还是做其他事情? 如果我在我的自定义函数中声明了一个损失变量来表示模型的最终损失,我应该放 requires_
我想知道 PyTorch Parameter 和 Tensor 的区别? 现有answer适用于使用变量的旧 PyTorch? 最佳答案 这就是 Parameter 的全部想法。类(附加)在单个图像中
给定以下张量(这是网络的结果 [注意 grad_fn]): tensor([121., 241., 125., 1., 108., 238., 125., 121., 13., 117., 12
什么是__constants__在 pytorch class Linear(Module):定义于 https://pytorch.org/docs/stable/_modules/torch/nn
我在哪里可以找到pytorch函数conv2d的源代码? 它应该在 torch.nn.functional 中,但我只找到了 _add_docstr 行, 如果我搜索conv2d。我在这里看了: ht
如 documentation 中所述在 PyTorch 中,Conv2d 层使用默认膨胀为 1。这是否意味着如果我想创建一个简单的 conv2d 层,我必须编写 nn.conv2d(in_chann
我阅读了 Pytorch 的源代码,发现它没有实现 convolution_backward 很奇怪。函数,唯一的 convolution_backward_overrideable 函数是直接引发错
我对编码真的很陌生,现在我正在尝试将我的标签变成一种热门编码。我已经完成将 np.array 传输到张量,如下所示 tensor([4., 4., 4., 4., 4., 4., 4., 4., 4.
我正在尝试实现 text classification model使用CNN。据我所知,对于文本数据,我们应该使用一维卷积。我在 pytorch 中看到了一个使用 Conv2d 的示例,但我想知道如何
我有一个多标签分类问题,我正试图用 Pytorch 中的 CNN 解决这个问题。我有 80,000 个训练示例和 7900 个类;每个示例可以同时属于多个类,每个示例的平均类数为 130。 问题是我的
我是一名优秀的程序员,十分优秀!