Keras 常见问题

Keras FAQ: Frequently Asked Keras Questions

How can I run Keras on GPU?  如何在GPU上运行?

How can I save a Keras model? 如何保存模型?

Why is the training loss much higher than the testing loss? 为什么训练损失值远大于测试损失值?

How can I visualize the output of an intermediate layer?  如何可视化中间层的输入?

Isn't there a bug with Merge or Graph related to input concatenation? 是不是有一个错误的合并或输入级联相关图?

How can I use Keras with datasets that don't fit in memory?  内存不能加载数据集怎么办?

How can I interrupt training when the validation loss isn't decreasing anymore?  损失值不再下降的时候,如何停止训练

How is the validation split computed?  验证集是如何分割计算的?

Is the data shuffled during training? 在训练时 数据是否随机打乱

How can I record the training / validation loss / accuracy at each epoch? 如何在每次迭代的时候都显示 训练或测试loss,或准确率


How can I run Keras on GPU?

Method 1: use Theano flags.

THEANO_FLAGS=device=gpu,floatX=float32 python my_keras_script.py

The name 'gpu' might have to be changed depending on your device's identifier (e.g. gpu0gpu1, etc).

Method 2: set up your .theanorcInstructions

Method 3: manually set theano.config.devicetheano.config.floatX at the beginning of your code:

import theano
theano.config.device = 'gpu'
theano.config.floatX = 'float32'

How can I save a Keras model?

It is not recommended to use pickle or cPickle to save a Keras model.

If you only need to save the architecture of a model, and not its weights, you can do:

# save as JSON
json_string = model.to_json()

# save as YAML
yaml_string = model.to_yaml()

You can then build a fresh model from this data:

# model reconstruction from JSON:
from keras.models import model_from_json
model = model_from_json(json_string)

# model reconstruction from YAML
model = model_from_yaml(yaml_string)

If you need to save the weights of a model, you can do so in HDF5:

model.save_weights('my_model_weights.h5')

Assuming you have code for instantiating your model, you can then load the weights you saved into a model with the same architecture:

model.load_weights('my_model_weights.h5')

This leads us to a way to save and reconstruct models from only serialized data:

json_string = model.to_json()
open('my_model_architecture.json', 'w').write(json_string)
model.save_weights('my_model_weights.h5')

# elsewhere...
model = model_from_json(open('my_model_architecture.json').read())
model.load_weights('my_model_weights.h5')

Why is the training loss much higher than the testing loss?

A Keras model has two modes: training and testing. Regularization mechanisms, such as Dropout and L1/L2 weight regularization, are turned off at testing time.

Besides, the training loss is the average of the losses over each batch of training data. Because your model is changing over time, the loss over the first batches of an epoch is generally higher than over the last batches. On the other hand, the testing loss for an epoch is computed using the model as it is at the end of the epoch, resulting in a lower loss.


How can I visualize the output of an intermediate layer?

You can build a Theano function that will return the output of a certain layer given a certain input, for example:

# with a Sequential model
get_3rd_layer_output = theano.function([model.layers[0].input], 
                                       model.layers[3].get_output(train=False))
layer_output = get_3rd_layer_output(X)

# with a Graph model
get_conv_layer_output = theano.function([model.inputs[i].input for i in model.input_order],
                                        model.outputs['conv'].get_output(train=False),
                                        on_unused_input='ignore')
conv_output = get_conv_output(input_data_dict)

Yes, there was a known bug with tensor concatenation in Thenao that was fixed early 2015. Please upgrade to the latest version of Theano:

sudo pip install git+git://github.com/Theano/Theano.git

How can I use Keras with datasets that don't fit in memory?

You can do batch training using model.train_on_batch(X, y) and model.test_on_batch(X, y). See themodels documentation.

You can also see batch training in action in our CIFAR10 example.


How can I interrupt training when the validation loss isn't decreasing anymore?

You can use an EarlyStopping callback:

from keras.callbacks import EarlyStopping
early_stopping = EarlyStopping(monitor='val_loss', patience=2)
model.fit(X, y, validation_split=0.2, callbacks=[early_stopping])

Find out more in the callbacks documentation.


How is the validation split computed?

If you set the validation_split arugment in model.fit to e.g. 0.1, then the validation data used will be the last 10% of the data. If you set it to 0.25, it will be the last 25% of the data, etc.


Is the data shuffled during training?

Yes, if the shuffle argument in model.fit is set to True (which is the default), the training data will be randomly shuffled at each epoch.

Validation data isn't shuffled.


How can I record the training / validation loss / accuracy at each epoch?

The model.fit method returns an History callback, which has a history attribute containing the lists of successive losses / accuracies.

hist = model.fit(X, y, validation_split=0.2)
print(hist.history)

 
 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值