一开始写模型的时候,网上学习到的都是那种非常简单的模型,可是我要实现的模型是将多个模型套起来,一个大模型,一起训练。
其实只要先定义几个小模型,然后将其定义在大模型的init中,然后用forward来控制内部的运作即可,直接看代码把。
class Model_one(nn.Module):
def __init__(self, n_input, n_output):
super(Model_one, self).__init__()
self.predict = nn.Linear(n_input, n_output, bias=False)
def forward(self, x):
out = self.predict(x)
return out
class Model_two(nn.Module):
def __init__(self, n_input, n_output):
super(Model_two, self).__init__()
self.hidden1 = nn.Linear(n_input, 128)
self.hidden2 = nn.Linear(128, 64)
self.predict = nn.Linear(64, n_output)
self.dropout = nn.Dropout(p=0.5)
self.dropout2 = nn.Dropout(p=0.8)
def forward(self, x):
out = self.hidden1(x)
out = self.dropout(out)
out = F.relu(out)
out = self.hidden2(out)
out = self.dropout(out)
out = F.relu(out)
predict = self.predict(out)
predict = self.dropout2(predict)
predict = torch.sigmoid(predict)
return predict
def forward(self, x):
out = self.hidden1(x)
out = F.relu(out)
out = self.hidden2(out)
out = F.relu(out)
out = self.hidden3(out)
out = F.relu(out)
predict = self.predict(out)
predict = torch.sigmoid(predict)
return predict
class Model(nn.Module):
def __init__(self, category_len, grid_len, K):
super(Model, self).__init__()
self.category_model = Model_one(category_len, K)
self.grid_model = Model_one(grid_len, K)
self.category_bias_model = Model_one(category_len, K)
self.grid_bias_model = Model_two(grid_len, K)
self.MLP_model = Model_two(2 * K, K)
self.NeuMF_model = Model_one(2 * K, 1)
def forward(self, category_input, grid_input):
category_out = self.category_model(category_input)
grid_out = self.grid_model(grid_input)
SVD_out = category_out * grid_out
bu = self.category_bias_model(category_input)
bi = self.grid_bias_model(grid_input)
SVD_out = SVD_out + bu + bi
MLP_input = torch.cat((category_out, grid_out), 1) # 横向拼接
MLP_out = self.MLP_model(MLP_input)
NeuMF_RS_input = torch.cat((SVD_out, MLP_out), 1) # 横向拼接
NeuMF_RS_out = self.NeuMF_model(NeuMF_RS_input)
return NeuMF_RS_out
这里的Model_one和Model_two定义的小模型都是大模型的组成部分,只要控制好其中输入输出的衔接即可。
其中的dropout在这是用来防止过拟合的,不用关注,关注网络是如何写的就可以了。
太笨了,一开始完全不懂怎么写