系列文章往期回顾
飞桨深度学习零基础入门(序)——Python实现梯度下降
一、导入相关依赖包
import paddle
from paddle.nn import Linear
import paddle.nn.functional as F
import numpy as np
import os
import random
其中: Linear全连接层,在本文中仅采用一个全连接层即单个神经网络对波士顿房价进行预测
二、构建单层神经网络回归类
class Regressor(paddle.nn.Layer):
def __init__(self):
super(Regressor, self).__init__()
self.fc = Linear(in_features=13, out_features=1)
def forward(self, inputs):
x = self.fc(inputs)
return x
输入的特征是13个,对于结果仅输出对房价的预测
三、设置参数
model = Regressor()
model.train()
opt = paddle.optimizer.SGD(learning_rate=0.01, parameters=model.parameters())
将单层神经网络回归类实例化之后开启模型训练模式(model.train),设置优化器为随机梯度下降(SGD),并设置学习率为0.01,梯度下载的参数设置为模型所需的参数
四、导入数据集
由于个人很好奇数据集导入的格式,所以采用了keras.datasets来导入了数据集,常规的导入情况请参照上一篇文章
from keras.datasets import boston_housing
(training_data, train_targets), (test_data, test_targets) = boston_housing.load_data()
train_targets = train_targets.reshape((-1, 1))
training_data = np.array(np.concatenate([training_data, train_targets], axis=1).astype(np.double))
test_targets = test_targets.reshape((-1, 1))
test_data = np.array(np.concatenate([test_data, test_targets], axis=1).astype(np.double))
五、数据归一化
maxinums, mininums, avg = training_data.max(axis=0), training_data.min(axis=0), training_data.sum(axis=0) / training_data.shape[0]
training_data, test_data = (training_data - mininums) / (maxinums - mininums), (test_data - mininums) / (maxinums - mininums)
六、训练并保存模型
EPOCH_NUM = 100
BATCH_SIZE = 10
for epoch_id in range(EPOCH_NUM):
np.random.shuffle(training_data)
mini_batches = [training_data[k:k+BATCH_SIZE] for k in range(0, len(training_data), BATCH_SIZE)]
for iter_id, mini_batch in enumerate(mini_batches):
x = np.array(mini_batch[:, :-1])
y = np.array(mini_batch[:, -1:])
house_features = paddle.to_tensor(x)
house_features = paddle.cast(house_features, dtype="float32")
prices = paddle.to_tensor(y)
prices = paddle.cast(prices, dtype="float32")
predicts = model(house_features)
loss = F.square_error_cost(predicts, label=prices)
avg_loss = paddle.mean(loss)
if iter_id % 20==0:
print("epoch: {}, iter: {}, loss is: {}".format(epoch_id, iter_id, avg_loss.numpy()))
avg_loss.backward()
opt.step()
opt.clear_grad()
paddle.save(model.state_dict(), 'LR_model.pdparams')
print("模型保存成功,模型参数保存在LR_model.pdparams中")
七、模型测试
def load_one_example():
idx = np.random.randint(0, test_data.shape[0])
idx = -10
one_data, label = test_data[idx, :-1], test_data[idx, -1]
one_data = one_data.reshape([1,-1])
return one_data, label
model_dict = paddle.load('LR_model.pdparams')
model.load_dict(model_dict)
model.eval()
one_data, label = load_one_example()
one_data = paddle.to_tensor(one_data)
one_data = paddle.cast(one_data, dtype='float32')
predict = model(one_data)
predict = predict * (maxinums[-1] - mininums[-1]) + avg[-1]
label = label * (maxinums[-1] - mininums[-1]) + avg[-1]
print("Inference result is {}, the corresponding label is {}".format(predict.numpy(), label))
运行结果: 由于初始化和模型训练轮次的缘故,结果可能不会相同,但最后预测结果和最终标签应该不会差太多
代码下载
代码链接
keras相关版本的全连接层
keras版本的全连接层实现可以参照博主另一个系列的文章: 睿智的keras深度学习(一)——阈值逻辑单元和多层感知机
|