import torch
from torch import nn
class MLP(nn.Module):
# 声明带有模型参数的层,这里声明了两个全连接层
def __init__(self, **kwargs):
# 调用MLP父类Module的构造函数来进行必要的初始化。这样在构造实例时还可以指定其他函数
# 参数,如“模型参数的访问、初始化和共享”一节将介绍的模型参数params
super(MLP, self).__init__(**kwargs)
self.hidden = nn.Linear(784, 256) # 隐藏层
self.act = nn.ReLU()
self.output = nn.Linear(256, 10) # 输出层
# 定义模型的前向计算,即如何根据输入x计算返回所需要的模型输出
def forward(self, x):
a = self.act(self.hidden(x))
return self.output(a)
Hi,
**kwargs
enables you to pass additional parameters to a function with specific variable names. To demonstrate, –
def explaining_kwargs(**kwargs):
for key, value in kwargs.items():
print ("Key {} Value {}".format(key, value))
explaining_kwargs(Yes="Shi",I="Yishi",teacher="Laoshi")
This would print:
Key Yes Value Shi
Key I Value Yishi
Key teacher Value Laoshi
Does that answer your query?