将Keras权值保存为动画视频,更好地了解模型是如何学习的

2017年08月08日 由 yining 发表 384470 0
将Keras权值矩阵保存为简短的动画视频,从而更好地理解你的神经网络模型是如何学习的。下面是第一个LSTM层的例子,以及一个经过一个学习周期训练的6级RNN模型的最终输出层。蓝色代表低值,红色代表高值。

Keras

Keras

开始


依赖关系
# clone the repo, preferably somewhere in your PYTHONPATH
git clone https://github.com/brannondorsey/keras_weight_animator
cd keras_weight_animator

pip install -r requirements.txt

为了从保存的权值图像中渲染视频,你还必须在你的机器上安装以下包:

使用模块


这个模块被命名为keras_weight_animator。它公开了一个可以在任何模型fit(.)方法中包含的Keras回调函数
## you can optionally add this module to your python path by appending 
## its parent directory in sys.path. Uncomment below to do this.
# import sys
# sys.path.append('path/to/keras_weight_animator/..')

from keras_weight_animator import image_saver_callback

# assumes a keras model named "model"
callbacks = [image_saver_callback(model, 'output_directory')]

model.fit(X_train, y_train, callbacks=callbacks)

image_saver_callback(...)的两个必需参数是Keras模型和一个output_directory,可以定期地保存权值图像。在默认情况下,keras_weight_animator将每100个批处理的层权值以PNGs格式保存在名为epoch_XXX-layer_NAME-weights_YY.的文件夹中的output_directory里。一旦训练完成,使用[bin/create_image_sequence.sh](bin/create_image_sequence.sh) path/to/output_directory,你可以从保存在output_directory中的图像序列创建短动画视频片段。使用parallel,mogrify和ffmpeg来创建一个来自output_directory中的每个文件夹中的图像序列,并以.mp4为后缀。视频文件将被命名为epoch_XXX-layer_NAME-weights_YY.mp4.这样的形式。你可以通过将render_videos=True参数传递给image_saver_callback(...),从而自动从你的训练脚本中运行该脚本。

可选参数


weight_image_sequences(...)采取了各种可选的关键字参数。

  • epochinterval(default=1):在每个epoch_interval周期保存权值图像。在默认情况下,每一个周期都要保存权值,但是如果你要训练很多的周期,你可能会想要改变这个问题。

  • batch_interval(default=100):在每个batch_interval批次保存权值图像. batch_interval=1将为每个批次保存权值.

  • cmap (default='gray'): Matplotlib color map. 我建议尝试一些不同color maps,特别是RdBu.

  • render_videos (default=False):在model.fit()的结尾部分可以选择创建一个系统来调用create_image_sequences.sh (使用了on_train_end(...) 内部的函数)。将这个设置为True之后会自动为你提供mp4格式的视频。


# example
callbacks = [image_saver_callback(model, 'weight_image_sequences', epoch_interval=10, batch_interval=1000, cmap='gray', render_videos=True)]
model.fit(X_train, y_train, callbacks=callbacks)

例子


我已经在examples/.py中包含了这个模块的示例用法。这个例子使用智能手机从WISDM中的加速度计数据来对人类活动任务进行分类,比如走路、站立、坐着、上楼等等。本例使用一个层的LSTM来对一组60个数据点进行分类,这是属于六个类中的一个。它将图像序列和视频输出到data/wisdm。
# download and uncompress the WISDM data
cd data
curl http://www.cis.fordham.edu/wisdm/includes/datasets/latest/WISDM_ar_latest.tar.gz -o WISDM_ar_latest.tar.gz
tar -xzf WISDM_ar_latest.tar.gz
rm WISDM_ar_latest.tar.gz
cd ../example

python wisdm.py

想法


使用bash脚本来利用并行、ImageMagick,和FFMPEG并不是最好的解决方案,但它值得我花时间去操作。
lil项目的目标是编写一个快速的工具,让我能够更好地理解在各种神经网络中权重是如何变化的。
如果你有兴趣贡献或维护一个更“干净”的版本,请联系(mailto:brannon@brannondorsey.com)。

归因和许可


这个模块是© Brannon Dorsey 2017 在麻省理工学院的许可下发布。你可以自由使用、修改、分发、销售等,这个软件基于这些条款下, 地址:https://github.com/brannondorsey/keras_weight_animator/blob/master/LICENSE。示例数据来自于福特汉姆大学发布的 WIreless Sensor Datamining Actitracker数据集:
Jennifer R. Kwapisz, Gary M. Weiss and Samuel A. Moore (2010). Activity Recognition using Cell Phone Accelerometers, Proceedings of the Fourth International Workshop on Knowledge Discovery from Sensor Data (at KDD-10), Washington DC. [PDF]

 

 

此文为编译作品,出处为:https://github.com/brannondorsey/keras_weight_animator#optional-parameters
欢迎关注ATYUN官方公众号
商务合作及内容投稿请联系邮箱:bd@atyun.com
评论 登录
热门职位
Maluuba
20000~40000/月
Cisco
25000~30000/月 深圳市
PilotAILabs
30000~60000/年 深圳市
写评论取消
回复取消