1.首先,一定记住:这个是 继承来自 torch.tensor的,所以本质是一个tensor.
不过,它可以train更新参数
2.参考文章:
(1)这篇文章 和 例子超级 简洁清晰
理解torch.nn.Parameter - 知乎 (zhihu.com)
里面的实例:
import torch
import torch.nn as nn
from torch.optim import Adam
class NN_Network(nn.Module):
def __init__(self, in_dim, hid, out_dim):
super(NN_Network, self).__init__()
self.linear1 = nn.Linear(in_dim, hid)
self.linear2 = nn.Linear(hid, out_dim)
self.linear1.weight = torch.nn.Parameter(torch.zeros(in_dim,hid))
self.linear1.bias = torch.nn.Parameter(torch.ones(hid))
self.linear2.weight = torch.nn.Parameter(torch.zeros(hid,out_dim))
self.linear2.bias = torch.nn.Parameter(torch.ones(out_dim))
def forward(self, input_array):
h = self.linear1(input_array)
y_pred = self.linear2(h)
return y_pred
in_d = 5
hidn = 2
out_d = 3
net = NN_Network(in_d, hidn, out_d)
(2)其他参考文章: