# 基于matplotlib和keras的神经网络结果可视化

2.1 开发环境以及要求

2.2 训练数据的产生

2.3 网络的结构

3.1 网络的定义以及实现

3.2 训练模型保存

3.3 模型的搭建和保存代码

2.1 开发环境以及要求

2.2 训练数据的产生

----------------------------------------------

x y

1 0.093 -0.81

2 0.58 -0.45

3 1.04 -0.007

4 1.55 0.48

5 2.15 0.89

6 2.62 0.997

7 2.71 0.995

8 2.73 0.993

9 3.03 0.916

10 3.14 0.86

11 3.58 0.57

12 3.66 0.504

13 3.81 0.369

14 3.83 0.35

15 4.39 -0.199

16 4.44 -0.248

17 4.6 -0.399

18 5.39 -0.932

19 5.54 -0.975

20 5.76 -0.999

----------------------------------------------

2.3 网络的结构

3.1 网络的定义以及实现

```import math;
import random;
from matplotlib import pyplot as plt
from keras.models import Sequential
from keras.layers.core import Dense
import numpy as np
from keras.callbacks import ModelCheckpoint
import os

#采样函数
def sample(low, up, num):
data = [];
for i in range(num):
#采样
tmp = random.uniform(low, up);
data.append(tmp);
data.sort();
return data;

#sin函数
def func(x):
y = [];
for i in range(len(x)):
tmp = math.sin(x[i] - math.pi/3);
y.append(tmp);
return y;

#获取模型拟合结果
def getfit(model,x):
y = [];
for i in range(len(x)):
tmp = model.predict([x[i]], 10);
y.append(tmp[0][0]);
return y;

#删除同一目录下的所有文件
def del_file(path):
ls = os.listdir(path)
for i in ls:
c_path = os.path.join(path, i)
if os.path.isdir(c_path):
del_file(c_path)
else:
os.remove(c_path)

if __name__ == '__main__':
path = "E:/Model/";
del_file(path);

low = 0;
up = 2 * math.pi;
x = np.linspace(low, up, 1000);
y = func(x);

# 数据采样
#     x_sample = sample(low,up,20);
x_sample = [0.09326442022999694, 0.5812590520508311, 1.040490143783586, 1.5504427746047338, 2.1589557183817036, 2.6235357787018407, 2.712578091093361, 2.7379109336528167, 3.0339662651841186, 3.147676812083248, 3.58596337171837, 3.6621496731124314, 3.81130899864203, 3.833092859928872, 4.396611340802901, 4.4481080339256875, 4.609657879057151, 5.399731063412583, 5.54299720786794, 5.764084730699906];
y_sample = func(x_sample);

# callback
filepath="E:/Model/weights-improvement-{epoch:00d}.hdf5";
checkpoint= ModelCheckpoint(filepath, verbose=1, save_best_only=False, mode='max');
callbacks_list= [checkpoint];

# 建立顺序神经网络层次模型
model = Sequential();
model.fit(x_sample, y_sample, nb_epoch=1000, batch_size=20,callbacks=callbacks_list);

#测试数据
x_new = np.linspace(low, up, 1000);
y_new = getfit(model,x_new);

# 数据可视化
plt.plot(x,y);
plt.scatter(x_sample, y_sample);
plt.plot(x_new,y_new);

plt.show();```

3.2 训练模型保存

（1）filename： 字符串，保存模型的路径

（2）verbose： 0或1

（3）mode： ‘auto’，‘min’，‘max’

（4）monitor： 需要监视的值

（5）save_best_only： 当设置为True时，监测值有改进时才会保存当前的模型。在save_best_only=True时决定性能最佳模型的评判准则，例如，当监测值为val_acc时，模式应为max，当监测值为val_loss时，模式应为min。在auto模式下，评价准则由被监测值的名字自动推断

（6）save_weights_only： 若设置为True，则只保存模型权重，否则将保存整个模型（包括模型结构，配置信息等）

（7）period CheckPoint之间的间隔的epoch数

3.3 模型的搭建和保存代码

```    # callback
filepath="E:/Model/weights-improvement-{epoch:00d}.hdf5";
checkpoint= ModelCheckpoint(filepath, verbose=1, save_best_only=False, mode='max');
callbacks_list= [checkpoint];

# 建立顺序神经网络层次模型
model = Sequential();
model.fit(x_sample, y_sample, nb_epoch=1000, batch_size=20,callbacks=callbacks_list);```

```import math;
import random;
from matplotlib import pyplot as plt
from keras.models import Sequential
from keras.layers.core import Dense
import numpy as np
import matplotlib.animation as animation
from PIL import Image

#定义kdd99数据预处理函数
def sample(low, up, num):
data = [];
for i in range(num):
#采样
tmp = random.uniform(low, up);
data.append(tmp);
data.sort();
return data;

def func(x):
y = [];
for i in range(len(x)):
tmp = math.sin(x[i] - math.pi/3);
y.append(tmp);
return y;

def getfit(model,x):
y = [];
for i in range(len(x)):
tmp = model.predict([x[i]], 10);
y.append(tmp[0][0]);
return y;

def init():
fpath = "E:/imgs/0.jpg";
img = Image.open(fpath);
plt.axis('off') # 关掉坐标轴为 off
return plt.imshow(img);

def update(i):
fpath = "E:/imgs/" + str(i) + ".jpg";
img = Image.open(fpath);
plt.axis('off') # 关掉坐标轴为 off
return plt.imshow(img);

if __name__ == '__main__':
low = 0;
up = 2 * math.pi;
x = np.linspace(low, up, 1000);
y = func(x);

# 数据采样
#     x_sample = sample(low,up,20);
x_sample = [0.09326442022999694, 0.5812590520508311, 1.040490143783586, 1.5504427746047338, 2.1589557183817036, 2.6235357787018407, 2.712578091093361, 2.7379109336528167, 3.0339662651841186, 3.147676812083248, 3.58596337171837, 3.6621496731124314, 3.81130899864203, 3.833092859928872, 4.396611340802901, 4.4481080339256875, 4.609657879057151, 5.399731063412583, 5.54299720786794, 5.764084730699906];
y_sample = func(x_sample);

# 建立顺序神经网络层次模型
model = Sequential();

plt.ion(); #开启interactive mode 成功的关键函数
fig = plt.figure(1);

for i in range(100):
filepath="E:/Model/weights-improvement-" + str(i + 1) + ".hdf5";
#测试数据
x_new = np.linspace(low, up, 1000);
y_new = getfit(model,x_new);
# 显示数据
plt.clf();
plt.plot(x,y);
plt.scatter(x_sample, y_sample);
plt.plot(x_new,y_new);

ffpath = "E:/imgs/" + str(i) + ".jpg";
plt.savefig(ffpath);

plt.pause(0.01)             # 暂停0.01秒

ani = animation.FuncAnimation(plt.figure(2), update,range(100),init_func=init, interval=500);
ani.save("E:/test.gif",writer='pillow');

plt.ioff()                 # 关闭交互模式```

462 篇文章45 人订阅

0 条评论