Keras model.summary() 对象到字符串 [英] Keras model.summary() object to string

查看:30
本文介绍了Keras model.summary() 对象到字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想用神经网络超参数和模型架构写一个 *.txt 文件.是否可以将对象 model.summary() 写入我的输出文件?

I want to write a *.txt file with the neural network hyperparameters and the model architecture. Is it possible to write the object model.summary() to my output file?

(...)
summary = str(model.summary())
(...)
out = open(filename + 'report.txt','w')
out.write(summary)
out.close

正如您在下面看到的那样,我得到了无".

It happens that I'm getting "None" as you can see below.

Hyperparameters
=========================

learning_rate: 0.01
momentum: 0.8
decay: 0.0
batch size: 128
no. epochs: 3
dropout: 0.5
-------------------------

None
val_acc: 0.232323229313
val_loss: 3.88496732712
train_acc: 0.0965207634216
train_loss: 4.07161939425
train/val loss ratio: 1.04804469418

知道如何处理吗?

推荐答案

使用我的 Keras (2.0.6) 和 Python (3.5.0) 版本,这对我有用:

With my version of Keras (2.0.6) and Python (3.5.0), this works for me:

# Create an empty model
from keras.models import Sequential
model = Sequential()

# Open the file
with open(filename + 'report.txt','w') as fh:
    # Pass the file handle in as a lambda function to make it callable
    model.summary(print_fn=lambda x: fh.write(x + '
'))

这会将以下几行输出到文件中:

This outputs the following lines to the file:

_________________________________________________________________
Layer (type)                 Output Shape              Param #   
=================================================================
Total params: 0
Trainable params: 0
Non-trainable params: 0
_________________________________________________________________

这篇关于Keras model.summary() 对象到字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆