[英]For each layer in neural networks (pytorch), how many biases should be there?
[英]PyTorch and Neural Networks: How many parameters in a layer?
我看到很多消息来源都在谈论 neural.network 中的参数数量,并提到它的计算方式是:
num parameters = ((shape of filter width * filter height * shape of height * 上一层filter个数+1)* filter个数)
但我一直无法理解它如何适用于使用火炬中的 nn 创建的网络
例如 this.network 有多少个参数?
class NeuralNetwork(nn.Module):
def __init__(self):
super(NeuralNetwork, self).__init__()
self.flatten = nn.Flatten()
self.linear_relu_stack = nn.Sequential(
nn.Linear(28*28, 512),
nn.ReLU(),
nn.Linear(512, 512),
nn.ReLU(),
nn.Linear(512, 10)
)
def forward(self, x):
x = self.flatten(x)
logits = self.linear_relu_stack(x)
return logits
对象nn.Linear
表示维度为 [m, n] 的矩阵。 例如, nn.Linear(28*28, 512)
有(28*28)*512
参数(权重)。 在这里查看更多关于它的信息。
对象nn.Flatten()
和nn.ReLU()
不包含参数。
PyTorch 有一个内置函数,可用于打印网络摘要,其中包括网络结构、参数数量和总大小。
在您的情况下,您可以将其打印为,
from torchsummary import summary
net = NeuralNetwork()
summary(net, (1, 28, 28))
除了Pathi_rao的回答,来自 torchsummary 模块的摘要 function 有一个默认值为cuda
的设备参数,您需要将设备更改为 cpu summary.net, (1,28,28), device='cpu')
或将 model 设备更改为 cuda summary.net.to('cuda'), (1,28,28))
否则会引发以下 RuntimeError
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.