python显示3d模型动起来_【python】matplotlib动态显示详解

本文介绍了如何使用matplotlib进行动态绘图,展示3D模型动起来的过程。通过开启interactive mode,动态加载神经网络训练的模型权重,实时绘制并保存拟合曲线图片,最后将图片组合成gif动画,实现模型训练过程的可视化。
摘要由CSDN通过智能技术生成

1.matplotlib动态绘图

python在绘图的时候,需要开启 interactive mode。核心代码如下:

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

fig = plt.figure(1);

for i in range(100):

filepath="E:/Model/weights-improvement-" + str(i + 1) + ".hdf5";

model.load_weights(filepath);

#测试数据

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() # 关闭交互模式

2.实例

已知下面采样自Sin函数的数据:

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

通过一个简单的三层神经网络训练一个Sin函数的拟合器,并可视化模型训练过程的拟合曲线。

2.1 网络训练实现

主要做的事情是定义一个三层的神经网络,输入层节点数为1

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值