pytorch保存模型参数并用于预测(回归模型)

数据介绍:用’name’, ‘regDate’, ‘model’, ‘brand’, ‘bodyType’, ‘fuelType’, ‘gearbox’, ‘power’, ‘kilometer’, ‘regionCode’, ‘seller’, ‘creatDate’这12个指标预测’price’
数据集划分成了X_train,y_train 和 X_test,y_test
我用自己定义的Dataset,作用就是把X,y向量化了而已,然后用torch.utils.data中的DataLoader导入的train_dataloader和test_dataloader
打印我的train_dataloader看下
pytorch保存模型参数并用于预测(回归模型)_第1张图片
这里面包含两个tensor,一个是X_train生成的,一个是y_train生成的
下面开始正题
定义网络结构:

class Net(torch.nn.Module):
    def __init__(self, n_feature, n_hidden, n_output):
        super(Net, self).__init__()
        self.hidden = torch.nn.Linear(n_feature, n_hidden)
        self.output = torch.nn.Linear(n_hidden, n_output)

    def forward(self, data):
        hidden = self.hidden(data)
        hidden = F.relu(hidden)
        output = self.output(hidden)
        return output

实例化model

model = Net(12, 20, 1)
print(model)

训练过程:

NUM_EPOCHS = 10

LEARNING_RATE = 0.2
optimizer = torch.optim.SGD(model.parameters(), lr=LEARNING_RATE)
loss_func = torch.nn.MSELoss()  # 均方差
for epoch in range(NUM_EPOCHS):
    model.train()
    it = iter(train_dataloader)
    for i, (batch_x, batch_y) in enumerate(it):  # for each training step
        # X_train = torch.tensor(X_train, dtype=torch.float32)
        output_train = model(batch_x)
        # print(batch_x,batch_y)
        # break
        loss = loss_func(output_train, batch_y)  # 一定要prediction在前, y在后

        # backward
        optimizer.zero_grad()  # 梯度降零
        loss.backward()
        optimizer.step()
        if i%100 == 0:
            print('Epoch: ', epoch, 'iteration: ', i, ' loss: ', loss)
# 保存网络参数
torch.save(model.state_dict(), 'car.pkl')

这里要注意的是,
1 我用dataloader生成的训练数据必须由enumerate生成迭代器来访问,访问方式为i,(x,y)
其中i是迭代次数,x是我dataloader中的第一个tensor,y是dataloader中的第二个tensor
2 训练模型时,要写model.train()

模型加载并用于预测
在同一个文件中,我们要加载保存的模型,需要先重新实例化一个网络:

best_model = Net(12, 20, 1)

然后加载保存的参数:

best_model.load_state_dict(torch.load('car.pkl'))

评估:

test_loss = evaluate(best_model, test_dataloader)
print("perplexity: ", test_loss)

其中evaluate函数为:

def evaluate(model, test_dataloader):
    model.eval()
    it = iter(test_dataloader)
    total_count = 0.
    total_loss = 0.
    with torch.no_grad():
        for i, (batch_x, batch_y) in enumerate(it):
            output_test = model(batch_x)

            with torch.no_grad():
                loss = loss_func(output_test, batch_y)
                # print(type(loss))  # 
                # print(loss)  # tensor(41197140.)
                total_count += np.multiply(*batch_x.size())
                total_loss += loss.item() * np.multiply(*batch_x.size())

    loss = total_loss / total_count
    model.train()
    return loss

这里要注意的是:
1 最开头要写model.eval()
2 跳出for循环以后,要再加上model.train(),否则会报错

你可能感兴趣的:(pytorch)