## 深度学习中重要内容

### 建立模型——神经元

w和b就是我们需要在训练中需要寻找的，

## 实践——波士顿房价预测

### 绘图

```## 绘图
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()```

### 网络搭建

```'''

'''
def __init__(self):
super(MyDNN, self).__init__()
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()
epochs_num = 100
for epochs in range(epochs_num):
feature = data[0]
label = data[1]
predict = model(feature)
loss = mse_loss(predict, label)
loss.backward()
opt.step()
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]))
draw_train_loss(Batchs,all_train_loss)
model = MyDNN()
model.eval()
model.set_state_dict(para_state)
losses = []
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 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()
'''

'''
print(train_dataset[1])
'''

'''
def __init__(self):
super(MyDNN, self).__init__()
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()
epochs_num = 100
for epochs in range(epochs_num):
feature = data[0]
label = data[1]
predict = model(feature)
loss = mse_loss(predict, label)
loss.backward()
opt.step()
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]))
draw_train_loss(Batchs,all_train_loss)
model = MyDNN()
model.eval()
model.set_state_dict(para_state)
losses = []
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)```