方法3:在代码的开头处手动设置 import theano theano.config.device = 'gpu' theano.config.floatX = 'float32' 如何保存Keras模型?我们不推荐使用pickle或cPickle来保存Keras模型 你可以使用
使用 例子: from keras.models import load_modelmodel.save('my_model.h5') # creates a HDF5 file 'my_model.h5'del model # deletes the existing model# returns a compiled model# identical to the previous onemodel = load_model('my_model.h5') 如果你只是希望保存模型的结构,而不包含其权重或配置信息,可以使用: # save as JSONjson_string = model.to_json()# save as YAMLyaml_string = model.to_yaml() 这项操作将把模型序列化为json或yaml文件,这些文件对人而言也是友好的,如果需要的话你甚至可以手动打开这些文件并进行编辑。 当然,你也可以从保存好的json文件或yaml文件中载入模型: # model reconstruction from JSON:from keras.models import model_from_jsonmodel = model_from_json(json_string)# model reconstruction from YAMLmodel = model_from_yaml(yaml_string) 如果需要保存模型的权重,可通过下面的代码利用HDF5进行保存。注意,在使用前需要确保你已安装了HDF5和其Python库h5py model.save_weights('my_model_weights.h5') 如果你需要在代码中初始化一个完全相同的模型,请使用: model.load_weights('my_model_weights.h5') 如果你需要加载权重到不同的网络结构(有些层一样)中,例如fine-tune或transfer-learning,你可以通过层名字来加载模型: model.load_weights('my_model_weights.h5', by_name=True) 例如: '''假如原模型为: model = Sequential() model.add(Dense(2, input_dim=3, name='dense_1')) model.add(Dense(3, name='dense_2')) ... model.save_weights(fname)'''# new modelmodel = Sequential()model.add(Dense(2, input_dim=3, name='dense_1')) # will be loadedmodel.add(Dense(10, name='new_dense')) # will not be loaded# load weights from first model; will only affect the first layer, dense_1.model.load_weights(fname, by_name=True) 为什么训练误差比测试误差高很多?一个Keras的模型有两个模式:训练模式和测试模式。一些正则机制,如Dropout,L1/L2正则项在测试模式下将不被启用。 另外,训练误差是训练数据每个batch的误差的平均。在训练过程中,每个epoch起始时的batch的误差要大一些,而后面的batch的误差要小一些。另一方面,每个epoch结束时计算的测试误差是由模型在epoch结束时的状态决定的,这时候的网络将产生较小的误差。 【Tips】可以通过定义回调函数将每个epoch的训练误差和测试误差并作图,如果训练误差曲线和测试误差曲线之间有很大的空隙,说明你的模型可能有过拟合的问题。当然,这个问题与Keras无关。【@BigMoyan】 如何获取中间层的输出?一种简单的方法是创建一个新的 from keras.models import Modelmodel = ... # create the original modellayer_name = 'my_layer'intermediate_layer_model = Model(input=model.input, output=model.get_layer(layer_name).output)intermediate_output = intermediate_layer_model.predict(data 此外,我们也可以建立一个Keras的函数来达到这一目的: from keras import backend as K# with a Sequential modelget_3rd_layer_output = K.function([model.layers[0].input], [model.layers[3].output])layer_output = get_3rd_layer_output([X])[0] 当然,我们也可以直接编写Theano和TensorFlow的函数来完成这件事 注意,如果你的模型在训练和测试两种模式下不完全一致,例如你的模型中含有Dropout层,批规范化(BatchNormalization)层等组件,你需要在函数中传递一个learning_phase的标记,像这样: get_3rd_layer_output = K.function([model.layers[0].input, K.learning_phase()], [model.layers[3].output])# output in test mode = 0layer_output = get_3rd_layer_output([X, 0])[0]# output in train mode = 1layer_output = get_3rd_layer_output([X, 1])[0] 如何利用Keras处理超过机器内存的数据集?可以使用 另外,也可以编写一个每次产生一个batch样本的生成器函数,并调用 这种方式在Keras代码包的example文件夹下CIFAR10例子里有示范,也可点击这里在github上浏览。 当验证集的loss不再下降时,如何中断训练?可以定义 from keras.callbacks import EarlyStoppingearly_stopping = EarlyStopping(monitor='val_loss', patience=2)model.fit(X, y, validation_split=0.2, callbacks=[early_stopping]) 请参考回调函数 验证集是如何从训练集中分割出来的?如果在 训练数据在训练时会被随机洗乱吗?是的,如果 验证集的数据不会被洗乱 如何在每个epoch后记录训练/测试的loss和正确率?
hist = model.fit(X, y, validation_split=0.2)print(hist.history) 如何使用状态RNN(statful RNN)?一个RNN是状态RNN,意味着训练时每个batch的状态都会被重用于初始化下一个batch的初始状态。 当使用状态RNN时,有如下假设
要使用状态RNN,我们需要
要重置网络的状态,使用:
例子: X # this is our input data, of shape (32, 21, 16)# we will feed it to our model in sequences of length 10model = Sequential()model.add(LSTM(32, batch_input_shape=(32, 10, 16), stateful=True))model.add(Dense(16, activation='softmax'))model.compile(optimizer='rmsprop', loss='categorical_crossentropy')# we train the network to predict the 11th timestep given the first 10:model.train_on_batch(X[:, :10, :], np.reshape(X[:, 10, :], (32, 16)))# the state of the network has changed. We can feed the follow-up sequences:model.train_on_batch(X[:, 10:20, :], np.reshape(X[:, 20, :], (32, 16)))# let's reset the states of the LSTM layer:model.reset_states()# another way to do it in this case:model.layers[0].reset_states() 注意, 如何使用Keras进行分布式/多GPU运算?Keras在使用TensorFlow作为后端的时候可以进行分布式/多GPU的运算,Keras对多GPU和分布式的支持是通过TF完成的。 with tf.device('/gpu:0'): x = tf.placeholder(tf.float32, shape=(None, 20, 64)) y = LSTM(32)(x) # all ops in the LSTM layer will live on GPU:0with tf.device('/gpu:1'): x = tf.placeholder(tf.float32, shape=(None, 20, 64)) y = LSTM(32)(x) # all ops in the LSTM layer will live on GPU:1 注意,上例中由LSTM创建的变量不在GPU上:所有的TensorFlow变量总是在CPU上生存,而与它们在哪创建无关。各个设备上的变量转换TensorFlow会自动完成。 如果你想在不同的GPU上训练同一个模型的不同副本,但在不同的副本中共享权重,你应该首先在一个设备上实例化你的模型,然后在不同的设备上多次调用该对象,例如: with tf.device('/cpu:0'): x = tf.placeholder(tf.float32, shape=(None, 784)) # shared model living on CPU:0 # it won't actually be run during training; it acts as an op template # and as a repository for shared variables model = Sequential() model.add(Dense(32, activation='relu', input_dim=784)) model.add(Dense(10, activation='softmax'))# replica 0with tf.device('/gpu:0'): output_0 = model(x) # all ops in the replica will live on GPU:0# replica 1with tf.device('/gpu:1'): output_1 = model(x) # all ops in the replica will live on GPU:1# merge outputs on CPUwith tf.device('/cpu:0'): preds = 0.5 * (output_0 + output_1)# we only run the `preds` tensor, so that only the two# replicas on GPU get run (plus the merge op on CPU)output_value = sess.run([preds], feed_dict={x: data}) 要想完成分布式的训练,你需要将Keras注册在连接一个集群的TensorFlow会话上: server = tf.train.Server.create_local_server()sess = tf.Session(server.target)from keras import backend as KK.set_session(sess) 关于分布式训练的更多信息,请参考这里 如何“冻结”网络的层?“冻结”一个层指的是该层将不参加网络训练,即该层的权重永不会更新。在进行fine-tune时我们经常会需要这项操作。在使用固定的embedding层处理文本输入时,也需要这个技术。 可以通过向层的构造函数传递 frozen_layer = Dense(32,trainable=False) 此外,也可以通过将层对象的 x = Input(shape=(32,))layer = Dense(32)layer.trainable = Falsey = layer(x)frozen_model = Model(x, y)# in the model below, the weights of `layer` will not be updated during trainingfrozen_model.compile(optimizer='rmsprop', loss='mse')layer.trainable = Truetrainable_model = Model(x, y)# with this model the weights of the layer will be updated during training# (which will also affect the above model since it uses the same layer instance)trainable_model.compile(optimizer='rmsprop', loss='mse')frozen_model.fit(data, labels) # this does NOT update the weights of `layer`trainable_model.fit(data, labels) # this updates the weights of `layer` 如何从Sequential模型中去除一个层?可以通过调用 model = Sequential()model.add(Dense(32, activation='relu', input_dim=784))model.add(Dense(32, activation='relu'))print(len(model.layers)) # '2'model.pop()print(len(model.layers)) # '1' 【Tips】模型的.layers属性保存了模型中的层对象,数据类型是list,在model没有 如何在Keras中使用预训练的模型?我们提供了下面这些图像分类的模型代码及预训练权重:
可通过 from keras.applications.vgg16 impoprt VGG16from keras.applications.vgg19 impoprt VGG19from keras.applications.resnet50 impoprt ResNet50from keras.applications.inception_v3 impoprt InceptionV3model = VGG16(weights='imagenet', include_top=True) 这些代码的使用示例请参考 下面的图像分类模型提供了模型搭建的代码和相应的预训练权重 使用这些预训练模型进行特征抽取或fine-tune的例子可以参考此博客 VGG模型也是很多Keras例子的基础模型,如: |
|