十年网站开发经验 + 多家企业客户 + 靠谱的建站团队
量身定制 + 运营维护+专业推广+无忧售后,网站问题一站解决
我们使用深度学习网络实现波士顿房价预测,深度学习的目的就是寻找一个合适的函数输出我们想要的结果。深度学习实际上是机器学习领域中一个研究方向,深度学习的目标是让机器能够像人一样具有分析学习的能力,能够识别文字、图像、声音等数据。我认为深度学习与机器学习最主要的区别就是神经元。
公司主营业务:成都网站建设、网站设计、移动网站开发等业务。帮助企业客户真正实现互联网宣传,提高企业的竞争能力。创新互联是一支青春激扬、勤奋敬业、活力青春激扬、勤奋敬业、活力澎湃、和谐高效的团队。公司秉承以“开放、自由、严谨、自律”为核心的企业文化,感谢他们对我们的高要求,感谢他们从不同领域给我们带来的挑战,让我们激情的团队有机会用头脑与智慧不断的给客户带来惊喜。创新互联推出坡头免费做网站回馈大家。
基本构造
为什么引入激活函数
激活函数的种类
神经网络解决的问题有很多,例如分类、预测、回归等。这里我们给出两个解决类型。
分类
预测
使用paddle飞桨波士顿数据集
https://www.paddlepaddle.org.cn/documentation/docs/zh/api/paddle/text/UCIHousing_cn.html
## 绘图
Batch = 0
Batchs = []
all_train_accs = []
def draw_train_acc(Batchs,train_accs):
title = "training accs"
plt.title(title)
plt.xlabel("batch")
plt.ylabel("acc")
plt.plot(Batchs, train_accs, color = 'green', label = 'training accs')
plt.legend()
plt.grid()
plt.show()
all_train_loss = []
def draw_train_loss(Batchs,train_loss):
title = "training loss"
plt.title(title)
plt.xlabel("batch")
plt.ylabel("loss")
plt.plot(Batchs, train_loss, color = 'red', label = 'training loss')
plt.legend()
plt.grid()
plt.show()
## 绘制真实值与预测值的对比图
def draw_infer_result(groud_truths, infer_results):
title = 'Boston'
plt.title(title)
x = np.arange(1,20)
y = x
plt.plot(x,y);
plt.xlabel("ground truth")
plt.ylabel("infer result")
plt.scatter(groud_truths,infer_results,color='green',label='training cost')
plt.grid()
plt.show()
'''
核心
网络搭建
'''
class MyDNN(paddle.nn.Layer):
def __init__(self):
super(MyDNN, self).__init__()
#self.linear1 = paddle.nn.Linear(13,1,None) #全连接层,paddle.nn.Linear(in_features,out_features,weight)
self.linear1 = paddle.nn.Linear(13, 32, None)
self.linear2 = paddle.nn.Linear(32, 64, None)
self.linear3 = paddle.nn.Linear(64, 32, None)
self.linear4 = paddle.nn.Linear(32, 1, None)
def forward(self, inputs): ## 传播函数
x = self.linear1(inputs)
x = self.linear2(x)
x = self.linear3(x)
x = self.linear4(x)
return x
'''
网络训练与测试
'''
## 实例化
model = MyDNN()
model.train()
mse_loss = paddle.nn.MSELoss()
opt = paddle.optimizer.SGD(learning_rate=0.001, parameters=model.parameters())
epochs_num = 100
for epochs in range(epochs_num):
for batch_id,data in enumerate(train_loader()):
feature = data[0]
label = data[1]
predict = model(feature)
loss = mse_loss(predict, label)
loss.backward()
opt.step()
opt.clear_grad()
if batch_id!=0 and batch_id%10 == 0:
Batch = Batch+10
Batchs.append(Batch)
all_train_loss.append(loss.numpy()[0])
print("epoch{},step:{},train_loss:{}".format(epochs,batch_id,loss.numpy()[0]))
paddle.save(model.state_dict(),"UCIHousingDNN")
draw_train_loss(Batchs,all_train_loss)
para_state = paddle.load("UCIHousingDNN")
model = MyDNN()
model.eval()
model.set_state_dict(para_state)
losses = []
for batch_id,data in enumerate(eval_loader()):
feature = data[0]
label = data[1]
predict = model(feature)
loss = mse_loss(predict,label)
losses.append(loss.numpy()[0])
avg_loss = np.mean(losses)
print(avg_loss)
draw_infer_result(label,predict)
## 深度学习框架
import paddle
import numpy as np
import os
import matplotlib.pyplot as plt
## 绘图
Batch = 0
Batchs = []
all_train_accs = []
def draw_train_acc(Batchs,train_accs):
title = "training accs"
plt.title(title)
plt.xlabel("batch")
plt.ylabel("acc")
plt.plot(Batchs, train_accs, color = 'green', label = 'training accs')
plt.legend()
plt.grid()
plt.show()
all_train_loss = []
def draw_train_loss(Batchs,train_loss):
title = "training loss"
plt.title(title)
plt.xlabel("batch")
plt.ylabel("loss")
plt.plot(Batchs, train_loss, color = 'red', label = 'training loss')
plt.legend()
plt.grid()
plt.show()
## 绘制真实值与预测值的对比图
def draw_infer_result(groud_truths, infer_results):
title = 'Boston'
plt.title(title)
x = np.arange(1,20)
y = x
plt.plot(x,y);
plt.xlabel("ground truth")
plt.ylabel("infer result")
plt.scatter(groud_truths,infer_results,color='green',label='training cost')
plt.grid()
plt.show()
'''
数据集加载
'''
train_dataset = paddle.text.datasets.UCIHousing(mode="train")
eval_dataset = paddle.text.datasets.UCIHousing(mode="test")
train_loader = paddle.io.DataLoader(train_dataset,batch_size=32, shuffle=True)
eval_loader = paddle.io.DataLoader(eval_dataset,batch_size=8,shuffle=False)
print(train_dataset[1])
'''
核心
网络搭建
'''
class MyDNN(paddle.nn.Layer):
def __init__(self):
super(MyDNN, self).__init__()
#self.linear1 = paddle.nn.Linear(13,1,None) #全连接层,paddle.nn.Linear(in_features,out_features,weight)
self.linear1 = paddle.nn.Linear(13, 32, None)
self.linear2 = paddle.nn.Linear(32, 64, None)
self.linear3 = paddle.nn.Linear(64, 32, None)
self.linear4 = paddle.nn.Linear(32, 1, None)
def forward(self, inputs): ## 传播函数
x = self.linear1(inputs)
x = self.linear2(x)
x = self.linear3(x)
x = self.linear4(x)
return x
'''
网络训练与测试
'''
## 实例化
model = MyDNN()
model.train()
mse_loss = paddle.nn.MSELoss()
opt = paddle.optimizer.SGD(learning_rate=0.001, parameters=model.parameters())
epochs_num = 100
for epochs in range(epochs_num):
for batch_id,data in enumerate(train_loader()):
feature = data[0]
label = data[1]
predict = model(feature)
loss = mse_loss(predict, label)
loss.backward()
opt.step()
opt.clear_grad()
if batch_id!=0 and batch_id%10 == 0:
Batch = Batch+10
Batchs.append(Batch)
all_train_loss.append(loss.numpy()[0])
print("epoch{},step:{},train_loss:{}".format(epochs,batch_id,loss.numpy()[0]))
paddle.save(model.state_dict(),"UCIHousingDNN")
draw_train_loss(Batchs,all_train_loss)
para_state = paddle.load("UCIHousingDNN")
model = MyDNN()
model.eval()
model.set_state_dict(para_state)
losses = []
for batch_id,data in enumerate(eval_loader()):
feature = data[0]
label = data[1]
predict = model(feature)
loss = mse_loss(predict,label)
losses.append(loss.numpy()[0])
avg_loss = np.mean(losses)
print(avg_loss)
draw_infer_result(label,predict)